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@2813: ingo@2813: import java.math.BigDecimal; ingo@2813: ingo@2817: import java.util.List; ingo@2817: ingo@2813: import org.apache.log4j.Logger; ingo@2813: ingo@2817: import org.hibernate.Session; ingo@2817: import org.hibernate.Query; ingo@2817: teichmann@5829: import org.dive4elements.river.model.SedimentDensity; teichmann@5829: import org.dive4elements.river.model.SedimentDensityValue; ingo@2813: ingo@2813: ingo@2813: public class ImportSedimentDensityValue { ingo@2813: ingo@2813: private static final Logger log = ingo@2813: Logger.getLogger(ImportSedimentDensityValue.class); ingo@2813: ingo@2813: ingo@2813: protected SedimentDensityValue peer; ingo@2813: ingo@2816: protected BigDecimal station; ingo@2813: tom@5507: protected BigDecimal shoreOffset; tom@5507: ingo@2813: protected BigDecimal density; ingo@2813: rrenkert@4524: private BigDecimal year; rrenkert@4524: ingo@2813: protected String description; ingo@2813: ingo@2813: ingo@2816: public ImportSedimentDensityValue( ingo@2816: BigDecimal station, tom@5507: BigDecimal shoreOffset, ingo@2816: BigDecimal density, rrenkert@4524: BigDecimal year, ingo@2816: String description ingo@2816: ) { ingo@2816: this.station = station; tom@5507: this.shoreOffset = shoreOffset; ingo@2816: this.density = density; rrenkert@4524: this.year = year; ingo@2816: this.description = description; ingo@2816: } ingo@2816: ingo@2816: ingo@2813: public void storeDependencies(SedimentDensity sedimentDensity) { ingo@2813: log.info("store dependencies"); ingo@2817: ingo@2817: getPeer(sedimentDensity); ingo@2813: } ingo@2813: ingo@2813: ingo@2813: public SedimentDensityValue getPeer(SedimentDensity sedimentDensity) { ingo@2813: log.info("get peer"); ingo@2813: ingo@2817: if (peer == null) { tom@8856: Session session = ImporterSession.getInstance() tom@8856: .getDatabaseSession(); ingo@2817: ingo@2817: Query query = session.createQuery( ingo@2817: "from SedimentDensityValue where " + ingo@2817: " sedimentDensity=:sedimentDensity and " + ingo@2817: " station=:station and " + tom@5507: " shoreOffset=:shoreOffset and " + ingo@2817: " density=:density and " + rrenkert@4524: " year=:year and " + ingo@2817: " description=:description"); ingo@2817: ingo@2817: query.setParameter("sedimentDensity", sedimentDensity); ingo@2817: query.setParameter("station", station); tom@5507: query.setParameter("shoreOffset", shoreOffset); ingo@2817: query.setParameter("density", density); rrenkert@4524: query.setParameter("year", year); ingo@2817: query.setParameter("description", description); ingo@2817: ingo@2817: List values = query.list(); ingo@2817: if (values.isEmpty()) { ingo@2817: log.debug("Create new SedimentDensityValue DB instance."); ingo@2817: ingo@2817: peer = new SedimentDensityValue( ingo@2817: sedimentDensity, ingo@2817: station, tom@5507: shoreOffset, ingo@2817: density, rrenkert@4524: year, ingo@2817: description); ingo@2817: ingo@2817: session.save(peer); ingo@2817: } ingo@2817: else { ingo@2817: peer = values.get(0); ingo@2817: } ingo@2817: } ingo@2817: ingo@2817: return peer; ingo@2813: } ingo@2813: } ingo@2813: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :