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; tom@8559: tom@8559: import org.hibernate.Session; tom@8559: import org.hibernate.Query; tom@8559: tom@8559: import org.dive4elements.river.model.BedHeight; tom@8559: import org.dive4elements.river.model.BedHeightValue; tom@8559: tom@8559: tom@8559: public class ImportBedHeightValue { tom@8559: tom@8559: private static final Logger log = tom@8559: 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; tom@8559: tom@8559: protected BedHeightValue peer; tom@8559: tom@8559: tom@8559: public ImportBedHeightValue( tom@8559: ImportBedHeight bedHeight, tom@8559: Double station, tom@8559: Double height, tom@8559: Double uncertainty, tom@8559: Double dataGap, tom@8559: Double soundingWidth tom@8559: ) { 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; tom@8559: } tom@8559: tom@8559: tom@8559: public void storeDependencies(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: */ tom@8559: public BedHeightValue getPeer(BedHeight bedHeight) { tom@8559: if (peer == null) { tom@8559: Session session = ImporterSession.getInstance().getDatabaseSession(); tom@8559: tom@8559: Query query = session.createQuery( tom@8559: "from BedHeightValue where " + tom@8559: " bedHeight=:bedHeight and " + tom@8559: " station=:station and " + tom@8559: " height=:height and " + tom@8559: " uncertainty=:uncertainty and " + tom@8559: " dataGap=:dataGap and " + tom@8559: " soundingWidth=:soundingWidth"); tom@8559: tom@8559: query.setParameter("bedHeight", bedHeight); tom@8559: query.setParameter("station", station); tom@8559: query.setParameter("height", height); tom@8559: query.setParameter("uncertainty", uncertainty); tom@8559: query.setParameter("dataGap", dataGap); tom@8559: query.setParameter("soundingWidth", soundingWidth); tom@8559: tom@8559: List values = query.list(); tom@8559: if (values.isEmpty()) { tom@8559: peer = new BedHeightValue( tom@8559: bedHeight, tom@8559: station, tom@8559: height, tom@8559: uncertainty, tom@8559: dataGap, tom@8559: soundingWidth tom@8559: ); tom@8559: session.save(peer); tom@8559: } tom@8559: else { tom@8559: peer = values.get(0); tom@8559: } tom@8559: } tom@8559: tom@8559: return peer; tom@8559: } tom@8559: } tom@8559: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :