Mercurial > lada > lada-server
changeset 337:cb47c33b119d
Imporved error/warning handling and fixed some bugs in the importer module.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Wed, 28 Aug 2013 15:11:50 +0200 |
parents | 53417b61391c |
children | b6f770956cae |
files | src/main/java/de/intevation/lada/data/importer/AttributeMapper.java src/main/java/de/intevation/lada/data/importer/LAFImporter.java src/main/java/de/intevation/lada/data/importer/LAFParser.java src/main/java/de/intevation/lada/data/importer/LAFProducer.java src/main/java/de/intevation/lada/data/importer/Producer.java src/main/java/de/intevation/lada/rest/LAFImportService.java |
diffstat | 6 files changed, 336 insertions(+), 410 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/data/importer/AttributeMapper.java Wed Aug 28 15:10:19 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/importer/AttributeMapper.java Wed Aug 28 15:11:50 2013 +0200 @@ -17,6 +17,7 @@ import javax.inject.Named; import javax.persistence.EntityManager; +import de.intevation.lada.data.QueryBuilder; import de.intevation.lada.data.Repository; import de.intevation.lada.model.LKommentarM; import de.intevation.lada.model.LKommentarP; @@ -26,8 +27,14 @@ import de.intevation.lada.model.LOrt; import de.intevation.lada.model.LProbe; import de.intevation.lada.model.LProbeInfo; +import de.intevation.lada.model.LZusatzWert; +import de.intevation.lada.model.Ort; +import de.intevation.lada.model.SDatenbasis; import de.intevation.lada.model.SMessEinheit; import de.intevation.lada.model.SMessgroesse; +import de.intevation.lada.model.SProbenZusatz; +import de.intevation.lada.model.SProbenart; +import de.intevation.lada.model.SUmwelt; import de.intevation.lada.rest.Response; @Stateless @@ -41,6 +48,10 @@ private Repository probeRepo; @Inject + @Named("ortrepository") + private Repository ortRepo; + + @Inject @Named("readonlyrepository") private Repository sRepository; @@ -53,79 +64,125 @@ } public LProbe addAttribute(String key, Object value, LProbe probe) { DateFormat format = new SimpleDateFormat("yyyyMMdd HHmm"); - if ("datenbasis_s".equals(key)) { + if ("datenbasis_s".equals(key) && probe.getDatenbasisId() == null) { Integer v = Integer.valueOf(value.toString()); probe.setDatenbasisId(v); } - else if ("probe_id".equals(key)) { - if (probeRepo.findById(LProbeInfo.class, value.toString()) != null) { - errors.add(new ReportData("probe_id", value.toString(), 662)); + else if ("datenbasis_s".equals(key) && probe.getDatenbasisId() != null){ + this.warnings.add(new ReportData(key, value.toString(), 672)); + } + + if ("datenbasis".equals(key) && probe.getDatenbasisId() == null) { + QueryBuilder<SDatenbasis> builder = + new QueryBuilder<SDatenbasis>( + this.sRepository.getEntityManager(), SDatenbasis.class); + builder.and("datenbasis", value.toString()); + Response response = sRepository.filter(builder.getQuery()); + List<SDatenbasis> datenbasis = (List<SDatenbasis>)response.getData(); + Integer v = Integer.valueOf(datenbasis.get(0).getDatenbasisId()); + probe.setDatenbasisId(v); + } + else if ("datenbasis".equals(key) && probe.getDatenbasisId() != null){ + this.warnings.add(new ReportData(key, value.toString(), 672)); + } + + if ("probe_id".equals(key)) { + Response response = + probeRepo.findById(LProbeInfo.class, value.toString()); + List<LProbeInfo> info = (List<LProbeInfo>)response.getData(); + if (info != null && info.size() > 0) { + errors.add(new ReportData("probe_id", value.toString(), 671)); return null; } probe.setProbeId(value.toString()); } - else if ("hauptprobennummer".equals(key)) { + + if ("hauptprobennummer".equals(key)) { probe.setHauptprobenNr(value.toString()); } - else if ("mpr_id".equals(key)) { + + if ("mpr_id".equals(key)) { Integer v = Integer.valueOf(value.toString()); probe.setMprId(v); } - else if ("netzkennung".equals(key)) { + + if ("netzkennung".equals(key)) { probe.setNetzbetreiberId(value.toString()); } - else if ("messprogramm_land".equals(key)) { - probe.setMplId(value.toString()); - } - else if ("messstelle".equals(key)) { + + if ("messstelle".equals(key)) { probe.setMstId(value.toString()); } - else if ("betriebsart".equals(key)) { + + if ("messprogramm_s".equals(key) && probe.getBaId() == null) { probe.setBaId(value.toString()); } - else if ("soll_datum_uhrzeit_a".equals(key)) { + else if ("messprogramm_s".equals(key) && probe.getBaId() != null){ + this.warnings.add(new ReportData(key, value.toString(), 672)); + } + + if ("soll_datum_uhrzeit_a".equals(key)) { try { Date d = format.parse(value.toString()); probe.setSolldatumBeginn(d); } catch (ParseException e) { - //TODO handle warning. + this.warnings.add(new ReportData(key, value.toString(), 674)); } } - else if ("soll_datum_uhrzeit_e".equals(key)) { + if ("soll_datum_uhrzeit_e".equals(key)) { try { Date d = format.parse(value.toString()); probe.setSolldatumEnde(d); } catch (ParseException e) { - //TODO handle warning. + this.warnings.add(new ReportData(key, value.toString(), 674)); } } - else if ("probenahme_datum_uhrzeit_a".equals(key)) { + if ("probenahme_datum_uhrzeit_a".equals(key)) { try { Date d = format.parse(value.toString()); probe.setProbeentnahmeBeginn(d); } catch (ParseException e) { - //TODO handle warning. + this.warnings.add(new ReportData(key, value.toString(), 674)); } } - else if ("probenahme_datum_uhrzeit_e".equals(key)) { + if ("probenahme_datum_uhrzeit_e".equals(key)) { try { Date d = format.parse(value.toString()); probe.setProbeentnahmeEnde(d); } catch (ParseException e) { - //TODO handle warning. + this.warnings.add(new ReportData(key, value.toString(), 674)); } } - else if ("umweltbereich_s".equals(key)) { + + if ("umweltbereich_s".equals(key) && probe.getUmwId() == null) { probe.setUmwId(value.toString()); } - else if ("deskriptoren".equals(key)) { + else if ("umweltbereich_s".equals(key) && probe.getUmwId() != null){ + this.warnings.add(new ReportData(key, value.toString(), 672)); + } + if ("umweltbereich_c".equals(key) && probe.getUmwId() == null) { + QueryBuilder<SUmwelt> builder = + new QueryBuilder<SUmwelt>( + sRepository.getEntityManager(), SUmwelt.class); + int length = value.toString().length() > 80 ? 80 : value.toString().length(); + builder.and("umweltBereich", value.toString().substring(0, length)); + Response response = sRepository.filter(builder.getQuery()); + List<SUmwelt> umw = (List<SUmwelt>)response.getData(); + probe.setUmwId(umw.get(0).getUmwId()); + } + else if ("umweltbereich_c".equals(key) && probe.getUmwId() != null){ + this.warnings.add(new ReportData(key, value.toString(), 672)); + } + + if ("deskriptoren".equals(key)) { probe.setMediaDesk(value.toString()); } - else if ("testdaten".equals(key)) { + + if ("testdaten".equals(key)) { if (!value.toString().equals("0")) { probe.setTest(true); } @@ -133,8 +190,20 @@ probe.setTest(false); } } - - probe.setProbenartId(1); + + if ("medium".equals(key)) { + probe.setMedia(value.toString()); + } + + if ("probenart".equals(key)) { + QueryBuilder<SProbenart> builder = + new QueryBuilder<SProbenart>( + sRepository.getEntityManager(), SProbenart.class); + builder.and("probenart", value.toString()); + Response response = sRepository.filter(builder.getQuery()); + List<SProbenart> art = (List<SProbenart>)response.getData(); + probe.setProbenartId(Integer.valueOf(art.get(0).getProbenartId())); + } return probe; } @@ -153,7 +222,7 @@ kommentar.setKDatum(d); } catch (ParseException e) { - //TODO: handle warning. + this.warnings.add(new ReportData(key, values.toString(), 674)); } String text = v.substring(23, v.length() -1); kommentar.setErzeuger(erzeuger); @@ -176,7 +245,7 @@ kommentar.setKDatum(d); } catch (ParseException e) { - //TODO: handle warning. + this.warnings.add(new ReportData(key, values.toString(), 674)); } String text = v.substring(23, v.length() -1); kommentar.setErzeuger(erzeuger); @@ -206,7 +275,7 @@ messung.setMesszeitpunkt(d); } catch (ParseException e) { - //TODO: handle warnings. + this.warnings.add(new ReportData(key, values.toString(), 674)); } } else if ("messzeit_sekunden".equals(key)) { @@ -264,8 +333,8 @@ } } if (!foundEinheit) { + this.errors.add(new ReportData("messeinheit", "null", 673)); return null; - //TODO: handle warning! } Response responseGroesse = sRepository.findAll(SMessgroesse.class); List<SMessgroesse> messgroessen = @@ -279,8 +348,8 @@ } } if (!foundGroesse) { + this.errors.add(new ReportData("messgroesse", "null", 673)); return null; - //TODO: handle warning! } } //TODO: Match the other values. @@ -296,12 +365,77 @@ Integer v = Integer.valueOf(values.toString()); ort.setOrtId(v); } - else if ("ort_typ".equals(key)) { + if ("ort_code".equals(key)) { + QueryBuilder<Ort> builder = + new QueryBuilder<Ort>(ortRepo.getEntityManager(), Ort.class); + builder.and("bezeichnung", values.toString()); + Response response = ortRepo.filter(builder.getQuery()); + List<Ort> orte = (List<Ort>)response.getData(); + Integer v = Integer.valueOf(orte.get(0).getOrtId()); + ort.setOrtId(v); + } + if ("ort_typ".equals(key)) { ort.setOrtsTyp(values.toString()); } + if ("ort_zusatz".equals(key)) { + ort.setOrtszusatztext(values.toString()); + } return ort; } + public LZusatzWert addAttribute( + String lKey, + Object values, + LZusatzWert wert + ) { + String v = values.toString().substring(1); + int ndx = v.indexOf("\""); + String groesse = v.substring(0, ndx); + v = v.substring(ndx + 2); + ndx = v.indexOf(" "); + String w = v.substring(0, ndx); + v = v.substring(ndx + 2); + ndx = v.indexOf("\""); + String einheit = v.substring(0, ndx); + String fehler = v.substring(ndx + 2); + QueryBuilder<SProbenZusatz> builder = + new QueryBuilder<SProbenZusatz>( + sRepository.getEntityManager(), SProbenZusatz.class); + builder.and("zusatzwert", groesse); + Response response = sRepository.filter(builder.getQuery()); + List<SProbenZusatz> list = (List<SProbenZusatz>)response.getData(); + if (list == null || list.isEmpty()) { + this.errors.add(new ReportData(lKey, "zusatzwert", 673)); + return null; + } + wert.getId().setPzsId(list.get(0).getPzsId()); + wert.setPzsId(list.get(0).getPzsId()); + wert.setMesswertPzs(Float.valueOf(w)); + wert.setMessfehler(Float.valueOf(fehler)); + return wert; + } + + public LZusatzWert addAttributeS( + String lKey, + Object values, + LZusatzWert wert + ) { + String v = values.toString().substring(1); + int ndx = v.indexOf("\""); + String groesse = v.substring(0, ndx); + v = v.substring(ndx + 2); + ndx = v.indexOf(" "); + String w = v.substring(0, ndx); + v = v.substring(ndx + 2); + ndx = v.indexOf(" "); + String einheit = v.substring(0, ndx); + String fehler = v.substring(ndx + 2); + wert.getId().setPzsId(groesse); + wert.setPzsId(groesse); + wert.setMesswertPzs(Float.valueOf(w)); + wert.setMessfehler(Float.valueOf(fehler)); + return wert; + } /** * @return the warnings */ @@ -317,7 +451,7 @@ } public void reset() { - errors.clear(); - warnings.clear(); + errors = new ArrayList<ReportData>(); + warnings = new ArrayList<ReportData>(); } }
--- a/src/main/java/de/intevation/lada/data/importer/LAFImporter.java Wed Aug 28 15:10:19 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/importer/LAFImporter.java Wed Aug 28 15:11:50 2013 +0200 @@ -16,6 +16,7 @@ import javax.persistence.Query; import de.intevation.lada.auth.AuthenticationResponse; +import de.intevation.lada.data.QueryBuilder; import de.intevation.lada.data.Repository; import de.intevation.lada.model.LKommentarM; import de.intevation.lada.model.LKommentarP; @@ -23,6 +24,7 @@ import de.intevation.lada.model.LMesswert; import de.intevation.lada.model.LOrt; import de.intevation.lada.model.LProbe; +import de.intevation.lada.model.LProbeInfo; import de.intevation.lada.rest.Response; import de.intevation.lada.validation.ValidationException; import de.intevation.lada.validation.Validator; @@ -99,301 +101,24 @@ this.errors.clear(); this.parser.reset(); try { - boolean success = parser.parse(content); - if (success) { - List<LProbe> proben = parser.getProben(); - List<LMessung> messungen = parser.getMessungen(); - List<LOrt> orte = parser.getOrte(); - List<LKommentarP> pKommentare = parser.getProbeKommentare(); - List<LKommentarM> mKommentare = parser.getMessungKommentare(); - List<LMesswert> messwerte = parser.getMesswerte(); - writeProben(auth, proben); - writeMessungen(auth, messungen); - //writeOrte(auth, orte); - writePKommentare(auth, pKommentare); - writeMKommentare(auth, mKommentare); - writeMesswerte(auth, messwerte); - } - else { + boolean success = parser.parse(auth, content); + if (!success) { List<ReportData> report = new ArrayList<ReportData>(); - report.add( new ReportData("parser", "no success", 660)); + report.add(new ReportData("parser", "no success", 660)); errors.put("parser", report); + warnings.put("parser", new ArrayList<ReportData>()); return false; } } catch (LAFParserException e) { List<ReportData> report = new ArrayList<ReportData>(); - report.add(new ReportData("parser", e.getMessage(), 660)); + report.add(new ReportData("parser", e.getMessage(), 670)); errors.put("parser", report); + warnings.put("parser", new ArrayList<ReportData>()); return false; } - Map<String, Map<String, List<ReportData>>> data = - new HashMap<String, Map<String, List<ReportData>>>(); this.warnings.putAll(this.parser.getWarnings()); this.errors.putAll(this.parser.getErrors()); - data.put("warnings", warnings); - data.put("errors", errors); return true; } - - private void writeMessungen( - AuthenticationResponse auth, - List<LMessung> messungen - ) { - for(LMessung messung: messungen) { - try { - - Map<String, Integer> warn = - messungValidator.validate(messung, false); - messungRepository.create(messung); - if (warn != null) { - for (String key : warn.keySet()) { - // warnings.put(messung.getProbeId(), - // new ReportData(key, "", warn.get(key))); - } - } - } - catch (ValidationException e) { - //errors.put(messung.getProbeId(), e.getErrors()); - //warnings.put( - // messung.getProbeId(), - // e.getWarnings()); - } - } - } - - @TransactionAttribute(TransactionAttributeType.REQUIRED) - private void writeMesswerte( - AuthenticationResponse auth, - List<LMesswert> messwerte - ) { - for(LMesswert messwert: messwerte) { - try { - Map<String, Integer> warn = - messwertValidator.validate(messwert, false); - Response r = messwertRepository.create(messwert); - if (warn != null) { - // warnings.put( - // messwert.getProbeId(), - // warn); - } - } - catch (ValidationException e) { - //errors.put(messwert.getProbeId(), e.getErrors()); - //warnings.put( - // messwert.getProbeId(), - // e.getWarnings()); - } - } - } - - @TransactionAttribute(TransactionAttributeType.REQUIRED) - private void writePKommentare( - AuthenticationResponse auth, - List<LKommentarP> kommentare - ) { - for(LKommentarP kommentar: kommentare) { - try { - pKommentarRepository.create(kommentar); - } - catch(Exception e) { - Map<String, Integer> err = new HashMap<String, Integer>(); - err.put( - kommentar.getProbeId() + " - " + - kommentar.getkId(), 661); - //errors.put("lkommentarp", err); - } - } - } - - @TransactionAttribute(TransactionAttributeType.REQUIRED) - private void writeMKommentare( - AuthenticationResponse auth, - List<LKommentarM> kommentare - ) { - for(LKommentarM kommentar: kommentare) { - Query q = - em.createNativeQuery( - "select nextval('kommentar_m_id_seq')"); - BigInteger seqId = (BigInteger)q.getSingleResult(); - kommentar.getId().setKId(seqId.intValue()); - mKommentarRepository.create(kommentar); - } - } - - @TransactionAttribute(TransactionAttributeType.REQUIRED) - private void writeOrte( - AuthenticationResponse auth, - List<LOrt> orte - ) { - for(LOrt ort: orte) { - try { - Map<String, Integer> warn = - ortValidator.validate(ort, false); - ortRepository.create(ort); - if (warn != null) { - // warnings.put(String.valueOf(ort.getOrtId()), warn); - } - } - catch (ValidationException e) { - //errors.put(String.valueOf(ort.getOrtId()), e.getErrors()); - //warnings.put( - // String.valueOf(ort.getOrtId()), - // e.getWarnings()); - } - } - } - - @TransactionAttribute(TransactionAttributeType.REQUIRED) - private void writeProben(AuthenticationResponse auth, List<LProbe> proben) { - for (LProbe probe: proben) { - if (!authorized(probe, auth)) { - Map<String, Integer> err = new HashMap<String, Integer>(); - err.put("not authorized", 699); - //errors.put(probe.getProbeId(), err); - continue; - } - try { - Map<String, Integer> warn = - probeValidator.validate(probe, false); - if (warn != null) { - // warnings.put(probe.getProbeId(), warn); - } - } - catch (ValidationException e) { - //errors.put(probe.getProbeId(), e.getErrors()); - //warnings.put(probe.getProbeId(), e.getWarnings()); - continue; - } - persist(probe); - } - } - - private boolean authorized(LProbe probe, AuthenticationResponse auth) { - if (auth.getNetzbetreiber().contains(probe.getNetzbetreiberId()) && - auth.getMst().contains(probe.getMstId())) { - return true; - } - return false; - } - - @TransactionAttribute(TransactionAttributeType.REQUIRED) - private void persist(LProbe probe) { - String queryColumns = "insert into l_probe (probe_id, ba_id, test," + - " datenbasis_id, netzbetreiber_id, mst_id, probenart_id, umw_id"; - String queryParameter = " values (:probe_id, :ba_id, :test," + - " :datenbasis_id, :netzbetreiber_id, :mst_id, :probenart_id," + - " :umw_id"; - if (probe.getErzeugerId() != null) { - queryColumns += ", erzeuger_id"; - queryParameter += ", :erzeuger_id"; - } - if (probe.getHauptprobenNr() != null) { - queryColumns += ", hauptproben_nr"; - queryParameter += ", :hauptproben_nr"; - } - if (probe.getLetzteAenderung() != null) { - queryColumns += ", letzte_aenderung"; - queryParameter += ", :letzte_aenderung"; - } - if (probe.getMedia() != null) { - queryColumns += ", media"; - queryParameter += ", :media"; - } - if (probe.getMediaDesk() != null) { - queryColumns += ", media_desk"; - queryParameter += ", :media_desk"; - } - if (probe.getMittelungsdauer() != null) { - queryColumns += ", mittelungsdauer"; - queryParameter += ", :mittelungsdauer"; - } - if (probe.getMpKat() != null) { - queryColumns += ", mp_kat"; - queryParameter += ", mp_kat"; - } - if (probe.getMplId() != null) { - queryColumns += ", mpl_id"; - queryParameter += ", :mpl_id"; - } - if (probe.getMprId() != null) { - queryColumns += ", mpr_id"; - queryParameter += ", :mpr_id"; - } - if (probe.getProbeNehmerId() != null) { - queryColumns += ", probe_nehmer_id"; - queryParameter += ", :probe_nehmer_id"; - } - if (probe.getProbeentnahmeBeginn() != null) { - queryColumns += ", probeentnahme_beginn"; - queryParameter += ", :probeentnahme_beginn"; - } - if (probe.getProbeentnahmeEnde() != null) { - queryColumns += ", probeentnahme_ende"; - queryParameter += ", :probeentnahme_ende"; - } - if (probe.getSolldatumBeginn() != null) { - queryColumns += ", solldatum_beginn"; - queryParameter += ", :solldatum_beginn"; - } - if (probe.getSolldatumEnde() != null) { - queryColumns += ", solldatum_ende"; - queryParameter += ", :solldatum_ende"; - } - queryColumns += ") " + queryParameter + ")"; - - Query insert = em.createNativeQuery(queryColumns); - insert.setParameter("probe_id", probe.getProbeId()); - insert.setParameter("ba_id", probe.getBaId()); - insert.setParameter("datenbasis_id", probe.getDatenbasisId()); - insert.setParameter("mst_id", probe.getMstId()); - insert.setParameter("netzbetreiber_id", probe.getNetzbetreiberId()); - insert.setParameter("probenart_id", probe.getProbenartId()); - insert.setParameter("test", probe.isTest()); - insert.setParameter("umw_id", probe.getUmwId()); - if (probe.getErzeugerId() != null) { - insert.setParameter("erzeuger_id", probe.getErzeugerId()); - } - if (probe.getHauptprobenNr() != null) { - insert.setParameter("hauptproben_nr", probe.getHauptprobenNr()); - } - if (probe.getLetzteAenderung() != null) { - insert.setParameter("letzte_aenderung", probe.getLetzteAenderung()); - } - if (probe.getMedia() != null) { - insert.setParameter("media", probe.getMedia()); - } - if (probe.getMediaDesk() != null) { - insert.setParameter("media_desk", probe.getMediaDesk()); - } - if (probe.getMittelungsdauer() != null) { - insert.setParameter("mittelungsdauer", probe.getMittelungsdauer()); - } - if (probe.getMpKat() != null) { - insert.setParameter("mp_kat", probe.getMpKat()); - } - if (probe.getMplId() != null) { - insert.setParameter("mpl_id", probe.getMplId()); - } - if (probe.getMprId() != null) { - insert.setParameter("mpr_id", probe.getMprId()); - } - if (probe.getProbeNehmerId() != null) { - insert.setParameter("probe_nehmer_id", probe.getProbeNehmerId()); - } - if (probe.getProbeentnahmeBeginn() != null) { - insert.setParameter("probeentnahme_beginn", probe.getProbeentnahmeBeginn()); - } - if (probe.getProbeentnahmeEnde() != null) { - insert.setParameter("probeentnahme_ende", probe.getProbeentnahmeEnde()); - } - if (probe.getSolldatumBeginn() != null) { - insert.setParameter("solldatum_beginn", probe.getSolldatumBeginn()); - } - if (probe.getSolldatumEnde() != null) { - insert.setParameter("solldatum_ende", probe.getSolldatumEnde()); - } - int res = insert.executeUpdate(); - int i = res; - } }
--- a/src/main/java/de/intevation/lada/data/importer/LAFParser.java Wed Aug 28 15:10:19 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/importer/LAFParser.java Wed Aug 28 15:11:50 2013 +0200 @@ -2,20 +2,13 @@ import java.util.ArrayList; import java.util.HashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import javax.inject.Inject; import javax.inject.Named; -import de.intevation.lada.model.LKommentarM; -import de.intevation.lada.model.LKommentarP; -import de.intevation.lada.model.LMessung; -import de.intevation.lada.model.LMesswert; -import de.intevation.lada.model.LOrt; -import de.intevation.lada.model.LProbe; -import de.intevation.lada.model.LProbe; +import de.intevation.lada.auth.AuthenticationResponse; public class LAFParser { @@ -28,12 +21,9 @@ @Named("lafproducer") private Producer producer; - List<LProbe> proben; - List<LMessung> messungen; - List<LOrt> orte; - List<LMesswert> messwerte; - List<LKommentarP> probeKommentare; - List<LKommentarM> messungKommentare; + @Inject + @Named("lafwriter") + private Writer writer; private Map<String, List<ReportData>> warnings; private Map<String, List<ReportData>> errors; @@ -42,25 +32,11 @@ this.warnings = new HashMap<String, List<ReportData>>(); this.errors = new HashMap<String, List<ReportData>>(); this.setDryRun(false); - //this.producer = new LAFProducer(); - this.proben = new ArrayList<LProbe>(); - this.messungen = new ArrayList<LMessung>(); - this.orte = new ArrayList<LOrt>(); - this.messwerte = new ArrayList<LMesswert>(); - this.probeKommentare = new ArrayList<LKommentarP>(); - this.messungKommentare = new ArrayList<LKommentarM>(); } - public boolean parse(String laf) + public boolean parse(AuthenticationResponse auth, String laf) throws LAFParserException { - this.proben.clear(); - this.messungen.clear(); - this.orte.clear(); - this.messwerte.clear(); - this.probeKommentare.clear(); - this.messungKommentare.clear(); - if (!laf.startsWith("%PROBE%\n")) { throw new LAFParserException("No %PROBE% at the begining."); } @@ -74,9 +50,23 @@ laf = laf.substring(nextPos + 1); try { readAll(single); + this.warnings.putAll(producer.getWarnings()); + this.errors.putAll(producer.getErrors()); + writeAll(auth); + this.producer.reset(); } catch (LAFParserException lpe) { - this.errors.putAll(producer.getErrors()); + Map<String, List<ReportData>> pErr = producer.getErrors(); + if (pErr.isEmpty()) { + List<ReportData> err = new ArrayList<ReportData>(); + err.add(new ReportData("parser", lpe.getMessage(), 673)); + this.errors.put("parser", err); + this.warnings.put("parser", new ArrayList<ReportData>()); + } + else { + this.errors.putAll(pErr); + this.warnings.putAll(producer.getWarnings()); + } this.producer.reset(); continue; } @@ -84,27 +74,65 @@ else { try { readAll(laf); + this.warnings.putAll(producer.getWarnings()); + this.errors.putAll(producer.getErrors()); + writeAll(auth); + this.producer.reset(); laf = ""; } catch (LAFParserException lpe) { - this.errors.putAll(producer.getErrors()); + Map<String, List<ReportData>> pErr = producer.getErrors(); + if (pErr.isEmpty()) { + List<ReportData> err = new ArrayList<ReportData>(); + err.add(new ReportData("parser", lpe.getMessage(), 673)); + this.errors.put("parser", err); + this.warnings.put("parser", new ArrayList<ReportData>()); + } + else { + this.errors.putAll(pErr); + this.warnings.putAll(producer.getWarnings()); + } + this.producer.reset(); laf = ""; continue; } } - if (!this.dryRun) { - proben.add(producer.getProbe()); - messungen.addAll(producer.getMessungen()); - orte.addAll(producer.getOrte()); - messwerte.addAll(producer.getMesswerte()); - probeKommentare.addAll(producer.getProbenKommentare()); - messungKommentare.addAll(producer.getMessungsKommentare()); - producer.reset(); - } } return parsed; } + private void writeAll(AuthenticationResponse auth) { + String probeId = producer.getProbe().getProbeId(); + boolean p = writer.writeProbe(auth, producer.getProbe()); + if (!p) { + this.errors.put(probeId, writer.getErrors()); + this.warnings.put(probeId, writer.getWarnings()); + return; + } + writer.writeProbenKommentare(auth, producer.getProbenKommentare()); + boolean m = writer.writeMessungen(auth, producer.getMessungen()); + if (!m) { + return; + } + writer.writeOrte(auth, producer.getOrte()); + writer.writeMessungKommentare(auth, producer.getMessungsKommentare()); + writer.writeMesswerte(auth, producer.getMesswerte()); + List<ReportData> err = this.errors.get(probeId); + if (err == null) { + this.errors.put(probeId, writer.getErrors()); + } + else { + err.addAll(writer.getErrors()); + } + List<ReportData> warn = this.warnings.get(probeId); + if (warn == null) { + this.warnings.put(probeId, writer.getWarnings()); + } + else { + warn.addAll(writer.getWarnings()); + } + } + private void readAll(String content) throws LAFParserException { @@ -140,7 +168,10 @@ white = true; continue; } - else if (current != ' ' && white) { + else if (current != ' ' && + current != '\n' && + current != '\r' && + white) { value = true; white = false; } @@ -188,7 +219,7 @@ valueString = ""; continue; } - else if ((current == '\n' || current == '\r') && key) { + if ((current == '\n' || current == '\r') && (key || white)) { throw new LAFParserException("No value for key: " + keyString); } @@ -217,48 +248,6 @@ } /** - * @return the proben - */ - public List<LProbe> getProben() { - return proben; - } - - /** - * @return the messungen - */ - public List<LMessung> getMessungen() { - return messungen; - } - - /** - * @return the orte - */ - public List<LOrt> getOrte() { - return orte; - } - - /** - * @return the messwerte - */ - public List<LMesswert> getMesswerte() { - return messwerte; - } - - /** - * @return the probeKommentare - */ - public List<LKommentarP> getProbeKommentare() { - return probeKommentare; - } - - /** - * @return the messungKommentare - */ - public List<LKommentarM> getMessungKommentare() { - return messungKommentare; - } - - /** * @return the warnings */ public Map<String, List<ReportData>> getWarnings() {
--- a/src/main/java/de/intevation/lada/data/importer/LAFProducer.java Wed Aug 28 15:10:19 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/importer/LAFProducer.java Wed Aug 28 15:11:50 2013 +0200 @@ -9,6 +9,8 @@ import javax.inject.Inject; import javax.inject.Named; +import de.intevation.lada.data.QueryBuilder; +import de.intevation.lada.data.Repository; import de.intevation.lada.model.LKommentarM; import de.intevation.lada.model.LKommentarMId; import de.intevation.lada.model.LKommentarP; @@ -19,12 +21,19 @@ import de.intevation.lada.model.LOrt; import de.intevation.lada.model.LProbe; import de.intevation.lada.model.LProbe; +import de.intevation.lada.model.LZusatzWert; +import de.intevation.lada.model.LZusatzWertId; +import de.intevation.lada.model.SProbenZusatz; @Named("lafproducer") public class LAFProducer implements Producer { + @Inject + @Named("readonlyrepository") + private Repository sRepository; + private LProbe probe; private LMessung messung; private LOrt ort; @@ -34,6 +43,7 @@ private List<LMessung> messungen; private List<LOrt> orte; private List<LMesswert> messwerte; + private List<LZusatzWert> zusatzwerte; private List<EntryFormat> probenFormat; private List<EntryFormat> messungFormat; @@ -84,10 +94,42 @@ mapper.addAttribute(lKey, values, kommentar)); } else if (lKey.equals("probenzusatzbeschreibung")) { - //TODO: implement this! + LZusatzWertId zusatzId = new LZusatzWertId(); + zusatzId.setProbeId(this.probe.getProbeId()); + LZusatzWert wert = new LZusatzWert(); + wert.setId(zusatzId); + LZusatzWert zusatzWert = mapper.addAttribute(lKey, values, wert); + if (zusatzWert != null) { + this.zusatzwerte.add(zusatzWert); + } + else { + List<ReportData> err = this.errors.get(probe.getProbeId()); + if (err == null) { + this.errors.put(probe.getProbeId(), mapper.getErrors()); + } + else { + err.addAll(mapper.getErrors()); + } + } } else if (lKey.equals("pzb_s")) { - //TODO: implement this! + LZusatzWertId zusatzId = new LZusatzWertId(); + zusatzId.setProbeId(this.probe.getProbeId()); + LZusatzWert wert = new LZusatzWert(); + wert.setId(zusatzId); + LZusatzWert zusatzWert = mapper.addAttributeS(lKey, values, wert); + if (zusatzWert != null) { + this.zusatzwerte.add(zusatzWert); + } + else { + List<ReportData> err = this.errors.get(probe.getProbeId()); + if (err == null) { + this.errors.put(probe.getProbeId(), mapper.getErrors()); + } + else { + err.addAll(mapper.getErrors()); + } + } } else if (lKey.equals("messwert")) { LMesswertId id = new LMesswertId(); @@ -101,6 +143,15 @@ if (wert != null) { this.messwerte.add(wert); } + else { + List<ReportData> err = this.errors.get(probe.getProbeId()); + if (err == null) { + this.errors.put(probe.getProbeId(), mapper.getErrors()); + } + else { + err.addAll(mapper.getErrors()); + } + } } else if (isValidMessung(lKey, values.toString())) { this.messung = mapper.addAttribute(lKey, values, this.messung); @@ -181,17 +232,22 @@ } @Override + public List<LZusatzWert> getZusatzwerte() { + return this.zusatzwerte; + } + + @Override public void reset() { - this.errors.clear(); - this.warnings.clear(); + this.errors = new HashMap<String, List<ReportData>>(); + this.warnings = new HashMap<String, List<ReportData>>(); this.probe = new LProbe(); - this.messungen.clear(); + this.messungen = new ArrayList<LMessung>(); this.messung = null; - this.orte.clear(); + this.orte = new ArrayList<LOrt>(); this.ort = null; - this.messwerte.clear(); - this.mKommentare.clear(); - this.pKommentare.clear(); + this.messwerte = new ArrayList<LMesswert>(); + this.mKommentare = new ArrayList<LKommentarM>(); + this.pKommentare = new ArrayList<LKommentarP>(); mapper.reset(); } @@ -218,6 +274,16 @@ * @return the warnings */ public Map<String, List<ReportData>> getWarnings() { + if (this.probe == null) { + return this.warnings; + } + List<ReportData> warn = this.warnings.get(probe.getProbeId()); + if (warn == null) { + this.warnings.put(probe.getProbeId(), mapper.getWarnings()); + } + else { + warn.addAll(mapper.getWarnings()); + } return this.warnings; } @@ -225,6 +291,16 @@ * @return the errors */ public Map<String, List<ReportData>> getErrors() { + if (this.probe == null) { + return this.errors; + } + List<ReportData> err = this.errors.get(this.probe.getProbeId()); + if (err == null) { + this.errors.put(probe.getProbeId(), mapper.getErrors()); + } + else { + err.addAll(mapper.getErrors()); + } return this.errors; } }
--- a/src/main/java/de/intevation/lada/data/importer/Producer.java Wed Aug 28 15:10:19 2013 +0200 +++ b/src/main/java/de/intevation/lada/data/importer/Producer.java Wed Aug 28 15:11:50 2013 +0200 @@ -9,6 +9,7 @@ import de.intevation.lada.model.LMesswert; import de.intevation.lada.model.LOrt; import de.intevation.lada.model.LProbe; +import de.intevation.lada.model.LZusatzWert; public interface Producer @@ -21,6 +22,7 @@ public List<LKommentarP> getProbenKommentare(); public List<LKommentarM> getMessungsKommentare(); public List<LMesswert> getMesswerte(); + public List<LZusatzWert> getZusatzwerte(); public void reset(); public void newMessung(); public void newOrt();
--- a/src/main/java/de/intevation/lada/rest/LAFImportService.java Wed Aug 28 15:10:19 2013 +0200 +++ b/src/main/java/de/intevation/lada/rest/LAFImportService.java Wed Aug 28 15:11:50 2013 +0200 @@ -94,7 +94,7 @@ respData.put("filename", name); int code = 200; if (!success) { - code = 660; + code = 670; } Response response = new Response(success, code, respData); return response;