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@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;
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@647: QueryBuilder builder =
raimund@647: new QueryBuilder(
raimund@647: repository.entityManager("stamm"),
raimund@647: PflichtMessgroesse.class);
tom@1097: builder.and("messMethodeId", messung.getMmtId());
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@682: boolean missing = false;
raimund@647: for (PflichtMessgroesse p : pflicht) {
tom@1097: for (Messwert wert : messwerte) {
raimund@682: if (!p.getMessgroesseId().equals(wert.getMessgroesseId())) {
raimund@682: missing = true;
raimund@647: }
raimund@647: }
raimund@682: }
raimund@682: if (missing) {
raimund@682: violation.addWarning("pflichtmessgroesse", 631);
raimund@647: }
raimund@647: return violation.hasWarnings() ? violation : null;
raimund@647: }
raimund@647: }