raimund@711: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@711: * Software engineering by Intevation GmbH
raimund@711: *
raimund@711: * This file is Free Software under the GNU GPL (v>=3)
raimund@711: * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@711: * the documentation coming with IMIS-Labordaten-Application for details.
raimund@711: */
raimund@538: package de.intevation.lada.validation.rules.probe;
raimund@538:
raimund@538: import java.util.List;
raimund@538:
raimund@538: import javax.inject.Inject;
raimund@538:
raimund@538: import de.intevation.lada.model.land.LProbe;
raimund@538: import de.intevation.lada.util.annotation.RepositoryConfig;
raimund@538: import de.intevation.lada.util.data.QueryBuilder;
raimund@538: import de.intevation.lada.util.data.Repository;
raimund@538: import de.intevation.lada.util.data.RepositoryType;
raimund@538: import de.intevation.lada.util.rest.Response;
raimund@538: import de.intevation.lada.validation.Violation;
raimund@538: import de.intevation.lada.validation.annotation.ValidationRule;
raimund@538: import de.intevation.lada.validation.rules.Rule;
raimund@538:
raimund@711: /**
raimund@711: * Validation rule for probe.
raimund@711: * Validates if the probe has a unique "hauptprobennr".
raimund@711: *
raimund@711: * @author Raimund Renkert
raimund@711: */
raimund@538: @ValidationRule("Probe")
raimund@538: public class UniqueHauptprobenNr implements Rule {
raimund@538:
raimund@538: @Inject
raimund@538: @RepositoryConfig(type=RepositoryType.RO)
raimund@538: private Repository repo;
raimund@538:
raimund@538: @SuppressWarnings("unchecked")
raimund@538: @Override
raimund@538: public Violation execute(Object object) {
raimund@538: LProbe probe = (LProbe)object;
raimund@538: QueryBuilder builder = new QueryBuilder(
raimund@538: repo.entityManager("land"),
raimund@538: LProbe.class);
raimund@538: builder.and("hauptprobenNr", probe.getHauptprobenNr());
raimund@538: Response response = repo.filter(builder.getQuery(), "land");
raimund@538: if (!((List)response.getData()).isEmpty()) {
raimund@538: LProbe found = ((List)response.getData()).get(0);
raimund@538: // The probe found in the db equals the new probe. (Update)
raimund@591: if (probe.getId() != null && probe.getId().equals(found.getId())) {
raimund@538: return null;
raimund@538: }
raimund@538: Violation violation = new Violation();
raimund@538: violation.addError("hauptprobenNr", 611);
raimund@538: return violation;
raimund@538: }
raimund@538: return null;
raimund@538: }
raimund@538:
raimund@538: }