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@677: import java.util.ArrayList;
raimund@647: import java.util.List;
raimund@647:
raimund@647: import javax.inject.Inject;
raimund@647:
raimund@677: import org.apache.log4j.Logger;
raimund@677:
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.stammdaten.Messgroesse;
tom@1097: import de.intevation.lada.model.stammdaten.MmtMessgroesse;
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 "messgroesse" fits the "messmethode".
raimund@711: *
raimund@711: * @author Raimund Renkert
raimund@711: */
raimund@647: @ValidationRule("Messung")
raimund@647: public class MessgroesseToMessmethode implements Rule {
raimund@647:
raimund@647: @Inject
raimund@677: private Logger logger;
raimund@677:
raimund@677: @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: String mmt = messung.getMmtId();
tom@1097: QueryBuilder builder =
tom@1097: new QueryBuilder(
tom@1097: repository.entityManager("land"), Messwert.class);
raimund@677: builder.and("messungsId", messung.getId());
raimund@647: Response response = repository.filter(builder.getQuery(), "land");
raimund@685: @SuppressWarnings("unchecked")
tom@1097: List messwerte = (List)response.getData();
raimund@647:
raimund@647: QueryBuilder mmtBuilder =
raimund@647: new QueryBuilder(
raimund@647: repository.entityManager("stamm"), MmtMessgroesse.class);
raimund@647:
raimund@647: Response results =
raimund@647: repository.filter(mmtBuilder.getQuery(), "stamm");
raimund@685: @SuppressWarnings("unchecked")
raimund@647: List messgroessen =
raimund@647: (List)results.getData();
raimund@677: List found = new ArrayList();
raimund@677: for (MmtMessgroesse mg: messgroessen) {
tom@1097: if (mg.getMmtId().equals(mmt)) {
raimund@677: found.add(mg);
raimund@677: }
raimund@677: }
raimund@647: Violation violation = new Violation();
tom@1097: for(Messwert messwert: messwerte) {
raimund@647: boolean hit = false;
raimund@677: for (MmtMessgroesse messgroesse: found) {
raimund@647: if (messwert.getMessgroesseId().equals(
raimund@1336: messgroesse.getMessgroesseId())) {
raimund@647: hit = true;
raimund@647: }
raimund@647: }
raimund@647: if (!hit) {
raimund@1336: Messgroesse mg = repository.getByIdPlain(
raimund@1336: Messgroesse.class,
raimund@1336: messwert.getMessgroesseId(),
raimund@1336: "stamm");
raimund@1336: violation.addWarning("messgroesse#" + mg.getMessgroesse(), 632);
raimund@647: }
raimund@647: }
raimund@647: return violation.hasWarnings() ? violation : null;
raimund@647: }
raimund@647: }