comparison 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
comparison
equal deleted inserted replaced
9619:63bbd5e45839 9620:26e113e8224f
10 10
11 package org.dive4elements.river.model.sinfo; 11 package org.dive4elements.river.model.sinfo;
12 12
13 import java.io.Serializable; 13 import java.io.Serializable;
14 import java.util.List; 14 import java.util.List;
15 import java.util.Set;
15 16
16 import javax.persistence.Column; 17 import javax.persistence.Column;
17 import javax.persistence.Entity; 18 import javax.persistence.Entity;
18 import javax.persistence.GeneratedValue; 19 import javax.persistence.GeneratedValue;
19 import javax.persistence.GenerationType; 20 import javax.persistence.GenerationType;
146 query.setParameter("kmHi", new Double(kmHi)); 147 query.setParameter("kmHi", new Double(kmHi));
147 return query.list(); 148 return query.list();
148 } 149 }
149 150
150 /** 151 /**
151 * Selects from the database the infrastructure values of a km range of a river and a river side 152 * Selects from the database the infrastructure values of a km range of a river and a group/type/river-side selection
152 */ 153 */
153 public static List<InfrastructureValue> getValues(final River river, final double kmLo, final double kmHi, final AttributeKey riverside) { 154 public static List<InfrastructureValue> getValues(final River river, final double kmLo, final double kmHi, final AttributeKey riverside,
155 final Set<String> groupTypes) {
154 final Session session = SessionHolder.HOLDER.get(); 156 final Session session = SessionHolder.HOLDER.get();
155 final Query query = session.createQuery("FROM InfrastructureValue v" 157 final Query query = session.createQuery("FROM InfrastructureValue"
156 + " WHERE (v.infrastructure.river=:river)" 158 + " WHERE (infrastructure.river=:river)"
157 + " AND (v.station BETWEEN :kmLo - 0.0001 AND :kmHi + 0.0001)" 159 + " AND (station BETWEEN :kmLo - 0.0001 AND :kmHi + 0.0001)"
158 + getRiversideClause(riverside, "v", "attr_id") 160 + getRiversideClause(riverside, "", "attr_id")
159 + " ORDER BY v.station, v.attribute.id"); 161 + getGroupTypeClause(groupTypes, "")
162 + " ORDER BY station, attribute.id");
160 query.setParameter("river", river); 163 query.setParameter("river", river);
161 query.setParameter("kmLo", new Double(kmLo)); 164 query.setParameter("kmLo", new Double(kmLo));
162 query.setParameter("kmHi", new Double(kmHi)); 165 query.setParameter("kmHi", new Double(kmHi));
163 if (!getRiversideClause(riverside, "v", "attr_id").isEmpty()) 166 if (!getRiversideClause(riverside, "", "attr_id").isEmpty())
164 query.setParameter("attr_id", riverside.getId()); 167 query.setParameter("attr_id", riverside.getId());
165 return query.list(); 168 return query.list();
166 } 169 }
167 170
168 /** 171 /**
169 * Gets a query's and-where-clause for a riverside key 172 * Gets a query's and-where-clause for a set of infrastructure group-type-pairs (as tab-separated strings)
170 */ 173 */
171 public static String getRiversideClause(final AttributeKey riverside, final String tablealias, final String variable) { 174 private static String getGroupTypeClause(final Set<String> groupTypes, final String tableprefix) {
175 if (groupTypes.size() == 0)
176 return "";
177 String clause = " AND (";
178 String sep = "";
179 for (final String groupType : groupTypes) {
180 clause += sep + "(" + tableprefix + "infrastructure.group.name='" + groupType.split("\t")[0] + "'"
181 + " AND " + tableprefix + "infrastructure.type.name='" + groupType.split("\t")[1] + "')";
182 sep = " OR ";
183 }
184 if (sep.length() >= 1)
185 clause += ")";
186 return clause;
187 }
188
189 /**
190 * Gets a query's and-where-clause for a riverside key (tableprefix empty or starting with a dot)
191 */
192 public static String getRiversideClause(final AttributeKey riverside, final String tableprefix, final String variable) {
172 if ((riverside == AttributeKey.LEFT) || (riverside == AttributeKey.RIGHT)) 193 if ((riverside == AttributeKey.LEFT) || (riverside == AttributeKey.RIGHT))
173 return " AND (" + tablealias + ".attribute.id=:" + variable + ")"; 194 return " AND (" + tableprefix + "attribute.id=:" + variable + ")";
174 else 195 else
175 return ""; 196 return "";
176 } 197 }
177 } 198 }

http://dive4elements.wald.intevation.org