[PATCH 1 of 2] Fetching year informations for waterlevels used in sinfo

Wald Commits scm-commit at wald.intevation.org
Wed Mar 28 17:04:26 CEST 2018


# HG changeset patch
# User gernotbelger
# Date 1522249391 -7200
# Node ID 708f210ff242b514c840c3cae9ddd173ee5f5951
# Parent  8a1c6e2ad48b23b0ef94e9ae3842dbcf9cc4bd0d
Fetching year informations for waterlevels used in sinfo

diff -r 8a1c6e2ad48b -r 708f210ff242 artifacts/src/main/java/org/dive4elements/river/artifacts/model/WstColumnFactory.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WstColumnFactory.java	Wed Mar 28 17:03:11 2018 +0200
@@ -0,0 +1,41 @@
+/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
+ * Software engineering by
+ *  Björnsen Beratende Ingenieure GmbH
+ *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+package org.dive4elements.river.artifacts.model;
+
+import java.util.List;
+
+import org.dive4elements.river.backend.SessionHolder;
+import org.dive4elements.river.model.Wst;
+import org.dive4elements.river.model.WstColumn;
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+/**
+ * @author Gernot Belger
+ */
+public final class WstColumnFactory {
+    private WstColumnFactory() {
+        throw new UnsupportedOperationException("helper class");
+    }
+
+    public static WstColumn getWstColumn(final int wst_id, final int columnPosition) {
+        final Session session = SessionHolder.HOLDER.get();
+
+        final Wst wst = WstFactory.getWst(wst_id);
+
+        final Query query = session.createQuery("from WstColumn where wst=:wst_id and position = :position");
+        query.setParameter("wst_id", wst);
+        query.setInteger("position", columnPosition);
+
+        final List<WstColumn> columns = query.list();
+
+        return columns.isEmpty() ? null : columns.get(0);
+    }
+}
\ No newline at end of file
diff -r 8a1c6e2ad48b -r 708f210ff242 artifacts/src/main/java/org/dive4elements/river/artifacts/model/WstFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WstFactory.java	Wed Mar 28 14:35:01 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WstFactory.java	Wed Mar 28 17:03:11 2018 +0200
@@ -52,5 +52,17 @@
 
         return wsts.isEmpty() ? null : wsts.get(0);
     }
+
+
+    public static Wst getWst(final int id) {
+        final Session session = SessionHolder.HOLDER.get();
+
+        final Query query = session.createQuery("from Wst where id=:id");
+        query.setInteger("id", id);
+
+        final List<Wst> wsts = query.list();
+
+        return wsts.isEmpty() ? null : wsts.get(0);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
diff -r 8a1c6e2ad48b -r 708f210ff242 artifacts/src/main/java/org/dive4elements/river/artifacts/states/WaterlevelFetcher.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/WaterlevelFetcher.java	Wed Mar 28 14:35:01 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/WaterlevelFetcher.java	Wed Mar 28 17:03:11 2018 +0200
@@ -9,6 +9,8 @@
  */
 package org.dive4elements.river.artifacts.states;
 
+import java.util.Calendar;
+import java.util.Date;
 import java.util.List;
 
 import org.apache.log4j.Logger;
@@ -24,9 +26,12 @@
 import org.dive4elements.river.artifacts.model.Segment;
 import org.dive4elements.river.artifacts.model.WKms;
 import org.dive4elements.river.artifacts.model.WQKms;
+import org.dive4elements.river.artifacts.model.WstColumnFactory;
 import org.dive4elements.river.artifacts.model.fixings.FixRealizingCalculationExtended;
 import org.dive4elements.river.artifacts.model.fixings.FixRealizingResult;
 import org.dive4elements.river.artifacts.states.DefaultState.ComputeType;
+import org.dive4elements.river.model.TimeInterval;
+import org.dive4elements.river.model.WstColumn;
 import org.dive4elements.river.utils.RiverUtils;
 
 /**
@@ -82,8 +87,9 @@
         return null;
     }
 
-    // REMARK/TODO: instead of several helper methods here this would be a good place for abstraction, in order to push
-    // this logic back to the corresponding artifacts
+    // REMARK: instead of several helper methods here this would be a good place for abstraction, in order to push
+    // this logic back to the corresponding artifacts. However this will most certainly break existing
+    // artifact-serialization
 
     private WaterlevelData fetchStaticWKmsArtifactWaterlevel(final StaticWKmsArtifact staticWKms, final int idx,
             final double from, final double to) {
@@ -94,8 +100,7 @@
 
         if (wkms != null)
         {
-            // FIXME: woher bekommen?: eventuell zusammenhang mit tabelle 'time_intervals'
-            final int year = -1;
+            final int year = fetchStaticWKmsYear(staticWKms);
             return new WaterlevelData(wkms, year);
         }
 
@@ -112,8 +117,7 @@
 
         if (wkms != null)
         {
-            // FIXME: woher bekommen?: eventuell zusammenhang mit tabelle 'time_intervals'
-            final int year = -1;
+            final int year = fetchStaticWKmsYear(staticWKms);
             return new WaterlevelData(wkms, year);
         }
 
@@ -164,4 +168,32 @@
 
         return new WaterlevelData(frR.getWQKms()[idx], year, showAllGauges).filterByRange(from, to);
     }
+
+    /**
+     * Fetches the 'year' for a staticXXX-artifact.
+     * REMARK: actually this should happen inside the staticWKms artifact and eventually in the WKmsFactory, but the code
+     * there is already awful and it will also break the old artifact-serialization...
+     */
+    private int fetchStaticWKmsYear(final D4EArtifact staticWKms) {
+
+        final int colPos = Integer.parseInt(staticWKms.getDataAsString("col_pos"));
+        final int wstId = Integer.parseInt(staticWKms.getDataAsString("wst_id"));
+
+        final WstColumn wstColumn = WstColumnFactory.getWstColumn(wstId, colPos);
+        final TimeInterval timeInterval = wstColumn.getTimeInterval();
+        if (timeInterval == null)
+            return -1;
+
+        final Date startTime = timeInterval.getStartTime();
+        if (startTime == null)
+            return -1;
+
+        // REMARK: the times are stored without timezone in the DB, so it is unclear what hibernate makes of it.
+        // We simply use the default timezone here and hope we never get problems...
+        // Actually we always have 12:00 as time in the db data, so a smal timeshift due to winter/sommertime or UTC/GMT+1 will
+        // no change anything regarding the year.
+        final Calendar cal = Calendar.getInstance();
+        cal.setTime(startTime);
+        return cal.get(Calendar.YEAR);
+    }
 }
\ No newline at end of file


More information about the Dive4Elements-commits mailing list