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; sascha@1218: mschaefer@8986: import java.math.BigDecimal; sascha@1218: import java.util.ArrayList; sascha@1218: import java.util.List; sascha@1218: mschaefer@8986: import org.dive4elements.river.importer.common.StoreMode; mschaefer@8986: import org.dive4elements.river.model.HYKEntry; mschaefer@8986: import org.dive4elements.river.model.HYKFormation; sascha@1218: import org.hibernate.Query; mschaefer@8986: import org.hibernate.Session; sascha@1218: sascha@1218: public class ImportHYKFormation sascha@1218: { sascha@1218: protected int formationNum; sascha@1218: protected ImportHYKEntry entry; sascha@1218: protected BigDecimal top; sascha@1218: protected BigDecimal bottom; sascha@1218: protected BigDecimal distanceVL; sascha@1218: protected BigDecimal distanceHF; sascha@1218: protected BigDecimal distanceVR; sascha@1218: sascha@1218: protected List zones; sascha@1218: mschaefer@8986: protected StoreMode storeMode; mschaefer@8986: sascha@1218: protected HYKFormation peer; sascha@1218: sascha@1218: public ImportHYKFormation() { mschaefer@8986: this.zones = new ArrayList<>(); mschaefer@8986: this.storeMode = StoreMode.NONE; sascha@1218: } sascha@1218: sascha@1218: public ImportHYKFormation( mschaefer@8986: final int formationNum, mschaefer@8986: final ImportHYKEntry entry, mschaefer@8986: final BigDecimal top, mschaefer@8986: final BigDecimal bottom, mschaefer@8986: final BigDecimal distanceVL, mschaefer@8986: final BigDecimal distanceHF, mschaefer@8986: final BigDecimal distanceVR mschaefer@8986: ) { sascha@1218: this(); sascha@1218: this.formationNum = formationNum; sascha@1218: this.entry = entry; sascha@1218: this.top = top; sascha@1218: this.bottom = bottom; sascha@1218: this.distanceVL = distanceVL; sascha@1218: this.distanceHF = distanceHF; sascha@1218: this.distanceVR = distanceVR; sascha@1218: } sascha@1218: mschaefer@8986: public void addFlowZone(final ImportHYKFlowZone zone) { mschaefer@8986: this.zones.add(zone); sascha@1218: zone.setFormation(this); sascha@1218: } sascha@1218: sascha@1218: public int getFormationNum() { mschaefer@8986: return this.formationNum; sascha@1218: } sascha@1218: mschaefer@8986: public void setFormationNum(final int formationNum) { sascha@1218: this.formationNum = formationNum; sascha@1218: } sascha@1218: sascha@1218: public ImportHYKEntry getEntry() { mschaefer@8986: return this.entry; sascha@1218: } sascha@1218: mschaefer@8986: public void setEntry(final ImportHYKEntry entry) { sascha@1218: this.entry = entry; sascha@1218: } sascha@1218: sascha@1224: public BigDecimal getTop() { mschaefer@8986: return this.top; sascha@1224: } sascha@1224: mschaefer@8986: public void setTop(final BigDecimal top) { sascha@1224: this.top = top; sascha@1224: } sascha@1224: sascha@1224: public BigDecimal getBottom() { mschaefer@8986: return this.bottom; sascha@1224: } sascha@1224: mschaefer@8986: public void setBottom(final BigDecimal bottom) { sascha@1224: this.bottom = bottom; sascha@1224: } sascha@1224: sascha@1219: public BigDecimal getDistanceVL() { mschaefer@8986: return this.distanceVL; sascha@1219: } sascha@1219: mschaefer@8986: public void setDistanceVL(final BigDecimal distanceVL) { sascha@1219: this.distanceVL = distanceVL; sascha@1219: } sascha@1219: sascha@1219: public BigDecimal getDistanceHF() { mschaefer@8986: return this.distanceHF; sascha@1219: } sascha@1219: mschaefer@8986: public void setDistanceHF(final BigDecimal distanceHF) { sascha@1219: this.distanceHF = distanceHF; sascha@1219: } sascha@1219: sascha@1219: public BigDecimal getDistanceVR() { mschaefer@8986: return this.distanceVR; sascha@1219: } sascha@1219: mschaefer@8986: public void setDistanceVR(final BigDecimal distanceVR) { sascha@1219: this.distanceVR = distanceVR; sascha@1219: } sascha@1219: sascha@1221: public void storeDependencies() { sascha@1221: getPeer(); mschaefer@8986: for (final ImportHYKFlowZone zone: this.zones) { sascha@1221: zone.storeDependencies(); sascha@1221: } sascha@1221: } sascha@1221: sascha@1218: public HYKFormation getPeer() { mschaefer@8986: if (this.peer == null) { mschaefer@8986: final HYKEntry e = this.entry.getPeer(); mschaefer@8986: final Session session = ImporterSession.getInstance() mschaefer@8986: .getDatabaseSession(); mschaefer@8986: List formations; mschaefer@8986: if (this.entry.storeMode == StoreMode.INSERT) mschaefer@8986: formations = null; mschaefer@8986: else { mschaefer@8986: final Query query = session.createQuery( mschaefer@8986: "from HYKFormation where formationNum=:formationNum " + mschaefer@8986: "and entry=:entry and top=:top and bottom=:bottom " + mschaefer@8986: "and distanceVL=:distanceVL and distanceHF=:distanceHF " + mschaefer@8986: "and distanceVR=:distanceVR"); mschaefer@8986: query.setParameter("formationNum", this.formationNum); mschaefer@8986: query.setParameter("entry", e); mschaefer@8986: query.setParameter("top", this.top); mschaefer@8986: query.setParameter("bottom", this.bottom); mschaefer@8986: query.setParameter("distanceVL", this.distanceVL); mschaefer@8986: query.setParameter("distanceHF", this.distanceHF); mschaefer@8986: query.setParameter("distanceVR", this.distanceVR); mschaefer@8986: formations = query.list(); mschaefer@8986: } mschaefer@8986: if ((formations == null) || formations.isEmpty()) { mschaefer@8986: this.peer = new HYKFormation( mschaefer@8986: this.formationNum, e, this.top, this.bottom, mschaefer@8986: this.distanceVL, this.distanceHF, this.distanceVR); mschaefer@8986: session.save(this.peer); mschaefer@8986: this.storeMode = StoreMode.INSERT; sascha@1218: } sascha@1218: else { mschaefer@8986: this.peer = formations.get(0); mschaefer@8986: this.storeMode = StoreMode.UPDATE; sascha@1218: } sascha@1218: } mschaefer@8986: return this.peer; sascha@1218: } sascha@1218: } sascha@1218: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :