changeset 539:deb3fec601ed

Use multivalued map for errors and warnings to be able to store more than one error/warning per item.
author Raimund Renkert <raimund.renkert@intevation.de>
date Tue, 24 Feb 2015 14:56:46 +0100
parents fd927b584c54
children 7925f5eda6c4
files src/main/java/de/intevation/lada/util/rest/Response.java src/main/java/de/intevation/lada/validation/Violation.java src/test/java/de/intevation/lada/test/validator/Probe.java
diffstat 3 files changed, 44 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/util/rest/Response.java	Tue Feb 24 10:58:06 2015 +0100
+++ b/src/main/java/de/intevation/lada/util/rest/Response.java	Tue Feb 24 14:56:46 2015 +0100
@@ -11,6 +11,9 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+
 
 /**
 * Response object storing information about success, warnings, errors and
@@ -25,8 +28,8 @@
     private Boolean success;
     private String message;
     private Object data;
-    private Map<String, String> errors;
-    private Map<String, String> warnings;
+    private MultivaluedMap<String, Integer> errors;
+    private MultivaluedMap<String, Integer> warnings;
     private Boolean readonly;
     private int totalCount;
 
@@ -41,8 +44,8 @@
         this.success = success;
         this.message = Integer.toString(code);
         this.data = data;
-        this.errors = new HashMap<String, String>();
-        this.warnings = new HashMap<String, String>();
+        this.errors = new MultivaluedHashMap<String, Integer>();
+        this.warnings = new MultivaluedHashMap<String, Integer>();
         this.readonly = Boolean.FALSE;
         this.totalCount = 0;
     }
@@ -58,8 +61,8 @@
         this.success = success;
         this.message = Integer.toString(code);
         this.data = data;
-        this.errors = new HashMap<String, String>();
-        this.warnings = new HashMap<String, String>();
+        this.errors = new MultivaluedHashMap<String, Integer>();
+        this.warnings = new MultivaluedHashMap<String, Integer>();
         this.readonly = Boolean.FALSE;
         this.totalCount = totalCount;
     }
@@ -88,20 +91,22 @@
         this.data = data;
     }
 
-    public Map<String, String> getErrors() {
+    public MultivaluedMap<String, Integer> getErrors() {
         return errors;
     }
 
-    public void setErrors(Map<String, Integer> errors) {
-        this.errors = this.convertCodes(errors);
+    public void setErrors(MultivaluedMap<String, Integer> errors) {
+        this.errors.putAll(errors);
+//        this.errors = this.convertCodes(errors);
     }
 
-    public Map<String, String> getWarnings() {
+    public MultivaluedMap<String, Integer> getWarnings() {
         return warnings;
     }
 
-    public void setWarnings(Map<String, Integer> warnings) {
-        this.warnings = this.convertCodes(warnings);
+    public void setWarnings(MultivaluedMap<String, Integer> warnings) {
+        this.warnings.putAll(warnings);
+        //this.warnings = this.convertCodes(warnings);
     }
 
     public Boolean getReadonly() {
@@ -126,6 +131,7 @@
         this.totalCount = totalCount;
     }
 
+    @SuppressWarnings("unused")
     private HashMap<String, String> convertCodes(Map<String, Integer> codes) {
         HashMap<String, String> converted = new HashMap<String, String>();
         if (codes == null || codes.isEmpty()) {
--- a/src/main/java/de/intevation/lada/validation/Violation.java	Tue Feb 24 10:58:06 2015 +0100
+++ b/src/main/java/de/intevation/lada/validation/Violation.java	Tue Feb 24 14:56:46 2015 +0100
@@ -7,8 +7,8 @@
  */
 package de.intevation.lada.validation;
 
