teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5863: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5863: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.states; ingo@927: ingo@927: import org.w3c.dom.Element; ingo@927: ingo@927: import org.apache.log4j.Logger; ingo@927: teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; felix@4865: teichmann@5831: import org.dive4elements.artifacts.Artifact; teichmann@5831: import org.dive4elements.artifacts.CallContext; ingo@927: teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils; ingo@927: teichmann@5831: import org.dive4elements.artifactdatabase.data.StateData; teichmann@5831: import org.dive4elements.artifactdatabase.ProtocolUtils; ingo@927: ingo@927: ingo@927: /** ingo@927: * @author Ingo Weinzierl ingo@927: */ ingo@927: public class WaterlevelGroundDifferences extends RangeState { ingo@927: ingo@927: public static final String LOWER_FIELD = "diff_from"; ingo@927: public static final String UPPER_FIELD = "diff_to"; ingo@927: public static final String DIFF_FIELD = "diff_diff"; ingo@927: ingo@927: public static final double DEFAULT_STEP = 0d; ingo@927: ingo@927: ingo@927: private static Logger logger = ingo@927: Logger.getLogger(WaterlevelGroundDifferences.class); ingo@927: ingo@927: ingo@927: protected String getLowerField() { ingo@927: return LOWER_FIELD; ingo@927: } ingo@927: ingo@927: ingo@927: protected String getUpperField() { ingo@927: return UPPER_FIELD; ingo@927: } ingo@927: ingo@927: ingo@927: protected String getStepField() { ingo@927: return DIFF_FIELD; ingo@927: } ingo@927: ingo@927: ingo@927: @Override ingo@927: protected double[] getMinMax(Artifact artifact) { ingo@927: return new double[] { -Double.MAX_VALUE, Double.MAX_VALUE }; ingo@927: } ingo@927: ingo@927: ingo@927: @Override ingo@927: protected String getUIProvider() { ingo@928: return "waterlevel_ground_panel"; ingo@927: } ingo@927: ingo@927: ingo@927: protected double getDefaultStep() { ingo@927: return DEFAULT_STEP; ingo@927: } ingo@927: ingo@927: ingo@927: @Override ingo@927: protected Element[] createItems( ingo@927: XMLUtils.ElementCreator cr, ingo@927: Artifact artifact, ingo@927: String name, ingo@927: CallContext context) ingo@927: { ingo@927: double[] minmax = getMinMax(artifact); ingo@927: ingo@927: double minVal = Double.MIN_VALUE; ingo@927: double maxVal = Double.MAX_VALUE; ingo@927: ingo@927: if (minmax != null) { ingo@927: minVal = minmax[0]; ingo@927: maxVal = minmax[1]; ingo@927: } ingo@927: else { ingo@927: logger.warn("Could not read min/max distance values!"); ingo@927: } ingo@927: ingo@927: if (name.equals(LOWER_FIELD)) { ingo@927: Element min = createItem( ingo@927: cr, ingo@927: new String[] {"min", new Double(minVal).toString()}); ingo@927: ingo@927: return new Element[] { min }; ingo@927: } ingo@927: else if (name.equals(UPPER_FIELD)) { ingo@927: Element max = createItem( ingo@927: cr, ingo@927: new String[] {"max", new Double(maxVal).toString()}); ingo@927: ingo@927: return new Element[] { max }; ingo@927: } ingo@927: else { ingo@927: Element step = createItem( ingo@927: cr, ingo@927: new String[] {"step", String.valueOf(getDefaultStep())}); ingo@927: return new Element[] { step }; ingo@927: } ingo@927: } ingo@927: ingo@927: ingo@927: protected Element createItem(XMLUtils.ElementCreator cr, Object obj) { ingo@927: Element item = ProtocolUtils.createArtNode(cr, "item", null, null); ingo@927: Element label = ProtocolUtils.createArtNode(cr, "label", null, null); ingo@927: Element value = ProtocolUtils.createArtNode(cr, "value", null, null); ingo@927: ingo@927: String[] arr = (String[]) obj; ingo@927: ingo@927: label.setTextContent(arr[0]); ingo@927: value.setTextContent(arr[1]); ingo@927: ingo@927: item.appendChild(label); ingo@927: item.appendChild(value); ingo@927: ingo@927: return item; ingo@927: } felix@4865: felix@4865: @Override felix@4865: public boolean validate(Artifact artifact) felix@4865: throws IllegalArgumentException felix@4865: { teichmann@5867: D4EArtifact flys = (D4EArtifact) artifact; felix@4865: felix@4865: StateData dFrom = getData(flys, getLowerField()); felix@4865: StateData dTo = getData(flys, getUpperField()); felix@4865: StateData dStep = getData(flys, getStepField()); felix@4865: felix@4865: String fromStr = dFrom != null ? (String) dFrom.getValue() : null; felix@4865: String toStr = dTo != null ? (String) dTo.getValue() : null; felix@4865: String stepStr = dStep != null ? (String) dStep.getValue() : null; felix@4865: felix@4865: if (fromStr == null || toStr == null || stepStr == null) { felix@4865: throw new IllegalArgumentException("error_empty_state"); felix@4865: } felix@4865: felix@4865: try { felix@4865: double from = Double.parseDouble(fromStr); felix@4865: double to = Double.parseDouble(toStr); felix@4865: double step = Double.parseDouble(stepStr); felix@4865: felix@4865: double[] minmax = getMinMax(flys); felix@4865: felix@4865: return validateBounds(minmax[0], minmax[1], from, to, step); felix@4865: } felix@4865: catch (NumberFormatException nfe) { felix@4865: throw new IllegalArgumentException("error_invalid_double_value"); felix@4865: } felix@4865: } ingo@927: } ingo@927: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :