# HG changeset patch # User Tom Gottfried # Date 1478000845 -3600 # Node ID 48c0132dbc85c9af7d6f429fa29dc170b11e2d21 # Parent 830a47aeb15d6bcbcd3fd30779aa8e594db0e7c8 Adapt definition to LAF and validate uniqueness of idAlt. diff -r 830a47aeb15d -r 48c0132dbc85 db_schema/lada_schema.sql --- a/db_schema/lada_schema.sql Tue Nov 01 12:11:06 2016 +0100 +++ b/db_schema/lada_schema.sql Tue Nov 01 12:47:25 2016 +0100 @@ -192,7 +192,7 @@ CREATE TABLE probe ( id serial PRIMARY KEY, - id_alt character varying(20) UNIQUE NOT NULL + id_alt character varying(16) UNIQUE NOT NULL DEFAULT 'sss' || lpad(nextval('land.probe_probe_id_seq')::varchar, 12, '0') || 'Y', diff -r 830a47aeb15d -r 48c0132dbc85 src/main/java/de/intevation/lada/validation/rules/probe/UniqueIdAlt.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/validation/rules/probe/UniqueIdAlt.java Tue Nov 01 12:47:25 2016 +0100 @@ -0,0 +1,56 @@ +/* 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.probe; + +import java.util.List; + +import javax.inject.Inject; + +import de.intevation.lada.model.land.Probe; +import de.intevation.lada.util.annotation.RepositoryConfig; +import de.intevation.lada.util.data.QueryBuilder; +import de.intevation.lada.util.data.Repository; +import de.intevation.lada.util.data.RepositoryType; +import de.intevation.lada.validation.Violation; +import de.intevation.lada.validation.annotation.ValidationRule; +import de.intevation.lada.validation.rules.Rule; + +/** + * Validation rule for probe. + * Validates if the probe has a unique "id_alt". + * + */ +@ValidationRule("Probe") +public class UniqueIdAlt implements Rule { + + @Inject + @RepositoryConfig(type=RepositoryType.RO) + private Repository repo; + + @Override + public Violation execute(Object object) { + Probe probe = (Probe)object; + QueryBuilder builder = new QueryBuilder( + repo.entityManager("land"), + Probe.class); + builder.and("idAlt", probe.getIdAlt()); + List existing = repo.filterPlain(builder.getQuery(), "land"); + if (!existing.isEmpty()) { + Probe found = existing.get(0); + // The probe found in the db equals the new probe. (Update) + if (probe.getId() != null && probe.getId().equals(found.getId())) { + return null; + } + Violation violation = new Violation(); + violation.addError("idAlt", 611); + return violation; + } + return null; + } + +}