comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java @ 4007:d4d272e56d3a

S(Q) Fixed AIOOBE
author Sascha L. Teichmann <teichmann@intevation.de>
date Mon, 01 Oct 2012 23:05:21 +0200
parents d4e39cc5c10c
children fea3d9210948
comparison
equal deleted inserted replaced
4006:d4e39cc5c10c 4007:d4d272e56d3a
26 public class MeasurementFactory 26 public class MeasurementFactory
27 { 27 {
28 private static final Logger log = 28 private static final Logger log =
29 Logger.getLogger(MeasurementFactory.class); 29 Logger.getLogger(MeasurementFactory.class);
30 30
31 public static final String SQL_MEASSURE = 31 public static final String SQL_FACTIONS =
32 "SELECT m.datum AS DATUM," + 32 "SELECT m.datum AS DATUM," +
33 "g.GLOTRECHTEID AS GLOTRECHTEID," + 33 "g.GLOTRECHTEID AS GLOTRECHTEID," +
34 "gp.LFDNR AS LFDNR," + 34 "gp.LFDNR AS LFDNR," +
35 "g.UFERABST AS UFERABST," + 35 "g.UFERABST AS UFERABST," +
36 "g.UFERABLINKS AS UFERABLINKS," + 36 "g.UFERABLINKS AS UFERABLINKS," +
37 "m.TSCHWEB AS TSCHWEB," + 37 "m.TSCHWEB AS TSCHWEB," +
38 "m.TSAND AS TSAND," + 38 "m.TSAND AS TSAND," +
39 "gp.GTRIEB AS GTRIEB," + 39 "gp.GTRIEB AS GTRIEB," +
40 "m.TGESCHIEBE AS TGESCHIEBE," + 40 "m.TGESCHIEBE AS TGESCHIEBE," +
41 "sie.SIEB01 AS SIEB01," + 41 "sie.SIEB01 AS SIEB01, sie.SIEB02 AS SIEB02," +
42 "sie.SIEB02 AS SIEB02," + 42 "sie.SIEB03 AS SIEB03, sie.SIEB04 AS SIEB04," +
43 "sie.SIEB03 AS SIEB03," + 43 "sie.SIEB05 AS SIEB05, sie.SIEB06 AS SIEB06," +
44 "sie.SIEB04 AS SIEB04," + 44 "sie.SIEB07 AS SIEB07, sie.SIEB08 AS SIEB08," +
45 "sie.SIEB05 AS SIEB05," + 45 "sie.SIEB09 AS SIEB09, sie.SIEB10 AS SIEB10," +
46 "sie.SIEB06 AS SIEB06," + 46 "sie.SIEB11 AS SIEB11, sie.SIEB12 AS SIEB12," +
47 "sie.SIEB07 AS SIEB07," + 47 "sie.SIEB13 AS SIEB13, sie.SIEB14 AS SIEB14," +
48 "sie.SIEB08 AS SIEB08," + 48 "sie.SIEB15 AS SIEB15, sie.SIEB16 AS SIEB16," +
49 "sie.SIEB09 AS SIEB09," + 49 "sie.SIEB17 AS SIEB17, sie.SIEB18 AS SIEB18," +
50 "sie.SIEB10 AS SIEB10," + 50 "sie.SIEB19 AS SIEB19, sie.SIEB20 AS SIEB20," +
51 "sie.SIEB11 AS SIEB11," + 51 "sie.SIEB21 AS SIEB21," +
52 "sie.SIEB12 AS SIEB12," + 52 "gs.RSIEB01 AS RSIEB01, gs.RSIEB02 AS RSIEB02," +
53 "sie.SIEB13 AS SIEB13," + 53 "gs.RSIEB03 AS RSIEB03, gs.RSIEB04 AS RSIEB04," +
54 "sie.SIEB14 AS SIEB14," + 54 "gs.RSIEB05 AS RSIEB05, gs.RSIEB06 AS RSIEB06," +
55 "sie.SIEB15 AS SIEB15," + 55 "gs.RSIEB07 AS RSIEB07, gs.RSIEB08 AS RSIEB08," +
56 "sie.SIEB16 AS SIEB16," + 56 "gs.RSIEB09 AS RSIEB09, gs.RSIEB10 AS RSIEB10," +
57 "sie.SIEB17 AS SIEB17," + 57 "gs.RSIEB11 AS RSIEB11, gs.RSIEB12 AS RSIEB12," +
58 "sie.SIEB18 AS SIEB18," + 58 "gs.RSIEB13 AS RSIEB13, gs.RSIEB14 AS RSIEB14," +
59 "sie.SIEB19 AS SIEB19," + 59 "gs.RSIEB15 AS RSIEB15, gs.RSIEB16 AS RSIEB16," +
60 "sie.SIEB20 AS SIEB20," + 60 "gs.RSIEB17 AS RSIEB17, gs.RSIEB18 AS RSIEB18," +
61 "sie.SIEB21 AS SIEB21," + 61 "gs.RSIEB19 AS RSIEB19, gs.RSIEB20 AS RSIEB20," +
62 "gs.RSIEB01 AS RSIEB01," + 62 "gs.RSIEB21 AS RSIEB21, gs.REST AS REST " +
63 "gs.RSIEB02 AS RSIEB02," +
64 "gs.RSIEB03 AS RSIEB03," +
65 "gs.RSIEB04 AS RSIEB04," +
66 "gs.RSIEB05 AS RSIEB05," +
67 "gs.RSIEB06 AS RSIEB06," +
68 "gs.RSIEB07 AS RSIEB07," +
69 "gs.RSIEB08 AS RSIEB08," +
70 "gs.RSIEB09 AS RSIEB09," +
71 "gs.RSIEB10 AS RSIEB10," +
72 "gs.RSIEB11 AS RSIEB11," +
73 "gs.RSIEB12 AS RSIEB12," +
74 "gs.RSIEB13 AS RSIEB13," +
75 "gs.RSIEB14 AS RSIEB14," +
76 "gs.RSIEB15 AS RSIEB15," +
77 "gs.RSIEB16 AS RSIEB16," +
78 "gs.RSIEB17 AS RSIEB17," +
79 "gs.RSIEB18 AS RSIEB18," +
80 "gs.RSIEB19 AS RSIEB19," +
81 "gs.RSIEB20 AS RSIEB20," +
82 "gs.RSIEB21 AS RSIEB21," +
83 "gs.REST AS REST " +
84 "FROM MESSUNG m " + 63 "FROM MESSUNG m " +
85 "JOIN STATION s ON m.STATIONID = s.STATIONID " + 64 "JOIN STATION s ON m.STATIONID = s.STATIONID " +
86 "JOIN glotrechte g ON m.MESSUNGID = g.MESSUNGID " + 65 "JOIN glotrechte g ON m.MESSUNGID = g.MESSUNGID " +
87 "JOIN gprobe gp ON g.GLOTRECHTEID = gp.GLOTRECHTEID " + 66 "JOIN gprobe gp ON g.GLOTRECHTEID = gp.GLOTRECHTEID " +
88 "JOIN GSIEBUNG gs ON g.GLOTRECHTEID = gs.GLOTRECHTEID " + 67 "JOIN GSIEBUNG gs ON g.GLOTRECHTEID = gs.GLOTRECHTEID " +
105 "COALESCE(gs.RSIEB17, 0) + COALESCE(gs.RSIEB18, 0) +" + 84 "COALESCE(gs.RSIEB17, 0) + COALESCE(gs.RSIEB18, 0) +" +
106 "COALESCE(gs.RSIEB19, 0) + COALESCE(gs.RSIEB20, 0) +" + 85 "COALESCE(gs.RSIEB19, 0) + COALESCE(gs.RSIEB20, 0) +" +
107 "COALESCE(gs.RSIEB21, 0) + COALESCE(gs.REST, 0)) > 0 " + 86 "COALESCE(gs.RSIEB21, 0) + COALESCE(gs.REST, 0)) > 0 " +
108 "ORDER BY m.DATUM, g.UFERABST, g.GLOTRECHTEID, gp.LFDNR"; 87 "ORDER BY m.DATUM, g.UFERABST, g.GLOTRECHTEID, gp.LFDNR";
109 88
110 public static final class MeasurementResultTransformer 89 private static final int index(String s) {
111 extends BasicTransformerAdapter 90 return Integer.parseInt(s.substring(s.length()-2))-1;
112 { 91 }
113 public static MeasurementResultTransformer INSTANCE = 92
114 new MeasurementResultTransformer(); 93 public static final BasicTransformerAdapter FRACTIONS_TRANSFORMER =
115 94 new BasicTransformerAdapter() {
116 public MeasurementResultTransformer() { 95
117 } 96 @Override
118 97 public Object transformTuple(Object [] tuple, String [] aliases) {
119 private static final int index(String s) { 98 Map<String, Object> map = new HashMap<String, Object>();
120 return Integer.parseInt(s.substring(s.length()-2))-1; 99
121 } 100 Sieve [] sieves = new Sieve[21];
122 101
123 @Override 102 List<Sieve> validSieves = new ArrayList<Sieve>(21);
124 public Object transformTuple(Object [] tuple, String [] aliases) { 103
125 Map<String, Object> map = new HashMap<String, Object>(); 104 for (int i = 0; i < tuple.length; ++i) {
126 105 Object value = tuple[i];
127 Sieve [] sieves = new Sieve[20]; 106 if (value == null) {
128 107 continue;
129 List<Sieve> validSieves = new ArrayList<Sieve>(20); 108 }
130 109 String alias = aliases[i];
131 for (int i = 0; i < tuple.length; ++i) { 110 if (alias.startsWith("SIEB")) {
132 Object value = tuple[i]; 111 Sieve s = new Sieve((Double)value, 0d);
133 if (value == null) { 112 sieves[index(alias)] = s;
134 continue; 113 }
135 } 114 else if (alias.startsWith("RSIEB")) {
136 String alias = aliases[i]; 115 Sieve s = sieves[index(alias)];
137 if (alias.startsWith("SIEB")) { 116 if (s != null) {
138 Sieve s = new Sieve((Double)value, 0d); 117 s.setLoad((Double)value);
139 sieves[index(alias)] = s; 118 validSieves.add(s);
140 } 119 }
141 else if (alias.startsWith("RSIEB")) { 120 }
142 Sieve s = sieves[index(alias)]; 121 else if (alias.equals("REST")) {
143 if (s != null) { 122 Sieve s = new Sieve(0d, (Double)value);
144 s.setLoad((Double)value);
145 validSieves.add(s); 123 validSieves.add(s);
146 } 124 }
125 else {
126 map.put(alias, value);
127 }
147 } 128 }
148 else if (alias.equals("REST")) { 129
149 Sieve s = new Sieve(0d, (Double)value); 130 return new Measurement(map, validSieves);
150 validSieves.add(s);
151 }
152 else {
153 map.put(alias, value);
154 }
155 } 131 }
156 132 };
157 return new Measurement(map, validSieves);
158 }
159 } // class BasicTransformerAdapter
160 133
161 private MeasurementFactory() { 134 private MeasurementFactory() {
162 } 135 }
163 136
164 protected static Measurements load( 137 protected static Measurements load(
168 DateRange dateRange 141 DateRange dateRange
169 ) { 142 ) {
170 boolean debug = log.isDebugEnabled(); 143 boolean debug = log.isDebugEnabled();
171 144
172 if (debug) { 145 if (debug) {
173 log.debug(SQL_MEASSURE); 146 log.debug(SQL_FACTIONS);
174 } 147 }
175 148
176 SQLQuery query = session.createSQLQuery(SQL_MEASSURE) 149 SQLQuery query = session.createSQLQuery(SQL_FACTIONS)
177 .addScalar("Q_BPEGEL", StandardBasicTypes.DOUBLE) 150 .addScalar("Q_BPEGEL", StandardBasicTypes.DOUBLE)
178 .addScalar("DATUM", StandardBasicTypes.DATE) 151 .addScalar("DATUM", StandardBasicTypes.DATE)
179 .addScalar("GLOTRECHTEID", StandardBasicTypes.INTEGER) 152 .addScalar("GLOTRECHTEID", StandardBasicTypes.INTEGER)
180 .addScalar("LFDNR", StandardBasicTypes.INTEGER) 153 .addScalar("LFDNR", StandardBasicTypes.INTEGER)
181 .addScalar("UFERABST", StandardBasicTypes.DOUBLE) 154 .addScalar("UFERABST", StandardBasicTypes.DOUBLE)
231 query.setString("river_name", river); 204 query.setString("river_name", river);
232 query.setDouble("location", location); 205 query.setDouble("location", location);
233 query.setDate("from", dateRange.getFrom()); 206 query.setDate("from", dateRange.getFrom());
234 query.setDate("to", dateRange.getTo()); 207 query.setDate("to", dateRange.getTo());
235 208
236 query.setResultTransformer(MeasurementResultTransformer.INSTANCE); 209 query.setResultTransformer(FRACTIONS_TRANSFORMER);
237 210
238 @SuppressWarnings("unchecked") 211 @SuppressWarnings("unchecked")
239 List<Measurement> measuments = (List<Measurement>)query.list(); 212 List<Measurement> measuments = (List<Measurement>)query.list();
240 213
241 List<Measurement> same = new ArrayList<Measurement>(); 214 List<Measurement> same = new ArrayList<Measurement>();

http://dive4elements.wald.intevation.org