tom@8841: /* Copyright (C) 2014 by Bundesanstalt für Gewässerkunde tom@8841: * Software engineering by Intevation GmbH tom@8841: * tom@8841: * This file is Free Software under the GNU AGPL (>=v3) tom@8841: * and comes with ABSOLUTELY NO WARRANTY! Check out the tom@8841: * documentation coming with Dive4Elements River for details. tom@8841: */ tom@8841: teichmann@8028: package org.dive4elements.river.importer; teichmann@8028: teichmann@8028: import java.util.List; teichmann@8028: mschaefer@8986: import org.dive4elements.river.importer.common.StoreMode; teichmann@8028: import org.dive4elements.river.model.MeasurementStation; teichmann@8028: import org.dive4elements.river.model.SedimentLoad; teichmann@8028: import org.dive4elements.river.model.SedimentLoadValue; teichmann@8028: import org.hibernate.Query; teichmann@8028: import org.hibernate.Session; teichmann@8028: teichmann@8028: public class ImportSedimentLoadValue { teichmann@8028: teichmann@8028: private SedimentLoadValue peer; teichmann@8028: tom@8056: private MeasurementStation station; tom@8056: private Double value; teichmann@8028: teichmann@8028: public ImportSedimentLoadValue() { teichmann@8028: } teichmann@8028: teichmann@8028: public ImportSedimentLoadValue( mschaefer@8986: final MeasurementStation station, mschaefer@8986: final Double value mschaefer@8986: ) { teichmann@8028: this.station = station; teichmann@8028: this.value = value; teichmann@8028: } teichmann@8028: mschaefer@8986: protected SedimentLoadValue getPeer(final SedimentLoad sedimentLoad, final StoreMode parentStoreMode) { teichmann@8028: mschaefer@8986: if (this.peer == null) { mschaefer@8986: final Session session = ImporterSession.getInstance() mschaefer@8986: .getDatabaseSession(); mschaefer@8986: List values; mschaefer@8986: if (parentStoreMode == StoreMode.INSERT) mschaefer@8986: values = null; mschaefer@8986: else { mschaefer@8986: final Query query = session.createQuery( mschaefer@8986: "from SedimentLoadValue where " + mschaefer@8986: " measurementStation = :station and " + mschaefer@8986: " sedimentLoad = :sedimentLoad and " + mschaefer@8986: " value = :value"); teichmann@8028: mschaefer@8986: query.setParameter("station", this.station); mschaefer@8986: query.setParameter("sedimentLoad", sedimentLoad); mschaefer@8986: query.setParameter("value", this.value); teichmann@8028: mschaefer@8986: values = query.list(); mschaefer@8986: } mschaefer@8986: if ((values == null) || values.isEmpty()) { mschaefer@8986: this.peer = new SedimentLoadValue(sedimentLoad, this.station, this.value); mschaefer@8986: session.save(this.peer); teichmann@8028: } teichmann@8028: else { mschaefer@8986: this.peer = values.get(0); teichmann@8028: } teichmann@8028: } teichmann@8028: mschaefer@8986: return this.peer; teichmann@8028: } teichmann@8028: mschaefer@8986: public void storeDependencies(final SedimentLoad sedimentLoad, final StoreMode parentStoreMode) { mschaefer@8986: getPeer(sedimentLoad, parentStoreMode); teichmann@8028: } teichmann@8028: } teichmann@8028: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :