changeset 1227:a0968660a4c5

Use reusable validation rule instead of in-service implementation.
author Tom Gottfried <tom@intevation.de>
date Fri, 09 Dec 2016 16:34:44 +0100 (2016-12-09)
parents 85a8221e957a
children 22d2123e1d2a
files src/main/java/de/intevation/lada/rest/stamm/OrtService.java src/main/java/de/intevation/lada/validation/OrtValidator.java src/main/java/de/intevation/lada/validation/rules/ort/IsUnique.java
diffstat 3 files changed, 125 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- 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;
     }
 
     /**
--- /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@intevation.de">Raimund Renkert</a>
+ */
+@ValidationConfig(type="Ort")
+@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;
+    }
+
+}
--- /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;
+
+@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;
+    }
+}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)