Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/data/importer/LAFImporter.java @ 331:5d11428e6a09
Made the importer a little more robust and introduced a better warning/error reporting.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Tue, 27 Aug 2013 15:28:21 +0200 |
parents | 5844d7457dde |
children | cb47c33b119d |
comparison
equal
deleted
inserted
replaced
330:046cdc094c72 | 331:5d11428e6a09 |
---|---|
1 package de.intevation.lada.data.importer; | 1 package de.intevation.lada.data.importer; |
2 | 2 |
3 import java.math.BigInteger; | 3 import java.math.BigInteger; |
4 import java.util.ArrayList; | |
4 import java.util.HashMap; | 5 import java.util.HashMap; |
6 import java.util.LinkedList; | |
5 import java.util.List; | 7 import java.util.List; |
6 import java.util.Map; | 8 import java.util.Map; |
7 | 9 |
8 import javax.ejb.Stateless; | 10 import javax.ejb.Stateless; |
9 import javax.ejb.TransactionAttribute; | 11 import javax.ejb.TransactionAttribute; |
67 private Repository mKommentarRepository; | 69 private Repository mKommentarRepository; |
68 @Inject | 70 @Inject |
69 @Named("lmesswertrepository") | 71 @Named("lmesswertrepository") |
70 private Repository messwertRepository; | 72 private Repository messwertRepository; |
71 | 73 |
72 private Map<String, Map<String, Integer>> warnings; | 74 private Map<String, List<ReportData>> warnings; |
73 private Map<String, Map<String, Integer>> errors; | 75 private Map<String, List<ReportData>> errors; |
74 | 76 |
75 public LAFImporter() { | 77 public LAFImporter() { |
76 warnings = new HashMap<String, Map<String, Integer>>(); | 78 warnings = new HashMap<String, List<ReportData>>(); |
77 errors = new HashMap<String, Map<String, Integer>>(); | 79 errors = new HashMap<String, List<ReportData>>(); |
78 } | 80 } |
79 | 81 |
80 /** | 82 /** |
81 * @return the warnings | 83 * @return the warnings |
82 */ | 84 */ |
83 public Map<String, Map<String, Integer>> getWarnings() { | 85 public Map<String, List<ReportData>> getWarnings() { |
84 return warnings; | 86 return warnings; |
85 } | 87 } |
86 | 88 |
87 /** | 89 /** |
88 * @return the errors | 90 * @return the errors |
89 */ | 91 */ |
90 public Map<String, Map<String, Integer>> getErrors() { | 92 public Map<String, List<ReportData>> getErrors() { |
91 return errors; | 93 return errors; |
92 } | 94 } |
93 | 95 |
94 @Override | 96 @Override |
95 public boolean importData(String content, AuthenticationResponse auth) { | 97 public boolean importData(String content, AuthenticationResponse auth) { |
98 this.warnings.clear(); | |
99 this.errors.clear(); | |
100 this.parser.reset(); | |
96 try { | 101 try { |
97 boolean success = parser.parse(content); | 102 boolean success = parser.parse(content); |
98 if (success) { | 103 if (success) { |
99 List<LProbe> proben = parser.getProben(); | 104 List<LProbe> proben = parser.getProben(); |
100 List<LMessung> messungen = parser.getMessungen(); | 105 List<LMessung> messungen = parser.getMessungen(); |
108 writePKommentare(auth, pKommentare); | 113 writePKommentare(auth, pKommentare); |
109 writeMKommentare(auth, mKommentare); | 114 writeMKommentare(auth, mKommentare); |
110 writeMesswerte(auth, messwerte); | 115 writeMesswerte(auth, messwerte); |
111 } | 116 } |
112 else { | 117 else { |
113 Map<String, Integer> err = new HashMap<String, Integer>(); | 118 List<ReportData> report = new ArrayList<ReportData>(); |
114 err.put("no success", 660); | 119 report.add( new ReportData("parser", "no success", 660)); |
115 errors.put("parser", err); | 120 errors.put("parser", report); |
116 return false; | 121 return false; |
117 } | 122 } |
118 } | 123 } |
119 catch (LAFParserException e) { | 124 catch (LAFParserException e) { |
120 Map<String, Integer> err = new HashMap<String, Integer>(); | 125 List<ReportData> report = new ArrayList<ReportData>(); |
121 err.put(e.getMessage(), 660); | 126 report.add(new ReportData("parser", e.getMessage(), 660)); |
122 errors.put("parser", err); | 127 errors.put("parser", report); |
123 return false; | 128 return false; |
124 } | 129 } |
125 Map<String, Map<String, Map<String, Integer>>> data = | 130 Map<String, Map<String, List<ReportData>>> data = |
126 new HashMap<String, Map<String,Map<String, Integer>>>(); | 131 new HashMap<String, Map<String, List<ReportData>>>(); |
132 this.warnings.putAll(this.parser.getWarnings()); | |
133 this.errors.putAll(this.parser.getErrors()); | |
127 data.put("warnings", warnings); | 134 data.put("warnings", warnings); |
128 data.put("errors", errors); | 135 data.put("errors", errors); |
129 return true; | 136 return true; |
130 } | 137 } |
131 | 138 |
138 | 145 |
139 Map<String, Integer> warn = | 146 Map<String, Integer> warn = |
140 messungValidator.validate(messung, false); | 147 messungValidator.validate(messung, false); |
141 messungRepository.create(messung); | 148 messungRepository.create(messung); |
142 if (warn != null) { | 149 if (warn != null) { |
143 warnings.put( | 150 for (String key : warn.keySet()) { |
144 messung.getMessungsId().toString(), | 151 // warnings.put(messung.getProbeId(), |
145 warn); | 152 // new ReportData(key, "", warn.get(key))); |
153 } | |
146 } | 154 } |
147 } | 155 } |
148 catch (ValidationException e) { | 156 catch (ValidationException e) { |
149 errors.put(messung.getProbeId(), e.getErrors()); | 157 //errors.put(messung.getProbeId(), e.getErrors()); |
150 warnings.put( | 158 //warnings.put( |
151 messung.getProbeId(), | 159 // messung.getProbeId(), |
152 e.getWarnings()); | 160 // e.getWarnings()); |
153 } | 161 } |
154 } | 162 } |
155 } | 163 } |
156 | 164 |
157 @TransactionAttribute(TransactionAttributeType.REQUIRED) | 165 @TransactionAttribute(TransactionAttributeType.REQUIRED) |
163 try { | 171 try { |
164 Map<String, Integer> warn = | 172 Map<String, Integer> warn = |
165 messwertValidator.validate(messwert, false); | 173 messwertValidator.validate(messwert, false); |
166 Response r = messwertRepository.create(messwert); | 174 Response r = messwertRepository.create(messwert); |
167 if (warn != null) { | 175 if (warn != null) { |
168 warnings.put( | 176 // warnings.put( |
169 messwert.getProbeId(), | 177 // messwert.getProbeId(), |
170 warn); | 178 // warn); |
171 } | 179 } |
172 } | 180 } |
173 catch (ValidationException e) { | 181 catch (ValidationException e) { |
174 errors.put(messwert.getProbeId(), e.getErrors()); | 182 //errors.put(messwert.getProbeId(), e.getErrors()); |
175 warnings.put( | 183 //warnings.put( |
176 messwert.getProbeId(), | 184 // messwert.getProbeId(), |
177 e.getWarnings()); | 185 // e.getWarnings()); |
178 } | 186 } |
179 } | 187 } |
180 } | 188 } |
181 | 189 |
182 @TransactionAttribute(TransactionAttributeType.REQUIRED) | 190 @TransactionAttribute(TransactionAttributeType.REQUIRED) |
191 catch(Exception e) { | 199 catch(Exception e) { |
192 Map<String, Integer> err = new HashMap<String, Integer>(); | 200 Map<String, Integer> err = new HashMap<String, Integer>(); |
193 err.put( | 201 err.put( |
194 kommentar.getProbeId() + " - " + | 202 kommentar.getProbeId() + " - " + |
195 kommentar.getkId(), 661); | 203 kommentar.getkId(), 661); |
196 errors.put("lkommentarp", err); | 204 //errors.put("lkommentarp", err); |
197 } | 205 } |
198 } | 206 } |
199 } | 207 } |
200 | 208 |
201 @TransactionAttribute(TransactionAttributeType.REQUIRED) | 209 @TransactionAttribute(TransactionAttributeType.REQUIRED) |
222 try { | 230 try { |
223 Map<String, Integer> warn = | 231 Map<String, Integer> warn = |
224 ortValidator.validate(ort, false); | 232 ortValidator.validate(ort, false); |
225 ortRepository.create(ort); | 233 ortRepository.create(ort); |
226 if (warn != null) { | 234 if (warn != null) { |
227 warnings.put(String.valueOf(ort.getOrtId()), warn); | 235 // warnings.put(String.valueOf(ort.getOrtId()), warn); |
228 } | 236 } |
229 } | 237 } |
230 catch (ValidationException e) { | 238 catch (ValidationException e) { |
231 errors.put(String.valueOf(ort.getOrtId()), e.getErrors()); | 239 //errors.put(String.valueOf(ort.getOrtId()), e.getErrors()); |
232 warnings.put( | 240 //warnings.put( |
233 String.valueOf(ort.getOrtId()), | 241 // String.valueOf(ort.getOrtId()), |
234 e.getWarnings()); | 242 // e.getWarnings()); |
235 } | 243 } |
236 } | 244 } |
237 } | 245 } |
238 | 246 |
239 @TransactionAttribute(TransactionAttributeType.REQUIRED) | 247 @TransactionAttribute(TransactionAttributeType.REQUIRED) |
240 private void writeProben(AuthenticationResponse auth, List<LProbe> proben) { | 248 private void writeProben(AuthenticationResponse auth, List<LProbe> proben) { |
241 for (LProbe probe: proben) { | 249 for (LProbe probe: proben) { |
242 if (!authorized(probe, auth)) { | 250 if (!authorized(probe, auth)) { |
243 Map<String, Integer> err = new HashMap<String, Integer>(); | 251 Map<String, Integer> err = new HashMap<String, Integer>(); |
244 err.put("not authorized", 699); | 252 err.put("not authorized", 699); |
245 errors.put(probe.getProbeId(), err); | 253 //errors.put(probe.getProbeId(), err); |
246 continue; | 254 continue; |
247 } | 255 } |
248 try { | 256 try { |
249 Map<String, Integer> warn = | 257 Map<String, Integer> warn = |
250 probeValidator.validate(probe, false); | 258 probeValidator.validate(probe, false); |
251 if (warn != null) { | 259 if (warn != null) { |
252 warnings.put(probe.getProbeId(), warn); | 260 // warnings.put(probe.getProbeId(), warn); |
253 } | 261 } |
254 } | 262 } |
255 catch (ValidationException e) { | 263 catch (ValidationException e) { |
256 errors.put(probe.getProbeId(), e.getErrors()); | 264 //errors.put(probe.getProbeId(), e.getErrors()); |
257 warnings.put(probe.getProbeId(), e.getWarnings()); | 265 //warnings.put(probe.getProbeId(), e.getWarnings()); |
258 continue; | 266 continue; |
259 } | 267 } |
260 persist(probe); | 268 persist(probe); |
261 } | 269 } |
262 } | 270 } |