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.util.ArrayList; ingo@2813: import java.util.List; ingo@2813: ingo@2813: import org.apache.log4j.Logger; ingo@3943: import org.hibernate.Query; ingo@2817: import org.hibernate.Session; ingo@2817: teichmann@5829: import org.dive4elements.river.model.River; teichmann@5829: import org.dive4elements.river.model.SedimentDensity; ingo@2813: ingo@2813: ingo@2813: public class ImportSedimentDensity { ingo@2813: ingo@2813: private static Logger log = Logger.getLogger(ImportSedimentDensity.class); ingo@2813: ingo@2813: protected SedimentDensity peer; ingo@2813: ingo@2813: protected ImportDepth depth; ingo@2813: ingo@2817: protected String description; ingo@2817: ingo@2813: protected List values; ingo@2813: ingo@2817: public ImportSedimentDensity(String description) { ingo@2817: this.description = description; ingo@3943: this.values = new ArrayList(); ingo@2817: } ingo@2817: ingo@2817: public String getDescription() { ingo@2817: return description; ingo@2813: } ingo@2813: ingo@2813: public void setDepth(ImportDepth depth) { ingo@2813: this.depth = depth; ingo@2813: } ingo@2813: ingo@2813: public void addValue(ImportSedimentDensityValue value) { ingo@2813: values.add(value); ingo@2813: } ingo@2813: tom@5416: public void storeDependencies(River river) { ingo@2813: log.info("store dependencies"); ingo@2817: ingo@2817: if (depth != null) { ingo@2817: depth.storeDependencies(); ingo@2817: } ingo@2817: ingo@2817: SedimentDensity peer = getPeer(river); ingo@2817: ingo@3943: if (peer != null) { ingo@3943: log.info("store sediment density values."); ingo@3943: for (ImportSedimentDensityValue value : values) { ingo@3943: value.storeDependencies(peer); ingo@3943: } ingo@2817: } ingo@2813: } ingo@2813: ingo@2813: public SedimentDensity getPeer(River river) { ingo@2813: log.info("get peer"); sascha@3942: ingo@3940: if (depth == null) { ingo@3943: log.warn("cannot store sediment density '" + description ingo@3943: + "': no depth"); ingo@3940: return null; ingo@3940: } sascha@3942: ingo@2817: if (peer == null) { ingo@3943: Session session = ImporterSession.getInstance() ingo@3943: .getDatabaseSession(); ingo@2817: ingo@3943: Query query = session.createQuery("from SedimentDensity where " tom@5441: + " river=:river and " + " depth=:depth"); ingo@2817: ingo@2817: query.setParameter("river", river); ingo@2817: query.setParameter("depth", depth.getPeer()); ingo@2817: ingo@2817: List density = query.list(); ingo@2817: ingo@2817: if (density.isEmpty()) { ingo@2817: log.debug("Create new SedimentDensity DB instance."); ingo@2817: ingo@3943: peer = new SedimentDensity(river, depth.getPeer(), tom@5441: description); ingo@2817: ingo@2817: session.save(peer); ingo@2817: } ingo@2817: else { ingo@2817: peer = density.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 :