changeset 228:e7110c4324eb

More validation for LMesswert and LMessung.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 04 Jul 2013 17:23:49 +0200
parents ef4c0e646b16
children 861b41fde030
files src/main/java/de/intevation/lada/data/LMesswertRepository.java src/main/java/de/intevation/lada/validation/LMessungValidator.java src/main/java/de/intevation/lada/validation/LMesswertValidator.java
diffstat 3 files changed, 125 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/data/LMesswertRepository.java	Thu Jul 04 15:15:17 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LMesswertRepository.java	Thu Jul 04 17:23:49 2013 +0200
@@ -1,6 +1,7 @@
 package de.intevation.lada.data;
 
 import java.util.List;
+import java.util.Map;
 
 import javax.ejb.EJBTransactionRolledbackException;
 import javax.enterprise.context.ApplicationScoped;
@@ -16,6 +17,8 @@
 import de.intevation.lada.manage.Manager;
 import de.intevation.lada.model.LMesswert;
 import de.intevation.lada.rest.Response;
+import de.intevation.lada.validation.ValidationException;
+import de.intevation.lada.validation.Validator;
 
 /**
  * This Container is an interface to read, write and update LMesswert obejcts.
@@ -39,6 +42,13 @@
     @Named("datamanager")
     private Manager manager;
 
+    /**
+     * The validator used for LMesswert.
+     */
+    @Inject
+    @Named("lmesswertvalidator")
+    private Validator validator;
+
     public EntityManager getEntityManager() {
         return this.em;
     }
@@ -97,7 +107,9 @@
         LMesswert messwert = (LMesswert)object;
         Response response = new Response(true, 200, messwert);
         try {
+            Map<String, Integer> warnings = validator.validate(messwert);
             manager.create(messwert);
+            response.setWarnings(warnings);
             return response;
         }
         catch (EntityExistsException eee) {
@@ -116,6 +128,10 @@
             response.setSuccess(false);
             response.setMessage(604);
         }
+        catch (ValidationException e) {
+            response.setSuccess(false);
+            response.setMessage(604);
+        }
         return response;
     }
 
--- a/src/main/java/de/intevation/lada/validation/LMessungValidator.java	Thu Jul 04 15:15:17 2013 +0200
+++ b/src/main/java/de/intevation/lada/validation/LMessungValidator.java	Thu Jul 04 17:23:49 2013 +0200
@@ -1,12 +1,18 @@
 package de.intevation.lada.validation;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
 import javax.inject.Named;
 
+import de.intevation.lada.data.QueryBuilder;
+import de.intevation.lada.data.Repository;
 import de.intevation.lada.model.LMessung;
+import de.intevation.lada.model.LProbe;
+import de.intevation.lada.rest.Response;
 
 /**
  * Validator for LMessung objects.
@@ -18,6 +24,13 @@
 public class LMessungValidator
 implements Validator
 {
+    @Inject
+    @Named("lproberepository")
+    private Repository probeRepository;
+
+    @Inject
+    @Named("lmessungrepository")
+    private Repository messungRepository;
 
     @Override
     public Map<String, Integer> validate(Object object)
@@ -30,8 +43,54 @@
         }
         LMessung messung = (LMessung)object;
 
-        //TODO: mode validation, see LSB: VI - Konsistenzregeln.
+        validateNebenprobenNr(messung, warnings);
+        validateDatum(messung, warnings);
         return warnings;
     }
 
+    private void validateNebenprobenNr(
+        LMessung messung,
+        Map<String, Integer> warnings)
+    throws ValidationException {
+        if (messung.getNebenprobenNr() == null ||
+            messung.getNebenprobenNr().equals("")) {
+            warnings.put("nebenprobenNr", 631);
+        }
+        QueryBuilder<LMessung> builder =
+            new QueryBuilder<LMessung>(
+                messungRepository.getEntityManager(),
+                LMessung.class);
+        builder.and("probeId", messung.getProbeId());
+        Response response = messungRepository.filter(builder.getQuery());
+        List<LMessung> list = (List<LMessung>) response.getData();
+        if (list.isEmpty()) {
+            return;
+        }
+        for (LMessung m: list) {
+            if (m.getNebenprobenNr().equals(messung.getNebenprobenNr())) {
+                Map<String, Integer> errors = new HashMap<String, Integer>();
+                errors.put("nebenprobenNr", 611);
+                throw new ValidationException(errors);
+            }
+        }
+    }
+
+    private void validateDatum(
+        LMessung messung,
+        Map<String, Integer> warnings)
+    throws ValidationException{
+        String probeId = messung.getProbeId();
+        Response response = probeRepository.findById(LProbe.class, probeId);
+        @SuppressWarnings("unchecked")
+        List<LProbe> list = (List<LProbe>) response.getData();
+        if (list.isEmpty()) {
+            Map<String, Integer> errors = new HashMap<String, Integer>();
+            errors.put("lprobe", 604);
+            throw new ValidationException(errors);
+        }
+        LProbe probe = list.get(0);
+        if (probe.getProbeentnahmeEnde().after(messung.getMesszeitpunkt())) {
+            warnings.put("messzeitpunkt", 661);
+        }
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/validation/LMesswertValidator.java	Thu Jul 04 17:23:49 2013 +0200
@@ -0,0 +1,49 @@
+package de.intevation.lada.validation;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Named;
+
+import de.intevation.lada.model.LMesswert;
+
+@Named("lmesswertvalidator")
+@ApplicationScoped
+public class LMesswertValidator
+implements Validator
+{
+
+    @Override
+    public Map<String, Integer> validate(Object object)
+    throws ValidationException {
+        Map<String, Integer> warnings = new HashMap<String, Integer>();
+        if (!(object instanceof LMesswert)) {
+            Map<String, Integer> errors = new HashMap<String, Integer>();
+            errors.put("lmesswert", 610);
+            throw new ValidationException(errors);
+        }
+        LMesswert messwert = (LMesswert)object;
+
+        validateMessunsicherheit(messwert, warnings);
+        return null;
+    }
+
+    private void validateMessunsicherheit(
+        LMesswert messwert,
+        Map<String, Integer> warnings)
+    throws ValidationException {
+        Float unsicherheit = messwert.getMessfehler();
+        Float nachweisgrenze = messwert.getNwgZuMesswert();
+        Float wert = messwert.getMesswert();
+        if (unsicherheit != null && unsicherheit > 0f) {
+            return;
+        }
+        else if (nachweisgrenze != null && wert < nachweisgrenze) {
+            return;
+        }
+        else {
+            warnings.put("messwert", 631);
+        }
+    }
+}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)