ingo@2190: package de.intevation.flys.artifacts.states;
ingo@2190:
ingo@2190: import org.w3c.dom.Element;
ingo@2190:
ingo@2202: import org.apache.log4j.Logger;
ingo@2202:
ingo@2190: import de.intevation.artifacts.Artifact;
ingo@2190: import de.intevation.artifacts.CallContext;
ingo@2190:
ingo@2190: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@2190: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
ingo@2190:
ingo@2190: import de.intevation.artifactdatabase.ProtocolUtils;
ingo@2190:
ingo@2190: import de.intevation.flys.artifacts.FLYSArtifact;
ingo@2190:
ingo@2190:
ingo@2190: /**
ingo@2190: * @author Ingo Weinzierl
ingo@2190: */
ingo@2190: public abstract class MinMaxState extends DefaultState {
ingo@2190:
ingo@2202: private static final Logger logger = Logger.getLogger(MinMaxState.class);
ingo@2202:
ingo@2190: @Override
ingo@2190: protected void appendItems(
ingo@2190: Artifact artifact,
ingo@2190: ElementCreator creator,
ingo@2190: String name,
ingo@2190: CallContext context,
ingo@2190: Element select
ingo@2190: ) {
ingo@2190: FLYSArtifact flys = (FLYSArtifact) artifact;
ingo@2190:
ingo@2190: select.setAttributeNS(
ingo@2190: ArtifactNamespaceContext.NAMESPACE_URI,
ingo@2190: "art:type",
ingo@2190: getType());
ingo@2190:
ingo@2202: String[] defMinMax = getDefaults(artifact, name);
ingo@2202:
ingo@2190: Element min = ProtocolUtils.createArtNode(
ingo@2190: creator,
ingo@2190: "min",
ingo@2202: new String[] { "value", "default" },
ingo@2202: new String[] { String.valueOf(getLower(flys)), defMinMax[0] });
ingo@2190:
ingo@2190: Element max = ProtocolUtils.createArtNode(
ingo@2190: creator,
ingo@2190: "max",
ingo@2202: new String[] { "value", "default" },
ingo@2202: new String[] { String.valueOf(getUpper(flys)), defMinMax[1] });
ingo@2190:
ingo@2190: select.appendChild(min);
ingo@2190: select.appendChild(max);
ingo@2190: }
ingo@2190:
ingo@2190:
ingo@2202: /**
ingo@2205: * @param cc
ingo@2205: * @param name
ingo@2205: * @param value
ingo@2205: * @param type
ingo@2205: *
ingo@2205: * @return
ingo@2205: */
ingo@2205: @Override
ingo@2205: protected String getLabelFor(
ingo@2205: CallContext cc,
ingo@2205: String name,
ingo@2205: String value,
ingo@2205: String type
ingo@2205: ) {
ingo@2205: if (type.indexOf("range") > 0) {
ingo@2205: String[] minmax = extractRangeAsString(value);
ingo@2205:
ingo@2205: if (minmax != null) {
ingo@2205: return minmax[0] + " - " + minmax[1];
ingo@2205: }
ingo@2205: }
ingo@2205:
ingo@2205: return super.getLabelFor(cc, name, value, type);
ingo@2205: }
ingo@2205:
ingo@2205:
ingo@2205: /**
ingo@2205: * Returns a string array with [min,max] from rawValue.
ingo@2205: * rawValue should be a string like "1999;2001".
ingo@2205: *
ingo@2205: * @param rawValue A string with min and max separated by a ';'.
ingo@2205: *
ingo@2205: * @return the min and max as string array ([min,max]).
ingo@2205: */
ingo@2205: protected String[] extractRangeAsString(String rawValue) {
ingo@2205: return rawValue.split(";");
ingo@2205: }
ingo@2205:
ingo@2205:
ingo@2205: /**
ingo@2202: * This method returns the default values for min and max. If the static
ingo@2202: * field DefaultState.USE_DEFAULTS is set, the minimum and maximum inserted
ingo@2202: * by the user is returned as string. Otherwise, the absolute minimum and
ingo@2202: * maximum are returned.
ingo@2202: *
ingo@2202: * @param artifact The FLYSArtifact.
ingo@2202: * @param name The name of the parameter.
ingo@2202: *
ingo@2202: * @return a string array [min,max] that contains the minimum and maximum
ingo@2202: * values for the parameter name.
ingo@2202: */
ingo@2202: protected String[] getDefaults(Artifact artifact, String name) {
ingo@2202: if (DefaultState.USE_DEFAULTS) {
ingo@2202: String[] tmp = getMinMaxByParameter(artifact, name);
ingo@2202:
ingo@2202: return tmp != null ? tmp : getMinMaxDefaults(artifact, name);
ingo@2202: }
ingo@2202: else {
ingo@2202: return getMinMaxDefaults(artifact, name);
ingo@2202: }
ingo@2202: }
ingo@2202:
ingo@2202:
ingo@2202: /**
ingo@2202: * Returns a string array with minimum and maximum inserted by the user as
ingo@2202: * [min,max].
ingo@2202: *
ingo@2202: * @param artifact The FLYSArtifact that stores the parameter.
ingo@2202: * @param name The name of the parameter for raw min/max value string.
ingo@2202: *
ingo@2202: * @return a string array [min,max].
ingo@2202: */
ingo@2202: protected String[] getMinMaxByParameter(Artifact artifact, String name) {
ingo@2202: FLYSArtifact flys = (FLYSArtifact) artifact;
ingo@2202: String rawValue = flys.getDataAsString(name);
ingo@2202:
ingo@2202: if (rawValue == null) {
ingo@2202: logger.debug("No value for '" + rawValue + "' existing.");
ingo@2202: return null;
ingo@2202: }
ingo@2202:
ingo@2202: logger.debug("Raw value for '" + name + "' = " + rawValue);
ingo@2202:
ingo@2205: return extractRangeAsString(rawValue);
ingo@2202: }
ingo@2202:
ingo@2202:
ingo@2202: /**
ingo@2202: * Returns a string array with absolute minimum and maximum as [min,max].
ingo@2202: *
ingo@2202: * @param artifact The FLYSArtifact (not used in this implementation).
ingo@2202: * @param name The parameter name (not used in this implementation).
ingo@2202: *
ingo@2202: * @return a string array [min,max].
ingo@2202: */
ingo@2202: protected String[] getMinMaxDefaults(Artifact artifact, String name) {
ingo@2202: FLYSArtifact flys = (FLYSArtifact) artifact;
ingo@2202:
ingo@2202: Object lower = getLower(flys);
ingo@2202: Object upper = getUpper(flys);
ingo@2202:
ingo@2202: return new String[] { String.valueOf(lower), String.valueOf(upper) };
ingo@2202: }
ingo@2202:
ingo@2202:
ingo@2190: protected abstract Object getLower(FLYSArtifact flys);
ingo@2190:
ingo@2190: protected abstract Object getUpper(FLYSArtifact flys);
ingo@2190:
ingo@2190: protected abstract String getType();
ingo@2190: }
ingo@2190: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :