changeset 66:54ff6ded05b2

New service to create a LProbe.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 31 May 2013 14:04:01 +0200
parents 8078fde8b02b
children 530a941dbf79
files src/main/java/de/intevation/lada/rest/LProbeService.java
diffstat 1 files changed, 64 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/rest/LProbeService.java	Fri May 31 14:02:45 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/LProbeService.java	Fri May 31 14:04:01 2013 +0200
@@ -1,11 +1,14 @@
 package de.intevation.lada.rest;
 
 import java.util.List;
+import java.util.Map;
 import java.util.logging.Logger;
 
 import javax.enterprise.context.RequestScoped;
 import javax.inject.Inject;
+import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
+import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
@@ -91,13 +94,66 @@
         return repository.filter(mstId, uwbId, begin);
     }
 
+    @POST
+    @Path("/create")
+    @Produces("text/json")
+    @Consumes("application/json")
+    public String create(LProbe probe) {
+        boolean success = repository.create(probe);
+        if(success) {
+            return "[{" +
+                   "returncode: 200," +
+                   "errorfields: []," +
+                   createWarningFields() +
+                   "}]";
+        }
+        else {
+            int generalError = repository.getGeneralError();
+            String response = "[{ \"returncode\": " + generalError + ", ";
+            response += "\"errors\": {" + createErrorFields() + "}, ";
+            response += "\"warnings\": {" + createWarningFields() + "}";
+            response += "}]";
+            return response;
+        }
+    }
 
-    //    200 Ok
-    //    Anderes ist Fehler.
-    //    {
-    //        returncode: 500,
-    //        field: [
-    //           {fieldname: validator_error_code},
-    //        ]
-    //    }
+    private String createWarningFields() {
+        Map<String, Integer> warnings = repository.getWarnings();
+        String response = "\"fields\": [";
+        if (warnings == null || warnings.isEmpty()) {
+            response += "]";
+            return response;
+        }
+        boolean first = true;
+        for (Map.Entry<String, Integer> entry: warnings.entrySet()) {
+            if (!first) {
+                response +=",";
+            }
+            response += "{" + "\"" + entry.getKey() +
+                "\": " + entry.getValue() + "}";
+            first = false;
+        }
+        response += "]";
+        return response;
+    }
+
+    private String createErrorFields() {
+        Map<String, Integer> errors = repository.getErrors();
+        String response = "\"fields\": [";
+        if (errors.isEmpty()) {
+            response += "]";
+            return response;
+        }
+        boolean first = true;
+        for (Map.Entry<String, Integer> entry: errors.entrySet()) {
+            if (!first) {
+                response +=",";
+            }
+            response += "{" + "\"" + entry.getKey() +
+                "\": " + entry.getValue() + "}";
+            first = false;
+        }
+        response += "]";
+        return response;
+    }
 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)