changeset 9159:64e56a51db3f

Added methods to select a gauge's discharge table and main value list; added a main value type key enum
author mschaefer
date Tue, 19 Jun 2018 14:23:15 +0200
parents 4a41b7fba4da
children 38e8febd11b6
files backend/src/main/java/org/dive4elements/river/model/DischargeTable.java backend/src/main/java/org/dive4elements/river/model/MainValue.java backend/src/main/java/org/dive4elements/river/model/MainValueType.java
diffstat 3 files changed, 248 insertions(+), 89 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/model/DischargeTable.java	Tue Jun 19 14:20:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/DischargeTable.java	Tue Jun 19 14:23:15 2018 +0200
@@ -23,10 +23,11 @@
 import javax.persistence.OrderBy;
 import javax.persistence.SequenceGenerator;
 import javax.persistence.Table;
+import javax.persistence.Transient;
 
+import org.dive4elements.river.backend.SessionHolder;
+import org.hibernate.Query;
 import org.hibernate.Session;
-import org.hibernate.Query;
-import org.dive4elements.river.backend.SessionHolder;
 
 @Entity
 @Table(name = "discharge_tables")
@@ -43,20 +44,20 @@
     private List<DischargeTableValue> dischargeTableValues;
 
     public DischargeTable() {
-        kind = 0;
+        this.kind = 0;
     }
 
-    public DischargeTable(Gauge gauge) {
+    public DischargeTable(final Gauge gauge) {
         this(gauge, null, null, 0, null);
     }
 
     public DischargeTable(
-        Gauge        gauge,
-        String       description,
-        String       bfgId,
-        Integer      kind,
-        TimeInterval timeInterval
-    ) {
+            final Gauge        gauge,
+            final String       description,
+            final String       bfgId,
+            final Integer      kind,
+            final TimeInterval timeInterval
+            ) {
         this.gauge        = gauge;
         this.description  = description;
         this.bfgId        = bfgId;
@@ -66,65 +67,65 @@
 
     @Id
     @SequenceGenerator(
-        name           = "SEQUENCE_DISCHARGE_TABLES_ID_SEQ",
-        sequenceName   = "DISCHARGE_TABLES_ID_SEQ",
-        allocationSize = 1)
+            name           = "SEQUENCE_DISCHARGE_TABLES_ID_SEQ",
+            sequenceName   = "DISCHARGE_TABLES_ID_SEQ",
+            allocationSize = 1)
     @GeneratedValue(
-        strategy  = GenerationType.SEQUENCE,
-        generator = "SEQUENCE_DISCHARGE_TABLES_ID_SEQ")
+            strategy  = GenerationType.SEQUENCE,
+            generator = "SEQUENCE_DISCHARGE_TABLES_ID_SEQ")
     @Column(name = "id")
     public Integer getId() {
-        return id;
+        return this.id;
     }
 
-    public void setId(Integer id) {
+    public void setId(final Integer id) {
         this.id = id;
     }
 
     @OneToOne
     @JoinColumn(name = "gauge_id")
     public Gauge getGauge() {
-        return gauge;
+        return this.gauge;
     }
 
-    public void setGauge(Gauge gauge) {
+    public void setGauge(final Gauge gauge) {
         this.gauge = gauge;
     }
 
     @Column(name = "description")
     public String getDescription() {
-        return description;
+        return this.description;
     }
 
-    public void setDescription(String description) {
+    public void setDescription(final String description) {
         this.description = description;
     }
 
     @Column(name = "bfg_id")
     public String getBfgId() {
-        return bfgId;
+        return this.bfgId;
     }
 
-    public void setBfgId(String bfgId) {
+    public void setBfgId(final String bfgId) {
         this.bfgId = bfgId;
     }
 
     @Column(name = "kind")
     public Integer getKind() {
-        return kind;
+        return this.kind;
     }
 
-    public void setKind(Integer kind) {
+    public void setKind(final Integer kind) {
         this.kind = kind;
     }
 
     @OneToOne
     @JoinColumn(name = "time_interval_id")
     public TimeInterval getTimeInterval() {
-        return timeInterval;
+        return this.timeInterval;
     }
 
-    public void setTimeInterval(TimeInterval timeInterval) {
+    public void setTimeInterval(final TimeInterval timeInterval) {
         this.timeInterval = timeInterval;
     }
 
@@ -132,34 +133,43 @@
     @JoinColumn(name = "table_id")
     @OrderBy("q")
     public List<DischargeTableValue> getDischargeTableValues() {
-        return dischargeTableValues;
+        return this.dischargeTableValues;
     }
 
     public void setDischargeTableValues(
-        List<DischargeTableValue> dischargeTableValues
-    ) {
+            final List<DischargeTableValue> dischargeTableValues
+            ) {
         this.dischargeTableValues = dischargeTableValues;
     }
 
+    @Transient
+    public double[] getWs() {
+        final double[] ws = new double[this.dischargeTableValues.size()];
+        for (int i=0; i<=this.dischargeTableValues.size()-1; i++)
+            ws[i] = this.dischargeTableValues.get(i).getW().doubleValue();
+
+        return ws;
+    }
+
     @Override
-    public int compareTo(DischargeTable o) {
+    public int compareTo(final DischargeTable o) {
         if (getKind() == 0 && o.getKind() != 0) {
             return 1;
         }
 
-        TimeInterval other = o.getTimeInterval();
-        if (other == null && timeInterval == null) {
+        final TimeInterval other = o.getTimeInterval();
+        if (other == null && this.timeInterval == null) {
             return 1;
         }
         else if (other == null) {
             return -1;
         }
-        else if (timeInterval == null) {
+        else if (this.timeInterval == null) {
             return 1;
         }
 
-        Date otherStartTime = other.getStartTime();
-        Date thisStartTime  = timeInterval.getStartTime();
+        final Date otherStartTime = other.getStartTime();
+        final Date thisStartTime  = this.timeInterval.getStartTime();
 
         if (otherStartTime == null) {
             return -1;
@@ -168,8 +178,8 @@
             return 1;
         }
 
-        long otherStart = otherStartTime.getTime();
-        long thisStart  = thisStartTime.getTime();
+        final long otherStart = otherStartTime.getTime();
+        final long thisStart  = thisStartTime.getTime();
 
         if (otherStart < thisStart) {
             return 1;
@@ -178,8 +188,8 @@
             return -1;
         }
 
-        Date otherStopTime  = other.getStopTime();
-        Date thisStopTime  = timeInterval.getStopTime();
+        final Date otherStopTime  = other.getStopTime();
+        final Date thisStopTime  = this.timeInterval.getStopTime();
 
         if (otherStopTime == null) {
             return -1;
@@ -188,8 +198,8 @@
             return 1;
         }
 
-        long otherEnd   = otherStopTime.getTime();
-        long thisEnd    = thisStopTime.getTime();
+        final long otherEnd   = otherStopTime.getTime();
+        final long thisEnd    = thisStopTime.getTime();
 
         if (otherEnd < thisEnd) {
             return 1;
@@ -202,15 +212,35 @@
         }
     }
 
-    public static DischargeTable getDischargeTableById(int dtId)
+    public static DischargeTable getDischargeTableById(final int dtId)
     {
-        Session session = SessionHolder.HOLDER.get();
-        Query query = session.createQuery(
-            "from DischargeTable where id =:dtId");
+        final Session session = SessionHolder.HOLDER.get();
+        final Query query = session.createQuery("from DischargeTable where id =:dtId");
         query.setParameter("dtId", dtId);
 
-        List<DischargeTable> list = query.list();
+        final List<DischargeTable> list = query.list();
         return list.isEmpty() ? null : list.get(0);
     }
+
+    /**
+     * Selects from the database the main discharge table of a gauge
+     */
+    public static DischargeTable getGaugeMainDischargeTable(final Gauge gauge) {
+        final Session session = SessionHolder.HOLDER.get();
+        final Query query = session.createQuery("FROM DischargeTable WHERE gauge_id=:gaugeid AND kind=0");
+        query.setParameter("gaugeid", gauge.getId());
+        final List<DischargeTable> list = query.list();
+        return list.isEmpty() ? null : list.get(0);
+    }
+
+    /**
+     * Selects from the database the values of a discharge table sorted by W
+     */
+    public static List<DischargeTableValue> getValuesSortedByW(final DischargeTable dischargeTable) {
+        final Session session = SessionHolder.HOLDER.get();
+        final Query query = session.createQuery("FROM DischargeTableValue WHERE table_id=:parentid ORDER BY w");
+        query.setParameter("parentid", dischargeTable.getId());
+        return query.list();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/model/MainValue.java	Tue Jun 19 14:20:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/MainValue.java	Tue Jun 19 14:23:15 2018 +0200
@@ -9,18 +9,23 @@
 package org.dive4elements.river.model;
 
 import java.io.Serializable;
+import java.math.BigDecimal;
+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.OneToOne;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
 import javax.persistence.JoinColumn;
-import javax.persistence.GenerationType;
+import javax.persistence.OneToOne;
+import javax.persistence.SequenceGenerator;
+import javax.persistence.Table;
 
-import java.math.BigDecimal;
+import org.dive4elements.river.backend.SessionHolder;
+import org.dive4elements.river.model.MainValueType.MainValueTypeKey;
+import org.hibernate.Query;
+import org.hibernate.Session;
 
 
 /** A Main or Extreme value of a rivers gauge. */
@@ -43,11 +48,11 @@
     }
 
     public MainValue(
-        Gauge          gauge,
-        NamedMainValue mainValue,
-        BigDecimal     value,
-        TimeInterval   timeInterval
-    ) {
+            final Gauge          gauge,
+            final NamedMainValue mainValue,
+            final BigDecimal     value,
+            final TimeInterval   timeInterval
+            ) {
         this.gauge        = gauge;
         this.mainValue    = mainValue;
         this.value        = value;
@@ -56,58 +61,72 @@
 
     @Id
     @SequenceGenerator(
-        name           = "SEQUENCE_MAIN_VALUES_ID_SEQ",
-        sequenceName   = "MAIN_VALUES_ID_SEQ",
-        allocationSize = 1)
+            name           = "SEQUENCE_MAIN_VALUES_ID_SEQ",
+            sequenceName   = "MAIN_VALUES_ID_SEQ",
+            allocationSize = 1)
     @GeneratedValue(
-        strategy  = GenerationType.SEQUENCE,
-        generator = "SEQUENCE_MAIN_VALUES_ID_SEQ")
+            strategy  = GenerationType.SEQUENCE,
+            generator = "SEQUENCE_MAIN_VALUES_ID_SEQ")
     @Column(name = "id")
     public Integer getId() {
-        return id;
+        return this.id;
     }
 
-    public void setId(Integer id) {
+    public void setId(final Integer id) {
         this.id = id;
     }
 
     @OneToOne
     @JoinColumn(name = "gauge_id")
     public Gauge getGauge() {
-        return gauge;
+        return this.gauge;
     }
 
-    public void setGauge(Gauge gauge) {
+    public void setGauge(final Gauge gauge) {
         this.gauge = gauge;
     }
 
     @OneToOne
     @JoinColumn(name = "named_value_id")
     public NamedMainValue getMainValue() {
-        return mainValue;
+        return this.mainValue;
     }
 
-    public void setMainValue(NamedMainValue mainValue) {
+    public void setMainValue(final NamedMainValue mainValue) {
         this.mainValue = mainValue;
     }
 
     @Column(name = "value") // FIXME: type mapping needed?
     public BigDecimal getValue() {
-        return value;
+        return this.value;
     }
 
-    public void setValue(BigDecimal value) {
+    public void setValue(final BigDecimal value) {
         this.value = value;
     }
 
     @OneToOne
     @JoinColumn(name = "time_interval_id")
     public TimeInterval getTimeInterval() {
-        return timeInterval;
+        return this.timeInterval;
     }
 
-    public void setTimeInterval(TimeInterval timeInterval) {
+    public void setTimeInterval(final TimeInterval timeInterval) {
         this.timeInterval = timeInterval;
     }
+
+    /**
+     * Selects from the database the main values of a gauge and a type, in ascending value order
+     */
+    public static List<MainValue> getValuesOfGaugeAndType(final Gauge gauge, final MainValueTypeKey typekey) {
+        final Session session = SessionHolder.HOLDER.get();
+        final Query query = session.createQuery("SELECT mv"
+                + " FROM MainValue AS mv JOIN mv.mainValue AS nmv"
+                + " WHERE mv.gauge.id=:gaugeid AND nmv.type.id=:typeid"
+                + " ORDER BY value");
+        query.setParameter("gaugeid", gauge.getId());
+        query.setParameter("typeid", typekey.getId());
+        return query.list();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/backend/src/main/java/org/dive4elements/river/model/MainValueType.java	Tue Jun 19 14:20:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/MainValueType.java	Tue Jun 19 14:23:15 2018 +0200
@@ -9,53 +9,163 @@
 package org.dive4elements.river.model;
 
 import java.io.Serializable;
+import java.util.List;
 
+import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.SequenceGenerator;
 import javax.persistence.Table;
-import javax.persistence.GeneratedValue;
-import javax.persistence.Column;
-import javax.persistence.SequenceGenerator;
-import javax.persistence.GenerationType;
+
+import org.dive4elements.river.backend.SessionHolder;
+import org.hibernate.Query;
+import org.hibernate.Session;
 
 @Entity
 @Table(name = "main_value_types")
 public class MainValueType
 implements   Serializable
 {
+
+    /***** TYPES *****/
+
+    /**
+     * Main value type names
+     *
+     */
+    public enum MainValueTypeKey {
+        NONE(""), W("W"), Q("Q"), DURATION("D"), UNKNOWN("-");
+
+        private final String name;
+        private int id;
+        private boolean ready;
+
+        MainValueTypeKey(final String name) {
+            this.name = name;
+            this.id = 0;
+            this.ready = false;
+        }
+
+        /**
+         * Type name in the database
+         */
+        public String getName() {
+            return this.name;
+        }
+
+        /**
+         * Type id in the database
+         */
+        public int getId() {
+            initFromDatabase();
+            return this.id;
+        }
+
+        /**
+         * Set the type id
+         */
+        public void setId(final int id) {
+            this.id = id;
+        }
+
+        protected boolean getReady() {
+            return this.ready;
+        }
+
+        protected void setReady(final boolean ready) {
+            this.ready = ready;
+        }
+
+        /**
+         * Main value type key for a database name value
+         */
+        public static MainValueTypeKey forDbName(final String dbname) {
+            initFromDatabase();
+            for (final MainValueTypeKey k : MainValueTypeKey.values()) {
+                if (k.getName().equalsIgnoreCase(dbname))
+                    return k;
+            }
+            return NONE;
+        }
+
+        /**
+         * Main value type key for a database id value
+         */
+        public static MainValueTypeKey forDbId(final int dbid) {
+            initFromDatabase();
+            for (final MainValueTypeKey k : MainValueTypeKey.values()) {
+                if (k.getId() == dbid)
+                    return k;
+            }
+            return NONE;
+        }
+
+        /**
+         * Initially queries the database ids
+         */
+        private static void initFromDatabase() {
+            if (W.getReady())
+                return;
+            // Avoid recursion
+            for (final MainValueTypeKey k : MainValueTypeKey.values())
+                k.setReady(true);
+            // Select database ids
+            final Session session = SessionHolder.HOLDER.get();
+            final Query query = session.createQuery("FROM MainValueType");
+            final List<MainValueType> rows = query.list();
+            if (!rows.isEmpty()) {
+                for (int i = 0; i <= rows.size() - 1; i++) {
+                    if (forDbName(rows.get(i).getName()) != NONE)
+                        forDbName(rows.get(i).getName()).setId(rows.get(i).getId());
+                }
+            }
+        }
+    }
+
+    /***** FIELDS *****/
+
     private Integer id;
     private String  name;
+    // static private List<Integer> typeids;
+    // static private List<String> typenames;
+
+    /***** CONSTRUCTORS *****/
 
     public MainValueType() {
     }
 
-    public MainValueType(String name) {
+    public MainValueType(final String name) {
         this.name = name;
     }
 
+
+    /***** METHODS *****/
+
     @Id
     @SequenceGenerator(
-        name           = "SEQUENCE_MAIN_VALUE_TYPES_ID_SEQ",
-        sequenceName   = "MAIN_VALUE_TYPES_ID_SEQ",
-        allocationSize = 1)
+            name           = "SEQUENCE_MAIN_VALUE_TYPES_ID_SEQ",
+            sequenceName   = "MAIN_VALUE_TYPES_ID_SEQ",
+            allocationSize = 1)
     @GeneratedValue(
-        strategy  = GenerationType.SEQUENCE,
-        generator = "SEQUENCE_MAIN_VALUE_TYPES_ID_SEQ")
+            strategy  = GenerationType.SEQUENCE,
+            generator = "SEQUENCE_MAIN_VALUE_TYPES_ID_SEQ")
     @Column(name = "id")
     public Integer getId() {
-        return id;
+        return this.id;
     }
 
-    public void setId(Integer id) {
+    public void setId(final Integer id) {
         this.id = id;
     }
 
     @Column(name = "name") // FIXME: Type conversion needed?
     public String getName() {
-        return name;
+        return this.name;
     }
 
-    public void setName(String name) {
+    public void setName(final String name) {
         this.name = name;
     }
 }

http://dive4elements.wald.intevation.org