tom@8559: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde tom@8559: * Software engineering by Intevation GmbH tom@8559: * tom@8559: * This file is Free Software under the GNU AGPL (>=v3) tom@8559: * and comes with ABSOLUTELY NO WARRANTY! Check out the tom@8559: * documentation coming with Dive4Elements River for details. tom@8559: */ tom@8559: tom@8559: package org.dive4elements.river.importer; tom@8559: tom@8559: import java.util.List; tom@8559: tom@8559: import org.apache.log4j.Logger; mschaefer@8986: import org.dive4elements.river.importer.common.StoreMode; tom@8559: import org.dive4elements.river.model.BedHeight; tom@8559: import org.dive4elements.river.model.BedHeightValue; mschaefer@8975: import org.hibernate.Query; mschaefer@8975: import org.hibernate.Session; tom@8559: tom@8559: tom@8559: public class ImportBedHeightValue { tom@8559: mschaefer@8975: private static final Logger log = Logger.getLogger(ImportBedHeightValue.class); tom@8559: tom@8559: tom@8559: protected ImportBedHeight bedHeight; tom@8559: tom@8559: protected Double station; tom@8559: protected Double height; tom@8559: protected Double uncertainty; tom@8559: protected Double dataGap; tom@8559: protected Double soundingWidth; mschaefer@8975: protected Double minHeight; mschaefer@8975: protected Double maxHeight; mschaefer@9034: protected Double[] sectionHeight; tom@8559: tom@8559: protected BedHeightValue peer; tom@8559: tom@8559: mschaefer@8975: public ImportBedHeightValue(final ImportBedHeight bedHeight, final Double station, final Double height, final Double uncertainty, final Double dataGap, mschaefer@8975: final Double soundingWidth, final Double minHeight, final Double maxHeight) { tom@8559: this.bedHeight = bedHeight; tom@8559: this.station = station; tom@8559: this.height = height; tom@8559: this.uncertainty = uncertainty; tom@8559: this.dataGap = dataGap; tom@8559: this.soundingWidth = soundingWidth; mschaefer@8975: this.minHeight = minHeight; mschaefer@8975: this.maxHeight = maxHeight; mschaefer@9034: this.sectionHeight = new Double[10]; tom@8559: } tom@8559: tom@8559: mschaefer@9034: public void setSectionHeight(final int index, final Double value) { mschaefer@9034: this.sectionHeight[index - 1] = value; mschaefer@9034: } mschaefer@9034: mschaefer@8975: public void storeDependencies(final BedHeight bedHeight) { tom@8559: getPeer(bedHeight); tom@8559: } tom@8559: tom@8559: tom@8559: /** tom@8559: * Add this value to database or return database bound Value, assuring tom@8559: * that the BedHeight exists in db already. tom@8559: */ mschaefer@8975: public BedHeightValue getPeer(final BedHeight bedHeight) { mschaefer@8975: if (this.peer != null) mschaefer@8975: return this.peer; tom@8559: mschaefer@8986: List values; mschaefer@8975: final Session session = ImporterSession.getInstance().getDatabaseSession(); mschaefer@8986: if (this.bedHeight.storeMode == StoreMode.INSERT) mschaefer@8986: values = null; mschaefer@8986: else { mschaefer@8986: final Query query = session.createQuery("FROM BedHeightValue WHERE (bedHeight=:bedHeight)" mschaefer@8986: + " AND (station BETWEEN (:station-0.0001) AND (:station+0.0001))"); mschaefer@8986: query.setParameter("bedHeight", bedHeight); mschaefer@8986: query.setParameter("station", this.station); mschaefer@8986: values = query.list(); mschaefer@8986: } mschaefer@8986: if ((values == null) || values.isEmpty()) { mschaefer@8975: this.peer = new BedHeightValue(bedHeight, this.station, this.height, this.uncertainty, this.dataGap, this.soundingWidth, mschaefer@8975: this.minHeight, this.maxHeight); mschaefer@9034: for (int i = 1; i <= 10; i++) mschaefer@9034: this.peer.setSectionHeight(i, this.sectionHeight[i - 1]); mschaefer@8975: session.save(this.peer); mschaefer@8975: } else { mschaefer@8975: this.peer = values.get(0); tom@8559: } mschaefer@8975: return this.peer; tom@8559: } tom@8559: } tom@8559: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :