diff backend/src/main/java/org/dive4elements/river/model/sinfo/InfrastructureValue.java @ 9620:26e113e8224f

Nachtrag Pos. 20: flood duration calculation for multiple infrastructure groups/types, local class FloodDurationCalculationResult.Infrastructure renamed and extracted into own class
author mschaefer
date Thu, 10 Oct 2019 17:11:54 +0200
parents d889ffe2fb05
children 02ca823ec9c6
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/model/sinfo/InfrastructureValue.java	Thu Oct 10 16:08:47 2019 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/sinfo/InfrastructureValue.java	Thu Oct 10 17:11:54 2019 +0200
@@ -12,6 +12,7 @@
 
 import java.io.Serializable;
 import java.util.List;
+import java.util.Set;
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -148,29 +149,49 @@
     }
 
     /**
-     * Selects from the database the infrastructure values of a km range of a river and a river side
+     * Selects from the database the infrastructure values of a km range of a river and a group/type/river-side selection
      */
-    public static List<InfrastructureValue> getValues(final River river, final double kmLo, final double kmHi, final AttributeKey riverside) {
+    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 v"
-                + " WHERE (v.infrastructure.river=:river)"
-                + " AND (v.station BETWEEN :kmLo - 0.0001 AND :kmHi + 0.0001)"
-                + getRiversideClause(riverside, "v", "attr_id")
-                + " ORDER BY v.station, v.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));
-        if (!getRiversideClause(riverside, "v", "attr_id").isEmpty())
+        if (!getRiversideClause(riverside, "", "attr_id").isEmpty())
             query.setParameter("attr_id", riverside.getId());
         return query.list();
     }
 
     /**
-     * Gets a query's and-where-clause for a riverside key
+     * Gets a query's and-where-clause for a set of infrastructure group-type-pairs (as tab-separated strings)
      */
-    public static String getRiversideClause(final AttributeKey riverside, final String tablealias, final String variable) {
+    private static String getGroupTypeClause(final Set<String> groupTypes, final String tableprefix) {
+        if (groupTypes.size() == 0)
+            return "";
+        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] + "')";
+            sep = " OR ";
+        }
+        if (sep.length() >= 1)
+            clause += ")";
+        return clause;
+    }
+
+    /**
+     * Gets a query's and-where-clause for a riverside key (tableprefix empty or starting with a dot)
+     */
+    public static String getRiversideClause(final AttributeKey riverside, final String tableprefix, final String variable) {
         if ((riverside == AttributeKey.LEFT) || (riverside == AttributeKey.RIGHT))
-            return " AND (" + tablealias + ".attribute.id=:" + variable + ")";
+            return " AND (" + tableprefix + "attribute.id=:" + variable + ")";
         else
             return "";
     }

http://dive4elements.wald.intevation.org