comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedHeightFactory.java @ 8544:76113b975829

(Issue1754) Make BedDifference calculation range dependent.
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 13 Feb 2015 16:17:46 +0100
parents e4606eae8ea5
children 3a0522f1a532
comparison
equal deleted inserted replaced
8543:9a5b3079aad4 8544:76113b975829
31 /** Query to get km and ws for wst_id and column_pos. */ 31 /** Query to get km and ws for wst_id and column_pos. */
32 public static final String SQL_SELECT_SINGLE = 32 public static final String SQL_SELECT_SINGLE =
33 "SELECT bhsv.height, bhsv.station, bhsv.data_gap, bhsv.sounding_width," + 33 "SELECT bhsv.height, bhsv.station, bhsv.data_gap, bhsv.sounding_width," +
34 " bhs.year, bhsv.width" + 34 " bhs.year, bhsv.width" +
35 " FROM bed_height_single bhs" + 35 " FROM bed_height_single bhs" +
36 " JOIN bed_height_single_values bhsv on bhsv.bed_height_single_id = bhs.id" + 36 " JOIN bed_height_single_values bhsv on bhsv.bed_height_single_id = bhs.id";
37
38 public static final String ID_CLAUSE =
37 " WHERE bhs.id = :height_id" + 39 " WHERE bhs.id = :height_id" +
38 " ORDER BY bhsv.station"; 40 " ORDER BY bhsv.station";
39 41
42 public static final String ID_STATION_CLAUSE =
43 " WHERE bhs.id = :height_id AND" +
44 " bhsv.station BETWEEN :fromkm AND :tokm" +
45 " ORDER BY bhsv.station";
40 46
41 /** Query to get name (description) for wst_id. */ 47 /** Query to get name (description) for wst_id. */
42 public static final String SQL_SELECT_DESCR_SINGLE = 48 public static final String SQL_SELECT_DESCR_SINGLE =
43 "SELECT description FROM bed_height_single "+ 49 "SELECT description FROM bed_height_single "+
44 "WHERE id = :height_id"; 50 "WHERE id = :height_id";
45 51
46 private BedHeightFactory() { 52 private BedHeightFactory() {
47 } 53 }
48 54
49
50 /** 55 /**
51 * Get BedHeightData for given type and height_id, caring about the cache. 56 * Get BedHeightData for given type and height_id, caring about the cache.
57 * If from or to are NaN all values are returned. Otherwise only get
58 * values with stations between from and to.
52 */ 59 */
53 public static BedHeightData getHeight(String type, int height_id) { 60 public static BedHeightData getHeight(String type, int height_id, double from, double to) {
54 log.debug("BedHeightFactory.getHeight"); 61 log.debug("BedHeightFactory.getHeight");
55 Cache cache = CacheFactory.getCache("bedheight-value-table-static"); 62 Cache cache = CacheFactory.getCache("bedheight-value-table-static");
56 63
57 String cacheKey = Integer.toString(height_id); 64 String cacheKey = Integer.toString(height_id) + ":" +
65 Double.toString(from) + ":" + Double.toString(to);
58 66
59 if (cache != null) { 67 if (cache != null) {
68 /* We could be more intelligent here and reuse cached values for
69 * a complete river and filter the other stations out afterwards.
70 * It might even be better to cache all values first and filter
71 * later. */
60 Element element = cache.get(cacheKey); 72 Element element = cache.get(cacheKey);
61 if (element != null) { 73 if (element != null) {
62 log.debug("Got static bedheight values from cache"); 74 log.debug("Got static bedheight values from cache");
63 return (BedHeightData)element.getValue(); 75 return (BedHeightData)element.getValue();
64 } 76 }
65 } 77 }
66 else { 78 else {
67 cacheKey = null; 79 cacheKey = null;
68 } 80 }
69 81
70 BedHeightData values = getBedHeightUncached(type, height_id); 82 BedHeightData values = getBedHeightUncached(type, height_id, from, to);
71 83
72 if (values != null && cacheKey != null) { 84 if (values != null && cacheKey != null) {
73 log.debug("Store static bed height values in cache."); 85 log.debug("Store static bed height values in cache.");
74 Element element = new Element(cacheKey, values); 86 Element element = new Element(cacheKey, values);
75 cache.put(element); 87 cache.put(element);
76 } 88 }
77 return values; 89 return values;
90 }
91
92 /**
93 * Get BedHeightData for given type and height_id, caring about the cache.
94 */
95 public static BedHeightData getHeight(String type, int height_id) {
96 return getHeight(type, height_id, Double.NaN, Double.NaN);
78 } 97 }
79 98
80 /** Get name for a BedHeight. */ 99 /** Get name for a BedHeight. */
81 public static String getHeightName(String type, int height_id) { 100 public static String getHeightName(String type, int height_id) {
82 log.debug("BedHeightFactory.getHeightName height_id/" + height_id); 101 log.debug("BedHeightFactory.getHeightName height_id/" + height_id);
102 } 121 }
103 122
104 123
105 /** 124 /**
106 * Get BedHeightData from db. 125 * Get BedHeightData from db.
126 *
127 * If from or to are negative all stations are returned. Otherwise
128 * only the values with a station betweend from and to.
107 * @param height_id database id of the bed_height 129 * @param height_id database id of the bed_height
130 * @param from minimum station value or NaN
131 * @param to maximum station value or NaN
108 * @return according BedHeight. 132 * @return according BedHeight.
109 */ 133 */
110 public static BedHeightData getBedHeightUncached( 134 public static BedHeightData getBedHeightUncached(
111 String type, 135 String type,
112 int height_id) 136 int height_id,
137 double from,
138 double to)
113 { 139 {
114 if (log.isDebugEnabled()) { 140 if (log.isDebugEnabled()) {
115 log.debug("BedHeightFactory.getBedHeightUncached"); 141 log.debug("BedHeightFactory.getBedHeightUncached");
116 } 142 }
117 143
118 Session session = SessionHolder.HOLDER.get(); 144 Session session = SessionHolder.HOLDER.get();
119 SQLQuery sqlQuery = null; 145 SQLQuery sqlQuery = null;
120 if (type.equals("single")) { 146 if (type.equals("single")) {
121 BedHeightSingleData height = 147 BedHeightSingleData height =
122 new BedHeightSingleData(getHeightName(type, height_id)); 148 new BedHeightSingleData(getHeightName(type, height_id));
123 sqlQuery = session.createSQLQuery(SQL_SELECT_SINGLE) 149 String queryString = SQL_SELECT_SINGLE;
150 if (Double.isNaN(from) || Double.isNaN(to)) {
151 queryString += ID_CLAUSE;
152 } else {
153 queryString += ID_STATION_CLAUSE;
154 }
155 sqlQuery = session.createSQLQuery(queryString)
124 .addScalar("height", StandardBasicTypes.DOUBLE) 156 .addScalar("height", StandardBasicTypes.DOUBLE)
125 .addScalar("station", StandardBasicTypes.DOUBLE) 157 .addScalar("station", StandardBasicTypes.DOUBLE)
126 .addScalar("data_gap", StandardBasicTypes.DOUBLE) 158 .addScalar("data_gap", StandardBasicTypes.DOUBLE)
127 .addScalar("sounding_width", StandardBasicTypes.DOUBLE) 159 .addScalar("sounding_width", StandardBasicTypes.DOUBLE)
128 .addScalar("width", StandardBasicTypes.DOUBLE) 160 .addScalar("width", StandardBasicTypes.DOUBLE)
129 .addScalar("year", StandardBasicTypes.INTEGER); 161 .addScalar("year", StandardBasicTypes.INTEGER);
130 sqlQuery.setInteger("height_id", height_id); 162 sqlQuery.setInteger("height_id", height_id);
163 if (!Double.isNaN(from) && !Double.isNaN(to)) {
164 sqlQuery.setDouble("fromkm", from);
165 sqlQuery.setDouble("tokm", to);
166 }
131 List<Object []> results = sqlQuery.list(); 167 List<Object []> results = sqlQuery.list();
132 168
133 for (Object [] row: results) { 169 for (Object [] row: results) {
134 log.debug("got station: " + (Double)row[1]); 170 log.debug("got station: " + (Double)row[1]);
135 Double row0 = row[0] != null ? (Double)row[0] : Double.NaN; 171 Double row0 = row[0] != null ? (Double)row[0] : Double.NaN;

http://dive4elements.wald.intevation.org