changeset 2810:04eeb45df27b

Implemented model classes and importer classes for bed height epochs. flys-backend/trunk@4222 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 12 Apr 2012 12:50:49 +0000
parents f283212966e8
children 8926571e47fb
files flys-backend/ChangeLog flys-backend/doc/schema/oracle-minfo.sql flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java flys-backend/src/main/java/de/intevation/flys/importer/Config.java flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpoch.java flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpochValue.java flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java flys-backend/src/main/java/de/intevation/flys/model/BedHeightEpoch.java flys-backend/src/main/java/de/intevation/flys/model/BedHeightEpochValue.java
diffstat 9 files changed, 565 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Thu Apr 12 10:42:46 2012 +0000
+++ b/flys-backend/ChangeLog	Thu Apr 12 12:50:49 2012 +0000
@@ -1,3 +1,27 @@
+2012-04-12  Ingo Weinzierl <ingo@intevation.de>
+
+	* doc/schema/oracle-minfo.sql: Added a link to the river to bed_height_epoch
+	  table.
+
+	* src/main/java/de/intevation/flys/model/BedHeightEpochValue.java,
+	  src/main/java/de/intevation/flys/model/BedHeightEpoch.java: New model
+	  classes for MINFO bed height epochs.
+
+	* src/main/java/de/intevation/flys/importer/Config.java: Splitted up the
+	  config option to skip parsing bed heights. Now, we are able to skip single
+	  and epoch bed heights using the following options:
+
+	    -Dflys.backend.importer.skip.bed.height.single=true  (skip singles)
+	    -Dflys.backend.importer.skip.bed.height.epoch=true   (skip epochs)
+
+	* src/main/java/de/intevation/flys/importer/ImportBedHeightEpoch.java,
+	  src/main/java/de/intevation/flys/importer/ImportBedHeightEpochValue.java,
+	  src/main/java/de/intevation/flys/importer/ImportRiver.java: Implemented
+	  the whole stuff to parse those data.
+
+	* src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java:
+	  Registered the new model classes.
+
 2012-04-12  Ingo Weinzierl <ingo@intevation.de>
 
 	* doc/schema/oracle-minfo.sql,
--- a/flys-backend/doc/schema/oracle-minfo.sql	Thu Apr 12 10:42:46 2012 +0000
+++ b/flys-backend/doc/schema/oracle-minfo.sql	Thu Apr 12 12:50:49 2012 +0000
@@ -59,6 +59,7 @@
 
 CREATE TABLE bed_height_epoch (
     id                      NUMBER(38,0) NOT NULL,
+    river_id                NUMBER(38,0) NOT NULL,
     time_interval_id        NUMBER(38,0) NOT NULL,
     -- sounding_with           NUMBER(38,0) NOT NULL,
     -- type_id                 NUMBER(38,0) NOT NULL,
--- a/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java	Thu Apr 12 10:42:46 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java	Thu Apr 12 12:50:49 2012 +0000
@@ -24,6 +24,8 @@
 import de.intevation.flys.model.Annotation;
 import de.intevation.flys.model.AnnotationType;
 import de.intevation.flys.model.Attribute;
+import de.intevation.flys.model.BedHeightEpoch;
+import de.intevation.flys.model.BedHeightEpochValue;
 import de.intevation.flys.model.BedHeightSingle;
 import de.intevation.flys.model.BedHeightSingleValue;
 import de.intevation.flys.model.BedHeightType;
@@ -218,6 +220,8 @@
         cfg.addAnnotatedClass(Annotation.class);
         cfg.addAnnotatedClass(AnnotationType.class);
         cfg.addAnnotatedClass(Attribute.class);
+        cfg.addAnnotatedClass(BedHeightEpoch.class);
+        cfg.addAnnotatedClass(BedHeightEpochValue.class);
         cfg.addAnnotatedClass(BedHeightSingle.class);
         cfg.addAnnotatedClass(BedHeightSingleValue.class);
         cfg.addAnnotatedClass(BedHeightType.class);
--- a/flys-backend/src/main/java/de/intevation/flys/importer/Config.java	Thu Apr 12 10:42:46 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/Config.java	Thu Apr 12 12:50:49 2012 +0000
@@ -41,8 +41,11 @@
     public static final String SKIP_FLOOD_PROTECTION =
         "flys.backend.importer.skip.flood.protection";
 
-    public static final String SKIP_BED_HEIGHT =
-        "flys.backend.importer.skip.bed.height";
+    public static final String SKIP_BED_HEIGHT_SINGLE =
+        "flys.backend.importer.skip.bed.height.single";
+
+    public static final String SKIP_BED_HEIGHT_EPOCH =
+        "flys.backend.importer.skip.bed.height.epoch";
 
 
     public static final Config INSTANCE = new Config();
@@ -102,8 +105,12 @@
         return Boolean.getBoolean(SKIP_FLOOD_PROTECTION);
     }
 
-    public boolean skipBedHeight() {
-        return Boolean.getBoolean(SKIP_BED_HEIGHT);
+    public boolean skipBedHeightSingle() {
+        return Boolean.getBoolean(SKIP_BED_HEIGHT_SINGLE);
+    }
+
+    public boolean skipBedHeightEpoch() {
+        return Boolean.getBoolean(SKIP_BED_HEIGHT_EPOCH);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpoch.java	Thu Apr 12 10:42:46 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpoch.java	Thu Apr 12 12:50:49 2012 +0000
@@ -1,19 +1,43 @@
 package de.intevation.flys.importer;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import java.sql.SQLException;
+
 import org.apache.log4j.Logger;
 
+import org.hibernate.Session;
+import org.hibernate.Query;
+import org.hibernate.exception.ConstraintViolationException;
+
+import de.intevation.flys.model.BedHeightEpoch;
+import de.intevation.flys.model.ElevationModel;
+import de.intevation.flys.model.Range;
 import de.intevation.flys.model.River;
+import de.intevation.flys.model.TimeInterval;
 
 
 public class ImportBedHeightEpoch
 {
     private static Logger log = Logger.getLogger(ImportBedHeightEpoch.class);
 
+    protected String evaluationBy;
     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>();
     }
 
 
@@ -22,9 +46,115 @@
     }
 
 
-    public void storeDependencies(River river) {
+    public List<ImportBedHeightEpochValue> getValues() {
+        return values;
+    }
+
+
+    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;
+    }
+
+    public void addValue(ImportBedHeightEpochValue value) {
+        values.add(value);
+    }
+
+
+    public void storeDependencies(River river)
+    throws SQLException, ConstraintViolationException
+    {
         log.info("Store dependencies for epoch: '" + getDescription() + "'");
-        log.error("TODO: IMPLEMENT ME!");
+
+        if (curElevationModel != null) {
+            curElevationModel.storeDependencies();
+        }
+
+        if (oldElevationModel != null) {
+            oldElevationModel.storeDependencies();
+        }
+
+        BedHeightEpoch peer = getPeer(river);
+
+        log.debug("store values now...");
+
+        for (ImportBedHeightEpochValue value: values) {
+            value.storeDependencies(peer);
+        }
+    }
+
+
+    public BedHeightEpoch getPeer(River river) {
+        if (peer == null) {
+            ElevationModel theCurModel = curElevationModel != null
+                ? curElevationModel.getPeer()
+                : null;
+
+            TimeInterval theTime = timeInterval != null
+                ? timeInterval.getPeer()
+                : null;
+
+            Range theRange = range != null ? range.getPeer(river) : null;
+
+            if (theCurModel == null || theRange == null || theTime == null) {
+                log.warn("Skip invalid file '" + description + "'");
+                return null;
+            }
+
+            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
+                );
+            }
+            else {
+                peer = bedHeights.get(0);
+            }
+        }
+
+        return peer;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpochValue.java	Thu Apr 12 12:50:49 2012 +0000
@@ -0,0 +1,75 @@
+package de.intevation.flys.importer;
+
+import java.util.List;
+
+import java.math.BigDecimal;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import de.intevation.flys.model.BedHeightEpoch;
+import de.intevation.flys.model.BedHeightEpochValue;
+
+
+public class ImportBedHeightEpochValue {
+
+    private static final Logger log =
+        Logger.getLogger(ImportBedHeightEpochValue.class);
+
+
+    private BigDecimal station;
+    private BigDecimal height;
+
+    private BedHeightEpochValue peer;
+
+
+    public ImportBedHeightEpochValue() {
+    }
+
+
+    public ImportBedHeightEpochValue(BigDecimal station, BigDecimal height) {
+        this.station = station;
+        this.height  = height;
+    }
+
+
+    public void storeDependencies(BedHeightEpoch bedHeight) {
+        getPeer(bedHeight);
+    }
+
+
+    public BedHeightEpochValue getPeer(BedHeightEpoch bedHeight) {
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+
+            Query query = session.createQuery(
+                "from BedHeightEpochValue where " +
+                "   bedHeight=:bedHeight and " +
+                "   station=:station and " +
+                "   height=:height");
+
+            query.setParameter("bedHeight", bedHeight);
+            query.setParameter("station", station);
+            query.setParameter("height", height);
+
+            List<BedHeightEpochValue> values = query.list();
+
+            if (values.isEmpty()) {
+                peer = new BedHeightEpochValue(
+                    bedHeight,
+                    station,
+                    height
+                );
+                session.save(peer);
+            }
+            else {
+                peer = values.get(0);
+            }
+        }
+
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Thu Apr 12 10:42:46 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Thu Apr 12 12:50:49 2012 +0000
@@ -222,11 +222,6 @@
 
 
     public void parseBedHeight() throws IOException {
-        if (Config.INSTANCE.skipBedHeight()) {
-            log.info("skip parsing bed height.");
-            return;
-        }
-
         log.info("Parse bed height.");
 
         File minfoDir     = getMinfoDir();
@@ -234,8 +229,19 @@
         File singlesDir   = new File(bedHeightDir, BED_HEIGHT_SINGLE_DIR);
         File epochDir     = new File(bedHeightDir, BED_HEIGHT_EPOCH_DIR);
 
-        parseBedHeightSingles(singlesDir);
-        //parseBedHeightEpochs(epochDir);
+        if (Config.INSTANCE.skipBedHeightSingle()) {
+            log.info("skip parsing bed height single.");
+        }
+        else {
+            parseBedHeightSingles(singlesDir);
+        }
+
+        if (Config.INSTANCE.skipBedHeightEpoch()) {
+            log.info("skip parsing bed height epochs.");
+        }
+        else {
+            parseBedHeightEpochs(epochDir);
+        }
 
         log.info("Finished parsing bed heights.");
     }
@@ -701,40 +707,66 @@
 
 
     public void storeBedHeight() {
-        if (!Config.INSTANCE.skipBedHeight()) {
-            log.info("store bed heights");
-            River river = getPeer();
-
-            if (bedHeightSingles != null) {
-                for (ImportBedHeightSingle single: bedHeightSingles) {
-                    String desc = single.getDescription();
+        if (!Config.INSTANCE.skipBedHeightSingle()) {
+            log.info("store bed heights single");
+            storeBedHeightSingle();
+        }
 
-                    log.debug("name: " + desc);
+        if (!Config.INSTANCE.skipBedHeightEpoch()) {
+            log.info("store bed height epoch.");
+            storeBedHeightEpoch();
+        }
+    }
 
-                    try {
-                        single.storeDependencies(river);
-                    }
-                    catch (SQLException sqle) {
-                        log.error("File '" + desc + "' is broken!");
-                    }
-                    catch (ConstraintViolationException cve) {
-                        log.error("File '" + desc + "' is broken!");
-                    }
+
+    private void storeBedHeightSingle() {
+        River river = getPeer();
+
+        if (bedHeightSingles != null) {
+            for (ImportBedHeightSingle single: bedHeightSingles) {
+                String desc = single.getDescription();
+
+                log.debug("name: " + desc);
+
+                try {
+                    single.storeDependencies(river);
+                }
+                catch (SQLException sqle) {
+                    log.error("File '" + desc + "' is broken!");
+                }
+                catch (ConstraintViolationException cve) {
+                    log.error("File '" + desc + "' is broken!");
                 }
             }
-            else {
-                log.info("No single bed heights to store.");
-            }
+        }
+        else {
+            log.info("No single bed heights to store.");
+        }
+    }
 
-            if (bedHeightEpochs != null) {
-                for (ImportBedHeightEpoch epoch: bedHeightEpochs) {
-                    log.debug("name: " + epoch.getDescription());
+
+    private void storeBedHeightEpoch() {
+        River river = getPeer();
+
+        if (bedHeightEpochs != null) {
+            for (ImportBedHeightEpoch epoch: bedHeightEpochs) {
+                String desc = epoch.getDescription();
+
+                log.debug("name: " + desc);
+
+                try {
                     epoch.storeDependencies(river);
                 }
+                catch (SQLException sqle) {
+                    log.error("File '" + desc + "' is broken!");
+                }
+                catch (ConstraintViolationException cve) {
+                    log.error("File '" + desc + "' is broken!");
+                }
             }
-            else {
-                log.info("No epoch bed heights to store.");
-            }
+        }
+        else {
+            log.info("No epoch bed heights to store.");
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/BedHeightEpoch.java	Thu Apr 12 12:50:49 2012 +0000
@@ -0,0 +1,160 @@
+package de.intevation.flys.model;
+
+import java.io.Serializable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Column;
+import javax.persistence.SequenceGenerator;
+import javax.persistence.GenerationType;
+import javax.persistence.JoinColumn;
+import javax.persistence.OneToOne;
+import javax.persistence.OneToMany;
+
+
+@Entity
+@Table(name = "bed_height_epoch")
+public class BedHeightEpoch implements Serializable {
+
+    private Integer id;
+
+    private River river;
+
+    private TimeInterval timeInterval;
+
+    private ElevationModel curElevationModel;
+    private ElevationModel oldElevationModel;
+
+    private Range range;
+
+    private String evaluationBy;
+    private String description;
+
+    private List<BedHeightEpochValue> values;
+
+
+    public BedHeightEpoch() {
+    }
+
+
+    public BedHeightEpoch(
+        River          river,
+        TimeInterval   timeInterval,
+        Range          range,
+        ElevationModel curElevationModel,
+        ElevationModel oldElevationModel,
+        String         evaluationBy,
+        String         description
+    ) {
+        this.river             = river;
+        this.timeInterval      = timeInterval;
+        this.range             = range;
+        this.curElevationModel = curElevationModel;
+        this.oldElevationModel = oldElevationModel;
+        this.evaluationBy      = evaluationBy;
+        this.description       = description;
+        this.values            = new ArrayList<BedHeightEpochValue>();
+    }
+
+
+    @Id
+    @SequenceGenerator(
+        name           = "SEQUENCE_BED_HEIGHT_EPOCH_ID_SEQ",
+        sequenceName   = "BED_HEIGHT_EPOCH_ID_SEQ",
+        allocationSize = 1)
+    @GeneratedValue(
+        strategy  = GenerationType.SEQUENCE,
+        generator = "SEQUENCE_BED_HEIGHT_EPOCH_ID_SEQ")
+    @Column(name = "id")
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    @OneToOne
+    @JoinColumn(name = "river_id" )
+    public River getRiver() {
+        return river;
+    }
+
+    public void setRiver(River river) {
+        this.river = river;
+    }
+
+    @OneToOne
+    @JoinColumn(name = "time_interval_id")
+    public TimeInterval getTimeInterval() {
+        return timeInterval;
+    }
+
+    public void setTimeInterval(TimeInterval timeInterval) {
+        this.timeInterval = timeInterval;
+    }
+
+    @OneToOne
+    @JoinColumn(name = "cur_elevation_model_id")
+    public ElevationModel getCurElevationModel() {
+        return curElevationModel;
+    }
+
+    public void setCurElevationModel(ElevationModel curElevationModel) {
+        this.curElevationModel = curElevationModel;
+    }
+
+    @OneToOne
+    @JoinColumn(name = "old_elevation_model_id")
+    public ElevationModel getOldElevationModel() {
+        return oldElevationModel;
+    }
+
+    public void setOldElevationModel(ElevationModel oldElevationModel) {
+        this.oldElevationModel = oldElevationModel;
+    }
+
+    @OneToOne
+    @JoinColumn(name = "range_id")
+    public Range getRange() {
+        return range;
+    }
+
+    public void setRange(Range range) {
+        this.range = range;
+    }
+
+    @Column(name = "evaluation_by")
+    public String getEvaluationBy() {
+        return evaluationBy;
+    }
+
+    public void setEvaluationBy(String evaluationBy) {
+        this.evaluationBy = evaluationBy;
+    }
+
+    @Column(name = "description")
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    @OneToMany
+    @JoinColumn(name = "bed_height_epoch_id")
+    public List<BedHeightEpochValue> getValues() {
+        return values;
+    }
+
+    public void setValues(List<BedHeightEpochValue> values) {
+        this.values = values;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/model/BedHeightEpochValue.java	Thu Apr 12 12:50:49 2012 +0000
@@ -0,0 +1,93 @@
+package de.intevation.flys.model;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Column;
+import javax.persistence.SequenceGenerator;
+import javax.persistence.GenerationType;
+import javax.persistence.JoinColumn;
+import javax.persistence.OneToOne;
+
+import org.apache.log4j.Logger;
+
+
+@Entity
+@Table(name = "bed_height_epoch_values")
+public class BedHeightEpochValue
+implements   Serializable
+{
+    private static Logger logger =
+        Logger.getLogger(BedHeightEpochValue.class);
+
+    private Integer id;
+
+    private BedHeightEpoch bedHeight;
+
+    private BigDecimal station;
+    private BigDecimal height;
+
+
+    public BedHeightEpochValue() {
+    }
+
+    public BedHeightEpochValue(
+        BedHeightEpoch bedHeight,
+        BigDecimal station,
+        BigDecimal height
+    ) {
+        this.bedHeight = bedHeight;
+        this.station   = station;
+        this.height    = height;
+    }
+
+    @Id
+    @SequenceGenerator(
+        name           = "SEQUENCE_BED_EPOCH_VALUE_ID_SEQ",
+        sequenceName   = "BED_EPOCH_VALUES_ID_SEQ",
+        allocationSize = 1)
+    @GeneratedValue(
+        strategy  = GenerationType.SEQUENCE,
+        generator = "SEQUENCE_BED_EPOCH_VALUE_ID_SEQ")
+    @Column(name = "id")
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    @OneToOne
+    @JoinColumn(name = "bed_height_epoch_id" )
+    public BedHeightEpoch getBedHeight() {
+        return bedHeight;
+    }
+
+    public void setBedHeight(BedHeightEpoch bedHeight) {
+        this.bedHeight = bedHeight;
+    }
+
+    @Column(name = "station")
+    public BigDecimal getStation() {
+        return station;
+    }
+
+    public void setStation(BigDecimal station) {
+        this.station = station;
+    }
+
+    @Column(name = "height")
+    public BigDecimal getHeight() {
+        return height;
+    }
+
+    public void setHeight(BigDecimal height) {
+        this.height = height;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org