1 |
|
|
2 |
|
|
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 |
|
|
16 |
|
|
17 |
0 |
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 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
0 |
public USDAFood(String str, Map groups) { |
30 |
0 |
String[] parts = str.split("\\^"); |
31 |
0 |
for (int i = 0; i < parts.length; i++) { |
32 |
0 |
parts[i] = parts[i].replaceAll("^~", ""); |
33 |
0 |
parts[i] = parts[i].replaceAll("~$", ""); |
34 |
|
} |
35 |
|
|
36 |
0 |
ndb_id = parts[0]; |
37 |
0 |
foodgroup = (String) groups.get(parts[1]); |
38 |
0 |
description = parts[2]; |
39 |
0 |
} |
40 |
|
|
41 |
|
public void addToDB(Connection c) { |
42 |
|
try { |
43 |
0 |
SQLInsert s = new SQLInsert("Food"); |
44 |
0 |
s.getColumns().add("ID", null); |
45 |
0 |
s.getColumns().add("ndb_id", ndb_id); |
46 |
0 |
s.getColumns().add("description", description); |
47 |
0 |
s.getColumns().add("foodgroup", foodgroup); |
48 |
0 |
s.getColumns().add("source", "USDA sr17"); |
49 |
0 |
s.getColumns().add("sourceUID", "sql.usda-sr17." + ndb_id); |
50 |
0 |
s.execute(c); |
51 |
|
|
52 |
|
|
53 |
0 |
ResultSet rs = c.createStatement().executeQuery("CALL IDENTITY()"); |
54 |
0 |
rs.next(); |
55 |
0 |
ID = rs.getInt(1); |
56 |
0 |
} catch (SQLException e) { |
57 |
0 |
logger.error("parseFood(String)", e); |
58 |
0 |
} |
59 |
0 |
} |
60 |
|
} |