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; tom@8559: tom@8559: import org.hibernate.Session; tom@8559: import org.hibernate.Query; tom@8559: 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; 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; tom@8559: tom@8559: protected List values; tom@8559: tom@8559: protected BedHeight peer; tom@8559: tom@8559: tom@8559: public ImportBedHeight(String description) { tom@8559: this.description = description; tom@8559: this.values = new ArrayList(); tom@8559: } tom@8559: tom@8559: tom@8559: public String getDescription() { tom@8559: return description; tom@8559: } tom@8559: tom@8559: public int getValueCount() { tom@8559: return values.size(); tom@8559: } tom@8559: tom@8559: tom@8559: public void setYear(int year) { tom@8559: this.year = year; tom@8559: } tom@8559: tom@8559: public void setTimeInterval(ImportTimeInterval timeInterval) { tom@8559: // do nothing tom@8559: } tom@8559: tom@8559: public void setEvaluationBy(String evaluationBy) { tom@8559: this.evaluationBy = evaluationBy; tom@8559: } tom@8559: tom@8559: public void setDescription(String description) { tom@8559: this.description = description; tom@8559: } tom@8559: tom@8559: public void setRange(ImportRange range) { tom@8559: this.range = range; tom@8559: } tom@8559: tom@8559: public void setType(ImportBedHeightType type) { tom@8559: this.type = type; tom@8559: } tom@8559: tom@8559: public void setLocationSystem(ImportLocationSystem locationSystem) { tom@8559: this.locationSystem = locationSystem; tom@8559: } tom@8559: tom@8559: public void setCurElevationModel(ImportElevationModel curElevationModel) { tom@8559: this.curElevationModel = curElevationModel; tom@8559: } tom@8559: tom@8559: public void setOldElevationModel(ImportElevationModel oldElevationModel) { tom@8559: this.oldElevationModel = oldElevationModel; tom@8559: } tom@8559: tom@8559: public void addValue(ImportBedHeightValue value) { tom@8559: values.add((ImportBedHeightValue) value); tom@8559: } tom@8559: tom@8559: public void storeDependencies(River river) { tom@8559: log.info("Store dependencies for single: '" + getDescription() + "'"); tom@8559: tom@8559: if (type != null) { tom@8559: type.storeDependencies(); tom@8559: } tom@8559: tom@8559: if (locationSystem != null) { tom@8559: locationSystem.storeDependencies(); tom@8559: } tom@8559: tom@8559: if (curElevationModel != null) { tom@8559: curElevationModel.storeDependencies(); tom@8559: } tom@8559: tom@8559: if (oldElevationModel != null) { tom@8559: oldElevationModel.storeDependencies(); tom@8559: } tom@8559: tom@8559: BedHeight peer = getPeer(river); tom@8559: tom@8559: if (peer != null) { tom@8559: for (ImportBedHeightValue value: values) { tom@8559: value.storeDependencies(peer); tom@8559: } tom@8559: } tom@8559: tom@8559: Session session = ImporterSession.getInstance().getDatabaseSession(); tom@8559: session.flush(); tom@8559: } tom@8559: tom@8559: public BedHeight getPeer(River river) { tom@8559: if (peer == null) { tom@8559: BedHeightType theType = type != null ? type.getPeer() : null; tom@8559: ElevationModel theCurModel = curElevationModel.getPeer(); tom@8856: Range theRange = range != null tom@8856: ? range.getPeer(river) tom@8856: : null; tom@8559: tom@8559: if (theType == null) { tom@8559: log.warn("BHS: No bed height type given. Skip file '" + tom@8559: description + "'"); tom@8559: return null; tom@8559: } tom@8559: tom@8559: if (theCurModel == null) { tom@8559: log.warn("BHS: No elevation model given. Skip file '" + tom@8559: description + "'"); tom@8559: return null; tom@8559: } tom@8559: tom@8559: if (theRange == null) { tom@8559: log.warn("BHS: No km-range given: '" + tom@8559: description + "'"); tom@8559: } tom@8559: tom@8856: Session session = ImporterSession.getInstance() tom@8856: .getDatabaseSession(); tom@8559: tom@8559: Query query = session.createQuery( tom@8559: "from BedHeight where " + tom@8560: "river=:river and year=:year " + tom@8559: "and type=:type and locationSystem=:locationSystem and " + tom@8559: "curElevationModel=:curElevationModel and range=:range"); tom@8559: tom@8559: query.setParameter("river", river); tom@8559: query.setParameter("year", year); tom@8559: query.setParameter("type", theType); tom@8559: query.setParameter("locationSystem", locationSystem.getPeer()); tom@8559: query.setParameter("curElevationModel", theCurModel); tom@8559: query.setParameter("range", range.getPeer(river)); tom@8559: tom@8559: List bedHeights = query.list(); tom@8559: if (bedHeights.isEmpty()) { tom@8559: log.info("Create new BedHeight DB instance."); tom@8559: tom@8559: peer = new BedHeight( tom@8559: river, tom@8559: year, tom@8559: theType, tom@8559: locationSystem.getPeer(), tom@8559: theCurModel, tom@8856: oldElevationModel != null tom@8856: ? oldElevationModel.getPeer() tom@8856: : null, tom@8559: range.getPeer(river), tom@8559: evaluationBy, tom@8559: description tom@8559: ); tom@8559: tom@8559: session.save(peer); tom@8559: } tom@8559: else { tom@8559: peer = bedHeights.get(0); tom@8559: } tom@8559: } tom@8559: tom@8559: return peer; tom@8559: } tom@8559: } tom@8559: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :