tom@8559: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde tom@8559: * Software engineering by Intevation GmbH tom@8559: * tom@8559: * This file is Free Software under the GNU AGPL (>=v3) tom@8559: * and comes with ABSOLUTELY NO WARRANTY! Check out the tom@8559: * documentation coming with Dive4Elements River for details. tom@8559: */ tom@8559: tom@8559: package org.dive4elements.river.importer; tom@8559: tom@8559: import java.util.ArrayList; tom@8559: import java.util.List; tom@8559: tom@8559: import org.apache.log4j.Logger; mschaefer@8986: import org.dive4elements.river.importer.common.StoreMode; tom@8559: import org.dive4elements.river.model.BedHeight; tom@8559: import org.dive4elements.river.model.BedHeightType; tom@8559: import org.dive4elements.river.model.ElevationModel; tom@8559: import org.dive4elements.river.model.Range; tom@8559: import org.dive4elements.river.model.River; mschaefer@8975: import org.hibernate.Query; mschaefer@8975: import org.hibernate.Session; tom@8559: tom@8559: tom@8559: public class ImportBedHeight tom@8559: { tom@8559: private static Logger log = Logger.getLogger(ImportBedHeight.class); tom@8559: tom@8559: protected Integer year; tom@8559: tom@8559: protected String evaluationBy; tom@8559: protected String description; tom@8559: tom@8559: protected ImportRange range; tom@8559: protected ImportBedHeightType type; tom@8559: protected ImportLocationSystem locationSystem; tom@8559: protected ImportElevationModel curElevationModel; tom@8559: protected ImportElevationModel oldElevationModel; mschaefer@8975: protected String sounding_width_info; mschaefer@9038: protected String notes; tom@8559: tom@8559: protected List values; tom@8559: mschaefer@8986: protected StoreMode storeMode; mschaefer@8986: tom@8559: protected BedHeight peer; tom@8559: tom@8559: mschaefer@8975: public ImportBedHeight(final String description) { tom@8559: this.description = description; mschaefer@8975: this.values = new ArrayList<>(); mschaefer@8986: this.storeMode = StoreMode.NONE; tom@8559: } tom@8559: tom@8559: tom@8559: public String getDescription() { mschaefer@8975: return this.description; tom@8559: } tom@8559: tom@8559: public int getValueCount() { mschaefer@8975: return this.values.size(); tom@8559: } tom@8559: tom@8559: mschaefer@8975: public void setYear(final int year) { tom@8559: this.year = year; tom@8559: } tom@8559: mschaefer@8975: public void setTimeInterval(final ImportTimeInterval timeInterval) { tom@8559: // do nothing tom@8559: } tom@8559: mschaefer@8975: public void setEvaluationBy(final String evaluationBy) { tom@8559: this.evaluationBy = evaluationBy; tom@8559: } tom@8559: mschaefer@8975: public void setDescription(final String description) { tom@8559: this.description = description; tom@8559: } tom@8559: mschaefer@8975: public void setRange(final ImportRange range) { tom@8559: this.range = range; tom@8559: } tom@8559: mschaefer@8975: public void setType(final ImportBedHeightType type) { tom@8559: this.type = type; tom@8559: } tom@8559: mschaefer@8975: public void setLocationSystem(final ImportLocationSystem locationSystem) { tom@8559: this.locationSystem = locationSystem; tom@8559: } tom@8559: mschaefer@8975: public void setCurElevationModel(final ImportElevationModel curElevationModel) { tom@8559: this.curElevationModel = curElevationModel; tom@8559: } tom@8559: mschaefer@8975: public void setOldElevationModel(final ImportElevationModel oldElevationModel) { tom@8559: this.oldElevationModel = oldElevationModel; tom@8559: } tom@8559: mschaefer@8975: public void setSoundingWidthInfo(final String sounding_width_info) { mschaefer@8975: this.sounding_width_info = sounding_width_info; tom@8559: } tom@8559: mschaefer@9038: public void setNotes(final String notes) { mschaefer@9038: this.notes = notes; mschaefer@8975: } mschaefer@8975: mschaefer@8975: public void addValue(final ImportBedHeightValue value) { mschaefer@8975: this.values.add(value); mschaefer@8975: } mschaefer@8975: mschaefer@8975: public void storeDependencies(final River river) { tom@8559: log.info("Store dependencies for single: '" + getDescription() + "'"); tom@8559: mschaefer@8975: if (this.type != null) { mschaefer@8975: this.type.storeDependencies(); tom@8559: } tom@8559: mschaefer@8975: if (this.locationSystem != null) { mschaefer@8975: this.locationSystem.storeDependencies(); tom@8559: } tom@8559: mschaefer@8975: if (this.curElevationModel != null) { mschaefer@8975: this.curElevationModel.storeDependencies(); tom@8559: } tom@8559: mschaefer@8975: if (this.oldElevationModel != null) { mschaefer@8975: this.oldElevationModel.storeDependencies(); mschaefer@8975: } mschaefer@8975: mschaefer@8975: final BedHeight peer = getPeer(river); tom@8559: tom@8559: if (peer != null) { mschaefer@8975: for (final ImportBedHeightValue value: this.values) { tom@8559: value.storeDependencies(peer); tom@8559: } tom@8559: } tom@8559: mschaefer@8975: final Session session = ImporterSession.getInstance().getDatabaseSession(); tom@8559: session.flush(); tom@8559: } tom@8559: mschaefer@8975: public BedHeight getPeer(final River river) { mschaefer@8975: if (this.peer != null) mschaefer@8975: return null; tom@8559: mschaefer@8975: final BedHeightType theType = this.type != null ? this.type.getPeer() : null; mschaefer@8975: final ElevationModel theCurModel = this.curElevationModel.getPeer(); mschaefer@8975: final Range theRange = (this.range != null) ? this.range.getPeer(river) : null; tom@8559: mschaefer@8975: if (theType == null) { mschaefer@8975: log.warn("BHS: No bed height type given. Skip file '" + this.description + "'"); mschaefer@8975: return null; tom@8559: } tom@8559: mschaefer@8975: if (theCurModel == null) { mschaefer@8975: log.warn("BHS: No elevation model given. Skip file '" + this.description + "'"); mschaefer@8975: return null; mschaefer@8975: } mschaefer@8975: mschaefer@8975: if (theRange == null) { mschaefer@8975: log.warn("BHS: No km-range given: '" + this.description + "'"); mschaefer@8975: } mschaefer@8975: mschaefer@8975: final Session session = ImporterSession.getInstance().getDatabaseSession(); mschaefer@8975: mschaefer@8975: final Query query = session.createQuery("FROM BedHeight WHERE (river=:river) AND (year=:year)" mschaefer@8975: + " AND (type=:type) AND (locationSystem=:locationSystem)" mschaefer@8975: + " AND (curElevationModel=:curElevationModel) AND (range=:range)"); mschaefer@8975: query.setParameter("river", river); mschaefer@8975: query.setParameter("year", this.year); mschaefer@8975: query.setParameter("type", theType); mschaefer@8975: query.setParameter("locationSystem", this.locationSystem.getPeer()); mschaefer@8975: query.setParameter("curElevationModel", theCurModel); mschaefer@8975: query.setParameter("range", this.range.getPeer(river)); mschaefer@8975: mschaefer@8975: final List bedHeights = query.list(); mschaefer@8975: if (bedHeights.isEmpty()) { mschaefer@8975: log.info("Create new BedHeight DB instance."); mschaefer@8975: this.peer = new BedHeight(river, this.year, theType, this.locationSystem.getPeer(), theCurModel, mschaefer@8975: (this.oldElevationModel != null) ? this.oldElevationModel.getPeer() : null, this.range.getPeer(river), mschaefer@9038: this.evaluationBy, this.description, this.sounding_width_info, this.notes); mschaefer@8975: session.save(this.peer); mschaefer@8986: this.storeMode = StoreMode.INSERT; mschaefer@8975: } mschaefer@8975: else { mschaefer@8975: this.peer = bedHeights.get(0); mschaefer@8986: this.storeMode = StoreMode.UPDATE; mschaefer@8975: } mschaefer@8975: mschaefer@8975: return this.peer; tom@8559: } tom@8559: } tom@8559: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :