[Lada-commits] [PATCH] Check mandatory fields before saving a Messprogramm
Wald Commits
scm-commit at wald.intevation.org
Mon Jun 27 17:04:55 CEST 2016
# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1467039842 -7200
# Node ID 962439442e48d24f18ca04494860a88ab4d0f664
# Parent 84f17f4609693f5687b7de366a22517080199055
Check mandatory fields before saving a Messprogramm.
diff -r 84f17f460969 -r 962439442e48 src/main/java/de/intevation/lada/rest/MessprogrammService.java
--- 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);
diff -r 84f17f460969 -r 962439442e48 src/main/java/de/intevation/lada/validation/MessprogrammValidator.java
--- /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.
+ *
+ */
+ at ValidationConfig(type="Messprogramm")
+ at 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;
+ }
+}
diff -r 84f17f460969 -r 962439442e48 src/main/java/de/intevation/lada/validation/rules/messprogramm/HasAllMandatory.java
--- /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.
+ */
+ at 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;
+ }
+}
More information about the Lada-commits
mailing list