1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
package ca.spaz.cron.datasource.sql.USDAsr17; |
5 |
|
|
6 |
|
import java.sql.*; |
7 |
|
import java.util.HashMap; |
8 |
|
|
9 |
|
import org.apache.log4j.Logger; |
10 |
|
|
11 |
|
import ca.spaz.sql.SQLUpdate; |
12 |
|
|
13 |
|
public class USDANutrient { |
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
0 |
private static final Logger logger = Logger.getLogger(USDAImporter.class); |
19 |
|
private static HashMap nutrientMap; |
20 |
|
|
21 |
|
String ndb_id; |
22 |
|
String n_id; |
23 |
|
double amount; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
0 |
public USDANutrient(String str) { |
31 |
0 |
String[] parts = str.split("\\^"); |
32 |
0 |
for (int i = 0; i < parts.length; i++) { |
33 |
0 |
parts[i] = parts[i].replaceAll("^~", ""); |
34 |
0 |
parts[i] = parts[i].replaceAll("~$", ""); |
35 |
|
} |
36 |
0 |
ndb_id = parts[0]; |
37 |
0 |
n_id = parts[1]; |
38 |
0 |
amount =Double.parseDouble(parts[2]); |
39 |
0 |
} |
40 |
|
|
41 |
|
public void addToDB(Connection c, HashMap foods) { |
42 |
|
try { |
43 |
0 |
USDAFood food = (USDAFood)foods.get(ndb_id); |
44 |
0 |
USDANutrientInfo ni = (USDANutrientInfo)getNutrientMap().get(n_id); |
45 |
0 |
if (ni != null) { |
46 |
0 |
ensureRow(c, ni.table, food.ID); |
47 |
0 |
SQLUpdate s = new SQLUpdate(ni.table); |
48 |
0 |
s.addWhere("FID", food.ID); |
49 |
0 |
s.add(ni.tag, amount); |
50 |
0 |
s.execute(c); |
51 |
|
} |
52 |
0 |
} catch (SQLException e) { |
53 |
0 |
logger.error("parseFood(String)", e); |
54 |
0 |
} |
55 |
0 |
} |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private boolean ensureRow(Connection c, String table, int FID) throws SQLException { |
69 |
0 |
Statement s = c.createStatement(); |
70 |
0 |
String q = "SELECT * from " + table + " WHERE FID='" + FID + "';"; |
71 |
0 |
ResultSet res = s.executeQuery(q); |
72 |
0 |
if (res.next()) { |
73 |
0 |
return false; |
74 |
|
} else { |
75 |
0 |
s.execute("INSERT INTO " + table + " (FID) VALUES ('" + FID + "');"); |
76 |
0 |
return true; |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
|
private static void makeNutrientMap() { |
81 |
0 |
addNutrient("AminoAcids", "TRP", "501"); |
82 |
0 |
addNutrient("AminoAcids", "THR", "502"); |
83 |
0 |
addNutrient("AminoAcids", "ILE", "503"); |
84 |
0 |
addNutrient("AminoAcids", "LEU", "504"); |
85 |
0 |
addNutrient("AminoAcids", "LYS", "505"); |
86 |
0 |
addNutrient("AminoAcids", "MET", "506"); |
87 |
0 |
addNutrient("AminoAcids", "CYS", "507"); |
88 |
0 |
addNutrient("AminoAcids", "PHE", "508"); |
89 |
0 |
addNutrient("AminoAcids", "TYR", "509"); |
90 |
0 |
addNutrient("AminoAcids", "VAL", "510"); |
91 |
0 |
addNutrient("AminoAcids", "ARG", "511"); |
92 |
0 |
addNutrient("AminoAcids", "HIS", "512"); |
93 |
0 |
addNutrient("AminoAcids", "ALA", "513"); |
94 |
0 |
addNutrient("AminoAcids", "ASP", "514"); |
95 |
0 |
addNutrient("AminoAcids", "GLU", "515"); |
96 |
0 |
addNutrient("AminoAcids", "GLY", "516"); |
97 |
0 |
addNutrient("AminoAcids", "PRO", "517"); |
98 |
0 |
addNutrient("AminoAcids", "SER", "518"); |
99 |
0 |
addNutrient("AminoAcids", "HYP", "521"); |
100 |
|
|
101 |
0 |
addNutrient("Minerals", "calcium", "301"); |
102 |
0 |
addNutrient("Minerals", "copper", "312"); |
103 |
0 |
addNutrient("Minerals", "iron", "303"); |
104 |
0 |
addNutrient("Minerals", "magnesium", "304"); |
105 |
0 |
addNutrient("Minerals", "manganese", "315"); |
106 |
0 |
addNutrient("Minerals", "phosphorus", "305"); |
107 |
0 |
addNutrient("Minerals", "potassium", "306"); |
108 |
0 |
addNutrient("Minerals", "selenium", "317"); |
109 |
0 |
addNutrient("Minerals", "sodium", "307"); |
110 |
0 |
addNutrient("Minerals", "zinc", "309"); |
111 |
|
|
112 |
0 |
addNutrient("MacroNutrients", "kcals", "208"); |
113 |
0 |
addNutrient("MacroNutrients", "protein", "203"); |
114 |
0 |
addNutrient("MacroNutrients", "lipid", "204"); |
115 |
0 |
addNutrient("MacroNutrients", "carbs", "205"); |
116 |
0 |
addNutrient("MacroNutrients", "ash", "207"); |
117 |
0 |
addNutrient("MacroNutrients", "starch", "209"); |
118 |
0 |
addNutrient("MacroNutrients", "water", "255"); |
119 |
0 |
addNutrient("MacroNutrients", "kj", "268"); |
120 |
0 |
addNutrient("MacroNutrients", "sugar", "269"); |
121 |
0 |
addNutrient("MacroNutrients", "fiber", "291"); |
122 |
|
|
123 |
0 |
addNutrient("Vitamins", "vit_a", "318"); |
124 |
0 |
addNutrient("Vitamins", "retinol", "319"); |
125 |
0 |
addNutrient("Vitamins", "alpha_carotene", "322"); |
126 |
0 |
addNutrient("Vitamins", "beta_carotene", "321"); |
127 |
0 |
addNutrient("Vitamins", "beta_cryptoxanthin", "334"); |
128 |
0 |
addNutrient("Vitamins", "lycopene", "337"); |
129 |
0 |
addNutrient("Vitamins", "lutein", "338"); |
130 |
0 |
addNutrient("Vitamins", "vit_b6", "415"); |
131 |
0 |
addNutrient("Vitamins", "vit_b12", "418"); |
132 |
0 |
addNutrient("Vitamins", "vit_c", "401"); |
133 |
0 |
addNutrient("Vitamins", "vit_d", "324"); |
134 |
0 |
addNutrient("Vitamins", "vit_e", "323"); |
135 |
0 |
addNutrient("Vitamins", "beta_tocopherol", "341"); |
136 |
0 |
addNutrient("Vitamins", "delta_tocopherol", "343"); |
137 |
0 |
addNutrient("Vitamins", "gamma_tocopherol", "342"); |
138 |
0 |
addNutrient("Vitamins", "vit_k", "430"); |
139 |
0 |
addNutrient("Vitamins", "thiamin", "404"); |
140 |
0 |
addNutrient("Vitamins", "riboflavin", "405"); |
141 |
0 |
addNutrient("Vitamins", "niacin", "406"); |
142 |
0 |
addNutrient("Vitamins", "panto_acid", "410"); |
143 |
0 |
addNutrient("Vitamins", "folate", "417"); |
144 |
|
|
145 |
0 |
addNutrient("Lipids", "saturated", "606"); |
146 |
0 |
addNutrient("Lipids", "monounsaturated", "645"); |
147 |
0 |
addNutrient("Lipids", "polyunsaturated", "646"); |
148 |
0 |
addNutrient("Lipids", "transfats", "605"); |
149 |
0 |
addNutrient("Lipids", "cholesterol", "601"); |
150 |
0 |
addNutrient("Lipids", "phytosterol", "636"); |
151 |
|
|
152 |
|
|
153 |
0 |
} |
154 |
|
|
155 |
|
private static void addNutrient(String table, String tag, String nid) { |
156 |
0 |
USDANutrientInfo ni = new USDANutrientInfo(); |
157 |
0 |
ni.table = table; |
158 |
0 |
ni.tag = tag; |
159 |
0 |
getNutrientMap().put(nid, ni); |
160 |
0 |
} |
161 |
|
|
162 |
|
public static HashMap getNutrientMap() { |
163 |
0 |
if (nutrientMap == null) { |
164 |
0 |
nutrientMap = new HashMap(); |
165 |
0 |
makeNutrientMap(); |
166 |
|
} |
167 |
0 |
return nutrientMap; |
168 |
|
} |
169 |
|
|
170 |
|
public static class USDANutrientInfo { |
171 |
|
public USDANutrientInfo() {} |
172 |
|
String table; |
173 |
|
String tag; |
174 |
|
} |
175 |
|
|
176 |
|
} |