Mercurial > lada > lada-server
changeset 110:30e3ae65cd94
Added validation for LProbe attributes.
Now validates:
- Hauptprobennummer
- Entnahmeort
- Probeentnahmebegin
- Umweltbereich
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Wed, 12 Jun 2013 13:17:09 +0200 |
parents | a2427e766398 |
children | 39a501f3ace2 |
files | src/main/java/de/intevation/lada/validation/LProbeValidator.java |
diffstat | 1 files changed, 72 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/validation/LProbeValidator.java Wed Jun 12 13:15:19 2013 +0200 +++ b/src/main/java/de/intevation/lada/validation/LProbeValidator.java Wed Jun 12 13:17:09 2013 +0200 @@ -1,10 +1,21 @@ package de.intevation.lada.validation; +import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; +import javax.inject.Inject; import javax.inject.Named; +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import de.intevation.lada.data.LOrtRepository; +import de.intevation.lada.data.LProbeRepository; +import de.intevation.lada.model.LOrt; import de.intevation.lada.model.LProbe; /** @@ -22,6 +33,14 @@ */ private Map<String, Integer> warnings; + @Inject + private EntityManager em; + + @Inject + private LProbeRepository probeRepository; + @Inject + private LOrtRepository ortRepository; + /** * Validate a LProbe object. * @@ -36,26 +55,62 @@ errors.put("lprobe", 610); throw new ValidationException(errors); } - // Dummy warning. - //warnings.put("entnahmeort", 612); - validateId(probe); + LProbe p = (LProbe)probe; + + validateHauptProbenNummer(p); + validateEntnahmeOrt(p); + validateProbenahmeBegin(p); + validateUWB(p); } - /** - * Validate the LProbe id. - * - * @param probe The LProbe object. - * @throws ValidationException - */ - private void validateId(Object probe) - throws ValidationException{ - Map<String, Integer> errors = new HashMap<String, Integer>(); - // TODO Implement me! + private void validateHauptProbenNummer(LProbe p) + throws ValidationException { + String hpn = p.getHauptprobenNr(); + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery<LProbe> criteria = cb.createQuery(LProbe.class); + Root<LProbe> member = criteria.from(LProbe.class); + Predicate mstId = cb.equal(member.get("mstId"), p.getMstId()); + Predicate hpNr = cb.equal(member.get("hauptprobenNr"), hpn); + criteria.where(cb.and(mstId, hpNr)); - //// Dummy error. - //errors.put("probeId", 611); - //ValidationException ve = new ValidationException(errors); - //throw ve; + List<LProbe> proben = probeRepository.filter(criteria); + if (!proben.isEmpty()) { + Map<String, Integer> errors = new HashMap<String, Integer>(); + errors.put("hauptprobenNr", 611); + throw new ValidationException(errors); + } + } + + private void validateEntnahmeOrt(LProbe probe) { + String pid = probe.getProbeId(); + + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery<LOrt> criteria = cb.createQuery(LOrt.class); + Root<LOrt> member = criteria.from(LOrt.class); + Predicate probeId = cb.equal(member.get("probeId"), pid); + criteria.where(probeId); + + 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); + } + } + + private void validateUWB(LProbe probe) { + String uwb = probe.getUmwId(); + if (uwb == null || uwb.equals("")) { + this.warnings.put("uwb", 631); + } } /**