teichmann@5844: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5844: * Software engineering by Intevation GmbH teichmann@5844: * teichmann@5992: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5844: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5992: * documentation coming with Dive4Elements River for details. teichmann@5844: */ teichmann@5844: teichmann@5829: package org.dive4elements.river.importer; ingo@2827: ingo@2827: import java.math.BigDecimal; ingo@2827: import java.util.List; ingo@2827: mschaefer@8986: import org.dive4elements.river.importer.common.StoreMode; teichmann@5829: import org.dive4elements.river.model.FlowVelocityModel; teichmann@5829: import org.dive4elements.river.model.FlowVelocityModelValue; mschaefer@8986: import org.hibernate.Query; mschaefer@8986: import org.hibernate.Session; ingo@2827: ingo@2827: ingo@2827: public class ImportFlowVelocityModelValue { ingo@2827: mschaefer@8986: private final BigDecimal station; mschaefer@8986: private final BigDecimal q; mschaefer@8986: private final BigDecimal totalChannel; mschaefer@8986: private final BigDecimal mainChannel; mschaefer@8986: private final BigDecimal shearStress; ingo@2827: ingo@2827: private FlowVelocityModelValue peer; ingo@2827: ingo@2827: ingo@2827: public ImportFlowVelocityModelValue( mschaefer@8986: final BigDecimal station, mschaefer@8986: final BigDecimal q, mschaefer@8986: final BigDecimal totalChannel, mschaefer@8986: final BigDecimal mainChannel, mschaefer@8986: final BigDecimal shearStress mschaefer@8986: ) { ingo@2827: this.station = station; ingo@2828: this.q = q; ingo@2827: this.totalChannel = totalChannel; ingo@2827: this.mainChannel = mainChannel; ingo@2827: this.shearStress = shearStress; ingo@2827: } ingo@2827: ingo@2827: mschaefer@8986: public void storeDependencies(final FlowVelocityModel model, final StoreMode parentStoreMode) { mschaefer@8986: getPeer(model, parentStoreMode); ingo@2827: } ingo@2827: ingo@2827: mschaefer@8986: public FlowVelocityModelValue getPeer(final FlowVelocityModel model, final StoreMode parentStoreMode) { mschaefer@8986: if (this.peer == null) { mschaefer@8986: List values; mschaefer@8986: final Session session = ImporterSession.getInstance().getDatabaseSession(); mschaefer@8986: if (parentStoreMode == StoreMode.INSERT) mschaefer@8986: values = null; mschaefer@8986: else { mschaefer@8986: final Query query = session.createQuery( mschaefer@8986: "from FlowVelocityModelValue where " + mschaefer@8986: " flowVelocity=:model and " + mschaefer@8986: " station between :station - 0.00001 and :station + 0.00001" mschaefer@8986: ); ingo@2827: mschaefer@8986: query.setParameter("model", model); mschaefer@8986: query.setParameter("station", this.station.doubleValue()); ingo@2827: mschaefer@8986: values = query.list(); mschaefer@8986: } mschaefer@8986: if ((values == null) || values.isEmpty()) { mschaefer@8986: this.peer = new FlowVelocityModelValue( mschaefer@8986: model, this.station, this.q, this.totalChannel, this.mainChannel, this.shearStress); ingo@2827: mschaefer@8986: session.save(this.peer); ingo@2827: } ingo@2827: else { mschaefer@8986: this.peer = values.get(0); ingo@2827: } ingo@2827: } ingo@2827: mschaefer@8986: return this.peer; ingo@2827: } ingo@2827: } ingo@2827: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :