ingo@921: package de.intevation.flys.artifacts.states;
ingo@921:
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@921: import de.intevation.flys.model.River;
ingo@921:
ingo@921: import de.intevation.flys.artifacts.FLYSArtifact;
ingo@921: import de.intevation.flys.artifacts.WINFOArtifact;
ingo@921: import de.intevation.flys.artifacts.model.CalculationResult;
ingo@921: import de.intevation.flys.artifacts.model.FacetTypes;
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:
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:
ingo@921: /** The default step width.*/
ingo@921: public static final int DEFAULT_STEP = 100;
ingo@921:
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:
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@921: String name = 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@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 :