[Lada-commits] [PATCH 7 of 8] Use reusable validation rule instead of in-service implementation

Wald Commits scm-commit at wald.intevation.org
Fri Dec 9 16:58:43 CET 2016


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1481297684 -3600
# Node ID a0968660a4c55d2817ca738875602aa8f717aa4b
# Parent  85a8221e957a7af4a8db70390235640194cf5bd8
Use reusable validation rule instead of in-service implementation.

diff -r 85a8221e957a -r a0968660a4c5 src/main/java/de/intevation/lada/rest/stamm/OrtService.java
--- a/src/main/java/de/intevation/lada/rest/stamm/OrtService.java	Fri Dec 09 16:33:42 2016 +0100
+++ b/src/main/java/de/intevation/lada/rest/stamm/OrtService.java	Fri Dec 09 16:34:44 2016 +0100
@@ -38,6 +38,9 @@
 import de.intevation.lada.util.data.RepositoryType;
 import de.intevation.lada.util.rest.RequestMethod;
 import de.intevation.lada.util.rest.Response;
+import de.intevation.lada.validation.Validator;
+import de.intevation.lada.validation.Violation;
+import de.intevation.lada.validation.annotation.ValidationConfig;
 
 /**
  * REST service for SOrt objects.
@@ -95,6 +98,10 @@
     @Inject
     private OrtFactory ortFactory;
 
+    @Inject
+    @ValidationConfig(type="Ort")
+    private Validator validator;
+
     /**
      * Get all SOrt objects.
      * <p>
@@ -262,22 +269,22 @@
         ) {
             return new Response(false, 699, ort);
         }
-        QueryBuilder<Ort> builder =
-            new QueryBuilder<Ort>(
-                repository.entityManager("stamm"),
-                Ort.class
-            );
-        builder.and("ortId", ort.getOrtId());
-        builder.and("netzbetreiberId", ort.getNetzbetreiberId());
 
-        List<Ort> orte =
-            repository.filterPlain(builder.getQuery(), "stamm");
-        if (orte.isEmpty() ||
-            orte.get(0).getId() == ort.getId()) {
-            ortFactory.transformCoordinates(ort);
-            return repository.create(ort, "stamm");
+        Violation violation = validator.validate(ort);
+        if (violation.hasErrors()) {
+            Response response = new Response(false, 604, ort);
+            response.setErrors(violation.getErrors());
+            response.setWarnings(violation.getWarnings());
+            return response;
         }
-        return new Response(false, 672, null);
+
+        ortFactory.transformCoordinates(ort);
+        Response response = repository.create(ort, "stamm");
+        if(violation.hasWarnings()) {
+            response.setWarnings(violation.getWarnings());
+        }
+
+        return response;
     }
 
     /**
diff -r 85a8221e957a -r a0968660a4c5 src/main/java/de/intevation/lada/validation/OrtValidator.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/validation/OrtValidator.java	Fri Dec 09 16:34:44 2016 +0100
@@ -0,0 +1,56 @@
+/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU GPL (v>=3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out
+ * the documentation coming with IMIS-Labordaten-Application for details.
+ */
+package de.intevation.lada.validation;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Instance;
+import javax.inject.Inject;
+
+import de.intevation.lada.model.stammdaten.Ort;
+import de.intevation.lada.validation.annotation.ValidationConfig;
+import de.intevation.lada.validation.annotation.ValidationRule;
+import de.intevation.lada.validation.rules.Rule;
+
+/**
+ * Validation for ort objects.
+ *
+ * Instantiates the set of rules for ort objects and uses these rules to
+ * validate the object.
+ *
+ * @author <a href="mailto:raimund.renkert at intevation.de">Raimund Renkert</a>
+ */
+ at ValidationConfig(type="Ort")
+ at ApplicationScoped
+public class OrtValidator implements Validator {
+
+    @Inject
+    @ValidationRule("Ort")
+    private Instance<Rule> rules;
+
+    @Override
+    public Violation validate(Object object) {
+        Violation violations = new Violation();
+        if (!(object instanceof Ort)) {
+            violations.addError("ort", 602);
+            return violations;
+        }
+        for(Rule rule: rules) {
+            Violation result = rule.execute(object);
+            if (result != null) {
+                if (result.hasWarnings()) {
+                    violations.addWarnings(result.getWarnings());
+                }
+                if (result.hasErrors()) {
+                    violations.addErrors(result.getErrors());
+                }
+            }
+        }
+        return violations;
+    }
+
+}
diff -r 85a8221e957a -r a0968660a4c5 src/main/java/de/intevation/lada/validation/rules/ort/IsUnique.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/validation/rules/ort/IsUnique.java	Fri Dec 09 16:34:44 2016 +0100
@@ -0,0 +1,48 @@
+/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU GPL (v>=3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out
+ * the documentation coming with IMIS-Labordaten-Application for details.
+ */
+package de.intevation.lada.validation.rules.ort;
+
+import javax.inject.Inject;
+
+import de.intevation.lada.model.stammdaten.Ort;
+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.validation.Violation;
+import de.intevation.lada.validation.annotation.ValidationRule;
+import de.intevation.lada.validation.rules.Rule;
+
+ at ValidationRule("Ort")
+public class IsUnique implements Rule {
+
+    @Inject
+    @RepositoryConfig(type=RepositoryType.RO)
+    private Repository repository;
+
+    @Override
+    public Violation execute(Object object) {
+        Ort ort = (Ort)object;
+
+        QueryBuilder<Ort> builder = new QueryBuilder<Ort>(
+            repository.entityManager("stamm"), Ort.class);
+        builder.and("netzbetreiberId", ort.getNetzbetreiberId());
+        builder.and("ortId", ort.getOrtId());
+        if (ort.getId() != null) {
+            // Consider UPDATE
+            builder.andNot("id", ort.getId());
+        }
+        if (!repository.filterPlain(builder.getQuery(), "stamm").isEmpty()) {
+            Violation violation = new Violation();
+            violation.addError("ortId", 672);
+            return violation;
+        }
+
+        return null;
+    }
+}


More information about the Lada-commits mailing list