# HG changeset patch # User Raimund Renkert # Date 1479308310 -3600 # Node ID 312aaa5d069d5f7da07713fc2091169a068d37b8 # Parent 33dc7dc6b7bc98e27b243ed2b1b5ed0e71ed05e5 Create a new ort object on demand while importing LAF data. diff -r 33dc7dc6b7bc -r 312aaa5d069d src/main/java/de/intevation/lada/importer/laf/LafObjectMapper.java --- a/src/main/java/de/intevation/lada/importer/laf/LafObjectMapper.java Wed Nov 16 15:57:14 2016 +0100 +++ b/src/main/java/de/intevation/lada/importer/laf/LafObjectMapper.java Wed Nov 16 15:58:30 2016 +0100 @@ -238,12 +238,12 @@ merger.mergeZusatzwerte(newProbe, zusatzwerte); // Merge entnahmeOrt - createEntnahmeOrt(object.getEntnahmeOrt(), newProbe.getId()); + createEntnahmeOrt(object.getEntnahmeOrt(), newProbe); // Create ursprungsOrte List uOrte = new ArrayList(); for (int i = 0; i < object.getUrsprungsOrte().size(); i++) { - Ortszuordnung tmp = createUrsprungsOrt(object.getUrsprungsOrte().get(i), newProbe.getId()); + Ortszuordnung tmp = createUrsprungsOrt(object.getUrsprungsOrte().get(i), newProbe); if (tmp != null) { uOrte.add(tmp); } @@ -596,13 +596,13 @@ private Ortszuordnung createUrsprungsOrt( Map ursprungsOrt, - Integer id + Probe probe ) { Ortszuordnung ort = new Ortszuordnung(); ort.setOrtszuordnungTyp("U"); - ort.setProbeId(id); + ort.setProbeId(probe.getId()); - Ort o = findOrCreateOrt(ursprungsOrt, "U_"); + Ort o = findOrCreateOrt(ursprungsOrt, "U_", probe); if (o == null) { return null; } @@ -625,13 +625,13 @@ private void createEntnahmeOrt( Map entnahmeOrt, - Integer id + Probe probe ) { Ortszuordnung ort = new Ortszuordnung(); ort.setOrtszuordnungTyp("E"); - ort.setProbeId(id); + ort.setProbeId(probe.getId()); - Ort o = findOrCreateOrt(entnahmeOrt, "P_"); + Ort o = findOrCreateOrt(entnahmeOrt, "P_", probe); if (o == null) { return; } @@ -639,10 +639,10 @@ if (entnahmeOrt.containsKey("P_ORTS_ZUSATZTEXT")) { ort.setOrtszusatztext(entnahmeOrt.get("P_ORTS_ZUSATZTEXT")); } - merger.mergeEntnahmeOrt(id, ort); + merger.mergeEntnahmeOrt(probe.getId(), ort); } - private Ort findOrCreateOrt(Map attributes, String type) { + private Ort findOrCreateOrt(Map attributes, String type, Probe probe) { // If laf contains coordinates, find a ort with matching coordinates or // create one. for (Entry entry : attributes.entrySet()) { @@ -689,7 +689,7 @@ return orte.get(0); } else { - return createNewOrt(attributes, type); + return createNewOrt(attributes, type, probe); } } // If laf contains gemeinde attributes, find a ort with matching gemId @@ -720,47 +720,48 @@ if (orte != null && orte.size() > 0) { return orte.get(0); } - else { - return createNewOrt(attributes, type); - } } - else { - // Create a new ort. - } - return createNewOrt(attributes, type); + return createNewOrt(attributes, type, probe); } - private Ort createNewOrt(Map attributes, String type) { + private Ort createNewOrt(Map attributes, String type, Probe probe) { Ort ort = new Ort(); ort.setOrtTyp(1); - String hLand = ""; - String staatFilter = ""; - if (attributes.get(type + "HERKUNFTSLAND_S") != null) { - staatFilter = "staatIso"; - hLand = attributes.get(type + "HERKUNFTSLAND_S"); - } - else if (attributes.get(type + "HERKUNFTSLAND_KURZ") != null) { - staatFilter = "staatKurz"; - hLand = attributes.get(type + "HERKUNFTSLAND_KURZ"); - } - else if (attributes.get(type + "HERKUNFTSLAND_LANG") != null) { - staatFilter = "staat"; - hLand = attributes.get(type + "HERKUNFTSLAND_LANG"); - } - QueryBuilder builderStaat = - new QueryBuilder( - repository.entityManager("stamm"), - Staat.class); - if (staatFilter.length() > 0) { - builderStaat.and(staatFilter, hLand); - List staat = - repository.filterPlain(builderStaat.getQuery(), "stamm"); - if (staat != null && staat.size() > 0) { - ort.setStaatId(staat.get(0).getId()); + String gemId = null; + MessStelle mst = repository.getByIdPlain(MessStelle.class, probe.getMstId(), "stamm"); + ort.setNetzbetreiberId(mst.getNetzbetreiberId()); + boolean hasKoord = false; + boolean hasGem = false; + boolean hasStaat = false; + + if ((attributes.get(type + "KOORDINATEN_ART") != null || + attributes.get(type + "KOORDINATEN_ART_S") != null) && + attributes.get(type + "KOORDINATEN_X") != null && + attributes.get(type + "KOORDINATEN_Y") != null + ) { + if (attributes.get(type + "KOORDINATEN_ART_S") != null) { + ort.setKdaId(Integer.valueOf(attributes.get(type + "KOORDINATEN_ART_S"))); } + else { + QueryBuilder builder = + new QueryBuilder( + repository.entityManager("stamm"), + KoordinatenArt.class); + builder.and("koordinatenart", attributes.get(type + "KOORDINATEN_ART")); + List art = repository.filterPlain(builder.getQuery(), "stamm"); + if (art == null || art.isEmpty()) { + + } + else { + ort.setKdaId(art.get(0).getId()); + } + } + ort.setKoordXExtern(attributes.get(type + "KOORDINATEN_X")); + ort.setKoordYExtern(attributes.get(type + "KOORDINATEN_Y")); + ort.setMpArt("D"); + hasKoord = true; } - String gemId = null; if (attributes.get(type + "GEMEINDENAME") != null) { QueryBuilder builder = new QueryBuilder( @@ -778,18 +779,72 @@ } if (gemId != null) { ort.setGemId(gemId); + hasGem = true; + Verwaltungseinheit v = repository.getByIdPlain(Verwaltungseinheit.class, gemId, "stamm"); + if (v == null) { + ReportItem err = new ReportItem(); + err.setCode(673); + err.setKey("ort"); + err.setValue(gemId); + currentErrors.add(err); + return null; + } + if (!hasKoord) { + ort.setMpArt("V"); + ort.setKdaId(v.getKdaId()); + ort.setKoordYExtern(v.getKoordYExtern()); + ort.setKoordXExtern(v.getKoordXExtern()); + } + ort.setKurztext(v.getBezeichnung()); + ort.setLangtext(v.getBezeichnung()); + ort.setOrtId(gemId); + ort.setBerichtstext(v.getBezeichnung()); } - if ((attributes.get(type + "KOORDINATEN_ART") != null || - attributes.get(type + "KOORDINATEN_ART_S") != null) && - attributes.get(type + "KOORDINATEN_X") != null && - attributes.get(type + "KOORDINATEN_Y") != null - ) { - if (attributes.get(type + "KOORDINATEN_ART_S") != null) { + + String hLand = ""; + String staatFilter = ""; + if (attributes.get(type + "HERKUNFTSLAND_S") != null) { + staatFilter = "id"; + hLand = attributes.get(type + "HERKUNFTSLAND_S"); + } + else if (attributes.get(type + "HERKUNFTSLAND_KURZ") != null) { + staatFilter = "staatKurz"; + hLand = attributes.get(type + "HERKUNFTSLAND_KURZ"); + } + else if (attributes.get(type + "HERKUNFTSLAND_LANG") != null) { + staatFilter = "staat"; + hLand = attributes.get(type + "HERKUNFTSLAND_LANG"); + } + + if (staatFilter.length() > 0) { + QueryBuilder builderStaat = + new QueryBuilder( + repository.entityManager("stamm"), + Staat.class); + builderStaat.and(staatFilter, hLand); + List staat = + repository.filterPlain(builderStaat.getQuery(), "stamm"); + if (staat != null && staat.size() > 0) { + ort.setStaatId(staat.get(0).getId()); + hasStaat = true; + Staat s = staat.get(0); + if (!hasGem && !hasKoord) { + ort.setMpArt("S"); + ort.setKdaId(s.getKdaId()); + ort.setKoordYExtern(s.getKoordYExtern()); + ort.setKoordXExtern(s.getKoordXExtern()); + } + if (hasKoord && !hasGem) { + ort.setKurztext(s.getStaat()); + ort.setLangtext(s.getStaat()); + ort.setOrtId("Staat_" + s.getStaatIso()); + ort.setBerichtstext(s.getStaat()); + } } } -// repository.create(ort, "stamm"); -// return ort; - return null; + + repository.create(ort, "stamm"); + return ort; } private void logProbe(Probe probe) {