[Lada-commits] [PATCH 09 of 13] Validate messung, messwert and ort objects

Wald Commits scm-commit at wald.intevation.org
Fri May 29 11:48:19 CEST 2015


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1432300552 -7200
# Node ID 328865fa3db7fa76c8affc5f990b060468d78ea9
# Parent  de47b7193bdb24d57caebf794e18ee525144d6c1
Validate messung, messwert and ort objects.

diff -r de47b7193bdb -r 328865fa3db7 src/main/java/de/intevation/lada/rest/MessungService.java
--- a/src/main/java/de/intevation/lada/rest/MessungService.java	Fri May 22 15:15:14 2015 +0200
+++ b/src/main/java/de/intevation/lada/rest/MessungService.java	Fri May 22 15:15:52 2015 +0200
@@ -41,6 +41,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 Messung objects.
@@ -105,6 +108,10 @@
     @AuthorizationConfig(type=AuthorizationType.OPEN_ID)
     private Authorization authorization;
 
+    @Inject
+    @ValidationConfig(type="Messung")
+    private Validator validator;
+
     /**
      * Get all Messung objects.
      * <p>
@@ -156,9 +163,17 @@
         @Context HttpServletRequest request,
         @PathParam("id") String id
     ) {
+        Response response =
+            defaultRepo.getById(LMessung.class, Integer.valueOf(id), "land");
+        LMessung messung = (LMessung)response.getData();
+        Violation violation = validator.validate(messung);
+        if (violation.hasErrors() || violation.hasWarnings()) {
+            response.setErrors(violation.getErrors());
+            response.setWarnings(violation.getWarnings());
+        }
         return authorization.filter(
             request,
-            defaultRepo.getById(LMessung.class, Integer.valueOf(id), "land"),
+            response,
             LMessung.class);
     }
 
@@ -205,6 +220,14 @@
             return new Response(false, 699, null);
         }
 
+        Violation violation = validator.validate(messung);
+        if (violation.hasErrors()) {
+            Response response = new Response(false, 604, messung);
+            response.setErrors(violation.getErrors());
+            response.setWarnings(violation.getWarnings());
+            return response;
+        }
+
         /* Persist the new messung object*/
         Response response = defaultRepo.create(messung, "land");
         LMessung ret = (LMessung)response.getData();
@@ -215,9 +238,13 @@
         /* Get and return the new probe object*/
         Response created =
             defaultRepo.getById(LMessung.class, ret.getId(), "land");
+        if(violation.hasWarnings()) {
+            created.setWarnings(violation.getWarnings());
+        }
+
         return authorization.filter(
             request,
-            new Response(true, 200, created.getData()),
+            created,
             LMessung.class);
     }
 
@@ -266,11 +293,21 @@
         if (lock.isLocked(messung)) {
             return new Response(false, 697, null);
         }
+        Violation violation = validator.validate(messung);
+        if (violation.hasErrors()) {
+            Response response = new Response(false, 604, messung);
+            response.setErrors(violation.getErrors());
+            response.setWarnings(violation.getWarnings());
+            return response;
+        }
         messung.setLetzteAenderung(new Timestamp(new Date().getTime()));
         Response response = defaultRepo.update(messung, "land");
         Response updated = defaultRepo.getById(
             LMessung.class,
             ((LMessung)response.getData()).getId(), "land");
