[Lada-commits] [PATCH] Added validation rules for probe deskriptor

Wald Commits scm-commit at wald.intevation.org
Wed May 13 11:23:41 CEST 2015


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1431509009 -7200
# Node ID fa149968e60d7027fe6855dae3b4b75652ce87c7
# Parent  04d1026243872d1bed56d2603e9ef4cd412af795
Added validation rules for probe deskriptor.

diff -r 04d102624387 -r fa149968e60d src/main/java/de/intevation/lada/validation/rules/probe/Deskriptor.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/validation/rules/probe/Deskriptor.java	Wed May 13 11:23:29 2015 +0200
@@ -0,0 +1,70 @@
+package de.intevation.lada.validation.rules.probe;
+
+import java.util.List;
+
+import javax.inject.Inject;
+
+import de.intevation.lada.model.land.LProbe;
+import de.intevation.lada.model.stamm.Deskriptoren;
+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;
+import de.intevation.lada.util.rest.Response;
+import de.intevation.lada.validation.Violation;
+import de.intevation.lada.validation.annotation.ValidationRule;
+import de.intevation.lada.validation.rules.Rule;
+
+ at ValidationRule("Probe")
+public class Deskriptor implements Rule {
+
+    @Inject
+    @RepositoryConfig(type=RepositoryType.RO)
+    private Repository repository;
+
+    @Override
+    public Violation execute(Object object) {
+        LProbe probe = (LProbe)object;
+        String[] mediaDesk = probe.getMediaDesk().split(" ");
+        boolean zebs = false;
+        Integer parent = null;
+        Integer hdParent = null;
+        Integer ndParent = null;
+        if ("01".equals(mediaDesk[1])) {
+            zebs = true;
+        }
+        for (int i = 1; i < mediaDesk.length; i++) {
+            if ("00".equals(mediaDesk[i])) {
+                continue;
+            }
+            if (zebs && i < 5) {
+                parent = hdParent;
+            }
+            else if (!zebs && i < 3) {
+                parent = hdParent;
+            }
+            else {
+                parent = ndParent;
+            }
+            QueryBuilder<Deskriptoren> builder = new QueryBuilder<Deskriptoren>(
+                repository.entityManager("stamm"), Deskriptoren.class);
+            if (parent != null) {
+                builder.and("vorgaenger", parent);
+            }
+            builder.and("sn", mediaDesk[i]);
+            builder.and("ebene", i - 1);
+            Response response = repository.filter(builder.getQuery(), "stamm");
+            List<Deskriptoren> data = (List<Deskriptoren>)response.getData();
+            if (data.isEmpty()) {
+                Violation violation = new Violation();
+                violation.addWarning("mediaDesk", 633);
+                return violation;
+            }
+            hdParent = data.get(0).getId();
+            if (i == 2) {
+                ndParent = data.get(0).getId();
+            }
+        }
+        return null;
+    }
+}
diff -r 04d102624387 -r fa149968e60d src/main/java/de/intevation/lada/validation/rules/probe/DeskriptorToUmwelt.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/validation/rules/probe/DeskriptorToUmwelt.java	Wed May 13 11:23:29 2015 +0200
@@ -0,0 +1,93 @@
+package de.intevation.lada.validation.rules.probe;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.inject.Inject;
+
+import de.intevation.lada.model.land.LProbe;
+import de.intevation.lada.model.stamm.DeskriptorUmwelt;
+import de.intevation.lada.model.stamm.Deskriptoren;
+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;
+import de.intevation.lada.util.rest.Response;
+import de.intevation.lada.validation.Violation;
+import de.intevation.lada.validation.annotation.ValidationRule;
+import de.intevation.lada.validation.rules.Rule;
+
+ at ValidationRule("Probe")
+public class DeskriptorToUmwelt implements Rule {
+
+    @Inject
+    @RepositoryConfig(type=RepositoryType.RO)
+    private Repository repository;
+
+    @Override
+    public Violation execute(Object object) {
+        LProbe probe = (LProbe)object;
+        String[] mediaDesk = probe.getMediaDesk().split(" ");
+        List<Integer> mediaIds = new ArrayList<Integer>();
+        boolean zebs = false;
+        Integer parent = null;
+        Integer hdParent = null;
+        Integer ndParent = null;
+        if ("01".equals(mediaDesk[1])) {
+            zebs = true;
+        }
+        for (int i = 1; i < mediaDesk.length; i++) {
+            if ("00".equals(mediaDesk[i])) {
+                continue;
+            }
+            if (zebs && i < 5) {
+                parent = hdParent;
+            }
+            else if (!zebs && i < 3) {
+                parent = hdParent;
+            }
+            else {
+                parent = ndParent;
+            }
+            QueryBuilder<Deskriptoren> builder = new QueryBuilder<Deskriptoren>(
+                repository.entityManager("stamm"), Deskriptoren.class);
+            if (parent != null) {
+                builder.and("vorgaenger", parent);
+            }
+            builder.and("sn", mediaDesk[i]);
+            builder.and("ebene", i - 1);
+            Response response = repository.filter(builder.getQuery(), "stamm");
+            List<Deskriptoren> data = (List<Deskriptoren>)response.getData();
+            if (data.isEmpty()) {
+                Violation violation = new Violation();
+                violation.addWarning("mediaDesk", 633);
+                return violation;
+            }
+            hdParent = data.get(0).getId();
+            mediaIds.add(data.get(0).getId());
+            if (i == 2) {
+                ndParent = data.get(0).getId();
+            }
+        }
+        Violation violation = validateUmwelt(mediaIds, probe);
+        return violation;
+    }
+
+    private Violation validateUmwelt(List<Integer> media, LProbe probe) {
+        QueryBuilder<DeskriptorUmwelt> builder =
+            new QueryBuilder<DeskriptorUmwelt>(
+                repository.entityManager("stamm"), DeskriptorUmwelt.class);
+        for (int i = media.size() - 1; i > 0; i--) {
+            String field = "s" + (i > 9 ? i : "0" + i);
+            builder.and(field, media.get(i));
+        }
+        Response response = repository.filter(builder.getQuery(), "stamm");
+        List<DeskriptorUmwelt> data = (List<DeskriptorUmwelt>)response.getData();
+        if (data.isEmpty() || !data.get(0).getUmwId().equals(probe.getUmwId())) {
+            Violation violation = new Violation();
+            violation.addWarning("umwId", 632);
+            return violation;
+        }
+        return null;
+    }
+}


More information about the Lada-commits mailing list