comparison src/main/java/de/intevation/lada/data/importer/LAFImporter.java @ 321:5844d7457dde

Completed importer for LAF format. Ort objects still need some attention.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 23 Aug 2013 11:35:24 +0200
parents 821557a17e5e
children 5d11428e6a09
comparison
equal deleted inserted replaced
320:6621f7345c06 321:5844d7457dde
1 package de.intevation.lada.data.importer; 1 package de.intevation.lada.data.importer;
2 2
3 import java.math.BigInteger;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import javax.ejb.Stateless;
9 import javax.ejb.TransactionAttribute;
10 import javax.ejb.TransactionAttributeType;
11 import javax.inject.Inject;
3 import javax.inject.Named; 12 import javax.inject.Named;
13 import javax.persistence.EntityManager;
14 import javax.persistence.Query;
4 15
5 import de.intevation.lada.auth.AuthenticationResponse; 16 import de.intevation.lada.auth.AuthenticationResponse;
17 import de.intevation.lada.data.Repository;
18 import de.intevation.lada.model.LKommentarM;
19 import de.intevation.lada.model.LKommentarP;
20 import de.intevation.lada.model.LMessung;
21 import de.intevation.lada.model.LMesswert;
22 import de.intevation.lada.model.LOrt;
23 import de.intevation.lada.model.LProbe;
6 import de.intevation.lada.rest.Response; 24 import de.intevation.lada.rest.Response;
25 import de.intevation.lada.validation.ValidationException;
26 import de.intevation.lada.validation.Validator;
7 27
8 @Named("lafimporter") 28 @Named("lafimporter")
29 @Stateless
9 public class LAFImporter 30 public class LAFImporter
10 implements Importer 31 implements Importer
11 { 32 {
33
34 @Inject
35 private EntityManager em;
36
37 @Inject
38 private LAFParser parser;
39
40 @Inject
41 @Named("lprobevalidator")
42 private Validator probeValidator;
43 @Inject
44 @Named("lmessungvalidator")
45 private Validator messungValidator;
46 @Inject
47 @Named("lortvalidator")
48 private Validator ortValidator;
49 @Inject
50 @Named("lmesswertvalidator")
51 private Validator messwertValidator;
52
53 @Inject
54 @Named("lproberepository")
55 private Repository probeRepository;
56 @Inject
57 @Named("lmessungrepository")
58 private Repository messungRepository;
59 @Inject
60 @Named("lortrepository")
61 private Repository ortRepository;
62 @Inject
63 @Named("lkommentarRepository")
64 private Repository pKommentarRepository;
65 @Inject
66 @Named("lkommentarmrepository")
67 private Repository mKommentarRepository;
68 @Inject
69 @Named("lmesswertrepository")
70 private Repository messwertRepository;
71
72 private Map<String, Map<String, Integer>> warnings;
73 private Map<String, Map<String, Integer>> errors;
74
75 public LAFImporter() {
76 warnings = new HashMap<String, Map<String, Integer>>();
77 errors = new HashMap<String, Map<String, Integer>>();
78 }
79
80 /**
81 * @return the warnings
82 */
83 public Map<String, Map<String, Integer>> getWarnings() {
84 return warnings;
85 }
86
87 /**
88 * @return the errors
89 */
90 public Map<String, Map<String, Integer>> getErrors() {
91 return errors;
92 }
93
12 @Override 94 @Override
13 public Response importData(String content, AuthenticationResponse auth) { 95 public boolean importData(String content, AuthenticationResponse auth) {
14 return new Response(true, 200, null); 96 try {
97 boolean success = parser.parse(content);
98 if (success) {
99 List<LProbe> proben = parser.getProben();
100 List<LMessung> messungen = parser.getMessungen();
101 List<LOrt> orte = parser.getOrte();
102 List<LKommentarP> pKommentare = parser.getProbeKommentare();
103 List<LKommentarM> mKommentare = parser.getMessungKommentare();
104 List<LMesswert> messwerte = parser.getMesswerte();
105 writeProben(auth, proben);
106 writeMessungen(auth, messungen);
107 //writeOrte(auth, orte);
108 writePKommentare(auth, pKommentare);
109 writeMKommentare(auth, mKommentare);
110 writeMesswerte(auth, messwerte);
111 }
112 else {
113 Map<String, Integer> err = new HashMap<String, Integer>();
114 err.put("no success", 660);
115 errors.put("parser", err);
116 return false;
117 }
118 }
119 catch (LAFParserException e) {
120 Map<String, Integer> err = new HashMap<String, Integer>();
121 err.put(e.getMessage(), 660);
122 errors.put("parser", err);
123 return false;
124 }
125 Map<String, Map<String, Map<String, Integer>>> data =
126 new HashMap<String, Map<String,Map<String, Integer>>>();
127 data.put("warnings", warnings);
128 data.put("errors", errors);
129 return true;
130 }
131
132 private void writeMessungen(
133 AuthenticationResponse auth,
134 List<LMessung> messungen
135 ) {
136 for(LMessung messung: messungen) {
137 try {
138
139 Map<String, Integer> warn =
140 messungValidator.validate(messung, false);
141 messungRepository.create(messung);
142 if (warn != null) {
143 warnings.put(
144 messung.getMessungsId().toString(),
145 warn);
146 }
147 }
148 catch (ValidationException e) {
149 errors.put(messung.getProbeId(), e.getErrors());
150 warnings.put(
151 messung.getProbeId(),
152 e.getWarnings());
153 }
154 }
155 }
156
157 @TransactionAttribute(TransactionAttributeType.REQUIRED)
158 private void writeMesswerte(
159 AuthenticationResponse auth,
160 List<LMesswert> messwerte
161 ) {
162 for(LMesswert messwert: messwerte) {
163 try {
164 Map<String, Integer> warn =
165 messwertValidator.validate(messwert, false);
166 Response r = messwertRepository.create(messwert);
167 if (warn != null) {
168 warnings.put(
169 messwert.getProbeId(),
170 warn);
171 }
172 }
173 catch (ValidationException e) {
174 errors.put(messwert.getProbeId(), e.getErrors());
175 warnings.put(
176 messwert.getProbeId(),
177 e.getWarnings());
178 }
179 }
180 }
181
182 @TransactionAttribute(TransactionAttributeType.REQUIRED)
183 private void writePKommentare(
184 AuthenticationResponse auth,
185 List<LKommentarP> kommentare
186 ) {
187 for(LKommentarP kommentar: kommentare) {
188 try {
189 pKommentarRepository.create(kommentar);
190 }
191 catch(Exception e) {
192 Map<String, Integer> err = new HashMap<String, Integer>();
193 err.put(
194 kommentar.getProbeId() + " - " +
195 kommentar.getkId(), 661);
196 errors.put("lkommentarp", err);
197 }
198 }
199 }
200
201 @TransactionAttribute(TransactionAttributeType.REQUIRED)
202 private void writeMKommentare(
203 AuthenticationResponse auth,
204 List<LKommentarM> kommentare
205 ) {
206 for(LKommentarM kommentar: kommentare) {
207 Query q =
208 em.createNativeQuery(
209 "select nextval('kommentar_m_id_seq')");
210 BigInteger seqId = (BigInteger)q.getSingleResult();
211 kommentar.getId().setKId(seqId.intValue());
212 mKommentarRepository.create(kommentar);
213 }
214 }
215
216 @TransactionAttribute(TransactionAttributeType.REQUIRED)
217 private void writeOrte(
218 AuthenticationResponse auth,
219 List<LOrt> orte
220 ) {
221 for(LOrt ort: orte) {
222 try {
223 Map<String, Integer> warn =
224 ortValidator.validate(ort, false);
225 ortRepository.create(ort);
226 if (warn != null) {
227 warnings.put(String.valueOf(ort.getOrtId()), warn);
228 }
229 }
230 catch (ValidationException e) {
231 errors.put(String.valueOf(ort.getOrtId()), e.getErrors());
232 warnings.put(
233 String.valueOf(ort.getOrtId()),
234 e.getWarnings());
235 }
236 }
237 }
238
239 @TransactionAttribute(TransactionAttributeType.REQUIRED)
240 private void writeProben(AuthenticationResponse auth, List<LProbe> proben) {
241 for (LProbe probe: proben) {
242 if (!authorized(probe, auth)) {
243 Map<String, Integer> err = new HashMap<String, Integer>();
244 err.put("not authorized", 699);
245 errors.put(probe.getProbeId(), err);
246 continue;
247 }
248 try {
249 Map<String, Integer> warn =
250 probeValidator.validate(probe, false);
251 if (warn != null) {
252 warnings.put(probe.getProbeId(), warn);
253 }
254 }
255 catch (ValidationException e) {
256 errors.put(probe.getProbeId(), e.getErrors());
257 warnings.put(probe.getProbeId(), e.getWarnings());
258 continue;
259 }
260 persist(probe);
261 }
262 }
263
264 private boolean authorized(LProbe probe, AuthenticationResponse auth) {
265 if (auth.getNetzbetreiber().contains(probe.getNetzbetreiberId()) &&
266 auth.getMst().contains(probe.getMstId())) {
267 return true;
268 }
269 return false;
270 }
271
272 @TransactionAttribute(TransactionAttributeType.REQUIRED)
273 private void persist(LProbe probe) {
274 String queryColumns = "insert into l_probe (probe_id, ba_id, test," +
275 " datenbasis_id, netzbetreiber_id, mst_id, probenart_id, umw_id";
276 String queryParameter = " values (:probe_id, :ba_id, :test," +
277 " :datenbasis_id, :netzbetreiber_id, :mst_id, :probenart_id," +
278 " :umw_id";
279 if (probe.getErzeugerId() != null) {
280 queryColumns += ", erzeuger_id";
281 queryParameter += ", :erzeuger_id";
282 }
283 if (probe.getHauptprobenNr() != null) {
284 queryColumns += ", hauptproben_nr";
285 queryParameter += ", :hauptproben_nr";
286 }
287 if (probe.getLetzteAenderung() != null) {
288 queryColumns += ", letzte_aenderung";
289 queryParameter += ", :letzte_aenderung";
290 }
291 if (probe.getMedia() != null) {
292 queryColumns += ", media";
293 queryParameter += ", :media";
294 }
295 if (probe.getMediaDesk() != null) {
296 queryColumns += ", media_desk";
297 queryParameter += ", :media_desk";
298 }
299 if (probe.getMittelungsdauer() != null) {
300 queryColumns += ", mittelungsdauer";
301 queryParameter += ", :mittelungsdauer";
302 }
303 if (probe.getMpKat() != null) {
304 queryColumns += ", mp_kat";
305 queryParameter += ", mp_kat";
306 }
307 if (probe.getMplId() != null) {
308 queryColumns += ", mpl_id";
309 queryParameter += ", :mpl_id";
310 }
311 if (probe.getMprId() != null) {
312 queryColumns += ", mpr_id";
313 queryParameter += ", :mpr_id";
314 }
315 if (probe.getProbeNehmerId() != null) {
316 queryColumns += ", probe_nehmer_id";
317 queryParameter += ", :probe_nehmer_id";
318 }
319 if (probe.getProbeentnahmeBeginn() != null) {
320 queryColumns += ", probeentnahme_beginn";
321 queryParameter += ", :probeentnahme_beginn";
322 }
323 if (probe.getProbeentnahmeEnde() != null) {
324 queryColumns += ", probeentnahme_ende";
325 queryParameter += ", :probeentnahme_ende";
326 }
327 if (probe.getSolldatumBeginn() != null) {
328 queryColumns += ", solldatum_beginn";
329 queryParameter += ", :solldatum_beginn";
330 }
331 if (probe.getSolldatumEnde() != null) {
332 queryColumns += ", solldatum_ende";
333 queryParameter += ", :solldatum_ende";
334 }
335 queryColumns += ") " + queryParameter + ")";
336
337 Query insert = em.createNativeQuery(queryColumns);
338 insert.setParameter("probe_id", probe.getProbeId());
339 insert.setParameter("ba_id", probe.getBaId());
340 insert.setParameter("datenbasis_id", probe.getDatenbasisId());
341 insert.setParameter("mst_id", probe.getMstId());
342 insert.setParameter("netzbetreiber_id", probe.getNetzbetreiberId());
343 insert.setParameter("probenart_id", probe.getProbenartId());
344 insert.setParameter("test", probe.isTest());
345 insert.setParameter("umw_id", probe.getUmwId());
346 if (probe.getErzeugerId() != null) {
347 insert.setParameter("erzeuger_id", probe.getErzeugerId());
348 }
349 if (probe.getHauptprobenNr() != null) {
350 insert.setParameter("hauptproben_nr", probe.getHauptprobenNr());
351 }
352 if (probe.getLetzteAenderung() != null) {
353 insert.setParameter("letzte_aenderung", probe.getLetzteAenderung());
354 }
355 if (probe.getMedia() != null) {
356 insert.setParameter("media", probe.getMedia());
357 }
358 if (probe.getMediaDesk() != null) {
359 insert.setParameter("media_desk", probe.getMediaDesk());
360 }
361 if (probe.getMittelungsdauer() != null) {
362 insert.setParameter("mittelungsdauer", probe.getMittelungsdauer());
363 }
364 if (probe.getMpKat() != null) {
365 insert.setParameter("mp_kat", probe.getMpKat());
366 }
367 if (probe.getMplId() != null) {
368 insert.setParameter("mpl_id", probe.getMplId());
369 }
370 if (probe.getMprId() != null) {
371 insert.setParameter("mpr_id", probe.getMprId());
372 }
373 if (probe.getProbeNehmerId() != null) {
374 insert.setParameter("probe_nehmer_id", probe.getProbeNehmerId());
375 }
376 if (probe.getProbeentnahmeBeginn() != null) {
377 insert.setParameter("probeentnahme_beginn", probe.getProbeentnahmeBeginn());
378 }
379 if (probe.getProbeentnahmeEnde() != null) {
380 insert.setParameter("probeentnahme_ende", probe.getProbeentnahmeEnde());
381 }
382 if (probe.getSolldatumBeginn() != null) {
383 insert.setParameter("solldatum_beginn", probe.getSolldatumBeginn());
384 }
385 if (probe.getSolldatumEnde() != null) {
386 insert.setParameter("solldatum_ende", probe.getSolldatumEnde());
387 }
388 int res = insert.executeUpdate();
389 int i = res;
15 } 390 }
16 } 391 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)