+        if(violation.hasWarnings()) {
+            updated.setWarnings(violation.getWarnings());
+        }
         return authorization.filter(
             request,
             updated,
diff -r de47b7193bdb -r 328865fa3db7 src/main/java/de/intevation/lada/rest/MesswertService.java
--- a/src/main/java/de/intevation/lada/rest/MesswertService.java	Fri May 22 15:15:14 2015 +0200
+++ b/src/main/java/de/intevation/lada/rest/MesswertService.java	Fri May 22 15:15:52 2015 +0200
@@ -41,6 +41,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 Messwert objects.
@@ -111,6 +114,10 @@
     @AuthorizationConfig(type=AuthorizationType.OPEN_ID)
     private Authorization authorization;
 
+    @Inject
+    @ValidationConfig(type="Messwert")
+    private Validator validator;
+
     /**
      * Get all Messwert objects.
      * <p>
@@ -163,9 +170,17 @@
         @Context HttpServletRequest request,
         @PathParam("id") String id
     ) {
+        Response response =
+            defaultRepo.getById(LMesswert.class, Integer.valueOf(id), "land");
+        LMesswert messwert = (LMesswert)response.getData();
+        Violation violation = validator.validate(messwert);
+        if (violation.hasErrors() || violation.hasWarnings()) {
+            response.setErrors(violation.getErrors());
+            response.setWarnings(violation.getWarnings());
+        }
         return authorization.filter(
             request,
-            defaultRepo.getById(LMesswert.class, Integer.valueOf(id), "land"),
+            response,
             LMesswert.class);
     }
 
@@ -211,10 +226,22 @@
         ) {
             return new Response(false, 699, null);
         }
+        Violation violation = validator.validate(messwert);
+        if (violation.hasErrors()) {
+            Response response = new Response(false, 604, messwert);
+            response.setErrors(violation.getErrors());
+            response.setWarnings(violation.getWarnings());
+            return response;
+        }
+
         /* Persist the new messung object*/
+        Response response = defaultRepo.create(messwert, "land");
+        if(violation.hasWarnings()) {
+            response.setWarnings(violation.getWarnings());
+        }
         return authorization.filter(
             request,
-            defaultRepo.create(messwert, "land"),
+            response,
             LMesswert.class);
     }
 
@@ -263,11 +290,23 @@
         if (lock.isLocked(messwert)) {
             return new Response(false, 697, null);
         }
+        Violation violation = validator.validate(messwert);
+        if (violation.hasErrors()) {
+            Response response = new Response(false, 604, messwert);
+            response.setErrors(violation.getErrors());
+            response.setWarnings(violation.getWarnings());
+            return response;
+        }
+
         messwert.setLetzteAenderung(new Timestamp(new Date().getTime()));
         Response response = defaultRepo.update(messwert, "land");
         Response updated = defaultRepo.getById(
             LMesswert.class,
             ((LMesswert)response.getData()).getId(), "land");
+        if(violation.hasWarnings()) {
+            updated.setWarnings(violation.getWarnings());
+        }
+
         return authorization.filter(
             request,
             updated,
diff -r de47b7193bdb -r 328865fa3db7 src/main/java/de/intevation/lada/rest/OrtService.java
--- a/src/main/java/de/intevation/lada/rest/OrtService.java	Fri May 22 15:15:14 2015 +0200
+++ b/src/main/java/de/intevation/lada/rest/OrtService.java	Fri May 22 15:15:52 2015 +0200
@@ -41,6 +41,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 Ort objects.
@@ -107,6 +110,10 @@
     @AuthorizationConfig(type=AuthorizationType.OPEN_ID)
     private Authorization authorization;
 
+    @Inject
+    @ValidationConfig(type="Ort")
+    private Validator validator;
+
     /**
      * Get all Ort objects.
      * <p>
@@ -160,9 +167,17 @@
         @Context HttpServletRequest request,
         @PathParam("id") String id
     ) {
+        Response response =
+            defaultRepo.getById(LOrt.class, Integer.valueOf(id), "land");
+        LOrt ort = (LOrt)response.getData();
+        Violation violation = validator.validate(ort);
+        if (violation.hasErrors() || violation.hasWarnings()) {
+            response.setErrors(violation.getErrors());
+            response.setWarnings(violation.getWarnings());
+        }
         return authorization.filter(
             request,
-            defaultRepo.getById(LOrt.class, Integer.valueOf(id), "land"),
+            response,
             LOrt.class);
     }
 
@@ -203,10 +218,23 @@
                 LOrt.class)) {
             return new Response(false, 699, null);
         }
+        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;
+        }
+
         /* Persist the new object*/
+        Response response = defaultRepo.create(ort, "land");
+        if(violation.hasWarnings()) {
+            response.setWarnings(violation.getWarnings());
+        }
+
         return authorization.filter(
             request,
-            defaultRepo.create(ort, "land"),
+            response,
             LOrt.class);
     }
 
@@ -250,11 +278,23 @@
         if (lock.isLocked(ort)) {
             return new Response(false, 697, null);
         }
+        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;
+        }
+
         ort.setLetzteAenderung(new Timestamp(new Date().getTime()));
         Response response = defaultRepo.update(ort, "land");
         Response updated = defaultRepo.getById(
             LOrt.class,
             ((LOrt)response.getData()).getId(), "land");
+        if(violation.hasWarnings()) {
+            updated.setWarnings(violation.getWarnings());
+        }
+
         return authorization.filter(
             request,
             updated,


More information about the Lada-commits mailing list