Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java @ 293:3419b1c8ca28
Removed the Backend reference from FLYSArtifactCollection.
flys-artifacts/trunk@1629 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 30 Mar 2011 16:00:51 +0000 |
parents | fbbb22e1e206 |
children | 9a0e1289bab6 |
line wrap: on
line source
package de.intevation.flys.artifacts; import java.awt.Color; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Vector; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.apache.log4j.Logger; import org.jfree.data.xy.DefaultXYDataset; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import de.intevation.artifacts.ArtifactNamespaceContext; import de.intevation.artifacts.CallContext; import de.intevation.artifactdatabase.ProtocolUtils; import de.intevation.artifactdatabase.state.Output; import de.intevation.artifactdatabase.state.State; import de.intevation.artifactdatabase.state.StateEngine; import de.intevation.artifactdatabase.transition.TransitionEngine; import de.intevation.artifacts.common.utils.XMLUtils; import de.intevation.flys.model.Gauge; import de.intevation.flys.artifacts.model.DischargeTables; import de.intevation.flys.artifacts.model.GaugesFactory; import de.intevation.flys.artifacts.states.DefaultState; import de.intevation.flys.artifacts.context.FLYSContext; import de.intevation.flys.exports.ChartExportHelper; /** * The default WINFO artifact. * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class WINFOArtifact extends FLYSArtifact { /** The logger for this class */ private static Logger logger = Logger.getLogger(WINFOArtifact.class); /** The name of the artifact.*/ public static final String ARTIFACT_NAME = "winfo"; /** XPath */ public static final String XPATH_STATIC_UI ="/art:result/art:ui/art:static"; /** * The default constructor. */ public WINFOArtifact() { } /** * This method returns a description of this artifact. * * @param data Some data. * @param context The CallContext. * * @return the description of this artifact. */ public Document describe(Document data, CallContext context) { logger.debug("Describe: the current state is: " + getCurrentStateId()); FLYSContext flysContext = null; if (context instanceof FLYSContext) { flysContext = (FLYSContext) context; } else { flysContext = (FLYSContext) context.globalContext(); } StateEngine stateEngine = (StateEngine) flysContext.get( FLYSContext.STATE_ENGINE_KEY); TransitionEngine transitionEngine = (TransitionEngine) flysContext.get( FLYSContext.TRANSITION_ENGINE_KEY); List<State> reachable = transitionEngine.getReachableStates( getCurrentState(context), stateEngine); Document description = XMLUtils.newDocument(); XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( description, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); Element root = ProtocolUtils.createRootNode(creator); description.appendChild(root); State current = getCurrentState(context); ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash()); ProtocolUtils.appendState(creator, root, current); ProtocolUtils.appendReachableStates(creator, root, reachable); Element ui = ProtocolUtils.createArtNode( creator, "ui", null, null); Element staticUI = ProtocolUtils.createArtNode( creator, "static", null, null); Element outs = ProtocolUtils.createArtNode( creator, "outputmodes", null, null); appendOutputModes(description, outs, context, identifier()); appendStaticUI(description, staticUI, context, identifier()); Element dynamic = current.describe( description, root, context, identifier()); if (dynamic != null) { ui.appendChild(dynamic); } ui.appendChild(staticUI); root.appendChild(ui); root.appendChild(outs); return description; } @Override public void out( Document format, OutputStream out, CallContext context) throws IOException { logger.info("WINFOArtifact.out"); String river = (String) getData("river").getValue(); String from = (String) getData("ld_from").getValue(); String to = (String) getData("ld_to").getValue(); double f = Double.parseDouble(from); double t = Double.parseDouble(to); List<double[]> ranges = new ArrayList<double[]>(); ranges.add(new double [] { f, t }); List<Gauge> gauges = GaugesFactory.getGauges(river); List<Gauge> filtered = GaugesFactory.filterRanges(gauges, ranges); if (logger.isDebugEnabled()) { int numGauges = gauges != null ? gauges.size() : 0; int numFiltered = filtered != null ? filtered.size() : 0; logger.debug("++++++++++++++++++++"); logger.debug("Search gauges for river: " + river); logger.debug("-> ... from range: " + from); logger.debug("-> ... to range: " + to); logger.debug("-> Found " + numGauges + " gauges in total."); logger.debug("-> Found " + numFiltered + " gauges after filtering."); logger.debug("++++++++++++++++++++"); } String[] gaugeNames = new String[filtered.size()]; int idx = 0; for (Gauge gauge: filtered) { gaugeNames[idx++] = gauge.getName(); } DischargeTables dt = new DischargeTables(river, gaugeNames); Map<String, double[][]> gaugeValues = dt.getValues(100d); DefaultXYDataset dataset = new DefaultXYDataset(); for (String gauge: gaugeNames) { double[][] values = gaugeValues.get(gauge); dataset.addSeries(gauge, values); } JFreeChart chart = ChartFactory.createXYLineChart( "Abflusskurven (" + river + ")", "Q", "W", dataset, PlotOrientation.VERTICAL, true, false, false); chart.setBackgroundPaint(Color.WHITE); chart.getPlot().setBackgroundPaint(Color.WHITE); ChartExportHelper.exportImage( out, chart, "png", 600, 400); } /** * Returns the name of the concrete artifact. * * @return the name of the concrete artifact. */ public String getName() { return ARTIFACT_NAME; } protected void appendOutputModes( Document doc, Element outs, CallContext context, String uuid) { Vector<String> stateIds = getPreviousStateIds(); XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); FLYSContext flysContext = getFlysContext(context); StateEngine engine = (StateEngine) flysContext.get( FLYSContext.STATE_ENGINE_KEY); for (String stateId: stateIds) { logger.debug("Append output modes for state: " + stateId); State state = engine.getState(stateId); List<Output> list = state.getOutputs(); if (list == null || list.size() == 0) { continue; } ProtocolUtils.appendOutputModes(creator, outs, list); } // TODO If the current state is already filled with data, the output is // available as well! So we need some code to append the outputs of the // current already filled state. } /** * This method appends the static data - that has already been inserted by * the user - to the static node of the DESCRIBE document. * * @param doc The document. * @param ui The root node. * @param context The CallContext. * @param uuid The identifier of the artifact. */ protected void appendStaticUI( Document doc, Node ui, CallContext context, String uuid) { Vector<String> stateIds = getPreviousStateIds(); FLYSContext flysContext = getFlysContext(context); StateEngine engine = (StateEngine) flysContext.get( FLYSContext.STATE_ENGINE_KEY); for (String stateId: stateIds) { logger.debug("Append static data for state: " + stateId); DefaultState state = (DefaultState) engine.getState(stateId); ui.appendChild(state.describeStatic(doc, ui, context, uuid)); } } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :