comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileOutputState.java @ 335:e964a3d8f7bc

Some Refactoring work done. Moved Transition to State gnv-artifacts/trunk@401 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 08 Dec 2009 08:39:03 +0000
parents
children 2e43542e6a11
comparison
equal deleted inserted replaced
334:e37930705daa 335:e964a3d8f7bc
1 /**
2 *
3 */
4 package de.intevation.gnv.state.profile.horizontal;
5
6 import com.vividsolutions.jts.geom.Point;
7 import com.vividsolutions.jts.io.WKTReader;
8 import com.vividsolutions.jts.io.ParseException;
9
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.io.UnsupportedEncodingException;
13 import java.text.SimpleDateFormat;
14 import java.util.Collection;
15 import java.util.Date;
16 import java.util.Iterator;
17 import java.util.Locale;
18
19 import org.apache.log4j.Logger;
20
21 import de.intevation.gnv.chart.Chart;
22 import de.intevation.gnv.chart.ChartLabels;
23 import de.intevation.gnv.chart.HorizontalProfileChart;
24 import de.intevation.gnv.geobackend.base.Result;
25 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
26 import de.intevation.gnv.state.exception.StateException;
27 import de.intevation.gnv.state.timeseries.TimeSeriesOutputState;
28 import de.intevation.gnv.statistics.HorizontalProfileStatistics;
29 import de.intevation.gnv.statistics.Statistics;
30
31 import de.intevation.gnv.exports.DefaultExport;
32 import de.intevation.gnv.exports.ShapeDataCollector;
33 import de.intevation.gnv.exports.DefaultProfile;
34 import de.intevation.gnv.exports.Export.Profile;
35
36 /**
37 * @author Tim Englich <tim.englich@intevation.de>
38 *
39 */
40 public class HorizontalProfileOutputState extends
41 TimeSeriesOutputState {
42 /**
43 * The UID of this class
44 */
45 private static final long serialVersionUID = 4401516087492028840L;
46
47 private static Logger log = Logger
48 .getLogger(HorizontalProfileOutputState.class);
49
50 public static final String DATE_FORMAT = "yyyy.MM.dd HH:mm:ss";
51
52 public static final String [] CHART_TITLE_META = {
53 "CRUISE",
54 "DEPTH",
55 "SHAPE"
56 };
57
58
59 public static final String [] CHART_TITLE_META_RESSOURCES = {
60 "cruiseid",
61 "depth",
62 "coordinate"
63 };
64
65 public static final String [] TIMESERIES_CSV_PROFILE_NAMES = {
66 "SHAPE",
67 "YORDINATE",
68 "GROUP1",
69 "GROUP2",
70 "GROUP3"
71 };
72
73 public static final Profile TIMESERIES_CSV_PROFILE =
74 new DefaultProfile(
75 null,
76 ',',
77 '"',
78 '"',
79 "CSV",
80 "ISO-8859-1");
81
82 /**
83 * Constructor
84 */
85 public HorizontalProfileOutputState() {
86 super();
87 super.domainLable = "Distance [km]";
88 }
89
90
91 @Override
92 protected Chart getChart(
93 ChartLabels chartLables,
94 Collection parameters,
95 Collection measurements,
96 Collection dates,
97 Collection result,
98 Locale locale,
99 String uuid,
100 boolean linesVisible,
101 boolean shapesVisible
102 ) {
103 Chart chart = null;
104
105 if (CACHE_CHART) {
106 log.info("Try to get horizontalprofile chart from cache.");
107 chart = (Chart) getChartFromCache(uuid);
108 }
109
110 if (chart != null)
111 return chart;
112
113 log.info("Chart not in cache yet.");
114 chart = new HorizontalProfileChart(
115 chartLables,
116 createStyle(),
117 parameters,
118 measurements,
119 dates,
120 result,
121 null,
122 locale,
123 linesVisible,
124 shapesVisible
125 );
126 chart.generateChart();
127
128 if (CACHE_CHART) {
129 log.info("Put chart into cache.");
130 purifyChart(chart, uuid);
131 }
132
133 return chart;
134 }
135
136
137 /**
138 * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#getStatisticsGenerator()
139 */
140 @Override
141 protected Statistics getStatisticsGenerator() {
142 return new HorizontalProfileStatistics();
143 }
144
145 @Override
146 protected void createCSV(OutputStream outputStream,
147 Collection<Result> chartResult)
148 throws UnsupportedEncodingException,
149 IOException,
150 StateException {
151
152 log.debug("createCSV for HorizontalProfileOutputState.");
153 DefaultExport export = new DefaultExport(
154 new ShapeDataCollector(TIMESERIES_CSV_PROFILE_NAMES));
155
156 export.create(TIMESERIES_CSV_PROFILE, outputStream, chartResult);
157 }
158
159
160 protected String createChartTitle(Locale locale, String uuid) {
161 String fisName = getFisName(locale);
162 log.debug("created title for horizontal profile chart: " + fisName);
163
164 return fisName;
165 }
166
167
168 protected String createChartSubtitle(Locale locale, String uuid) {
169 log.debug("create chart subtitle.");
170 String subtitle = createTimePeriod(locale, uuid);
171
172 // ODV results contain meta information about cruise, station and so on
173 Collection results = getODVResult(uuid);
174 if (results != null) {
175 Iterator iter = results.iterator();
176 Result result = iter.hasNext() ? (Result) iter.next() : null;
177
178 subtitle += subtitle.length() != 0 ? "\n" : "";
179 subtitle += createMetaChartSubtitle(locale, result);
180 }
181
182 return subtitle;
183 }
184
185
186 protected String createMetaChartSubtitle(Locale locale, Result result) {
187 log.debug("Fetch meta information and put it into subtitle.");
188 if (result == null)
189 return "";
190
191 StringBuilder meta = new StringBuilder();
192 WKTReader wktReader = new WKTReader();
193
194
195 for (int i = 0; i < CHART_TITLE_META.length; i++) {
196 String qry = CHART_TITLE_META[i];
197
198 if (qry.equals("SHAPE")) {
199 try {
200 Point p = (Point) wktReader.read(result.getString(qry));
201
202 meta.append(getMessage(locale,"coordinate","coordinate"));
203 meta.append(": ");
204
205 log.debug(
206 "Add " + qry + " to meta information of subtitle: "
207 + p.toString()
208 );
209 meta.append(p.getX() + ", " + p.getY());
210 }
211 catch (ParseException pe) {
212 log.warn("Error while parsing point.", pe);
213 }
214 }
215 else {
216 log.debug(
217 "Add " + qry + " to meta information of subtitle: "
218 + result.getString(qry)
219 );
220 meta.append(getMessage(
221 locale,
222 CHART_TITLE_META_RESSOURCES[i],
223 CHART_TITLE_META_RESSOURCES[i]
224 ));
225 meta.append(": ");
226 meta.append(result.getString(qry));
227 }
228
229 if (i != CHART_TITLE_META.length-1)
230 meta.append("\n");
231 }
232
233 log.debug("Meta title for chart: " + meta.toString());
234 return meta.toString();
235 }
236
237
238 protected String createTimePeriod(Locale locale, String uuid) {
239 log.debug("create time period for chart subtitle.");
240 String subTitle = null;
241 Date startDate = null;
242 Date endDate = null;
243
244 Collection dates = getDates(uuid);
245 if (dates == null) {
246 log.debug("No time period for subtitle.");
247 return "";
248 }
249
250 SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
251 KeyValueDescibeData data = null;
252
253 Iterator iter = dates.iterator();
254 while (iter.hasNext()) {
255 try {
256 data = (KeyValueDescibeData)iter.next();
257
258 if (!data.isSelected())
259 continue;
260
261 Date current = format.parse(data.getValue());
262 long time = current.getTime();
263
264 if (startDate == null) {
265 startDate = current;
266 endDate = current;
267 }
268 else if (time < startDate.getTime()) {
269 startDate = current;
270 }
271 else if (time > endDate.getTime()) {
272 endDate = current;
273 }
274 }
275 catch (java.text.ParseException pe) {
276 log.warn("Error while parsing date: " + data.getValue(), pe);
277 }
278 }
279
280 subTitle = format.format(startDate) + " - " + format.format(endDate);
281 log.debug("created title for horizontal profile chart: " + subTitle);
282
283 return subTitle;
284 }
285 }

http://dive4elements.wald.intevation.org