Mercurial > dive4elements > gnv-client
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileOutputState.java Tue Dec 08 08:39:03 2009 +0000 @@ -0,0 +1,285 @@ +/** + * + */ +package de.intevation.gnv.state.profile.horizontal; + +import com.vividsolutions.jts.geom.Point; +import com.vividsolutions.jts.io.WKTReader; +import com.vividsolutions.jts.io.ParseException; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.text.SimpleDateFormat; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; +import java.util.Locale; + +import org.apache.log4j.Logger; + +import de.intevation.gnv.chart.Chart; +import de.intevation.gnv.chart.ChartLabels; +import de.intevation.gnv.chart.HorizontalProfileChart; +import de.intevation.gnv.geobackend.base.Result; +import de.intevation.gnv.state.describedata.KeyValueDescibeData; +import de.intevation.gnv.state.exception.StateException; +import de.intevation.gnv.state.timeseries.TimeSeriesOutputState; +import de.intevation.gnv.statistics.HorizontalProfileStatistics; +import de.intevation.gnv.statistics.Statistics; + +import de.intevation.gnv.exports.DefaultExport; +import de.intevation.gnv.exports.ShapeDataCollector; +import de.intevation.gnv.exports.DefaultProfile; +import de.intevation.gnv.exports.Export.Profile; + +/** + * @author Tim Englich <tim.englich@intevation.de> + * + */ +public class HorizontalProfileOutputState extends + TimeSeriesOutputState { + /** + * The UID of this class + */ + private static final long serialVersionUID = 4401516087492028840L; + + private static Logger log = Logger + .getLogger(HorizontalProfileOutputState.class); + + public static final String DATE_FORMAT = "yyyy.MM.dd HH:mm:ss"; + + public static final String [] CHART_TITLE_META = { + "CRUISE", + "DEPTH", + "SHAPE" + }; + + + public static final String [] CHART_TITLE_META_RESSOURCES = { + "cruiseid", + "depth", + "coordinate" + }; + + public static final String [] TIMESERIES_CSV_PROFILE_NAMES = { + "SHAPE", + "YORDINATE", + "GROUP1", + "GROUP2", + "GROUP3" + }; + + public static final Profile TIMESERIES_CSV_PROFILE = + new DefaultProfile( + null, + ',', + '"', + '"', + "CSV", + "ISO-8859-1"); + + /** + * Constructor + */ + public HorizontalProfileOutputState() { + super(); + super.domainLable = "Distance [km]"; + } + + + @Override + protected Chart getChart( + ChartLabels chartLables, + Collection parameters, + Collection measurements, + Collection dates, + Collection result, + Locale locale, + String uuid, + boolean linesVisible, + boolean shapesVisible + ) { + Chart chart = null; + + if (CACHE_CHART) { + log.info("Try to get horizontalprofile chart from cache."); + chart = (Chart) getChartFromCache(uuid); + } + + if (chart != null) + return chart; + + log.info("Chart not in cache yet."); + chart = new HorizontalProfileChart( + chartLables, + createStyle(), + parameters, + measurements, + dates, + result, + null, + locale, + linesVisible, + shapesVisible + ); + chart.generateChart(); + + if (CACHE_CHART) { + log.info("Put chart into cache."); + purifyChart(chart, uuid); + } + + return chart; + } + + + /** + * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#getStatisticsGenerator() + */ + @Override + protected Statistics getStatisticsGenerator() { + return new HorizontalProfileStatistics(); + } + + @Override + protected void createCSV(OutputStream outputStream, + Collection<Result> chartResult) + throws UnsupportedEncodingException, + IOException, + StateException { + + log.debug("createCSV for HorizontalProfileOutputState."); + DefaultExport export = new DefaultExport( + new ShapeDataCollector(TIMESERIES_CSV_PROFILE_NAMES)); + + export.create(TIMESERIES_CSV_PROFILE, outputStream, chartResult); + } + + + protected String createChartTitle(Locale locale, String uuid) { + String fisName = getFisName(locale); + log.debug("created title for horizontal profile chart: " + fisName); + + return fisName; + } + + + protected String createChartSubtitle(Locale locale, String uuid) { + log.debug("create chart subtitle."); + String subtitle = createTimePeriod(locale, uuid); + + // ODV results contain meta information about cruise, station and so on + Collection results = getODVResult(uuid); + if (results != null) { + Iterator iter = results.iterator(); + Result result = iter.hasNext() ? (Result) iter.next() : null; + + subtitle += subtitle.length() != 0 ? "\n" : ""; + subtitle += createMetaChartSubtitle(locale, result); + } + + return subtitle; + } + + + protected String createMetaChartSubtitle(Locale locale, Result result) { + log.debug("Fetch meta information and put it into subtitle."); + if (result == null) + return ""; + + StringBuilder meta = new StringBuilder(); + WKTReader wktReader = new WKTReader(); + + + for (int i = 0; i < CHART_TITLE_META.length; i++) { + String qry = CHART_TITLE_META[i]; + + if (qry.equals("SHAPE")) { + try { + Point p = (Point) wktReader.read(result.getString(qry)); + + meta.append(getMessage(locale,"coordinate","coordinate")); + meta.append(": "); + + log.debug( + "Add " + qry + " to meta information of subtitle: " + + p.toString() + ); + meta.append(p.getX() + ", " + p.getY()); + } + catch (ParseException pe) { + log.warn("Error while parsing point.", pe); + } + } + else { + log.debug( + "Add " + qry + " to meta information of subtitle: " + + result.getString(qry) + ); + meta.append(getMessage( + locale, + CHART_TITLE_META_RESSOURCES[i], + CHART_TITLE_META_RESSOURCES[i] + )); + meta.append(": "); + meta.append(result.getString(qry)); + } + + if (i != CHART_TITLE_META.length-1) + meta.append("\n"); + } + + log.debug("Meta title for chart: " + meta.toString()); + return meta.toString(); + } + + + protected String createTimePeriod(Locale locale, String uuid) { + log.debug("create time period for chart subtitle."); + String subTitle = null; + Date startDate = null; + Date endDate = null; + + Collection dates = getDates(uuid); + if (dates == null) { + log.debug("No time period for subtitle."); + return ""; + } + + SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT); + KeyValueDescibeData data = null; + + Iterator iter = dates.iterator(); + while (iter.hasNext()) { + try { + data = (KeyValueDescibeData)iter.next(); + + if (!data.isSelected()) + continue; + + Date current = format.parse(data.getValue()); + long time = current.getTime(); + + if (startDate == null) { + startDate = current; + endDate = current; + } + else if (time < startDate.getTime()) { + startDate = current; + } + else if (time > endDate.getTime()) { + endDate = current; + } + } + catch (java.text.ParseException pe) { + log.warn("Error while parsing date: " + data.getValue(), pe); + } + } + + subTitle = format.format(startDate) + " - " + format.format(endDate); + log.debug("created title for horizontal profile chart: " + subTitle); + + return subTitle; + } +}