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@2808: ingo@2809: import java.util.List; ingo@2809: ingo@2808: import java.math.BigDecimal; ingo@2808: ingo@2809: import org.apache.log4j.Logger; ingo@2809: ingo@2809: import org.hibernate.Session; ingo@2809: import org.hibernate.Query; ingo@2809: teichmann@5829: import org.dive4elements.river.model.BedHeightSingle; teichmann@5829: import org.dive4elements.river.model.BedHeightSingleValue; ingo@2809: ingo@2808: ingo@2811: public class ImportBedHeightSingleValue implements ImportBedHeightValue { ingo@2808: ingo@2809: private static final Logger log = ingo@2809: Logger.getLogger(ImportBedHeightSingleValue.class); ingo@2809: ingo@2809: ingo@2808: protected ImportBedHeightSingle bedHeight; ingo@2808: tom@6201: protected BigDecimal station; tom@6201: protected BigDecimal height; tom@6201: protected BigDecimal uncertainty; tom@6201: protected BigDecimal dataGap; ingo@2808: protected BigDecimal soundingWidth; tom@6201: protected BigDecimal width; ingo@2808: ingo@2809: protected BedHeightSingleValue peer; ingo@2809: ingo@2808: ingo@2808: public ImportBedHeightSingleValue( ingo@2808: ImportBedHeightSingle bedHeight, tom@6201: BigDecimal station, tom@6201: BigDecimal height, tom@6201: BigDecimal uncertainty, tom@6201: BigDecimal dataGap, ingo@2808: BigDecimal soundingWidth, tom@6201: BigDecimal width ingo@2808: ) { ingo@2808: this.bedHeight = bedHeight; ingo@2808: this.station = station; ingo@2808: this.height = height; ingo@2808: this.uncertainty = uncertainty; ingo@2808: this.dataGap = dataGap; ingo@2808: this.soundingWidth = soundingWidth; ingo@2808: this.width = width; ingo@2808: } ingo@2809: ingo@2809: ingo@2809: public void storeDependencies(BedHeightSingle bedHeight) { ingo@2809: getPeer(bedHeight); ingo@2809: } ingo@2809: ingo@2809: felix@3954: /** felix@3954: * Add this value to database or return database bound Value, assuring felix@3954: * that the BedHeightSingle exists in db already. felix@3954: */ ingo@2809: public BedHeightSingleValue getPeer(BedHeightSingle bedHeight) { ingo@2809: if (peer == null) { ingo@2809: Session session = ImporterSession.getInstance().getDatabaseSession(); ingo@2809: ingo@2809: Query query = session.createQuery( ingo@2809: "from BedHeightSingleValue where " + ingo@2809: " bedHeight=:bedHeight and " + ingo@2809: " station=:station and " + ingo@2809: " height=:height and " + ingo@2809: " uncertainty=:uncertainty and " + ingo@2809: " dataGap=:dataGap and " + ingo@2809: " soundingWidth=:soundingWidth and " + ingo@2809: " width=:width"); ingo@2809: ingo@2809: query.setParameter("bedHeight", bedHeight); ingo@2809: query.setParameter("station", station); ingo@2809: query.setParameter("height", height); ingo@2809: query.setParameter("uncertainty", uncertainty); ingo@2809: query.setParameter("dataGap", dataGap); ingo@2809: query.setParameter("soundingWidth", soundingWidth); ingo@2809: query.setParameter("width", width); ingo@2809: ingo@2809: List values = query.list(); ingo@2809: if (values.isEmpty()) { ingo@2809: peer = new BedHeightSingleValue( ingo@2809: bedHeight, ingo@2809: station, ingo@2809: height, ingo@2809: uncertainty, ingo@2809: dataGap, ingo@2809: soundingWidth, ingo@2809: width ingo@2809: ); ingo@2809: session.save(peer); ingo@2809: } ingo@2809: else { ingo@2809: peer = values.get(0); ingo@2809: } ingo@2809: } ingo@2809: ingo@2809: return peer; ingo@2809: } ingo@2808: } ingo@2808: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :