diff gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java @ 480:211cad2fb5ba

Rebased "Horizonalschnitte" to own state class to break from the not well fitting TimeSeriesOutputState. gnv-artifacts/trunk@552 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 17 Jan 2010 12:22:56 +0000
parents ab29e4ff2fda
children 20dde2b6f1b5
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java	Fri Jan 15 20:39:30 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontalcrosssection/HorizontalCrossSectionMeshOutputState.java	Sun Jan 17 12:22:56 2010 +0000
@@ -3,65 +3,59 @@
  */
 package de.intevation.gnv.state.profile.horizontalcrosssection;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-
-import org.apache.log4j.Logger;
-
-import org.jfree.chart.ChartTheme;
-
-import org.w3c.dom.Node;
-
 import com.vividsolutions.jts.geom.Coordinate;
 import com.vividsolutions.jts.geom.Envelope;
 import com.vividsolutions.jts.geom.Polygon;
 
-import au.com.bytecode.opencsv.CSVWriter;
+import de.intevation.artifactdatabase.Config;
+import de.intevation.artifactdatabase.XMLUtils;
+
+import de.intevation.artifacts.ArtifactNamespaceContext;
+import de.intevation.artifacts.CallContext;
 
 import de.intevation.gnv.artifacts.cache.CacheFactory;
+
 import de.intevation.gnv.artifacts.context.GNVArtifactContext;
 
-import de.intevation.gnv.chart.Chart;
-import de.intevation.gnv.chart.ChartLabels;
-
 import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.geobackend.base.ResultDescriptor;
 
 import de.intevation.gnv.geobackend.base.query.QueryExecutor;
 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
+
 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
 
+import de.intevation.gnv.geobackend.sde.datasources.RasterObject;
+
+import de.intevation.gnv.math.AttributedPoint2ds;
+
 import de.intevation.gnv.state.InputData;
+import de.intevation.gnv.state.OutputStateBase;
 
 import de.intevation.gnv.state.exception.StateException;
 
 import de.intevation.gnv.state.timeseries.TimeSeriesOutputState;
 
-import de.intevation.gnv.statistics.Statistics;
-
 import de.intevation.gnv.utils.StringUtils;
 import de.intevation.gnv.utils.WKTUtils;
 
-import de.intevation.artifactdatabase.Config;
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
 
-import de.intevation.artifacts.CallContext;
+import java.util.Collection;
 
-import de.intevation.gnv.geobackend.sde.datasources.RasterObject;
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
 
 /**
  * @author Tim Englich         (tim.englich@intevation.de)
  * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
  */
 public class HorizontalCrossSectionMeshOutputState 
