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