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@2819: teichmann@5829: import org.dive4elements.river.model.MorphologicalWidth; teichmann@5829: import org.dive4elements.river.model.River; teichmann@5446: ingo@2819: import java.util.ArrayList; ingo@2819: import java.util.List; ingo@2819: ingo@2819: import org.apache.log4j.Logger; teichmann@5446: ingo@3943: import org.hibernate.Query; ingo@2819: import org.hibernate.Session; ingo@2819: ingo@2819: ingo@2819: public class ImportMorphWidth { ingo@2819: ingo@2819: private static Logger log = Logger.getLogger(ImportMorphWidth.class); ingo@2819: ingo@2819: protected MorphologicalWidth peer; ingo@2819: ingo@2819: protected ImportUnit unit; ingo@2819: ingo@2819: protected List values; ingo@2819: ingo@2821: public ImportMorphWidth() { ingo@2819: this.values = new ArrayList(); ingo@2819: } ingo@2819: ingo@2819: public void addValue(ImportMorphWidthValue value) { ingo@2819: this.values.add(value); ingo@2819: } ingo@2819: ingo@2821: public void setUnit(ImportUnit unit) { ingo@2821: this.unit = unit; ingo@2821: } ingo@2821: tom@5416: public void storeDependencies(River river) { ingo@2819: log.info("store dependencies"); ingo@2819: ingo@2819: MorphologicalWidth peer = getPeer(river); ingo@2819: ingo@3943: if (peer != null) { ingo@3943: log.info("store morphological width values"); ingo@2819: ingo@3943: for (ImportMorphWidthValue value : values) { ingo@3943: value.storeDependencies(peer); ingo@3943: } ingo@2819: } ingo@2819: } ingo@2819: ingo@2819: public MorphologicalWidth getPeer(River river) { ingo@2819: log.info("get peer"); ingo@2819: ingo@2819: if (peer == null) { ingo@3943: Session session = ImporterSession.getInstance() ingo@3943: .getDatabaseSession(); ingo@2819: ingo@3943: Query query = session.createQuery("from MorphologicalWidth where " ingo@3943: + " river=:river and " + " unit=:unit"); ingo@2819: ingo@2819: query.setParameter("river", river); ingo@2819: query.setParameter("unit", unit.getPeer()); ingo@2819: ingo@2819: List widths = query.list(); ingo@2819: ingo@2819: if (widths.isEmpty()) { ingo@2819: log.debug("Create new MorphologicalWidth DB instance."); ingo@2819: ingo@2819: peer = new MorphologicalWidth(river, unit.getPeer()); ingo@2819: ingo@2819: session.save(peer); ingo@2819: } ingo@2819: else { ingo@2819: peer = widths.get(0); ingo@2819: } ingo@2819: } ingo@2819: ingo@2819: return peer; ingo@2819: } ingo@2819: } ingo@2819: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :