# HG changeset patch # User Raimund Renkert # Date 1485525631 -3600 # Node ID dc7dde59bb54fd555b59b6863084db46ab4a8bdf # Parent 201cc62a1c57e90d7912880e72e6e35af552d9a4 Moved rules to create an ort object to ort factory. diff -r 201cc62a1c57 -r dc7dde59bb54 src/main/java/de/intevation/lada/factory/OrtFactory.java --- a/src/main/java/de/intevation/lada/factory/OrtFactory.java Fri Jan 27 13:27:08 2017 +0100 +++ b/src/main/java/de/intevation/lada/factory/OrtFactory.java Fri Jan 27 15:00:31 2017 +0100 @@ -27,7 +27,10 @@ import de.intevation.lada.importer.ReportItem; import de.intevation.lada.model.stammdaten.Ort; +import de.intevation.lada.model.stammdaten.Staat; +import de.intevation.lada.model.stammdaten.Verwaltungseinheit; import de.intevation.lada.util.annotation.RepositoryConfig; +import de.intevation.lada.util.data.QueryBuilder; import de.intevation.lada.util.data.Repository; import de.intevation.lada.util.data.RepositoryType; @@ -44,7 +47,7 @@ public void transformCoordinates(Ort ort) { errors = new ArrayList(); - int kda = ort.getKdaId(); + Integer kda = ort.getKdaId(); String epsg = null; String xCoord = null; String yCoord = null; @@ -94,6 +97,141 @@ } } + /** + * Use given attribute to try to add other attributes. + * To set futher attributes at least one of the following attribute set + * need to be present: + * - kda, x, y + * - gemId + * - staat + * + * @param kda The koordinatenart + * @param x The x coordinate + * @param y The y coordinate + * @param gemId The gemeinde id + * @param staat The staat id + */ + public Ort completeOrt(Ort ort) { + QueryBuilder builder = + new QueryBuilder( + repository.entityManager("stamm"), + Ort.class); + logger.debug("try to make a complete ort"); + if (ort.getKdaId() != null && + ort.getKoordXExtern() != null && + ort.getKoordYExtern() != null + ) { + logger.debug("has koordinates"); + builder.and("kdaId", ort.getKdaId()); + builder.and("koordXExtern", ort.getKoordXExtern()); + builder.and("koordYExtern", ort.getKoordYExtern()); + builder.and("ozId", ort.getOzId()); + builder.and("netzbetreiberId", ort.getNetzbetreiberId()); + List orte = repository.filterPlain(builder.getQuery(), "stamm"); + if (orte != null && orte.size() > 0) { + return orte.get(0); + } + } + else if (ort.getGemId() != null) { + logger.debug("has gemid"); + builder.and("gemId", ort.getGemId()); + builder.and("ozId", ort.getOzId()); + builder.and("netzbetreiberId", ort.getNetzbetreiberId()); + List orte = repository.filterPlain(builder.getQuery(), "stamm"); + if (orte != null && orte.size() > 0) { + logger.debug("found ort: " + orte.get(0).getId()); + return orte.get(0); + } + } + else if (ort.getStaatId() != null && + ort.getStaatId() != 0 + ) { + logger.debug("has staat"); + builder.and("staatId", ort.getGemId()); + builder.and("ozId", ort.getOzId()); + builder.and("netzbetreiberId", ort.getNetzbetreiberId()); + List orte = repository.filterPlain(builder.getQuery(), "stamm"); + if (orte != null && orte.size() > 0) { + return orte.get(0); + } + } + + logger.debug("no ort found"); + return createOrt(ort); + } + + private Ort createOrt(Ort ort) { + boolean hasKoord = false; + boolean hasGem = false; + boolean hasStaat = false; + if (ort.getKdaId() != null && + ort.getKoordXExtern() != null && + ort.getKoordYExtern() != null + ) { + logger.debug("transformCoordinates"); + transformCoordinates(ort); + hasKoord = true; + } + if (ort.getGemId() == null && hasKoord) { + logger.debug("findVerwaltungseinheit"); + findVerwaltungseinheit(ort); + } + if (ort.getGemId() != null){ + if (ort.getStaatId() == null) { + ort.setStaatId(0); + } + Verwaltungseinheit v = repository.getByIdPlain( + Verwaltungseinheit.class, + ort.getGemId(), + "stamm"); + if (!hasKoord) { + logger.debug("add coordinates"); + ort.setMpArt("V"); + ort.setKdaId(4); + ort.setKoordYExtern(String.valueOf(v.getMittelpunkt().getY())); + ort.setKoordXExtern(String.valueOf(v.getMittelpunkt().getX())); + } + if (ort.getKurztext() == null) { + ort.setKurztext(v.getBezeichnung()); + } + if (ort.getLangtext() == null) { + ort.setLangtext(v.getBezeichnung()); + } + if (ort.getBerichtstext() == null) { + ort.setBerichtstext(v.getBezeichnung()); + } + transformCoordinates(ort); + hasGem = true; + } + if (ort.getStaatId() != null && + ort.getStaatId() != 0 && + !hasKoord && + !hasGem + ) { + Staat staat = + repository.getByIdPlain(Staat.class, ort.getStaatId(), "stamm"); + ort.setKdaId(staat.getKdaId()); + ort.setKoordXExtern(staat.getKoordXExtern()); + ort.setKoordYExtern(staat.getKoordYExtern()); + ort.setKurztext(staat.getStaat()); + ort.setLangtext(staat.getStaat()); + if (staat.getStaatIso() != null) { + ort.setOrtId("Staat_" + staat.getStaatIso()); + } + ort.setBerichtstext(staat.getStaat()); + transformCoordinates(ort); + hasStaat = true; + } + return ort; + } + + /** + * Use the geom of an ort object to determine the verwaltungseinheit. + * If verwaltungseinheit was found the gemId is used as reference in the ort + * object. + * + * @param ort The ort object + */ public void findVerwaltungseinheit(Ort ort) { if (ort.getGeom() == null) { return; @@ -132,6 +270,6 @@ } public boolean hasErrors() { - return !errors.isEmpty(); + return !(errors == null) && !errors.isEmpty(); } } diff -r 201cc62a1c57 -r dc7dde59bb54 src/main/java/de/intevation/lada/importer/laf/LafObjectMapper.java --- a/src/main/java/de/intevation/lada/importer/laf/LafObjectMapper.java Fri Jan 27 13:27:08 2017 +0100 +++ b/src/main/java/de/intevation/lada/importer/laf/LafObjectMapper.java Fri Jan 27 15:00:31 2017 +0100 @@ -634,6 +634,11 @@ } private Ort findOrCreateOrt(Map attributes, String type, Probe probe) { + Integer kda = null; + Integer x = null; + Integer y = null; + String gemId = null; + Ort o = new Ort(); // If laf contains coordinates, find a ort with matching coordinates or // create one. if ((attributes.get(type + "KOORDINATEN_ART") != null || @@ -641,12 +646,8 @@ attributes.get(type + "KOORDINATEN_X") != null && attributes.get(type + "KOORDINATEN_Y") != null ) { - QueryBuilder builder = - new QueryBuilder( - repository.entityManager("stamm"), - Ort.class); if (attributes.get(type + "KOORDINATEN_ART_S") != null) { - builder.and("kdaId", Integer.valueOf(attributes.get(type + "KOORDINATEN_ART_S"))); + o.setKdaId(Integer.valueOf(attributes.get(type + "KOORDINATEN_ART_S"))); } else { QueryBuilder kdaBuilder = @@ -663,21 +664,13 @@ currentErrors.add(err); return null; } - builder.and("kdaId", arten.get(0).getId()); + o.setKdaId(arten.get(0).getId()); } - builder.and("koordXExtern", attributes.get(type + "KOORDINATEN_X")); - builder.and("koordYExtern", attributes.get(type + "KOORDINATEN_Y")); - List orte = repository.filterPlain(builder.getQuery(), "stamm"); - if (orte != null && orte.size() > 0) { - return orte.get(0); - } - else { - return createNewOrt(attributes, type, probe); - } + o.setKoordXExtern(attributes.get(type + "KOORDINATEN_X")); + o.setKoordYExtern(attributes.get(type + "KOORDINATEN_Y")); } // If laf contains gemeinde attributes, find a ort with matching gemId // or create one. - String gemId = null; if (attributes.get(type + "GEMEINDENAME") != null) { QueryBuilder builder = new QueryBuilder( @@ -687,24 +680,78 @@ List ves = repository.filterPlain(builder.getQuery(), "stamm"); if (ves != null && ves.size() > 0) { - gemId = ves.get(0).getId(); + o.setGemId(ves.get(0).getId()); } } else if (attributes.get(type + "GEMEINDESCHLUESSEL") != null) { - gemId = attributes.get(type + "GEMEINDESCHLUESSEL"); + o.setGemId(attributes.get(type + "GEMEINDESCHLUESSEL")); } - if (gemId != null) { - QueryBuilder builder = - new QueryBuilder( + 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"), - Ort.class); - builder.and("gemId", gemId); - List orte = repository.filterPlain(builder.getQuery(), "stamm"); - if (orte != null && orte.size() > 0) { - return orte.get(0); + Staat.class); + builderStaat.and(staatFilter, hLand); + List staat = + repository.filterPlain(builderStaat.getQuery(), "stamm"); + if (staat != null && staat.size() > 0) { + o.setStaatId(staat.get(0).getId()); } } - return createNewOrt(attributes, type, probe); + if (attributes.containsKey(type + "HOEHE_NN")) { + o.setHoeheUeberNn(Float.valueOf(attributes.get(type + "HOEHE_NN"))); + } + if (attributes.containsKey(type + "ORTS_ZUSATZCODE")) { + Ortszusatz zusatz = repository.getByIdPlain( + Ortszusatz.class, + attributes.get(type + "ORTS_ZUSATZCODE"), + "stamm"); + if (zusatz != null) { + o.setOzId(zusatz.getOzsId()); + } + } + MessStelle mst = repository.getByIdPlain(MessStelle.class, probe.getMstId(), "stamm"); + o.setNetzbetreiberId(mst.getNetzbetreiberId()); + o = ortFactory.completeOrt(o); + if (o == null) { + return null; + } + Violation violation = ortValidator.validate(o); + for (Entry> warn : + violation.getWarnings().entrySet()) { + for (Integer code : warn.getValue()) { + currentWarnings.add( + new ReportItem("validation", warn.getKey(), code)); + } + } + if (violation.hasErrors()) { + for (Entry> err : + violation.getErrors().entrySet()) { + for (Integer code : err.getValue()) { + // Add to warnings because Probe object might be imported + currentWarnings.add( + new ReportItem("validation", err.getKey(), code)); + } + } + return null; + } + repository.create(o, "stamm"); + return o; } private Ort createNewOrt(Map attributes, String type, Probe probe) {