Mercurial > lada > lada-server
changeset 982:962439442e48
Check mandatory fields before saving a Messprogramm.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Mon, 27 Jun 2016 17:04:02 +0200 |
parents | 84f17f460969 |
children | a3f9cf7e38c1 |
files | src/main/java/de/intevation/lada/rest/MessprogrammService.java src/main/java/de/intevation/lada/validation/MessprogrammValidator.java src/main/java/de/intevation/lada/validation/rules/messprogramm/HasAllMandatory.java |
diffstat | 3 files changed, 131 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/rest/MessprogrammService.java Fri Jun 17 12:17:08 2016 +0200 +++ b/src/main/java/de/intevation/lada/rest/MessprogrammService.java Mon Jun 27 17:04:02 2016 +0200 @@ -38,6 +38,9 @@ import de.intevation.lada.util.data.RepositoryType; import de.intevation.lada.util.rest.RequestMethod; import de.intevation.lada.util.rest.Response; +import de.intevation.lada.validation.Validator; +import de.intevation.lada.validation.Violation; +import de.intevation.lada.validation.annotation.ValidationConfig; /** * REST service for Messprogramm objects. @@ -102,6 +105,13 @@ @AuthorizationConfig(type=AuthorizationType.HEADER) private Authorization authorization; + /** + * The validator used for Messprogramm objects. + */ + @Inject + @ValidationConfig(type="Messprogramm") + private Validator validator; + @Inject private ProbeFactory factory; @@ -232,6 +242,14 @@ return new Response(false, 699, null); } + Violation violation = validator.validate(messprogramm); + if (violation.hasErrors()) { + Response response = new Response(false, 604, messprogramm); + response.setErrors(violation.getErrors()); + response.setWarnings(violation.getWarnings()); + return response; + } + if (messprogramm.getUmwId() == null || messprogramm.getUmwId().length() == 0) { messprogramm = factory.findUmweltId(messprogramm); } @@ -293,6 +311,15 @@ ) { return new Response(false, 699, null); } + + Violation violation = validator.validate(messprogramm); + if (violation.hasErrors()) { + Response response = new Response(false, 604, messprogramm); + response.setErrors(violation.getErrors()); + response.setWarnings(violation.getWarnings()); + return response; + } + messprogramm.setLetzteAenderung(new Timestamp(new Date().getTime())); if (messprogramm.getUmwId() == null || messprogramm.getUmwId().equals("")) { messprogramm = factory.findUmweltId(messprogramm);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/validation/MessprogrammValidator.java Mon Jun 27 17:04:02 2016 +0200 @@ -0,0 +1,54 @@ +/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=3) + * and comes with ABSOLUTELY NO WARRANTY! Check out + * the documentation coming with IMIS-Labordaten-Application for details. + */ +package de.intevation.lada.validation; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Instance; +import javax.inject.Inject; + +import de.intevation.lada.model.land.Messprogramm; +import de.intevation.lada.validation.annotation.ValidationConfig; +import de.intevation.lada.validation.annotation.ValidationRule; +import de.intevation.lada.validation.rules.Rule; + +/** + * Validation for Messprogramm objects. + * + * Instantiates the set of rules for Messprogramm objects + * and uses these rules to validate the object. + * + */ +@ValidationConfig(type="Messprogramm") +@ApplicationScoped +public class MessprogrammValidator implements Validator { + + @Inject + @ValidationRule("Messprogramm") + private Instance<Rule> rules; + + @Override + public Violation validate(Object object) { + Violation violations = new Violation(); + if (!(object instanceof Messprogramm)) { + violations.addError("messprogramm", 602); + return violations; + } + for(Rule rule: rules) { + Violation result = rule.execute(object); + if (result != null) { + if (result.hasWarnings()) { + violations.addWarnings(result.getWarnings()); + } + if (result.hasErrors()) { + violations.addErrors(result.getErrors()); + } + } + } + return violations; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/validation/rules/messprogramm/HasAllMandatory.java Mon Jun 27 17:04:02 2016 +0200 @@ -0,0 +1,50 @@ +/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=3) + * and comes with ABSOLUTELY NO WARRANTY! Check out + * the documentation coming with IMIS-Labordaten-Application for details. + */ +package de.intevation.lada.validation.rules.messprogramm; + +import de.intevation.lada.model.land.Messprogramm; +import de.intevation.lada.validation.Violation; +import de.intevation.lada.validation.annotation.ValidationRule; +import de.intevation.lada.validation.rules.Rule; + +/** + * Validation rule for Messprogramm. + * Validates if the Messprogramm has Datenbasis. + */ +@ValidationRule("Messprogramm") +public class HasAllMandatory implements Rule { + + @Override + public Violation execute(Object object) { + Messprogramm messprogramm = (Messprogramm)object; + Violation violation = new Violation(); + + if (messprogramm.getNetzbetreiberId() == null + | "".equals(messprogramm.getNetzbetreiberId())) { + violation.addError("netzbetreiberId", 631); + } + if (messprogramm.getMstId() == null + | "".equals(messprogramm.getMstId())) { + violation.addError("mstlabor", 631); + } + if (messprogramm.getLaborMstId() == null + | "".equals(messprogramm.getLaborMstId())) { + violation.addError("mstlabor", 631); + } + if (messprogramm.getDatenbasisId() == null) { + violation.addError("datenbasisId", 631); + } + if (messprogramm.getProbenartId() == null) { + violation.addError("probenartId", 631); + } + + return violation.hasErrors() + ? violation + :null; + } +}