View Javadoc

1   /*
2    * Created on 7-May-2005
3    */
4   package ca.spaz.cron.datasource.sql.USDAsr17;
5   
6   import java.sql.*;
7   import java.util.Map;
8   
9   import org.apache.log4j.Logger;
10  
11  import ca.spaz.sql.SQLInsert;
12  
13  public class USDAFood {
14     /***
15      * Logger for this class
16      */
17     private static final Logger logger = Logger.getLogger(USDAImporter.class);
18     
19     int ID;
20     String ndb_id;
21     String description;
22     String foodgroup;
23     
24     /***
25      * Construct from USDB Flat file string
26      * @param str a string from the USDB sr17 flat file
27      * @param groups map of ids to food groups
28      */
29     public USDAFood(String str, Map groups) {
30        String[] parts = str.split("//^");
31        for (int i = 0; i < parts.length; i++) {
32           parts[i] = parts[i].replaceAll("^~", "");
33           parts[i] = parts[i].replaceAll("~$", "");
34        }
35  
36        ndb_id = parts[0];
37        foodgroup = (String) groups.get(parts[1]);
38        description = parts[2];
39     }   
40     
41     public void addToDB(Connection c) {
42        try {
43           SQLInsert s = new SQLInsert("Food");
44           s.getColumns().add("ID", null);
45           s.getColumns().add("ndb_id", ndb_id);
46           s.getColumns().add("description", description);
47           s.getColumns().add("foodgroup", foodgroup);
48           s.getColumns().add("source", "USDA sr17");
49           s.getColumns().add("sourceUID", "sql.usda-sr17." + ndb_id);
50           s.execute(c);
51           
52           // get auto-incremented ID
53           ResultSet rs = c.createStatement().executeQuery("CALL IDENTITY()");
54           rs.next();
55           ID = rs.getInt(1);        
56        } catch (SQLException e) {
57           logger.error("parseFood(String)", e);
58        }
59     }
60  }