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@647: package de.intevation.lada.validation.rules.messung;
raimund@647:
raimund@1336: import java.util.ArrayList;
raimund@647: import java.util.List;
raimund@647:
raimund@647: import javax.inject.Inject;
raimund@647:
tom@1097: import de.intevation.lada.model.land.Messung;
tom@1097: import de.intevation.lada.model.land.Messwert;
raimund@1336: import de.intevation.lada.model.land.Probe;
raimund@1336: import de.intevation.lada.model.stammdaten.Messgroesse;
tom@1097: import de.intevation.lada.model.stammdaten.PflichtMessgroesse;
raimund@647: import de.intevation.lada.util.annotation.RepositoryConfig;
raimund@647: import de.intevation.lada.util.data.QueryBuilder;
raimund@647: import de.intevation.lada.util.data.Repository;
raimund@647: import de.intevation.lada.util.data.RepositoryType;
raimund@647: import de.intevation.lada.util.rest.Response;
raimund@647: import de.intevation.lada.validation.Violation;
raimund@647: import de.intevation.lada.validation.annotation.ValidationRule;
raimund@647: import de.intevation.lada.validation.rules.Rule;
raimund@647:
raimund@711: /**
raimund@711: * Validation rule for messungen.
raimund@711: * Validates if the messung has all "pflichtmessgroessen".
raimund@711: *
raimund@711: * @author Raimund Renkert
raimund@711: */
raimund@647: @ValidationRule("Messung")
raimund@647: public class HasPflichtmessgroessen implements Rule {
raimund@647:
raimund@647: @Inject
raimund@647: @RepositoryConfig(type=RepositoryType.RO)
raimund@647: private Repository repository;
raimund@647:
raimund@647: @Override
raimund@647: public Violation execute(Object object) {
tom@1097: Messung messung = (Messung)object;
raimund@1336: Probe probe = repository.getByIdPlain(Probe.class, messung.getProbeId(), "land");
raimund@1336:
raimund@647: QueryBuilder builder =
raimund@647: new QueryBuilder(
raimund@647: repository.entityManager("stamm"),
raimund@647: PflichtMessgroesse.class);
tom@1097: builder.and("messMethodeId", messung.getMmtId());
raimund@1336: builder.and("umwId", probe.getUmwId());
raimund@647: Response response = repository.filter(builder.getQuery(), "stamm");
raimund@685: @SuppressWarnings("unchecked")
raimund@647: List pflicht =
raimund@647: (List)response.getData();
raimund@647:
tom@1097: QueryBuilder wertBuilder =
tom@1097: new QueryBuilder(
tom@1097: repository.entityManager("land"), Messwert.class);
raimund@677: wertBuilder.and("messungsId", messung.getId());
raimund@647: Response wertResponse =
raimund@647: repository.filter(wertBuilder.getQuery(), "land");
raimund@685: @SuppressWarnings("unchecked")
tom@1097: List messwerte = (List)wertResponse.getData();
raimund@647: Violation violation = new Violation();
raimund@1336: List tmp = new ArrayList();
raimund@1336: for (Messwert wert : messwerte) {
raimund@1336: for (PflichtMessgroesse p : pflicht) {
raimund@1336: if (p.getMessgroesseId().equals(wert.getMessgroesseId())) {
raimund@1336: tmp.add(p);
raimund@647: }
raimund@647: }
raimund@682: }
raimund@1336: pflicht.removeAll(tmp);
raimund@1336: if (!pflicht.isEmpty()) {
raimund@1336: for (PflichtMessgroesse p : pflicht) {
raimund@1336: Messgroesse mg = repository.getByIdPlain(Messgroesse.class, p.getMessgroesseId(), "stamm");
raimund@1336: violation.addWarning("pflichtmessgroesse#" + mg.getMessgroesse(), 631);
raimund@1336: }
raimund@647: }
raimund@647: return violation.hasWarnings() ? violation : null;
raimund@647: }
raimund@647: }