diff backend/src/main/java/org/dive4elements/river/model/MainValue.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 4c3ccf2b0304
children 1614cb14308f
line wrap: on
line diff
--- 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 :

http://dive4elements.wald.intevation.org