-extends      TimeSeriesOutputState
+extends      OutputStateBase
 {
     private static Logger log = Logger
         .getLogger(HorizontalCrossSectionMeshOutputState.class);
@@ -77,188 +71,165 @@
      * Constructor
      */
     public HorizontalCrossSectionMeshOutputState() {
-        super();
-        super.domainLable = "chart.horizontalcrosssection.title.xaxis";
     }
 
-    @Override
-    protected Chart getChart(
-        ChartLabels  chartLables,
-        ChartTheme   theme,
-        Collection   parameters,
-        Collection   measurements,
-        Collection   dates,
-        Object       result,
-        Locale       locale,
-        String       uuid,
-        boolean      linesVisible,
-        boolean      shapesVisible,
-        CallContext  callContext
-    ) {
-        Chart chart = null;
+    public void out(
+        Document              format, 
+        Collection<InputData> inputData,
+        OutputStream          outputStream,
+        String                uuid,
+        CallContext           callContext
+    )
+    throws StateException 
+    {
+        String outputMode = XMLUtils.xpathString(
+            format, XPATH_OUTPUT_MODE, ArtifactNamespaceContext.INSTANCE);
 
-        if (CACHE_CHART) {
-            log.info("Try to get horizontalcrosssection chart from cache.");
-            chart = (Chart) getChartFromCache(uuid, callContext);
+        String mimeType = XMLUtils.xpathString(
+            format, XPATH_MIME_TYPE, ArtifactNamespaceContext.INSTANCE);
+
+        if (outputMode == null || mimeType == null) {
+            throw new StateException("cannot find outputMode or mime");
         }
 
-        if (chart != null)
-            return chart;
-
-        log.info("Chart not in cache yet.");
-        
-        log.warn("This sort of chart is not implemented yet.");
-        
-        InputData meshPolygon = inputData.get("mesh_polygon");
-        String meshPolygonWkt = null;
-        if (meshPolygon != null){
-            meshPolygonWkt = meshPolygon.getValue();
-            
-        }
-        log.debug("Used Polygon: "+meshPolygonWkt);
-
-        return chart;
-    }
-    
-    /**
-     * @see de.intevation.gnv.state.OutputStateBase#getChartResult(java.lang.String, de.intevation.artifacts.CallContext)
-     */
-    @Override
-    protected Object getChartResult(String uuid, CallContext callContext) {
-        log.debug("HorizontalProfileMeshCrossOutputState.getChartResult");
-        Collection<Result> result = null;
-        if (CacheFactory.getInstance().isInitialized()) {
-            String key = uuid + super.getID();
-            log.debug("Hash for Queryelements: " + key);
-            net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
-            if (value != null) {
-                result = (Collection<Result>) (value.getObjectValue());
-            }else{
-
-                InputData meshPolygon = inputData.get("mesh_polygon");
-                InputData meshId   = inputData.get("meshid");
-
-                if (meshPolygon == null) {
-                    log.error("mesh_polygon is not defined");
-                    throw new IllegalStateException("missing mesh_linestring");
-                }
-
-                if (meshId == null) {
-                    log.error("meshid is not defined");
-                    throw new IllegalStateException("missing meshid");
-                }
+        outputMode = outputMode.toLowerCase();
 
-                Polygon p = WKTUtils.toPolygon(meshPolygon.getValue());
-
-                if (p == null) {
-                    log.error("no valid polygon");
-                    throw new IllegalStateException("no valid polygon");
-                }
-
-                try {
-                    Envelope env  = p.getEnvelopeInternal();
-
-                    Coordinate [] coords = new Coordinate [] {
-                        new Coordinate(env.getMinX(), env.getMinY()),
-                        new Coordinate(env.getMinX(), env.getMaxY()),
-                        new Coordinate(env.getMaxX(), env.getMaxY()),
-                        new Coordinate(env.getMaxX(), env.getMinY()) };
-
-                    String additionWhere =
-                        WKTUtils.worldEnvelopeCoordinatesToIndex(
-                            coords,
-                            result,
-                            meshId.getValue(),
-                            ijkQueryID);
+        log.debug("---- asking for: " + outputMode);
 
-                    String[] addedFilterValues = StringUtils.append(
-                        generateFilterValuesFromInputData(),
-                        additionWhere);
-
-                    QueryExecutor queryExecutor = QueryExecutorFactory
-                        .getInstance()
-                        .getQueryExecutor();
-
-                    result = process(
-                        Arrays.asList(coords),
-                        numSamples(callContext),
-                        queryExecutor.executeQuery(
-                            queryID,
-                            addedFilterValues),
-                            p);
-                }
-                catch (QueryException e) {
-                    log.error(e,e);
-                }
-
-                if (CacheFactory.getInstance().isInitialized()) {
-                    CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, result));
-                }
+        if ("zip".equals(outputMode)) {
+        }
+        else if ("wms".equals(outputMode)) {
+        }
+        else if ("statistics".equals(outputMode)) {
+            // TODO: REMOVE THIS!
+            try {
+                outputStream.write("<fake/>\n".getBytes());
+            }
+            catch (IOException ioe) {
             }
         }
+        else {
+            throw new StateException("unsupported output mode");
+        }
+    }
+
+    protected Object getResult(String uuid, CallContext callContext)
+    throws StateException
+    {
+        CacheFactory cf  = CacheFactory.getInstance();
+        String       key = uuid + super.getID();
+
+        if (cf.isInitialized()) {
+            net.sf.ehcache.Element value = cf.getCache().get(key);
+            if (value != null) {
+                 return value.getObjectValue();
+            }
+        }
+
+        Object result = produceResult(callContext);
+
+        if (result != null && cf.isInitialized()) {
+            cf.getCache().put(new net.sf.ehcache.Element(key, result));
+        }
+
         return result;
     }
     
-    public static Collection<Result> process(
-            List<Coordinate>   path,
-            int                numSamples,
-            Collection<Result> input,
-            Polygon polygon
-        ) {
-        // TODO: IMPLEMENT ME INTEGRATE POLYGONCLIPPING
-        return null;
+    protected Object produceResult(CallContext callContext) 
+    throws StateException
+    {
+        InputData meshPolygon = inputData.get("mesh_polygon");
+        InputData meshId      = inputData.get("meshid");
+
+        if (meshPolygon == null) {
+            log.error("mesh_polygon is not defined");
+            throw new StateException("missing mesh_linestring");
+        }
+
+        if (meshId == null) {
+            log.error("meshid is not defined");
+            throw new StateException("missing meshid");
+        }
+
+        Polygon p = WKTUtils.toPolygon(meshPolygon.getValue());
+
+        if (p == null) {
+            log.error("no valid polygon");
+            throw new StateException("no valid polygon");
+        }
+
+        try {
+            Envelope env  = p.getEnvelopeInternal();
+
+            Coordinate [] coords = new Coordinate [] {
+                new Coordinate(env.getMinX(), env.getMinY()),
+                new Coordinate(env.getMinX(), env.getMaxY()),
+                new Coordinate(env.getMaxX(), env.getMaxY()),
+                new Coordinate(env.getMaxX(), env.getMinY()) };
+
+            String additionWhere =
+                WKTUtils.worldEnvelopeCoordinatesToIndex(
+                    coords,
+                    meshId.getValue(),
+                    ijkQueryID);
+
+            String[] addedFilterValues = StringUtils.append(
+                generateFilterValuesFromInputData(),
+                additionWhere);
+
+            QueryExecutor queryExecutor = QueryExecutorFactory
+                .getInstance()
+                .getQueryExecutor();
+
+            return process(
+                env,
+                p,
+                numSamples(callContext),
+                preprocess(
+                    queryExecutor.executeQuery(
+                        queryID,
+                        addedFilterValues)));
+        }
+        catch (QueryException e) {
+            log.error(e,e);
+        }
+
+        throw new StateException("no result produced");
     }
 
-    /**
-     * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#getStatisticsGenerator()
-     */
-    @Override
-    protected Statistics getStatisticsGenerator() {
-        return null; //Statistics are not supported for this kind of OutputState.
+    public AttributedPoint2ds preprocess(Collection<Result> results) {
+
+        boolean debug = log.isDebugEnabled();
+
+        if (debug) {
+            log.debug("--- preprocess: " + results.size() + " results");
+        }
+
+        AttributedPoint2ds ap2ds = new AttributedPoint2ds();
+
+        boolean first = true;
+
+        for (Result result: results) {
+
+            if (debug && first) {
+                first = false;
+                ResultDescriptor rd = result.getResultDescriptor();
+                log.debug(rd);
+            }
+        }
+
+        return ap2ds;
     }
     
-    /**
-     * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#createCSV(java.io.OutputStream,
-     *      java.util.Collection)
-     */
-    @Override
-    protected void createCSV(
-        OutputStream       outputStream,
-        Collection<Result> chartResult
-    ) throws UnsupportedEncodingException, IOException, StateException 
-    {
-        /*
-        if (chartResult != null) {
-            try {
-                CSVWriter writer = new CSVWriter(new OutputStreamWriter(
-                        outputStream, "ISO-8859-1"), ','); 
-                // USE THIS ENCODING BECAUSE OF
-                // PROBLEMS WITH EXCEL AND UTF-8
-                Iterator<Result> it = chartResult.iterator();
-                while (it.hasNext()) {
-                    Result result = it.next();
-                    int i = 0;
-                    String[] entries = new String[5];
-                    entries[i++] = result.getString("SHAPE");
-                    entries[i++] = result.getString("YORDINATE");
-                    entries[i++] = result.getString("IPOSITION");
-                    entries[i++] = result.getString("JPOSITION");
-                    entries[i++] = result.getString("KPOSITION");
-                    writer.writeNext(entries);
-                }
-                writer.close();
-            } catch (Exception e) {
-                log.error(e,e);
-                throw new StateException(
-                "Exception occured while parsing an Point from WKT.");
-            }
-        } else {
-            log.error("No Data given for generating an CSV-File.");
-            throw new StateException(
-                    "No Data given for generating an CSV-File.");
-        }
-        */
+    public Object process(
+        Envelope           env,
+        Polygon            polygon,
+        int                numSamples,
+        AttributedPoint2ds input
+    ) {
+        return input;
     }
+
     
     /**
      * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#setup(org.w3c.dom.Node)

http://dive4elements.wald.intevation.org