[Lada-commits] [PATCH] Validate probe objects in REST services

Wald Commits scm-commit at wald.intevation.org
Tue Feb 24 15:14:49 CET 2015


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1424787344 -3600
# Node ID 7a0e3d49ae33755be8f1be56b48f7ffb71c112ab
# Parent  8215619b208e27ec7f682d0cb347f9b465bc5f91
Validate probe objects in REST services.

diff -r 8215619b208e -r 7a0e3d49ae33 src/main/java/de/intevation/lada/rest/ProbeService.java
--- a/src/main/java/de/intevation/lada/rest/ProbeService.java	Tue Feb 24 14:58:20 2015 +0100
+++ b/src/main/java/de/intevation/lada/rest/ProbeService.java	Tue Feb 24 15:15:44 2015 +0100
@@ -45,6 +45,9 @@
 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.Validator;
+import de.intevation.lada.validation.Violation;
+import de.intevation.lada.validation.annotation.ValidationConfig;
 
 
 /**
@@ -75,6 +78,10 @@
     @AuthorizationConfig(type=AuthorizationType.NONE)
     private Authorization authorization;
 
+    @Inject
+    @ValidationConfig(type="Probe")
+    private Validator validator;
+
     /**
      * Get all probe objects.
      *
@@ -141,7 +148,13 @@
             logger.debug("User is not authenticated!");
             return new Response(false, 699, null);
         }
-        return defaultRepo.getById(LProbe.class, Integer.valueOf(id), "land");
+        Response response =
+            defaultRepo.getById(LProbe.class, Integer.valueOf(id), "land");
+        Violation violation = validator.validate(response.getData());
+        if (violation.hasWarnings()) {
+            response.setWarnings(violation.getWarnings());
+        }
+        return response;
     }
 
     /**
@@ -156,17 +169,27 @@
         if (!authentication.isAuthenticated(headers)) {
             return new Response(false, 699, null);
         }
+        Violation violation = validator.validate(probe);
+        if (violation.hasErrors()) {
+            Response response = new Response(false, 604, probe);
+            response.setErrors(violation.getErrors());
+            response.setWarnings(violation.getWarnings());
+            return response;
+        }
         /* Persist the new probe object*/
-        Response response = defaultRepo.create(probe, "land");
-        LProbe ret = (LProbe)response.getData();
+        Response newProbe = defaultRepo.create(probe, "land");
+        LProbe ret = (LProbe)newProbe.getData();
         /* Create and persist a new probe translation object*/
         ProbeTranslation trans = new ProbeTranslation();
         trans.setProbeId(ret);
         defaultRepo.create(trans, "land");
         /* Get and return the new probe object*/
-        Response created =
+        Response response =
             defaultRepo.getById(LProbe.class, ret.getId(), "land");
-        return new Response(true, 200, created.getData());
+        if(violation.hasWarnings()) {
+            response.setWarnings(violation.getWarnings());
+        }
+        return response;
     }
 
     /**
@@ -182,10 +205,20 @@
             logger.debug("User is not authenticated!");
             return new Response(false, 699, null);
         }
+        Violation violation = validator.validate(probe);
+        if (violation.hasErrors()) {
+            Response response = new Response(false, 604, probe);
+            response.setErrors(violation.getErrors());
+            response.setWarnings(violation.getWarnings());
+            return response;
+        }
         Response response = defaultRepo.update(probe, "land");
         Response updated = defaultRepo.getById(
             LProbe.class,
             ((LProbe)response.getData()).getId(), "land");
+        if (violation.hasWarnings()) {
+            updated.setWarnings(violation.getWarnings());
+        }
         return updated;
     }
 


More information about the Lada-commits mailing list