Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/services/DischargeTablesOverview.java @ 5831:bd047b71ab37
Repaired internal references
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:06:39 +0200 |
parents | flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DischargeTablesOverview.java@2fe120e1e4df |
children |
comparison
equal
deleted
inserted
replaced
5830:160f53ee0870 | 5831:bd047b71ab37 |
---|---|
1 package org.dive4elements.river.artifacts.services; | |
2 | |
3 import java.awt.Color; | |
4 import java.text.DateFormat; | |
5 import java.text.ParseException; | |
6 import java.util.ArrayList; | |
7 import java.util.Collections; | |
8 import java.util.Date; | |
9 import java.util.List; | |
10 import java.util.Locale; | |
11 | |
12 import org.apache.log4j.Logger; | |
13 import org.hibernate.Session; | |
14 import org.jfree.chart.ChartFactory; | |
15 import org.jfree.chart.JFreeChart; | |
16 import org.jfree.chart.plot.Marker; | |
17 import org.jfree.chart.plot.PlotOrientation; | |
18 import org.jfree.chart.plot.XYPlot; | |
19 import org.jfree.data.xy.XYSeries; | |
20 import org.jfree.data.xy.XYSeriesCollection; | |
21 import org.w3c.dom.Document; | |
22 import org.w3c.dom.Element; | |
23 import org.w3c.dom.NodeList; | |
24 | |
25 import org.dive4elements.artifacts.CallMeta; | |
26 import org.dive4elements.artifacts.GlobalContext; | |
27 import org.dive4elements.river.artifacts.model.DischargeTables; | |
28 import org.dive4elements.river.artifacts.model.GaugesFactory; | |
29 import org.dive4elements.river.artifacts.resources.Resources; | |
30 import org.dive4elements.river.backend.SessionHolder; | |
31 import org.dive4elements.river.model.DischargeTable; | |
32 import org.dive4elements.river.model.Gauge; | |
33 import org.dive4elements.river.model.MainValue; | |
34 import org.dive4elements.river.model.TimeInterval; | |
35 | |
36 | |
37 /** Generate Discharge Table chart. */ | |
38 public class DischargeTablesOverview extends AbstractChartService { | |
39 | |
40 private static final Logger log = Logger | |
41 .getLogger(DischargeTablesOverview.class); | |
42 | |
43 private static final long serialVersionUID = 1L; | |
44 | |
45 public static final String I18N_CHART_TITLE = "gauge.discharge.service.chart.title"; | |
46 public static final String DEFAULT_CHART_TITLE = "Pegel: XXX"; | |
47 | |
48 public static final String I18N_CHART_X_AXIS_TITLE = "gauge.discharge.service.chart.x.title"; | |
49 public static final String DEFAULT_X_AXIS_TITLE = "Q [m^3/s]"; | |
50 | |
51 public static final String I18N_CHART_Y_AXIS_TITLE = "gauge.discharge.service.chart.y.title"; | |
52 public static final String DEFAULT_Y_AXIS_TITLE = "W [cm]"; | |
53 | |
54 public static final String I18N_CHART_SERIES_TITLE = "gauge.discharge.service.chart.series.title"; | |
55 public static final String DEFAULT_CHART_SERIES_TITLE = "Abflusskurve"; | |
56 | |
57 public static final String I18N_CHART_SERIES_TITLE_MASTER = "gauge.discharge.service.chart.series.title.master"; | |
58 public static final String DEFAULT_CHART_SERIES_TITLE_MASTER = "Aktuelle Abflusskurve"; | |
59 | |
60 public static final DateFormat DATE_FORMAT = DateFormat.getDateInstance( | |
61 DateFormat.SHORT, Locale.GERMANY); | |
62 | |
63 private Session session; | |
64 | |
65 @Override | |
66 protected void init() { | |
67 session = SessionHolder.acquire(); | |
68 } | |
69 | |
70 @Override | |
71 protected void finish() { | |
72 if (session != null) { | |
73 session.close(); | |
74 SessionHolder.release(); | |
75 } | |
76 } | |
77 | |
78 protected JFreeChart createChart(Document data, | |
79 GlobalContext globalContext, CallMeta callMeta) { | |
80 | |
81 Gauge gauge = extractGauge(data); | |
82 | |
83 if (gauge == null) { | |
84 log.warn("Could not determine Gauge from request!"); | |
85 return null; | |
86 } | |
87 | |
88 log.info("create discharge chart for gauge '" + gauge.getName() + "'"); | |
89 TimeInterval timerange = extractTimeInterval(data); | |
90 | |
91 List<DischargeTable> dts = getDischargeTables(gauge, timerange); | |
92 XYSeriesCollection dataset = new XYSeriesCollection(); | |
93 | |
94 for (DischargeTable dt : dts) { | |
95 try { | |
96 XYSeries series = createSeries(callMeta, dt); | |
97 if (series != null) { | |
98 dataset.addSeries(series); | |
99 } | |
100 } | |
101 catch (IllegalArgumentException iae) { | |
102 log.warn("unable to create discharge curve: " | |
103 + iae.getMessage()); | |
104 } | |
105 } | |
106 | |
107 String title = Resources.format(callMeta, I18N_CHART_TITLE, | |
108 DEFAULT_CHART_TITLE, gauge.getName()); | |
109 | |
110 String xAxis = Resources.getMsg(callMeta, I18N_CHART_X_AXIS_TITLE, | |
111 DEFAULT_X_AXIS_TITLE); | |
112 | |
113 String yAxis = Resources.format(callMeta, I18N_CHART_Y_AXIS_TITLE, | |
114 DEFAULT_Y_AXIS_TITLE); | |
115 | |
116 JFreeChart chart = ChartFactory.createXYLineChart(title, xAxis, yAxis, | |
117 null, PlotOrientation.VERTICAL, true, true, false); | |
118 | |
119 chart.setBackgroundPaint(Color.white); | |
120 | |
121 XYPlot plot = (XYPlot) chart.getPlot(); | |
122 plot.setDataset(0, dataset); | |
123 plot.setBackgroundPaint(Color.white); | |
124 plot.setDomainGridlinePaint(Color.gray); | |
125 plot.setRangeGridlinePaint(Color.gray); | |
126 plot.setDomainGridlinesVisible(true); | |
127 plot.setRangeGridlinesVisible(true); | |
128 | |
129 applyMainValueMarkers( | |
130 plot, | |
131 gauge, | |
132 callMeta); | |
133 | |
134 return chart; | |
135 } | |
136 | |
137 protected XYSeries createSeries(CallMeta callMeta, DischargeTable dt) | |
138 throws IllegalArgumentException { | |
139 | |
140 double[][] xy = null; | |
141 | |
142 if (dt.getKind() == DischargeTables.MASTER) { | |
143 xy = DischargeTables.loadDischargeTableValues(dt, | |
144 DischargeTables.MASTER_SCALE); | |
145 } | |
146 else { | |
147 xy = DischargeTables.loadDischargeTableValues(dt, | |
148 DischargeTables.HISTORICAL_SCALE); | |
149 } | |
150 | |
151 XYSeries series = new XYSeries(createSeriesTitle(callMeta, dt), false); | |
152 for (int i = 0, n = xy[0].length; i < n; i++) { | |
153 series.add(xy[0][i], xy[1][i]); | |
154 } | |
155 | |
156 return series; | |
157 } | |
158 | |
159 | |
160 /** Add domain markers to plot that indicate mainvalues. */ | |
161 protected static void applyMainValueMarkers( | |
162 XYPlot plot, | |
163 Gauge gauge, | |
164 CallMeta meta | |
165 ) { | |
166 String river = gauge.getRiver().getName(); | |
167 double km = gauge.getStation().doubleValue(); | |
168 | |
169 // Get Gauge s mainvalues. | |
170 List<MainValue> mainValues = gauge.getMainValues(); | |
171 for (MainValue mainValue : mainValues) { | |
172 if (mainValue.getMainValue().getType().getName().equals("Q")) { | |
173 // Its a Q main value. | |
174 Marker m = FixingsKMChartService.createQSectorMarker( | |
175 mainValue.getValue().doubleValue(), | |
176 mainValue.getMainValue().getName()); | |
177 plot.addDomainMarker(m); | |
178 } | |
179 else if (mainValue.getMainValue().getType().getName().equals("W")) { | |
180 // Its a W main value. | |
181 Marker m = FixingsKMChartService.createQSectorMarker( | |
182 mainValue.getValue().doubleValue(), | |
183 mainValue.getMainValue().getName()); | |
184 plot.addRangeMarker(m); | |
185 } | |
186 } | |
187 } | |
188 | |
189 protected String createSeriesTitle(CallMeta callMeta, DischargeTable dt) | |
190 throws IllegalArgumentException { | |
191 TimeInterval timeInterval = dt.getTimeInterval(); | |
192 | |
193 if (timeInterval == null) { | |
194 return Resources.format(callMeta, DEFAULT_CHART_SERIES_TITLE); | |
195 } | |
196 | |
197 Date start = timeInterval.getStartTime(); | |
198 Date end = timeInterval.getStopTime(); | |
199 | |
200 if (start != null && end != null) { | |
201 return Resources.format(callMeta, I18N_CHART_SERIES_TITLE, | |
202 DEFAULT_CHART_SERIES_TITLE, start, end); | |
203 } | |
204 else if (start != null) { | |
205 return Resources.format(callMeta, I18N_CHART_SERIES_TITLE_MASTER, | |
206 DEFAULT_CHART_SERIES_TITLE, start); | |
207 } | |
208 else { | |
209 throw new IllegalArgumentException( | |
210 "Missing start date of DischargeTable " + dt.getId()); | |
211 } | |
212 } | |
213 | |
214 protected Gauge extractGauge(Document data) { | |
215 NodeList gauges = data.getElementsByTagName("gauge"); | |
216 | |
217 if (gauges.getLength() > 0) { | |
218 String name = ((Element) gauges.item(0)).getAttribute("name"); | |
219 | |
220 try { | |
221 long officialNumber = Long.valueOf(name); | |
222 return Gauge.getGaugeByOfficialNumber(officialNumber); | |
223 } | |
224 catch (NumberFormatException nfe) { | |
225 // it seems, that the client uses the name of the gauge instead | |
226 // of its official number | |
227 } | |
228 | |
229 if (name != null && name.length() > 0) { | |
230 return GaugesFactory.getGauge(name); | |
231 } | |
232 } | |
233 | |
234 return null; | |
235 } | |
236 | |
237 protected TimeInterval extractTimeInterval(Document data) { | |
238 NodeList timeranges = data.getElementsByTagName("timerange"); | |
239 | |
240 if (timeranges != null && timeranges.getLength() > 0) { | |
241 Element timerange = (Element) timeranges.item(0); | |
242 | |
243 String lower = timerange.getAttribute("lower"); | |
244 String upper = timerange.getAttribute("upper"); | |
245 | |
246 if (lower != null && upper != null) { | |
247 try { | |
248 Date d1 = DATE_FORMAT.parse(lower); | |
249 Date d2 = DATE_FORMAT.parse(upper); | |
250 | |
251 return new TimeInterval(d1, d2); | |
252 } | |
253 catch (ParseException pe) { | |
254 log.warn("Wrong time format: " + pe.getMessage()); | |
255 } | |
256 } | |
257 } | |
258 | |
259 return null; | |
260 } | |
261 | |
262 protected List<DischargeTable> getDischargeTables(Gauge gauge, | |
263 TimeInterval timerange) { | |
264 List<DischargeTable> all = gauge.getDischargeTables(); | |
265 Collections.sort(all); | |
266 | |
267 if (timerange == null) { | |
268 return all; | |
269 } | |
270 | |
271 List<DischargeTable> dts = new ArrayList<DischargeTable>(all.size()); | |
272 long startDate = timerange.getStartTime().getTime(); | |
273 long stopDate = timerange.getStopTime().getTime(); | |
274 | |
275 for (DischargeTable dt : all) { | |
276 TimeInterval tmp = dt.getTimeInterval(); | |
277 if (tmp == null) { | |
278 // this should never happen because all discharge tables should | |
279 // have a time interval set! | |
280 continue; | |
281 } | |
282 | |
283 Date start = tmp.getStartTime(); | |
284 Date stop = tmp.getStartTime(); | |
285 | |
286 if (start.getTime() > startDate && start.getTime() < stopDate) { | |
287 dts.add(dt); | |
288 continue; | |
289 } | |
290 else if (stop != null && stop.getTime() < stopDate | |
291 && stop.getTime() > startDate) { | |
292 dts.add(dt); | |
293 continue; | |
294 } | |
295 } | |
296 | |
297 return dts; | |
298 } | |
299 } | |
300 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |