# HG changeset patch # User Raimund Renkert # Date 1371041168 -7200 # Node ID 92b8b0e1e9fc5b0faebfb08a989f8c79728312d4 # Parent 92affd9467f8b888362f6d1a14e0f79557910418 Modified Validator interface and restructured the LProbe validation. diff -r 92affd9467f8 -r 92b8b0e1e9fc src/main/java/de/intevation/lada/data/LProbeRepository.java --- 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 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 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); diff -r 92affd9467f8 -r 92b8b0e1e9fc src/main/java/de/intevation/lada/validation/LProbeValidator.java --- 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 Raimund Renkert */ @Named("lprobevalidator") +@ApplicationScoped public class LProbeValidator implements Validator { - /** - * Warnings found while validating the LProbe + * The repositories used in this validator. */ - private Map 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 validate(Object probe) throws ValidationException { - warnings = new HashMap(); + Mapwarnings = new HashMap(); if (!(probe instanceof LProbe)) { Map errors = new HashMap(); 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 warnings) throws ValidationException { String hpn = p.getHauptprobenNr(); - CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaBuilder cb = probeRepository.getCriteriaBuilder(); CriteriaQuery criteria = cb.createQuery(LProbe.class); Root member = criteria.from(LProbe.class); Predicate mstId = cb.equal(member.get("mstId"), p.getMstId()); @@ -77,14 +74,14 @@ if (!proben.isEmpty()) { Map errors = new HashMap(); errors.put("hauptprobenNr", 611); - throw new ValidationException(errors); + throw new ValidationException(errors, warnings); } } - private void validateEntnahmeOrt(LProbe probe) { + private void validateEntnahmeOrt(LProbe probe, Map warnings) { String pid = probe.getProbeId(); - CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaBuilder cb = ortRepository.getCriteriaBuilder(); CriteriaQuery criteria = cb.createQuery(LOrt.class); Root member = criteria.from(LOrt.class); Predicate probeId = cb.equal(member.get("probeId"), pid); @@ -92,34 +89,24 @@ List 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 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 getWarnings() { - return warnings; + private void validateUWB(LProbe probe, Map warnings) { + String uwb = probe.getUmwId(); + if (uwb == null || uwb.equals("")) { + warnings.put("uwb", 631); + } } } diff -r 92affd9467f8 -r 92b8b0e1e9fc src/main/java/de/intevation/lada/validation/Validator.java --- 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 getWarnings(); + public Map validate(Object object) throws ValidationException; }