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@2810: ingo@2810: import java.util.List; ingo@2810: ingo@2810: import java.math.BigDecimal; ingo@2810: ingo@2810: import org.apache.log4j.Logger; ingo@2810: ingo@2810: import org.hibernate.Session; ingo@2810: import org.hibernate.Query; ingo@2810: teichmann@5829: import org.dive4elements.river.model.BedHeightEpoch; teichmann@5829: import org.dive4elements.river.model.BedHeightEpochValue; ingo@2810: ingo@2810: ingo@2811: public class ImportBedHeightEpochValue implements ImportBedHeightValue { ingo@2810: ingo@2810: private static final Logger log = ingo@2810: Logger.getLogger(ImportBedHeightEpochValue.class); ingo@2810: ingo@2810: ingo@2810: private BigDecimal station; ingo@2810: private BigDecimal height; ingo@2810: ingo@2810: private BedHeightEpochValue peer; ingo@2810: ingo@2810: ingo@2810: public ImportBedHeightEpochValue() { ingo@2810: } ingo@2810: ingo@2810: ingo@2810: public ImportBedHeightEpochValue(BigDecimal station, BigDecimal height) { ingo@2810: this.station = station; ingo@2810: this.height = height; ingo@2810: } ingo@2810: ingo@2810: ingo@2810: public void storeDependencies(BedHeightEpoch bedHeight) { ingo@2810: getPeer(bedHeight); ingo@2810: } ingo@2810: ingo@2810: ingo@2810: public BedHeightEpochValue getPeer(BedHeightEpoch bedHeight) { ingo@2810: if (peer == null) { ingo@2810: Session session = ImporterSession.getInstance().getDatabaseSession(); ingo@2810: ingo@2810: Query query = session.createQuery( ingo@2810: "from BedHeightEpochValue where " + ingo@2810: " bedHeight=:bedHeight and " + ingo@2810: " station=:station and " + ingo@2810: " height=:height"); ingo@2810: ingo@2810: query.setParameter("bedHeight", bedHeight); ingo@2810: query.setParameter("station", station); ingo@2810: query.setParameter("height", height); ingo@2810: ingo@2810: List values = query.list(); ingo@2810: ingo@2810: if (values.isEmpty()) { ingo@2810: peer = new BedHeightEpochValue( ingo@2810: bedHeight, ingo@2810: station, ingo@2810: height ingo@2810: ); ingo@2810: session.save(peer); ingo@2810: } ingo@2810: else { ingo@2810: peer = values.get(0); ingo@2810: } ingo@2810: } ingo@2810: ingo@2810: return peer; ingo@2810: } ingo@2810: } ingo@2810: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :