diff backend/src/main/java/org/dive4elements/river/model/DischargeTable.java @ 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 ff27548d078c
children 491e1a434457
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 :

http://dive4elements.wald.intevation.org