-import java.util.HashMap;
-import java.util.Map;
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
 
 /**
  *
@@ -16,37 +16,41 @@
  */
 public class Violation {
 
-    private Map<String, Integer> warnings;
+    private MultivaluedMap<String, Integer> warnings;
 
-    private Map<String, Integer> errors;
+    private MultivaluedMap<String, Integer> errors;
 
     public Violation() {
-        this.warnings = new HashMap<String, Integer>();
-        this.errors = new HashMap<String, Integer>();
+        this.warnings = new MultivaluedHashMap<String, Integer>();
+        this.errors = new MultivaluedHashMap<String, Integer>();
     }
 
-    public Map<String, Integer> getWarnings() {
+    public MultivaluedMap<String, Integer> getWarnings() {
         return this.warnings;
     }
 
-    public Map<String, Integer> getErrors() {
+    public MultivaluedMap<String, Integer> getErrors() {
         return this.errors;
     }
 
     public void addWarning(String key, Integer value) {
-        this.warnings.put(key, value);
+        this.warnings.add(key, value);
     }
 
     public void addError(String key, Integer value) {
-        this.errors.put(key, value);
+        this.errors.add(key, value);
     }
 
-    public void addWarnings(Map<String, Integer> warnings) {
-        this.warnings.putAll(warnings);
+    public void addWarnings(MultivaluedMap<String, Integer> warnings) {
+        for (String key: warnings.keySet()) {
+            this.warnings.addAll(key, warnings.get(key));
+        }
     }
 
-    public void addErrors(Map<String, Integer> errors) {
-        this.errors.putAll(errors);
+    public void addErrors(MultivaluedMap<String, Integer> errors) {
+        for (String key: errors.keySet()) {
+            this.errors.addAll(key, errors.get(key));
+        }
     }
 
     public boolean hasWarnings() {
--- a/src/test/java/de/intevation/lada/test/validator/Probe.java	Tue Feb 24 10:58:06 2015 +0100
+++ b/src/test/java/de/intevation/lada/test/validator/Probe.java	Tue Feb 24 14:56:46 2015 +0100
@@ -45,7 +45,7 @@
         Violation violation = validator.validate(probe);
         Assert.assertTrue(violation.hasErrors());
         Assert.assertTrue(violation.getErrors().containsKey("hauptprobenNr"));
-        Assert.assertTrue(violation.getErrors().get("hauptprobenNr") == 631);
+        Assert.assertTrue(violation.getErrors().get("hauptprobenNr").contains(631));
         prot.setPassed(true);
     }
 
@@ -61,7 +61,7 @@
         Violation violation = validator.validate(probe);
         Assert.assertTrue(violation.hasErrors());
         Assert.assertTrue(violation.getErrors().containsKey("hauptprobenNr"));
-        Assert.assertTrue(violation.getErrors().get("hauptprobenNr") == 611);
+        Assert.assertTrue(violation.getErrors().get("hauptprobenNr").contains(611));
         prot.setPassed(true);
     }
 
@@ -113,7 +113,7 @@
         Violation violation = validator.validate(probe);
         Assert.assertTrue(violation.hasErrors());
         Assert.assertTrue(violation.getErrors().containsKey("hauptprobenNr"));
-        Assert.assertTrue(violation.getErrors().get("hauptprobenNr") == 611);
+        Assert.assertTrue(violation.getErrors().get("hauptprobenNr").contains(611));
         prot.setPassed(true);
     }
 
@@ -143,7 +143,7 @@
         Violation violation = validator.validate(probe);
         Assert.assertTrue(violation.hasWarnings());
         Assert.assertTrue(violation.getWarnings().containsKey("entnahmeOrt"));
-        Assert.assertTrue(violation.getWarnings().get("entnahmeOrt") == 631);
+        Assert.assertTrue(violation.getWarnings().get("entnahmeOrt").contains(631));
         prot.setPassed(true);
     }
 
@@ -155,6 +155,7 @@
         protocol.add(prot);
         LProbe probe = new LProbe();
         probe.setProbeentnahmeBeginn(new Timestamp(1376287046510l));
+        probe.setProbeentnahmeEnde(new Timestamp(1376287046511l));
         Violation violation = validator.validate(probe);
         if (violation.hasWarnings()) {
             Assert.assertFalse(violation.getWarnings().containsKey("probeentnahmeBegin"));
@@ -172,7 +173,9 @@
         Violation violation = validator.validate(probe);
         Assert.assertTrue(violation.hasWarnings());
         Assert.assertTrue(violation.getWarnings().containsKey("probeentnahmeBegin"));
-        Assert.assertTrue(violation.getWarnings().get("probeentnahmeBegin") == 631);
+        Assert.assertTrue(violation.getWarnings().get("probeentnahmeBegin").contains(631));
+        prot.setPassed(true);
+    }
         prot.setPassed(true);
     }
 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)