comparison src/main/java/de/intevation/lada/importer/laf/AttributeMapper.java @ 610:374a2e78cec5

Added importer impl for laf file format.
author Raimund Renkert <raimund.renkert@intevation.de>
date Thu, 16 Apr 2015 15:49:04 +0200
parents
children 3f57484e06f1
comparison
equal deleted inserted replaced
609:093bfdcdb09c 610:374a2e78cec5
1 package de.intevation.lada.importer.laf;
2
3 import java.math.BigInteger;
4 import java.sql.Timestamp;
5 import java.text.DateFormat;
6 import java.text.ParseException;
7 import java.text.SimpleDateFormat;
8 import java.util.ArrayList;
9 import java.util.Date;
10 import java.util.List;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13
14 import javax.ejb.Stateless;
15 import javax.inject.Inject;
16 import javax.persistence.Query;
17
18 import org.apache.log4j.Logger;
19
20 import de.intevation.lada.importer.ReportItem;
21 import de.intevation.lada.model.land.LKommentarM;
22 import de.intevation.lada.model.land.LKommentarP;
23 import de.intevation.lada.model.land.LMessung;
24 import de.intevation.lada.model.land.LMesswert;
25 import de.intevation.lada.model.land.LProbe;
26 import de.intevation.lada.model.land.LZusatzWert;
27 import de.intevation.lada.model.land.MessungTranslation;
28 import de.intevation.lada.model.land.ProbeTranslation;
29 import de.intevation.lada.util.annotation.RepositoryConfig;
30 import de.intevation.lada.util.data.QueryBuilder;
31 import de.intevation.lada.util.data.Repository;
32 import de.intevation.lada.util.data.RepositoryType;
33 import de.intevation.lada.util.rest.Response;
34
35 /**
36 * The AttributeMapper is used to set object attributes via string based
37 * key value pairs. The key represents a member of an entity object.
38 *
39 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
40 */
41 @Stateless
42 public class AttributeMapper
43 {
44
45 @Inject
46 private Logger logger;
47
48 @Inject
49 @RepositoryConfig(type=RepositoryType.RW)
50 private Repository repository;
51
52 private List<ReportItem> warnings;
53 private List<ReportItem> errors;
54
55 /**
56 * Default constructor to create a new AttributeMapper object.
57 */
58 public AttributeMapper() {
59 this.warnings = new ArrayList<ReportItem>();
60 this.errors = new ArrayList<ReportItem>();
61 }
62
63 /**
64 * Add an attribute to the given LProbe object.
65 *
66 * @param key The key mapping to a object member.
67 * @param value The value to set.
68 * @param probe The entity object.
69 * @return The updated entity object.
70 */
71 public LProbe addAttribute(String key, Object value, LProbe probe) {
72 DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm");
73 if ("datenbasis_s".equals(key) && probe.getDatenbasisId() == null) {
74 Integer v = Integer.valueOf(value.toString());
75 probe.setDatenbasisId(v);
76 }
77 else if ("datenbasis_s".equals(key) && probe.getDatenbasisId() != null){
78 this.warnings.add(new ReportItem(key, value.toString(), 672));
79 }
80
81 if ("datenbasis".equals(key) && probe.getDatenbasisId() == null) {
82 String nativeQuery = "select id from stammdaten.datenbasis where datenbasis = '";
83 nativeQuery += value.toString() + "'";
84 Query query = repository.entityManager("land").createNativeQuery(nativeQuery);
85 List<Object[]> result = query.getResultList();
86
87 Integer v = Integer.valueOf(result.get(0)[0].toString());
88 probe.setDatenbasisId(v);
89 }
90 else if ("datenbasis".equals(key) && probe.getDatenbasisId() != null){
91 this.warnings.add(new ReportItem(key, value.toString(), 672));
92 }
93
94 if ("probe_id".equals(key)) {
95 QueryBuilder<ProbeTranslation> builder =
96 new QueryBuilder<ProbeTranslation>(
97 repository.entityManager("land"), ProbeTranslation.class);
98 builder.and("probeIdAlt", value);
99 Response response =
100 repository.filter(builder.getQuery(), "land");
101 List<ProbeTranslation> info = (List<ProbeTranslation>)response.getData();
102 logger.debug("found " + info.size() + " items");
103 if (info != null && info.size() > 0) {
104 logger.debug("found probe with old id: " + value);
105 errors.add(new ReportItem("probe_id", value.toString(), 671));
106 return null;
107 }
108 probe.setTest(true);
109 probe.setProbenartId(1);
110 repository.create(probe, "land");
111 ProbeTranslation trans = new ProbeTranslation();
112
113 logger.debug("###### probeidalt: " + value);
114 trans.setProbeIdAlt(value.toString());
115 //Query q = repository.queryFromString("select nextval('bund.probe_id_seq')");
116 //BigInteger id = (BigInteger)q.getSingleResult();
117 //probe.setId(id.intValue());
118 //logger.debug("id of the new probe: " + id);
119 trans.setProbeId(probe);
120 repository.create(trans, "land");
121 }
122
123 if ("hauptprobennummer".equals(key)) {
124 probe.setHauptprobenNr(value.toString());
125 }
126
127 if ("mpr_id".equals(key)) {
128 Integer v = Integer.valueOf(value.toString());
129 probe.setMprId(v);
130 }
131
132 if ("netzkennung".equals(key)) {
133 probe.setNetzbetreiberId(value.toString());
134 }
135
136 if ("messstelle".equals(key)) {
137 probe.setMstId(value.toString());
138 }
139
140 if ("messprogramm_s".equals(key) && probe.getBaId() == null) {
141 probe.setBaId(value.toString());
142 }
143 else if ("messprogramm_s".equals(key) && probe.getBaId() != null){
144 this.warnings.add(new ReportItem(key, value.toString(), 672));
145 }
146
147 if ("soll_datum_uhrzeit_a".equals(key)) {
148 try {
149 Date d = format.parse(value.toString());
150 probe.setSolldatumBeginn(new Timestamp(d.getTime()));
151 }
152 catch (ParseException e) {
153 this.warnings.add(new ReportItem(key, value.toString(), 674));
154 }
155 }
156 if ("soll_datum_uhrzeit_e".equals(key)) {
157 try {
158 Date d = format.parse(value.toString());
159 probe.setSolldatumEnde(new Timestamp(d.getTime()));
160 }
161 catch (ParseException e) {
162 this.warnings.add(new ReportItem(key, value.toString(), 674));
163 }
164 }
165 if ("probenahme_datum_uhrzeit_a".equals(key)) {
166 try {
167 Date d = format.parse(value.toString());
168 probe.setProbeentnahmeBeginn(new Timestamp(d.getTime()));
169 }
170 catch (ParseException e) {
171 this.warnings.add(new ReportItem(key, value.toString(), 674));
172 }
173 }
174 if ("probenahme_datum_uhrzeit_e".equals(key)) {
175 try {
176 Date d = format.parse(value.toString());
177 probe.setProbeentnahmeEnde(new Timestamp(d.getTime()));
178 }
179 catch (ParseException e) {
180 this.warnings.add(new ReportItem(key, value.toString(), 674));
181 }
182 }
183
184 if ("umweltbereich_s".equals(key) && probe.getUmwId() == null) {
185 probe.setUmwId(value.toString());
186 }
187 else if ("umweltbereich_s".equals(key) && probe.getUmwId() != null){
188 this.warnings.add(new ReportItem(key, value.toString(), 672));
189 }
190 if ("umweltbereich_c".equals(key) && probe.getUmwId() == null) {
191 String nativeQuery = "select id from stammdaten.umwelt where umwelt_bereich= '";
192 int length = value.toString().length() > 80 ? 80 : value.toString().length();
193 nativeQuery += value.toString().substring(0, length) + "'";
194 Query query = repository.entityManager("land").createNativeQuery(nativeQuery);
195 List<Object[]> result = query.getResultList();
196 probe.setUmwId(result.get(0)[0].toString());
197 }
198 else if ("umweltbereich_c".equals(key) && probe.getUmwId() != null){
199 this.warnings.add(new ReportItem(key, value.toString(), 672));
200 }
201
202 if ("deskriptoren".equals(key)) {
203 probe.setMediaDesk(value.toString());
204 }
205
206 if ("testdaten".equals(key)) {
207 if (!value.toString().equals("0")) {
208 probe.setTest(true);
209 }
210 else {
211 probe.setTest(false);
212 }
213 }
214
215 if ("medium".equals(key)) {
216 probe.setMedia(value.toString());
217 }
218
219 if ("probenart".equals(key)) {
220 String nativeQuery = "select id from stammdaten.probenart where probenart = '";
221 nativeQuery += value.toString() + "'";
222 Query query = repository.entityManager("land").createNativeQuery(nativeQuery);
223 List<Object> result = query.getResultList();
224 probe.setProbenartId(Integer.valueOf(result.get(0).toString()));
225 }
226 return probe;
227 }
228
229 /**
230 * Add an attribute to the given LKommentarP object.
231 *
232 * @param key The key mapping to a object member.
233 * @param value The value to set.
234 * @param kommentar The entity object.
235 * @return The updated entity object.
236 */
237 public LKommentarP addAttribute(
238 String key,
239 Object value,
240 LKommentarP kommentar
241 ) {
242 DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm");
243 String v = value.toString();
244 String erzeuger = v.substring(1, 6);
245 logger.debug("erzeuger is " + erzeuger);
246 String date = v.substring(8, 21);
247 Date d;
248 try {
249 d = format.parse(date);
250 kommentar.setDatum(new Timestamp(d.getTime()));
251 }
252 catch (ParseException e) {
253 this.warnings.add(new ReportItem(key, value.toString(), 674));
254 }
255 String text = v.substring(23, v.length() -1);
256 kommentar.setErzeuger(erzeuger);
257 kommentar.setText(text);
258 return kommentar;
259 }
260
261 /**
262 * Add an attribute to the given LKommentarM object.
263 *
264 * @param key The key mapping to a object member.
265 * @param value The value to set.
266 * @param kommentar The entity object.
267 * @return The updated entity object.
268 */
269 public LKommentarM addAttribute(
270 String key,
271 Object value,
272 LKommentarM kommentar
273 ) {
274 DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm");
275 String v = value.toString();
276 String erzeuger = v.substring(1, 6);
277 String date = v.substring(8, 21);
278 Date d;
279 try {
280 d = format.parse(date);
281 kommentar.setDatum(new Timestamp(d.getTime()));
282 }
283 catch (ParseException e) {
284 this.warnings.add(new ReportItem(key, value.toString(), 674));
285 }
286 String text = v.substring(23, v.length() -1);
287 kommentar.setErzeuger(erzeuger);
288 kommentar.setText(text);
289 return kommentar;
290 }
291
292 /**
293 * Add an attribute to the given LMessung object.
294 *
295 * @param key The key mapping to a object member.
296 * @param value The value to set.
297 * @param messung The entity object.
298 * @return The updated entity object.
299 */
300 public LMessung addAttribute(
301 String key,
302 Object value,
303 LMessung messung
304 ) {
305 DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm");
306 if ("nebenprobennummer".equals(key)) {
307 messung.setNebenprobenNr(value.toString());
308 }
309 else if ("mess_datum_uhrzeit".equals(key)) {
310 try {
311 Date d = format.parse(value.toString());
312 messung.setMesszeitpunkt(new Timestamp(d.getTime()));
313 }
314 catch (ParseException e) {
315 this.warnings.add(new ReportItem(key, value.toString(), 674));
316 }
317 }
318 else if ("messzeit_sekunden".equals(key)) {
319 Integer i = Integer.valueOf(value.toString());
320 messung.setMessdauer(i);
321 }
322 else if ("messmethode_s".equals(key)) {
323 messung.setMmtId(value.toString());
324 }
325 else if ("bearbeitungsstatus".equals(key)) {
326 //ignored.!?
327 }
328 else if ("erfassung_abgeschlossen".equals(key)) {
329 if(!value.toString().equals("0")) {
330 messung.setFertig(true);
331 }
332 else {
333 messung.setFertig(false);
334 }
335 }
336 return messung;
337 }
338
339 public MessungTranslation addAttribute(
340 String key,
341 Object value,
342 MessungTranslation mt
343 ) {
344 logger.debug("###### set messungsidalt");
345 if ("messungs_id".equals(key)) {
346 logger.debug("###### set messungsid alt: " + value);
347 mt.setMessungsIdAlt(Integer.valueOf(value.toString()));
348 }
349 return mt;
350 }
351
352 /**
353 * Add an attribute to the given LMesswert object.
354 *
355 * @param key The key mapping to a object member.
356 * @param value The value to set.
357 * @param messwert The entity object.
358 * @return The updated entity object.
359 */
360 public LMesswert addAttribute(
361 String key,
362 Object value,
363 LMesswert messwert
364 ) {
365 Pattern p = Pattern.compile(
366 "(\".+\")( .+ )(\".+\")( .*)( .{1,12})( .{1,9})(.{0,9})(.{0,3})");
367 //TODO Does not perfectly match... Use better matching for floats.
368 Matcher m = p.matcher(value.toString());
369 if (m.matches()) {
370 String messgroesse = m.group(1).substring(1, m.group(1).length() - 1);
371 String wert = m.group(2);
372 String einheit = m.group(3).substring(1, m.group(3).length() - 1);
373 if (wert.startsWith(" <")) {
374 wert = wert.substring(2);
375 messwert.setGrenzwertueberschreitung(false);
376 }
377 else if (wert.startsWith(" >")) {
378 wert = wert.substring(2);
379 messwert.setGrenzwertueberschreitung(true);
380 }
381 float fWert = Float.valueOf(wert);
382 messwert.setMesswert(fWert);
383
384 String nativeQuery = "select id from stammdaten.mess_einheit where einheit = '";
385 nativeQuery += einheit + "'";
386 Query query = repository.entityManager("land").createNativeQuery(nativeQuery);
387 List<Object> result = query.getResultList();
388
389 if (result.isEmpty()) {
390 this.errors.add(new ReportItem("messeinheit", "null", 673));
391 return null;
392 }
393 else {
394 messwert.setMehId((Integer)result.get(0));
395 }
396
397 String nativeQuery2 = "select id from stammdaten.messgroesse where messgroesse = '";
398 nativeQuery2 += messgroesse + "'";
399 Query query2 = repository.entityManager("land").createNativeQuery(nativeQuery2);
400 List<Object> result2 = query2.getResultList();
401
402 if (result2.isEmpty()) {
403 this.errors.add(new ReportItem("messgroesse", "null", 673));
404 return null;
405 }
406 else {
407 messwert.setMessgroesseId((Integer)result2.get(0));
408 }
409 }
410 return messwert;
411 }
412
413 /**
414 * Add an attribute to the OrtCreator. The creator is used to build the
415 * two objects Ort and LOrt.
416 *
417 * @param key The key mapping to a object member.
418 * @param value The value to set.
419 * @param ort The creator object.
420 * @return The updated creator object.
421 */
422 public OrtCreator addAttribute(
423 String key,
424 Object value,
425 OrtCreator ort
426 ) {
427 if ("ort_code".equals(key)) {
428 ort.setOrtCode(value.toString());
429 }
430 if ("ort_typ".equals(key)) {
431 ort.setOrtTyp(value.toString());
432 }
433 if ("ort_zusatz".equals(key)) {
434 ort.setZusatztext(value.toString());
435 }
436 if ("ort_land_lang".equals(key)) {
437 ort.setLandLang(value.toString());
438 }
439 if ("ort_land_kurz".equals(key)) {
440 ort.setLandKurz(value.toString());
441 }
442 if ("ort_land_s".equals(key)) {
443 ort.setLandS(value.toString());
444 }
445 if ("ort_gemeindeschlüssel".equals(key)) {
446 ort.setGemSchluessel(value.toString());
447 }
448 if ("ort_bezeichnung".equals(key)) {
449 ort.setBezeichnung(value.toString());
450 }
451 if ("ort_beschreibung".equals(key)) {
452 ort.setBeschreibung(value.toString());
453 }
454 if ("ort_nuts_code".equals(key)) {
455 ort.setNuts(value.toString());
456 }
457 if ("ort_hoehe_land".equals(key)) {
458 ort.setHoehe(value.toString());
459 }
460 if ("ort_koordinaten".equals(key)) {
461 ort.setKoordinaten(value.toString());
462 }
463 if ("ort_koordinaten_s".equals(key)) {
464 ort.setKoordinatenS(value.toString());
465 }
466 return ort;
467 }
468
469 /**
470 * Add an attribute to the given LZusatzwert object.
471 *
472 * @param lKey The key mapping to a object member.
473 * @param value The value to set.
474 * @param wert The entity object.
475 * @return The updated entity object.
476 */
477 public LZusatzWert addAttribute(
478 String lKey,
479 Object value,
480 LZusatzWert wert
481 ) {
482 String v = value.toString().substring(1);
483 int ndx = v.indexOf("\"");
484 String groesse = v.substring(0, ndx);
485 v = v.substring(ndx + 2);
486 ndx = v.indexOf(" ");
487 String w = v.substring(0, ndx);
488 v = v.substring(ndx + 2);
489 ndx = v.indexOf("\"");
490 String einheit = v.substring(0, ndx);
491 String fehler = v.substring(ndx + 2);
492
493 String nativeQuery = "select id from stammdaten.probenzusatz where zusatzwert = '";
494 nativeQuery += groesse + "'";
495 Query query = repository.entityManager("land").createNativeQuery(nativeQuery);
496 List<Object[]> result = query.getResultList();
497
498 if (result == null || result.isEmpty()) {
499 this.errors.add(new ReportItem(lKey, "zusatzwert", 673));
500 return null;
501 }
502 wert.setPzsId(result.get(0)[0].toString());
503 wert.setMesswertPzs(Float.valueOf(w));
504 wert.setMessfehler(Float.valueOf(fehler));
505 return wert;
506 }
507
508 /**
509 * Add an attribute to the given LZusatzwert object.
510 *
511 * @param lKey The key mapping to a object member.
512 * @param value The value to set.
513 * @param wert The entity object.
514 * @return The updated entity object.
515 */
516 public LZusatzWert addAttributeS(
517 String lKey,
518 Object value,
519 LZusatzWert wert
520 ) {
521 String v = value.toString().substring(1);
522 int ndx = v.indexOf("\"");
523 String groesse = v.substring(0, ndx);
524 v = v.substring(ndx + 2);
525 ndx = v.indexOf(" ");
526 String w = v.substring(0, ndx);
527 v = v.substring(ndx + 2);
528 ndx = v.indexOf(" ");
529 String einheit = v.substring(0, ndx);
530 String fehler = v.substring(ndx + 2);
531 wert.setPzsId(groesse);
532 wert.setMesswertPzs(Float.valueOf(w));
533 wert.setMessfehler(Float.valueOf(fehler));
534 return wert;
535 }
536
537 /**
538 * @return the warnings
539 */
540 public List<ReportItem> getWarnings() {
541 return warnings;
542 }
543
544 /**
545 * @return the errors
546 */
547 public List<ReportItem> getErrors() {
548 return errors;
549 }
550
551 public void reset() {
552 errors = new ArrayList<ReportItem>();
553 warnings = new ArrayList<ReportItem>();
554 }
555 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)