ingo@927: package de.intevation.flys.artifacts.states;
ingo@927: 
ingo@927: import org.w3c.dom.Element;
ingo@927: 
ingo@927: import org.apache.log4j.Logger;
ingo@927: 
felix@4865: import de.intevation.flys.artifacts.FLYSArtifact;
felix@4865: 
ingo@927: import de.intevation.artifacts.Artifact;
ingo@927: import de.intevation.artifacts.CallContext;
ingo@927: 
ingo@927: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@927: 
felix@4865: import de.intevation.artifactdatabase.data.StateData;
ingo@927: import de.intevation.artifactdatabase.ProtocolUtils;
ingo@927: 
ingo@927: 
ingo@927: /**
ingo@927:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
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:     {
felix@4865:         FLYSArtifact flys = (FLYSArtifact) 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 :