teichmann@8031: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@8031: * Software engineering by Intevation GmbH teichmann@8031: * teichmann@8031: * This file is Free Software under the GNU AGPL (>=v3) teichmann@8031: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@8031: * documentation coming with Dive4Elements River for details. teichmann@8031: */ teichmann@8031: teichmann@8031: package org.dive4elements.river.importer; teichmann@8031: teichmann@8031: import java.util.ArrayList; teichmann@8031: import java.util.List; teichmann@8031: teichmann@8031: import org.apache.log4j.Logger; teichmann@8031: teichmann@8031: import org.hibernate.Session; teichmann@8031: import org.hibernate.Query; teichmann@8031: teichmann@8031: import org.dive4elements.river.model.GrainFraction; teichmann@8031: import org.dive4elements.river.model.River; teichmann@8031: import org.dive4elements.river.model.SedimentLoadLS; teichmann@8031: import org.dive4elements.river.model.TimeInterval; teichmann@8031: import org.dive4elements.river.model.Unit; teichmann@8031: teichmann@8031: teichmann@8031: public class ImportSedimentLoadLS { teichmann@8031: teichmann@8031: private static Logger log = Logger.getLogger(ImportSedimentLoadLS.class); teichmann@8031: teichmann@8031: private ImportGrainFraction grainFraction; teichmann@8031: teichmann@8031: private ImportUnit unit; teichmann@8031: teichmann@8031: private ImportTimeInterval timeInterval; teichmann@8031: teichmann@8031: private String description; teichmann@8031: teichmann@8031: private Integer kind; teichmann@8031: teichmann@8031: private List values; teichmann@8031: teichmann@8031: private SedimentLoadLS peer; teichmann@8031: teichmann@8031: public ImportSedimentLoadLS(String description) { teichmann@8031: this.values = new ArrayList(); teichmann@8031: this.description = description; teichmann@8031: } teichmann@8031: teichmann@8031: public void setTimeInterval(ImportTimeInterval timeInterval) { teichmann@8031: this.timeInterval = timeInterval; teichmann@8031: } teichmann@8031: teichmann@8031: public void setUnit(ImportUnit unit) { teichmann@8031: this.unit = unit; teichmann@8031: } teichmann@8031: teichmann@8031: public void setGrainFraction(ImportGrainFraction grainFraction) { teichmann@8031: this.grainFraction = grainFraction; teichmann@8031: } teichmann@8031: teichmann@8031: public void setKind(Integer kind) { teichmann@8031: this.kind = kind; teichmann@8031: } teichmann@8031: teichmann@8031: public void addValue(ImportSedimentLoadLSValue value) { teichmann@8031: this.values.add(value); teichmann@8031: } teichmann@8031: teichmann@8031: public void storeDependencies(River river) { teichmann@8031: log.debug("store dependencies"); teichmann@8031: teichmann@8031: if (grainFraction != null) { teichmann@8031: grainFraction.storeDependencies(); teichmann@8031: } teichmann@8031: teichmann@8031: SedimentLoadLS peer = getPeer(river); teichmann@8031: teichmann@8031: if (peer != null) { teichmann@8031: int i = 0; teichmann@8031: teichmann@8031: for (ImportSedimentLoadLSValue value : values) { teichmann@8031: value.storeDependencies(peer); teichmann@8031: i++; teichmann@8031: } teichmann@8031: tom@8032: log.info("stored " + i + " sediment load values."); teichmann@8031: } teichmann@8031: } teichmann@8031: teichmann@8031: public SedimentLoadLS getPeer(River river) { teichmann@8031: log.debug("get peer"); teichmann@8031: teichmann@8031: GrainFraction gf = grainFraction != null ? grainFraction.getPeer() teichmann@8031: : null; teichmann@8031: teichmann@8031: Unit u = unit != null ? unit.getPeer() : null; teichmann@8031: teichmann@8031: TimeInterval ti = timeInterval != null ? timeInterval.getPeer() : null; teichmann@8031: teichmann@8031: if (ti == null || u == null) { tom@8032: log.warn("Skip invalid SedimentLoadLS: time interval or unit null!"); teichmann@8031: return null; teichmann@8031: } teichmann@8031: teichmann@8031: if (peer == null) { teichmann@8031: Session session = ImporterSession.getInstance() teichmann@8031: .getDatabaseSession(); tom@8032: Query query = session.createQuery("from SedimentLoadLS where " teichmann@8031: + " river=:river and " teichmann@8031: + " grainFraction=:grainFraction and " + " unit=:unit and " teichmann@8031: + " timeInterval=:timeInterval and " teichmann@8031: + " description=:description"); teichmann@8031: teichmann@8031: query.setParameter("river", river); teichmann@8031: query.setParameter("grainFraction", gf); teichmann@8031: query.setParameter("unit", u); teichmann@8031: query.setParameter("timeInterval", ti); teichmann@8031: query.setParameter("description", description); teichmann@8031: tom@8032: List loads = query.list(); tom@8032: if (loads.isEmpty()) { tom@8032: log.debug("create new SedimentLoadLS"); teichmann@8031: teichmann@8031: peer = new SedimentLoadLS(river, u, ti, gf, description); teichmann@8031: peer.setKind(this.kind); teichmann@8031: session.save(peer); teichmann@8031: } teichmann@8031: else { tom@8032: peer = loads.get(0); teichmann@8031: } teichmann@8031: } teichmann@8031: teichmann@8031: return peer; teichmann@8031: } teichmann@8031: } teichmann@8031: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :