comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/StaticSQFactory.java @ 8543:9a5b3079aad4

(issue1750) Take Static SQ Relations based on a range and merge them together
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 13 Feb 2015 14:59:16 +0100
parents 17db08570637
children c6723e5b6857
comparison
equal deleted inserted replaced
8542:7b210881d6db 8543:9a5b3079aad4
25 25
26 public class StaticSQFactory 26 public class StaticSQFactory
27 { 27 {
28 private static final Logger log = 28 private static final Logger log =
29 Logger.getLogger(StaticSQFactory.class); 29 Logger.getLogger(StaticSQFactory.class);
30
31 public static final String SQL_STATIONS_AT_RANGE =
32 "SELECT "+
33 "ms.id AS ms_id " +
34 "FROM measurement_station ms " +
35 "JOIN ranges ra ON ra.id = ms.range_id " +
36 "JOIN rivers r ON r.id = ra.river_id " +
37 "WHERE r.name = :river AND " +
38 "ms.range_id = (SELECT range_id " +
39 "FROM measurement_station " +
40 "WHERE id = :ms_id)";
30 41
31 public static final String SQL_SQ = 42 public static final String SQL_SQ =
32 "SELECT " + 43 "SELECT " +
33 "sq.description AS description,"+ 44 "sq.description AS description,"+
34 "ti.start_time AS start_time," + 45 "ti.start_time AS start_time," +
61 "sqv.id = :dis_id "; 72 "sqv.id = :dis_id ";
62 73
63 private StaticSQFactory() { 74 private StaticSQFactory() {
64 } 75 }
65 76
77 /** Get SQ relations for a measurement station's location.
78 * Returns all SQRelations for the location of the station and
79 * not just for the station. E.g. for a "Geschiebemessstelle"
80 * and a "Schwebstoffmesstelle" at the same place.*/
81 public static StaticSQContainer getSQRelationsForLocation(
82 String river,
83 int measurementStation
84 ) {
85 Session session = SessionHolder.HOLDER.get();
86 Query query = session.createSQLQuery(SQL_STATIONS_AT_RANGE)
87 .setParameter("river", river)
88 .setParameter("ms_id", measurementStation);
89 /* Take the first container for the station requested. */
90 StaticSQContainer retval = getSQRelations(river, measurementStation);
91
92 /* And some others */
93 List<Integer> list = query.list();
94 if (list == null || list.isEmpty()) {
95 log.error("Did not even find one measurement station. Broken Query?");
96 return retval;
97 }
98
99 for (Integer stationId: list) {
100 log.debug("Collecting SQ Relations for: "+ stationId);
101 if (stationId == measurementStation) {
102 /* Skip the same station */
103 continue;
104 }
105
106 StaticSQContainer additional = getSQRelations(river, stationId);
107 if (additional == null || additional.getSQRelations() == null) {
108 continue;
109 }
110
111 if (retval == null || retval.getSQRelations() == null || retval.getSQRelations().isEmpty()) {
112 /* Old one is empty, just take the new one. */
113 retval = additional;
114 continue;
115 }
116
117 for (StaticSQRelation rel: additional.getSQRelations()) {
118 /* Check if we already have one for this parameter.
119 * This is highly unlikely in the data scheme of things. */
120 List<StaticSQRelation> old = retval.getRelationsByParameter(
121 rel.getParameter());
122 if (old != null || !old.isEmpty()) {
123 log.warn("Multiple SQ relation Parameters found for different " +
124 "measurement_stations at the same range. This should not happen.");
125 continue;
126 }
127 retval.addSQRelation(rel);
128 }
129
130 }
131 return retval;
132 }
133
134
66 public static StaticSQContainer getSQRelations( 135 public static StaticSQContainer getSQRelations(
67 String river, 136 String river,
68 int measurementStation 137 int measurementStation
69 ) { 138 ) {
70 Cache cache = CacheFactory.getCache(StaticSQCacheKey.CACHE_NAME); 139 Cache cache = CacheFactory.getCache(StaticSQCacheKey.CACHE_NAME);
73 142
74 if (cache != null) { 143 if (cache != null) {
75 cacheKey = new StaticSQCacheKey(river, measurementStation); 144 cacheKey = new StaticSQCacheKey(river, measurementStation);
76 Element element = cache.get(cacheKey); 145 Element element = cache.get(cacheKey);
77 if (element != null) { 146 if (element != null) {
78 log.debug("Got static bedheight values from cache"); 147 log.debug("Got static sq relations from cache");
79 return (StaticSQContainer)element.getValue(); 148 return (StaticSQContainer)element.getValue();
80 } 149 }
81 } 150 }
82 else { 151 else {
83 cacheKey = null; 152 cacheKey = null;

http://dive4elements.wald.intevation.org