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; mschaefer@8986: import org.dive4elements.river.importer.common.StoreMode; 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; mschaefer@8986: import org.hibernate.Query; mschaefer@8986: import org.hibernate.Session; 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: tom@8059: private ImportTimeInterval sqTimeInterval; tom@8059: mschaefer@8986: private final String description; teichmann@8031: teichmann@8031: private Integer kind; teichmann@8031: mschaefer@8986: private final List values; mschaefer@8986: mschaefer@8986: protected StoreMode storeMode; teichmann@8031: teichmann@8031: private SedimentLoadLS peer; teichmann@8031: mschaefer@8986: public ImportSedimentLoadLS(final String description) { mschaefer@8986: this.values = new ArrayList<>(); teichmann@8031: this.description = description; mschaefer@8986: this.storeMode = StoreMode.NONE; teichmann@8031: } teichmann@8031: mschaefer@8986: public void setTimeInterval(final ImportTimeInterval timeInterval) { teichmann@8031: this.timeInterval = timeInterval; teichmann@8031: } teichmann@8031: mschaefer@8986: public void setSQTimeInterval(final ImportTimeInterval sqTimeInterval) { tom@8059: this.sqTimeInterval = sqTimeInterval; tom@8059: } tom@8059: mschaefer@8986: public void setUnit(final ImportUnit unit) { teichmann@8031: this.unit = unit; teichmann@8031: } teichmann@8031: mschaefer@8986: public void setGrainFraction(final ImportGrainFraction grainFraction) { teichmann@8031: this.grainFraction = grainFraction; teichmann@8031: } teichmann@8031: mschaefer@8986: public void setKind(final Integer kind) { teichmann@8031: this.kind = kind; teichmann@8031: } teichmann@8031: mschaefer@8986: public void addValue(final ImportSedimentLoadLSValue value) { teichmann@8031: this.values.add(value); teichmann@8031: } teichmann@8031: mschaefer@8986: public void storeDependencies(final River river) { mschaefer@8986: log.info("store dependencies for '" + this.description + "'"); teichmann@8031: mschaefer@8986: final SedimentLoadLS peer = getPeer(river); teichmann@8031: teichmann@8031: if (peer != null) { teichmann@8031: int i = 0; teichmann@8031: mschaefer@8986: for (final ImportSedimentLoadLSValue value : this.values) { mschaefer@8986: value.storeDependencies(peer, this.storeMode); teichmann@8031: i++; teichmann@8031: } teichmann@8031: tom@8032: log.info("stored " + i + " sediment load values."); teichmann@8031: } teichmann@8031: } teichmann@8031: mschaefer@8986: public SedimentLoadLS getPeer(final River river) { teichmann@8031: log.debug("get peer"); teichmann@8031: mschaefer@8986: final GrainFraction gf = this.grainFraction != null ? this.grainFraction.getPeer() mschaefer@8986: : null; teichmann@8031: mschaefer@8986: final Unit u = this.unit != null ? this.unit.getPeer() : null; teichmann@8031: mschaefer@8986: final TimeInterval ti = (this.timeInterval != null) ? this.timeInterval.getPeer() : null; mschaefer@8986: final TimeInterval sqti = (this.sqTimeInterval != null) ? this.sqTimeInterval.getPeer() : null; teichmann@8031: teichmann@8031: if (ti == null || u == null) { tom@8856: log.warn( mschaefer@8986: "Skip invalid SedimentLoadLS: time interval or unit null!"); teichmann@8031: return null; teichmann@8031: } teichmann@8031: mschaefer@8986: if (this.peer == null) { mschaefer@8986: final Session session = ImporterSession.getInstance() mschaefer@8986: .getDatabaseSession(); tom@8059: mschaefer@8986: final String sqtquery = this.sqTimeInterval == null ? mschaefer@8986: "sq_time_interval_id is null" : mschaefer@8986: "sqTimeInterval = :sqTimeInterval"; mschaefer@8986: final Query query = session.createQuery("from SedimentLoadLS where " mschaefer@8986: + " river=:river and " mschaefer@8986: + " grainFraction=:grainFraction and " mschaefer@8986: + " unit=:unit and " mschaefer@8986: + " timeInterval=:timeInterval and " mschaefer@8986: + " description=:description and " mschaefer@8986: + " kind = :kind and " + mschaefer@8986: sqtquery); 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); tom@8059: if (sqti != null) { tom@8059: query.setParameter("sqTimeInterval", sqti); tom@8059: } mschaefer@8986: query.setParameter("description", this.description); mschaefer@8986: query.setParameter("kind", this.kind); teichmann@8031: mschaefer@8986: final List loads = query.list(); tom@8032: if (loads.isEmpty()) { tom@8032: log.debug("create new SedimentLoadLS"); teichmann@8031: mschaefer@8986: this.peer = new SedimentLoadLS(river, u, ti, sqti, gf, this.description); mschaefer@8986: this.peer.setKind(this.kind); mschaefer@8986: session.save(this.peer); mschaefer@8986: this.storeMode = StoreMode.INSERT; teichmann@8031: } teichmann@8031: else { mschaefer@8986: this.peer = loads.get(0); mschaefer@8986: this.storeMode = StoreMode.UPDATE; teichmann@8031: } teichmann@8031: } teichmann@8031: mschaefer@8986: return this.peer; teichmann@8031: } teichmann@8031: } teichmann@8031: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :