diff backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightEpoch.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightEpoch.java@18619c1e7c2a
children 4dd33b86dc61
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightEpoch.java	Thu Apr 25 15:23:37 2013 +0200
@@ -0,0 +1,198 @@
+package org.dive4elements.river.importer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+import org.dive4elements.river.model.BedHeightEpoch;
+import org.dive4elements.river.model.ElevationModel;
+import org.dive4elements.river.model.Range;
+import org.dive4elements.river.model.River;
+import org.dive4elements.river.model.TimeInterval;
+
+
+/** Import Bed Height Data, 'epoch' type from csv file. */
+public class ImportBedHeightEpoch implements ImportBedHeight {
+
+    /** Private logger. */
+    private static Logger log = Logger.getLogger(ImportBedHeightEpoch.class);
+
+    protected String evaluationBy;
+
+    /** De facto the file name. */
+    protected String description;
+
+    protected ImportTimeInterval timeInterval;
+    protected ImportRange range;
+    protected ImportElevationModel curElevationModel;
+    protected ImportElevationModel oldElevationModel;
+
+    protected List<ImportBedHeightEpochValue> values;
+
+    protected BedHeightEpoch peer;
+
+    public ImportBedHeightEpoch(String description) {
+        this.description = description;
+        this.values = new ArrayList<ImportBedHeightEpochValue>();
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public int getValueCount() {
+        return values.size();
+    }
+
+    public void setTimeInterval(ImportTimeInterval timeInterval) {
+        this.timeInterval = timeInterval;
+    }
+
+    public void setEvaluationBy(String evaluationBy) {
+        this.evaluationBy = evaluationBy;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public void setRange(ImportRange range) {
+        this.range = range;
+    }
+
+    public void setCurElevationModel(ImportElevationModel curElevationModel) {
+        this.curElevationModel = curElevationModel;
+    }
+
+    public void setOldElevationModel(ImportElevationModel oldElevationModel) {
+        this.oldElevationModel = oldElevationModel;
+    }
+
+    /** Does nothing. */
+    public void setYear(int year) {
+        // do nothing
+    }
+
+    /** Does nothing. */
+    public void setSoundingWidth(int soundingWidth) {
+        // do nothing
+    }
+
+
+    /** Does nothing. */
+    public void setLocationSystem(ImportLocationSystem locationSystem) {
+        // do nothing
+    }
+
+
+    /** Does nothing. */
+    public void setType(ImportBedHeightType type) {
+        // do nothing
+    }
+
+    @Override
+    public void addValue(ImportBedHeightValue value) {
+        values.add((ImportBedHeightEpochValue) value);
+    }
+
+    @Override
+    public void storeDependencies(River river) {
+        log.info("Store dependencies for epoch: '" + getDescription() + "'");
+
+        BedHeightEpoch peer = getPeer(river);
+
+        if (curElevationModel != null) {
+            curElevationModel.storeDependencies();
+        }
+
+        if (oldElevationModel != null) {
+            oldElevationModel.storeDependencies();
+        }
+
+        if (peer != null) {
+            log.debug("store values now...");
+
+            for (ImportBedHeightEpochValue value : values) {
+                value.storeDependencies(peer);
+            }
+        }
+
+        Session session = ImporterSession.getInstance().getDatabaseSession();
+        session.flush();
+    }
+
+    /**
+     * Asserts all dependent entities (ElevationModel, TimeInterval, Range,
+     * BedHeighEpoch) are in db and returns bound (either newly created or
+     * freshly fetched) BedHeightEpoch.
+     */
+    @Override
+    public BedHeightEpoch getPeer(River river) {
+        if (peer == null) {
+            ElevationModel theCurModel = null;
+            if (curElevationModel != null) {
+                curElevationModel.storeDependencies();
+                theCurModel = curElevationModel.getPeer();
+            }
+
+            if (theCurModel == null) {
+                log.warn("BHE: Skip file - invalid current elevation model.");
+                return null;
+            }
+
+            TimeInterval theTime = null;
+            if (timeInterval != null) {
+                theTime = timeInterval.getPeer();
+            }
+
+            if (theTime == null) {
+                log.warn("BHE: Skip file - invalid time range.");
+                return null;
+            }
+
+            Range theRange = range != null ? range.getPeer(river) : null;
+
+            if (theRange == null) {
+                log.warn("BHE: invalid km range.");
+            }
+
+            Session session = ImporterSession.getInstance()
+                .getDatabaseSession();
+
+            Query query = session.createQuery("from BedHeightEpoch where "
+                + "   river=:river and " + "   timeInterval=:timeInterval and "
+                + "   curElevationModel=:curElevationModel and "
+                + "   range=:range and " + "   evaluationBy=:evaluationBy and "
+                + "   description=:description");
+
+            query.setParameter("river", river);
+            query.setParameter("timeInterval", theTime);
+            query.setParameter("curElevationModel", theCurModel);
+            query.setParameter("range", theRange);
+            query.setParameter("evaluationBy", evaluationBy);
+            query.setParameter("description", description);
+
+            List<BedHeightEpoch> bedHeights = query.list();
+
+            if (bedHeights.isEmpty()) {
+                log.info("Create new BedHeightEpoch DB instance.");
+
+                peer = new BedHeightEpoch(river, theTime, theRange,
+                    theCurModel,
+                    oldElevationModel != null ? oldElevationModel.getPeer()
+                        : null, evaluationBy, description);
+
+                session.save(peer);
+            }
+            else {
+                peer = bedHeights.get(0);
+            }
+        }
+
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org