ingo@921: package de.intevation.flys.artifacts.states; ingo@921: ingo@1712: import java.util.Date; ingo@921: import java.util.List; ingo@921: ingo@921: import org.apache.log4j.Logger; ingo@921: ingo@921: import org.w3c.dom.Element; ingo@921: ingo@921: import de.intevation.artifacts.Artifact; ingo@921: import de.intevation.artifacts.CallContext; ingo@921: ingo@921: import de.intevation.artifacts.common.utils.XMLUtils; ingo@921: ingo@921: import de.intevation.artifactdatabase.ProtocolUtils; ingo@921: import de.intevation.artifactdatabase.data.StateData; ingo@921: import de.intevation.artifactdatabase.state.Facet; ingo@921: ingo@1712: import de.intevation.flys.model.DischargeTable; ingo@1712: import de.intevation.flys.model.Gauge; ingo@921: import de.intevation.flys.model.River; ingo@1712: import de.intevation.flys.model.TimeInterval; ingo@921: ingo@921: import de.intevation.flys.artifacts.FLYSArtifact; ingo@921: import de.intevation.flys.artifacts.WINFOArtifact; raimund@2132: import de.intevation.flys.artifacts.ChartArtifact; raimund@2132: ingo@921: import de.intevation.flys.artifacts.model.CalculationResult; ingo@921: import de.intevation.flys.artifacts.model.FacetTypes; ingo@1712: import de.intevation.flys.artifacts.model.GaugesFactory; ingo@921: import de.intevation.flys.artifacts.model.RiverFactory; ingo@921: import de.intevation.flys.artifacts.model.WaterlevelFacet; ingo@921: import de.intevation.flys.artifacts.model.WQKms; ingo@921: import de.intevation.flys.artifacts.resources.Resources; ingo@921: ingo@921: /** ingo@921: * @author Ingo Weinzierl ingo@921: */ ingo@921: public class ComputationRangeState ingo@921: extends RangeState ingo@921: implements FacetTypes ingo@921: { ingo@921: private static Logger logger = ingo@921: Logger.getLogger(ComputationRangeState.class); ingo@921: ingo@921: /** The name of the 'from' field. */ ingo@921: public static final String FROM = "ld_from"; ingo@921: ingo@921: /** The name of the 'to' field. */ ingo@921: public static final String TO = "ld_to"; ingo@921: ingo@921: /** The name of the 'step' field. */ ingo@921: public static final String STEP = "ld_step"; ingo@921: felix@1685: /** The default step width. */ ingo@921: public static final int DEFAULT_STEP = 100; ingo@921: ingo@921: ingo@921: public ComputationRangeState() { ingo@921: } ingo@921: ingo@921: ingo@921: @Override ingo@921: protected Element createData( ingo@921: XMLUtils.ElementCreator cr, ingo@921: Artifact artifact, ingo@921: StateData data, ingo@921: CallContext context) ingo@921: { ingo@921: Element select = ProtocolUtils.createArtNode( ingo@921: cr, "select", null, null); ingo@921: ingo@921: cr.addAttr(select, "name", data.getName(), true); ingo@921: ingo@921: Element label = ProtocolUtils.createArtNode( ingo@921: cr, "label", null, null); ingo@921: ingo@921: Element choices = ProtocolUtils.createArtNode( ingo@921: cr, "choices", null, null); ingo@921: ingo@921: label.setTextContent(Resources.getMsg( ingo@921: context.getMeta(), ingo@921: data.getName(), ingo@921: data.getName())); ingo@921: ingo@921: select.appendChild(label); ingo@921: ingo@921: return select; ingo@921: } ingo@921: ingo@921: ingo@921: @Override ingo@921: protected Element[] createItems( ingo@921: XMLUtils.ElementCreator cr, ingo@921: Artifact artifact, ingo@921: String name, ingo@921: CallContext context) ingo@921: { ingo@921: double[] minmax = getMinMax(artifact); ingo@921: ingo@921: double minVal = Double.MIN_VALUE; ingo@921: double maxVal = Double.MAX_VALUE; ingo@921: ingo@921: if (minmax != null) { ingo@921: minVal = minmax[0]; ingo@921: maxVal = minmax[1]; ingo@921: } ingo@921: else { ingo@921: logger.warn("Could not read min/max distance values!"); ingo@921: } ingo@921: ingo@921: if (name.equals("ld_from")) { ingo@921: Element min = createItem( ingo@921: cr, ingo@921: new String[] {"min", new Double(minVal).toString()}); ingo@921: ingo@921: return new Element[] { min }; ingo@921: } ingo@921: else if (name.equals("ld_to")) { ingo@921: Element max = createItem( ingo@921: cr, ingo@921: new String[] {"max", new Double(maxVal).toString()}); ingo@921: ingo@921: return new Element[] { max }; ingo@921: } ingo@921: else { ingo@921: Element step = createItem( ingo@921: cr, ingo@921: new String[] {"step", String.valueOf(getDefaultStep())}); ingo@921: return new Element[] { step }; ingo@921: } ingo@921: ingo@921: } ingo@921: ingo@921: ingo@921: protected Element createItem(XMLUtils.ElementCreator cr, Object obj) { ingo@921: Element item = ProtocolUtils.createArtNode(cr, "item", null, null); ingo@921: Element label = ProtocolUtils.createArtNode(cr, "label", null, null); ingo@921: Element value = ProtocolUtils.createArtNode(cr, "value", null, null); ingo@921: ingo@921: String[] arr = (String[]) obj; ingo@921: ingo@921: label.setTextContent(arr[0]); ingo@921: value.setTextContent(arr[1]); ingo@921: ingo@921: item.appendChild(label); ingo@921: item.appendChild(value); ingo@921: ingo@921: return item; ingo@921: } ingo@921: ingo@921: ingo@921: @Override ingo@921: public Object computeFeed( ingo@921: FLYSArtifact artifact, ingo@921: String hash, ingo@921: CallContext context, ingo@921: List facets, ingo@921: Object old ingo@921: ) { ingo@921: logger.debug("computeFeed"); ingo@921: raimund@2132: if (artifact instanceof ChartArtifact) { raimund@2132: return null; raimund@2132: } ingo@921: WINFOArtifact winfo = (WINFOArtifact)artifact; ingo@921: ingo@921: CalculationResult res = old instanceof CalculationResult ingo@921: ? (CalculationResult)old ingo@921: : winfo.getDischargeCurveData(); ingo@921: ingo@921: if (facets == null) { ingo@921: logger.debug("generate no facets"); ingo@921: return res; ingo@921: } ingo@921: ingo@921: WQKms [] wqkms = (WQKms [])res.getData(); ingo@921: ingo@921: logger.debug("generate " + wqkms.length + " facets."); ingo@921: ingo@921: String stateID = winfo.getCurrentStateId(); ingo@921: ingo@921: for (int i = 0; i < wqkms.length; ++i) { ingo@1712: String name = getSeriesName(context, wqkms[i].getName()); ingo@921: facets.add(new WaterlevelFacet( ingo@921: i, DISCHARGE_CURVE, name, ComputeType.FEED, stateID, hash)); ingo@921: } ingo@921: ingo@921: ingo@921: return res; ingo@921: } ingo@921: ingo@1712: protected String getSeriesName(CallContext cc, String gaugeName) { ingo@1712: Gauge gauge = GaugesFactory.getGauge(gaugeName); ingo@1712: ingo@1712: if (gauge == null) { ingo@1712: logger.warn("Cannot determine Gauge for name: " + gaugeName); ingo@1712: return gaugeName; ingo@1712: } ingo@1712: ingo@1712: List dts = gauge.getDischargeTables(); ingo@1712: ingo@1712: for (DischargeTable dt: dts) { ingo@1712: if (dt.getKind() == 0) { ingo@1712: TimeInterval ti = dt.getTimeInterval(); ingo@1712: ingo@1712: Date start = ti.getStartTime(); ingo@1712: Date end = ti.getStopTime(); ingo@1712: ingo@1712: String name = gauge.getName(); ingo@1712: ingo@1712: if (end == null) { ingo@1712: Object[] args = new Object[] { name, start }; ingo@1712: return Resources.getMsg( ingo@1712: cc.getMeta(), ingo@1712: "chart.discharge.curve.curve.valid.from", ingo@1712: "", ingo@1712: args); ingo@1712: } ingo@1712: else { ingo@1712: Object[] args = new Object[] { name, start, end }; ingo@1712: return Resources.getMsg( ingo@1712: cc.getMeta(), ingo@1712: "chart.discharge.curve.curve.valid.range", ingo@1712: "", ingo@1712: args); ingo@1712: } ingo@1712: } ingo@1712: } ingo@1712: ingo@1712: return gauge.getName(); ingo@1712: } ingo@1712: ingo@921: ingo@921: @Override ingo@921: protected double[] getMinMax(Artifact artifact) { ingo@921: FLYSArtifact flysArtifact = (FLYSArtifact) artifact; ingo@921: StateData data = getData(flysArtifact, "river"); ingo@921: ingo@921: String name = data != null ? (String) data.getValue() : ""; ingo@921: ingo@921: logger.debug("Search for the min/max distances of '" + name + "'"); ingo@921: ingo@921: River river = RiverFactory.getRiver(name); ingo@921: ingo@921: return river != null ? river.determineMinMaxDistance() : null; ingo@921: } ingo@921: ingo@921: ingo@921: protected double getDefaultStep() { ingo@921: return DEFAULT_STEP; ingo@921: } ingo@921: ingo@921: ingo@921: @Override ingo@921: protected String getLowerField() { ingo@921: return FROM; ingo@921: } ingo@921: ingo@921: ingo@921: @Override ingo@921: protected String getUpperField() { ingo@921: return TO; ingo@921: } ingo@921: ingo@921: ingo@921: @Override ingo@921: protected String getStepField() { ingo@921: return STEP; ingo@921: } ingo@921: } ingo@921: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :