# HG changeset patch # User Raimund Renkert # Date 1378394650 -7200 # Node ID f3c58d3c4bc7eb35712cfc0edfda29a78da0e10e # Parent 3df000288b450f96fad004df75406ab6176425b3 Validate that lat/lon coordinates are within the geometry of verwaltungseinheit. diff -r 3df000288b45 -r f3c58d3c4bc7 src/main/java/de/intevation/lada/validation/LOrtValidator.java --- a/src/main/java/de/intevation/lada/validation/LOrtValidator.java Thu Sep 05 17:22:44 2013 +0200 +++ b/src/main/java/de/intevation/lada/validation/LOrtValidator.java Thu Sep 05 17:24:10 2013 +0200 @@ -1,18 +1,40 @@ package de.intevation.lada.validation; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; import javax.inject.Named; +import com.vividsolutions.jts.geom.Coordinate; +import com.vividsolutions.jts.geom.CoordinateSequence; +import com.vividsolutions.jts.geom.GeometryFactory; +import com.vividsolutions.jts.geom.Point; +import com.vividsolutions.jts.geom.PrecisionModel; + +import de.intevation.lada.data.QueryBuilder; +import de.intevation.lada.data.Repository; +import de.intevation.lada.model.DeVg; import de.intevation.lada.model.LOrt; +import de.intevation.lada.model.Ort; +import de.intevation.lada.model.SVerwaltungseinheit; +import de.intevation.lada.rest.Response; @ApplicationScoped @Named("lortvalidator") public class LOrtValidator implements Validator { + @Inject + @Named("readonlyrepository") + private Repository readonlyRepo; + + @Inject + @Named("ortrepository") + private Repository ortRepo; + /** * Validate a LOrt object. * @@ -31,9 +53,49 @@ errors.put("lort", 610); throw new ValidationException(errors); } - + LOrt ort = (LOrt)object; + validateVerwaltungseinheit(ort, warnings); //TODO: more validation, see LSB: VI - Konsistenzregeln return warnings; } + private void validateVerwaltungseinheit(LOrt ort, Map warnings) { + if (!ort.getOrtsTyp().equals("E")) { + return; + } + QueryBuilder ortBuilder = + new QueryBuilder(ortRepo.getEntityManager(), Ort.class); + ortBuilder.and("ortId", ort.getOrtId()); + Response response = ortRepo.filter(ortBuilder.getQuery()); + List orte = (List)response.getData(); + QueryBuilder veBuilder = + new QueryBuilder( + readonlyRepo.getEntityManager(), SVerwaltungseinheit.class); + veBuilder.and("gemId", orte.get(0).getGemId()); + Response ver = readonlyRepo.filter(veBuilder.getQuery()); + SVerwaltungseinheit ve = ((List)ver.getData()).get(0); + QueryBuilder vg = + new QueryBuilder(readonlyRepo.getEntityManager(), DeVg.class); + vg.and("ags", ve.getGemId()); + Response rvg = readonlyRepo.filter(vg.getQuery()); + List vgs = (List)rvg.getData(); + if (vgs == null || vgs.isEmpty()) { + warnings.put("verwaltungseinheit", 653); + return; + } + double y = orte.get(0).getLatitude(); + double x = orte.get(0).getLongitude(); + Coordinate c = new Coordinate(x, y); + GeometryFactory gf = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING), 4326); + Point p = gf.createPoint(c); + boolean hit = false; + for (DeVg singlevg : vgs) { + if(singlevg.getGeom().contains(p)) { + hit = true; + } + } + if (!hit) { + warnings.put("verwaltungseinheit", 651); + } + } }