changeset 117:92b8b0e1e9fc

Modified Validator interface and restructured the LProbe validation.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 12 Jun 2013 14:46:08 +0200
parents 92affd9467f8
children ffe3234a4590
files src/main/java/de/intevation/lada/data/LProbeRepository.java src/main/java/de/intevation/lada/validation/LProbeValidator.java src/main/java/de/intevation/lada/validation/Validator.java
diffstat 3 files changed, 39 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/data/LProbeRepository.java	Wed Jun 12 14:43:46 2013 +0200
+++ b/src/main/java/de/intevation/lada/data/LProbeRepository.java	Wed Jun 12 14:46:08 2013 +0200
@@ -2,6 +2,7 @@
 
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 
 import javax.ejb.EJBTransactionRolledbackException;
 import javax.enterprise.context.ApplicationScoped;
@@ -18,7 +19,6 @@
 import de.intevation.lada.manage.LProbeManager;
 import de.intevation.lada.model.LProbe;
 import de.intevation.lada.rest.Response;
-import de.intevation.lada.validation.LProbeValidator;
 import de.intevation.lada.validation.ValidationException;
 import de.intevation.lada.validation.Validator;
 
@@ -43,6 +43,9 @@
     @Inject
     private LProbeManager manager;
 
+    @Inject
+    @Named("lprobevalidator")
+    private Validator validator;
     /**
      * Filter for LProbe objects used for calls from a service.
      *
@@ -105,12 +108,11 @@
      */
     public Response create(LProbe probe) {
         Response response = new Response(true, 200, probe);
-        Validator validator = new LProbeValidator();
         // Try to save the new LProbe.
         try {
-            validator.validate(probe);
+            Map<String, Integer> warnings = validator.validate(probe);
             manager.create(probe);
-            response.setWarnings(validator.getWarnings());
+            response.setWarnings(warnings);
             return response;
         }
         catch (EntityExistsException eee) {
@@ -129,7 +131,7 @@
             response.setSuccess(false);
             response.setMessage(604);
             response.setErrors(ve.getErrors());
-            response.setWarnings(validator.getWarnings());
+            response.setWarnings(ve.getWarnings());
         }
         catch (EJBTransactionRolledbackException te) {
             response.setSuccess(false);
@@ -140,12 +142,11 @@
 
     public Response update(LProbe probe) {
         Response response = new Response(true, 200, probe);
-        Validator validator = new LProbeValidator();
         // Try to save the new LProbe.
         try {
-            validator.validate(probe);
+            Map<String, Integer> warnings = validator.validate(probe);
             manager.update(probe);
-            response.setWarnings(validator.getWarnings());
+            response.setWarnings(warnings);
             return response;
         }
         catch (EntityExistsException eee) {
@@ -164,7 +165,7 @@
             response.setSuccess(false);
             response.setMessage(604);
             response.setErrors(ve.getErrors());
-            response.setWarnings(validator.getWarnings());
+            response.setWarnings(ve.getWarnings());
         }
         catch (EJBTransactionRolledbackException te) {
             response.setSuccess(false);
--- a/src/main/java/de/intevation/lada/validation/LProbeValidator.java	Wed Jun 12 14:43:46 2013 +0200
+++ b/src/main/java/de/intevation/lada/validation/LProbeValidator.java	Wed Jun 12 14:46:08 2013 +0200
@@ -5,6 +5,7 @@
 import java.util.List;
 import java.util.Map;
 
+import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
 import javax.inject.Named;
 import javax.persistence.EntityManager;
@@ -24,18 +25,13 @@
  * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a>
  */
 @Named("lprobevalidator")
+@ApplicationScoped
 public class LProbeValidator
 implements Validator
 {
-
     /**
-     * Warnings found while validating the LProbe
+     * The repositories used in this validator.
      */
-    private Map<String, Integer> warnings;
-
-    @Inject
-    private EntityManager em;
-
     @Inject
     private LProbeRepository probeRepository;
     @Inject
@@ -47,9 +43,9 @@
      * @param probe The LProbe object.
      */
     @Override
-    public void validate(Object probe)
+    public Map<String, Integer> validate(Object probe)
     throws ValidationException {
-        warnings = new HashMap<String, Integer>();
+        Map<String, Integer>warnings = new HashMap<String, Integer>();
         if (!(probe instanceof LProbe)) {
             Map<String, Integer> errors = new HashMap<String, Integer>();
             errors.put("lprobe", 610);
@@ -57,16 +53,17 @@
         }
         LProbe p = (LProbe)probe;
 
-        validateHauptProbenNummer(p);
-        validateEntnahmeOrt(p);
-        validateProbenahmeBegin(p);
-        validateUWB(p);
+        validateEntnahmeOrt(p, warnings);
+        validateProbenahmeBegin(p, warnings);
+        validateUWB(p, warnings);
+        validateHauptProbenNummer(p, warnings);
+        return warnings;
     }
 
-    private void validateHauptProbenNummer(LProbe p)
+    private void validateHauptProbenNummer(LProbe p, Map<String, Integer> warnings)
     throws ValidationException {
         String hpn = p.getHauptprobenNr();
-        CriteriaBuilder cb = em.getCriteriaBuilder();
+        CriteriaBuilder cb = probeRepository.getCriteriaBuilder();
         CriteriaQuery<LProbe> criteria = cb.createQuery(LProbe.class);
         Root<LProbe> member = criteria.from(LProbe.class);
         Predicate mstId = cb.equal(member.get("mstId"), p.getMstId());
@@ -77,14 +74,14 @@
         if (!proben.isEmpty()) {
             Map<String, Integer> errors = new HashMap<String, Integer>();
             errors.put("hauptprobenNr", 611);
-            throw new ValidationException(errors);
+            throw new ValidationException(errors, warnings);
         }
     }
 
-    private void validateEntnahmeOrt(LProbe probe) {
+    private void validateEntnahmeOrt(LProbe probe, Map<String, Integer> warnings) {
         String pid = probe.getProbeId();
 
-        CriteriaBuilder cb = em.getCriteriaBuilder();
+        CriteriaBuilder cb = ortRepository.getCriteriaBuilder();
         CriteriaQuery<LOrt> criteria = cb.createQuery(LOrt.class);
         Root<LOrt> member = criteria.from(LOrt.class);
         Predicate probeId = cb.equal(member.get("probeId"), pid);
@@ -92,34 +89,24 @@
 
         List<LOrt> orte = ortRepository.filter(criteria);
         if(orte.isEmpty()) {
-            this.warnings.put("entnahmeOrt", 631);
-        }
-    }
-
-    private void validateProbenahmeBegin(LProbe probe) {
-        Date begin = probe.getProbeentnahmeBeginn();
-        if (begin == null) {
-            this.warnings.put("probeentnahmeBegin", 631);
-        }
-        else if (begin.after(new Date())){
-            this.warnings.put("probeentnahmeBegin", 661);
+            warnings.put("entnahmeOrt", 631);
         }
     }
 
-    private void validateUWB(LProbe probe) {
-        String uwb = probe.getUmwId();
-        if (uwb == null || uwb.equals("")) {
-            this.warnings.put("uwb", 631);
+    private void validateProbenahmeBegin(LProbe probe, Map<String, Integer> warnings) {
+        Date begin = probe.getProbeentnahmeBeginn();
+        if (begin == null) {
+            warnings.put("probeentnahmeBegin", 631);
+        }
+        else if (begin.after(new Date())){
+            warnings.put("probeentnahmeBegin", 661);
         }
     }
 
-    /**
-     * Getter for warnings occurred while validating the LProbe.
-     *
-     * @return Map of field - warning code pairs.
-     */
-    @Override
-    public Map<String, Integer> getWarnings() {
-        return warnings;
+    private void validateUWB(LProbe probe, Map<String, Integer> warnings) {
+        String uwb = probe.getUmwId();
+        if (uwb == null || uwb.equals("")) {
+            warnings.put("uwb", 631);
+        }
     }
 }
--- a/src/main/java/de/intevation/lada/validation/Validator.java	Wed Jun 12 14:43:46 2013 +0200
+++ b/src/main/java/de/intevation/lada/validation/Validator.java	Wed Jun 12 14:46:08 2013 +0200
@@ -9,6 +9,5 @@
  */
 public interface Validator
 {
-    public void validate(Object object) throws ValidationException;
-    public Map<String, Integer> getWarnings();
+    public Map<String, Integer> validate(Object object) throws ValidationException;
 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)