[PATCH] Nachtrag Pos. 20: infrastructure query added, calculation updated for extended access class

Wald Commits scm-commit at wald.intevation.org
Mon Oct 14 08:20:48 CEST 2019


# HG changeset patch
# User mschaefer
# Date 1571033698 -7200
#      Mon Oct 14 08:14:58 2019 +0200
# Node ID 07f02019065ebefd09cb83f92d1606bd02dea983
# Parent  02ca823ec9c6400b66dbe45204713f404357190d
Nachtrag Pos. 20: infrastructure query added, calculation updated for extended access class

diff -r 02ca823ec9c6 -r 07f02019065e artifacts/doc/conf/meta-data.xml
--- a/artifacts/doc/conf/meta-data.xml	Fri Oct 11 18:30:36 2019 +0200
+++ b/artifacts/doc/conf/meta-data.xml	Mon Oct 14 08:14:58 2019 +0200
@@ -1418,23 +1418,84 @@
     <dc:macro name="predefined_infrastructures">
       <infrastructures TODO="i10n">
         <dc:context connection="system">
-          <dc:statement>
-            SELECT *
-            FROM infrastructure
-            WHERE river_id = ${river_id}
-          </dc:statement>
-          <dc:group expr="$group_id">
-            <infrastructure_group description="{dc:group-key()}">
-              <dc:for-each>
-                <!-- name="{$annotation_type_id}" -->
-                 <!-- not necessary: target_out="{$out}" -->
-                <infrastructure_type description="{$FILENAME}" factory="FAKE_FACTORY" group_label="{$group_id}" group_id="{$group_id}" type_id="{$annotation_type_id}" type_label="{$annotation_type_id}" />
-              </dc:for-each>
-            </infrastructure_group>
-          </dc:group>
+          <!--dc:choose>
+            <dc:when test="dc:contains($riverside, 'left')">
+              <dc:call-macro name="infrastructures_leftside_statement" />
+              <dc:call-macro name="infrastructures_groups_fill" />
+            </dc:when>
+            <dc:when test="dc:contains($riverside, 'right')">
+              <dc:call-macro name="infrastructures_rightside_statement" />
+              <dc:call-macro name="infrastructures_groups_fill" />
+            </dc:when>
+            <dc:otherwise>
+              <dc:call-macro name="infrastructures_bothsides_statement" />
+              <dc:call-macro name="infrastructures_groups_fill" />
+            </dc:otherwise>
+          </dc:choose-->
+          <dc:call-macro name="infrastructures_bothsides_statement" />
+          <dc:call-macro name="infrastructures_groups_fill" />
         </dc:context>
       </infrastructures>
     </dc:macro>
+    
+    <dc:macro name="infrastructures_groups_fill">
+      <dc:group expr="$group_label">
+        <infrastructure_group name="{dc:group-key()}" description="{dc:group-key()}">
+          <dc:for-each>
+            <!-- name="{$annotation_type_id}" -->
+             <!-- not necessary: target_out="{$out}" -->
+            <infrastructure_type name="{$type_label}" description="{$filename}" factory="FAKE_FACTORY" group_label="{$group_label}" group_id="{$group_id}" type_id="{$type_id}" type_label="{$type_label}" />
+          </dc:for-each>
+        </infrastructure_group>
+      </dc:group>
+    </dc:macro>
+    
+    <dc:macro name="infrastructures_leftside_statement">
+      <dc:statement>
+          SELECT s.id, MIN(s.filename) AS filename, MIN(s.group_id) AS group_id, MIN(g.name) AS group_label, 
+            MIN(s.annotation_type_id) AS type_id, MIN(t.name) AS type_label
+          FROM (((infrastructure AS s INNER JOIN infrastructure_values AS v ON v.infrastructure_id = s.id)
+            INNER JOIN annotation_types AS g ON s.group_id = g.id)
+            INNER JOIN annotation_types AS t ON s.annotation_type_id = t.id)
+            INNER JOIN attributes AS rs ON v.attribute_id = rs.id
+          WHERE (s.river_id = ${river_id})
+            AND (v.station BETWEEN ${fromkm}-0.0001 AND ${tokm}+0.0001)
+            AND (lower(rs.value) = 'links')
+          GROUP BY s.id
+          ORDER BY MIN(g.name), MIN(t.name)
+      </dc:statement>
+    </dc:macro>
+
+    <dc:macro name="infrastructures_rightside_statement">
+      <dc:statement>
+          SELECT s.id, MIN(s.filename) AS filename, MIN(s.group_id) AS group_id, MIN(g.name) AS group_label, 
+            MIN(s.annotation_type_id) AS type_id, MIN(t.name) AS type_label
+          FROM (((infrastructure AS s INNER JOIN infrastructure_values AS v ON v.infrastructure_id = s.id)
+            INNER JOIN annotation_types AS g ON s.group_id = g.id)
+            INNER JOIN annotation_types AS t ON s.annotation_type_id = t.id)
+            INNER JOIN attributes AS rs ON v.attribute_id = rs.id
+          WHERE (s.river_id = ${river_id})
+            AND (v.station BETWEEN ${fromkm}-0.0001 AND ${tokm}+0.0001)
+            AND (lower(rs.value) = 'rechts')
+          GROUP BY s.id
+          ORDER BY MIN(g.name), MIN(t.name)
+      </dc:statement>
+    </dc:macro>
+
+    <dc:macro name="infrastructures_bothsides_statement">
+      <dc:statement>
+          SELECT s.id, MIN(s.filename) AS filename, MIN(s.group_id) AS group_id, MIN(g.name) AS group_label, 
+            MIN(s.annotation_type_id) AS type_id, MIN(t.name) AS type_label
+          FROM (((infrastructure AS s INNER JOIN infrastructure_values AS v ON v.infrastructure_id = s.id)
+            INNER JOIN annotation_types AS g ON s.group_id = g.id)
+            INNER JOIN annotation_types AS t ON s.annotation_type_id = t.id)
+            INNER JOIN attributes AS rs ON v.attribute_id = rs.id
+          WHERE (s.river_id = ${river_id})
+            AND (v.station BETWEEN ${fromkm}-0.0001 AND ${tokm}+0.0001)
+          GROUP BY s.id
+          ORDER BY MIN(g.name), MIN(t.name)
+      </dc:statement>
+    </dc:macro>
 
     <!-- wst-data -->
 
diff -r 02ca823ec9c6 -r 07f02019065e artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculation.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculation.java	Fri Oct 11 18:30:36 2019 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculation.java	Mon Oct 14 08:14:58 2019 +0200
@@ -10,7 +10,6 @@
 package org.dive4elements.river.artifacts.sinfo.flood_duration;
 
 import java.util.List;
-import java.util.Set;
 
 import org.apache.commons.lang.math.DoubleRange;
 import org.dive4elements.artifacts.CallContext;
@@ -26,7 +25,6 @@
 import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils;
 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo;
 import org.dive4elements.river.model.River;
-import org.dive4elements.river.model.sinfo.Infrastructure;
 
 /**
  * Calculation of the flood durations of the infrastructures of the km range of a river
@@ -78,14 +76,13 @@
 
         final FloodDurationCalculator calculator = new FloodDurationCalculator(this.context, riverInfoProvider);
 
-        // FIXME: fetch from access (fetchInfrastructureTypes only for test purposes)
-        final List<Element> infrastructureChoicesnew = access.getInfrastructureChoices();
-        // int groupID =;
-        // int annottionTypeId =;
+        final List<Element> infrastructureChoices = access.getInfrastructureChoices();
 
-        // Deprecated denke ich.
-        final Set<String> infrastructureChoices = Infrastructure.fetchInfrastructureTypes(riverInfoProvider.getRiver(), calcRange.getMinimumDouble(),
-                calcRange.getMaximumDouble(), access.getRiverside().getAttributeKey());
+        /*
+         * zum testen:
+         * final Set<String> infrastructureChoicesTest = Infrastructure.fetchInfrastructureTypes(riverInfoProvider.getRiver(),
+         * calcRange.getMinimumDouble(), calcRange.getMaximumDouble(), access.getRiverside().getAttributeKey());
+         */
 
         calculator.execute(problems, label, calcRange, access.getRiverside().getAttributeKey(), infrastructureChoices, access.getIsWspl(), winfo, results);
     }
diff -r 02ca823ec9c6 -r 07f02019065e artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculator.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculator.java	Fri Oct 11 18:30:36 2019 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flood_duration/FloodDurationCalculator.java	Mon Oct 14 08:14:58 2019 +0200
@@ -33,6 +33,7 @@
 import org.dive4elements.river.artifacts.model.river.RiverInfoProvider;
 import org.dive4elements.river.artifacts.sinfo.common.GaugeDurationValuesFinder;
 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType;
+import org.dive4elements.river.artifacts.sinfo.flood_duration.InfrastructureServerClientXChange.Element;
 import org.dive4elements.river.exports.WaterlevelDescriptionBuilder;
 import org.dive4elements.river.model.Attribute.AttributeKey;
 import org.dive4elements.river.model.Gauge;
@@ -67,7 +68,7 @@
      * Calculate the infrastructures flood duration result rows
      */
     public void execute(final Calculation problems, final String label, final DoubleRange calcRange, final AttributeKey riverside,
-            final Set<String> infrastructureChoices, final boolean withWspl, final WINFOArtifact winfo, final FloodDurationCalculationResults results) {
+            final List<Element> infrastructureChoices, final boolean withWspl, final WINFOArtifact winfo, final FloodDurationCalculationResults results) {
 
         // Find all gauges of the calc range, and create the duration finders
         final Map<Gauge, GaugeDurationValuesFinder> durFinders = new HashMap<>();
@@ -76,8 +77,11 @@
         }
 
         // Find all infrastructures within the calc range
+        final Set<String> choices = new HashSet<>();
+        for (final Element ifch : infrastructureChoices)
+            choices.add(ifch.getGroupLabel() + "\t" + ifch.getTypeLabel());
         final List<InfrastructureValue> infras = InfrastructureValue.getValues(this.riverInfoProvider.getRiver(), calcRange.getMinimumDouble(),
-                calcRange.getMaximumDouble(), riverside, infrastructureChoices);
+                calcRange.getMaximumDouble(), riverside, choices);
 
         // Merge all stations (range/step, borders of gauge ranges, infrastructures)
         // final Map<Double, InfrastructureValue> allStations = new HashMap<>();
diff -r 02ca823ec9c6 -r 07f02019065e backend/src/main/java/org/dive4elements/river/model/sinfo/Infrastructure.java
--- a/backend/src/main/java/org/dive4elements/river/model/sinfo/Infrastructure.java	Fri Oct 11 18:30:36 2019 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/sinfo/Infrastructure.java	Mon Oct 14 08:14:58 2019 +0200
@@ -71,11 +71,13 @@
 
     private List<InfrastructureValue> values;
 
+
     /***** CONSTRUCTORS *****/
 
     public Infrastructure() {
     }
 
+
     public Infrastructure(final River river, final String filename, final String kmrange_info, final String notes, final AnnotationType type,
             final AnnotationType group, final Integer year, final String dataprovider, final String evaluation_by) {
         this.river = river;
@@ -90,6 +92,7 @@
         this.values = new ArrayList<>();
     }
 
+
     /***** METHODS *****/
 
     @Id
@@ -230,8 +233,8 @@
      * Fetches from the database the infrastructure groups and types of a river's km range and river side(s)
      * as tab-separated strings ordered by the infrastructure group, then the type
      */
-    @Deprecated
-    public static Set<String> fetchInfrastructureTypes(final River river, final double kmLo, final double kmHi, final AttributeKey riverside) {
+    public static Set<String> fetchInfrastructureTypes(final River river, final double kmLo, final double kmHi,
+            final AttributeKey riverside) {
         final Set<String> groupTypes = new HashSet<>();
         for (final Infrastructure ifs : fetchInfrastructureTypeList(river, kmLo, kmHi, riverside))
             groupTypes.add(ifs.getGroup().getName() + "\t" + ifs.getType().getName());
@@ -242,13 +245,18 @@
      * Fetches from the database the list of infrastructure types of a river's km range and river side(s)
      * ordered by type group
      */
-    private static List<Infrastructure> fetchInfrastructureTypeList(final River river, final double kmLo, final double kmHi, final AttributeKey riverside) {
+    private static List<Infrastructure> fetchInfrastructureTypeList(final River river, final double kmLo, final double kmHi,
+            final AttributeKey riverside) {
 
         final Session session = SessionHolder.HOLDER.get();
 
-        final Query query = session.createQuery("FROM Infrastructure" + " WHERE (river=:river)"
-                + " AND (id IN (SELECT v.infrastructure.id FROM InfrastructureValue v" + "  WHERE (v.station BETWEEN (:kmLo - 0.0001) AND (:kmHi + 0.0001))"
-                + InfrastructureValue.getRiversideClause(riverside, "v.", "attr_id") + "))" + " ORDER BY group, type");
+        final Query query = session.createQuery("FROM Infrastructure"
+                + " WHERE (river=:river)"
+                + " AND (id IN (SELECT v.infrastructure.id FROM InfrastructureValue v"
+                + "  WHERE (v.station BETWEEN (:kmLo - 0.0001) AND (:kmHi + 0.0001))"
+                + InfrastructureValue.getRiversideClause(riverside, "v.", "attr_id")
+                + "))"
+                + " ORDER BY group, type");
         query.setParameter("river", river);
         query.setParameter("kmLo", new Double(kmLo));
         query.setParameter("kmHi", new Double(kmHi));
diff -r 02ca823ec9c6 -r 07f02019065e backend/src/main/java/org/dive4elements/river/model/sinfo/InfrastructureValue.java
--- a/backend/src/main/java/org/dive4elements/river/model/sinfo/InfrastructureValue.java	Fri Oct 11 18:30:36 2019 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/sinfo/InfrastructureValue.java	Mon Oct 14 08:14:58 2019 +0200
@@ -32,6 +32,7 @@
 import org.hibernate.Query;
 import org.hibernate.Session;
 
+
 /**
  * Hibernate binding for the DB table infrastructure_values
  *
@@ -56,6 +57,7 @@
 
     private Double height;
 
+
     /***** CONSTRUCTORS *****/
 
     public InfrastructureValue() {
@@ -75,6 +77,7 @@
         this(infrastructure, Double.valueOf(km), attribute, Double.valueOf(height));
     }
 
+
     /***** METHODS *****/
 
     @Id
@@ -137,8 +140,8 @@
      */
     public static List<InfrastructureValue> getValues(final Infrastructure parent, final double kmLo, final double kmHi) {
         final Session session = SessionHolder.HOLDER.get();
-        final Query query = session
-                .createQuery("FROM InfrastructureValue WHERE (infrastructure=:parent)" + " AND (station >= :kmLo - 0.0001) AND (station <= :kmHi + 0.0001)");
+        final Query query = session.createQuery("FROM InfrastructureValue WHERE (infrastructure=:parent)"
+                + " AND (station >= :kmLo - 0.0001) AND (station <= :kmHi + 0.0001)");
         query.setParameter("parent", parent);
         query.setParameter("kmLo", new Double(kmLo));
         query.setParameter("kmHi", new Double(kmHi));
@@ -151,9 +154,12 @@
     public static List<InfrastructureValue> getValues(final River river, final double kmLo, final double kmHi, final AttributeKey riverside,
             final Set<String> groupTypes) {
         final Session session = SessionHolder.HOLDER.get();
-        final Query query = session
-                .createQuery("FROM InfrastructureValue" + " WHERE (infrastructure.river=:river)" + " AND (station BETWEEN :kmLo - 0.0001 AND :kmHi + 0.0001)"
-                        + getRiversideClause(riverside, "", "attr_id") + getGroupTypeClause(groupTypes, "") + " ORDER BY station, attribute.id");
+        final Query query = session.createQuery("FROM InfrastructureValue"
+                + " WHERE (infrastructure.river=:river)"
+                + " AND (station BETWEEN :kmLo - 0.0001 AND :kmHi + 0.0001)"
+                + getRiversideClause(riverside, "", "attr_id")
+                + getGroupTypeClause(groupTypes, "")
+                + " ORDER BY station, attribute.id");
         query.setParameter("river", river);
         query.setParameter("kmLo", new Double(kmLo));
         query.setParameter("kmHi", new Double(kmHi));
@@ -171,8 +177,8 @@
         String clause = " AND (";
         String sep = "";
         for (final String groupType : groupTypes) {
-            clause += sep + "(" + tableprefix + "infrastructure.group.name='" + groupType.split("\t")[0] + "'" + " AND " + tableprefix
-                    + "infrastructure.type.name='" + groupType.split("\t")[1] + "')";
+            clause += sep + "(" + tableprefix + "infrastructure.group.name='" + groupType.split("\t")[0] + "'"
+                    + " AND " + tableprefix + "infrastructure.type.name='" + groupType.split("\t")[1] + "')";
             sep = " OR ";
         }
         if (sep.length() >= 1)


More information about the Dive4Elements-commits mailing list