view gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxState.java @ 856:c9996913ff4b

Adjusted the subtitle creation for charts in 'Horizontalen Schnittprofilen' (see issue138). gnv-artifacts/trunk@974 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 23 Apr 2010 12:26:31 +0000
parents 499cfbbb61bc
children 41630b457b6a
line wrap: on
line source
package de.intevation.gnv.state;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import de.intevation.artifactdatabase.XMLUtils;
import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.CallMeta;
import de.intevation.gnv.artifacts.ressource.RessourceFactory;
import de.intevation.gnv.geobackend.base.Result;
import de.intevation.gnv.geobackend.util.DateUtils;
import de.intevation.gnv.state.describedata.DefaultMinMaxDescribeData;
import de.intevation.gnv.state.describedata.DescribeData;
import de.intevation.gnv.state.describedata.MinMaxDescribeData;
import de.intevation.gnv.state.exception.StateException;
import de.intevation.gnv.utils.InputValidator;

/**
 * This state handles input of a min and max value and validates the user input.
 * The min value needs to be equal or smaller than the max value, otherwise the
 * input results in an error.
 *
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class MinMaxState extends StateBase {

    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = Logger.getLogger(MinMaxState.class);

    /**
     * Constructor
     */
    public MinMaxState() {
        super();
    }

    /**
     * The objects returned by the database are searched for two fields with
     * 'MIN' and 'MAX' as names. These objects are stored and used to be
     * displayed in the gui to give the user an orientation of the range he is
     * able to insert.
     */
    @Override
    protected List<Object> purifyResult(
        Collection<Result> result, String uuid)
    {
        List<Object> describeData = new ArrayList<Object>();

        if (result != null && result.size() == 1) {
            Result value = result.iterator().next();
            DescribeData values = new DefaultMinMaxDescribeData(
                dataName,
                value.getObject("MIN"),
                value.getObject("MAX"),
                getID());
            describeData.add(values);
        } else {
            log.warn("Result cannot be handled as MinMax Resultset");
        }

        return describeData;
    }


    @Override
    public Document feed(
        CallContext           context,
        Collection<InputData> inputData,
        String                uuid)
    throws StateException {
        RessourceFactory resFactory = RessourceFactory.getInstance();

        if (inputData == null) {
            String msg = "No input data given.";
            log.warn(msg);
            return feedFailure(msg);
        }

        InputValidator iv = new InputValidator();
        Iterator iter     = inputData.iterator();

        Object min = null;
        Object max = null;

        while (iter.hasNext()) {
            InputData  tmp   = (InputData) iter.next();
            InputValue meta  = inputValues.get(tmp.getName());
            String     type  = meta.getType();
            String     value = tmp.getValue();
            String     name  = tmp.getName();

            if (meta == null) {
                String msg = "Input data not expected here. Data will be ignored.";
                log.warn(msg);
                return feedFailure(msg);
            }

            boolean valid = InputValidator.isInputValid(value, type);
            if (!valid) {
                String msg = "Input is not valid for this state.";
                log.error(msg);
                return feedFailure(msg);
            }

            if (name.equals(MINVALUEFIELDNAME)) {
                min = value;
            }

            if (name.equals(MAXVALUEFIELDNAME)) {
                max = value;
            }

            if (min != null && max != null) {
                if (!InputValidator.isInputValid((String) min, (String) max, type)) {
                    String msg = "Input is not valid for this state.";
                    log.error(msg);
                    return feedFailure(msg);
                }
            }
        }



        DescribeData values = new DefaultMinMaxDescribeData(
            dataName, min, max, getID());

        this.inputData.put(dataName, new DefaultInputData(dataName, values));

        return feedSuccess();
    }


    @Override
    protected void appendToStaticNode(
        XMLUtils.ElementCreator artCreator,
        XMLUtils.ElementCreator creator,
        Document                document,
        Node                    staticNode,
        CallMeta                callMeta
    ) {
        InputData  data = inputData.get(dataName);

        if (data == null) {
            return;
        }

        MinMaxDescribeData minMax = (MinMaxDescribeData) data.getObject();

        Object min = minMax.getMinValue();
        Object max = minMax.getMaxValue();
        if (min instanceof GregorianCalendar) {
            Date d = ((GregorianCalendar) min).getTime();
            min = DateUtils.getPatternedDateAmer(d);
        }

        if (max instanceof GregorianCalendar) {
            Date d = ((GregorianCalendar) max).getTime();
            max = DateUtils.getPatternedDateAmer(d);
        }

        Element groupNode = creator.create("group");
        artCreator.addAttr(groupNode, "state", minMax.getState(), true);

        creator.addAttr(groupNode, "ref", minMax.getName());
        Element groupNodeLableNode = creator.create("label");
        groupNodeLableNode.setTextContent(RessourceFactory
                .getInstance().getRessource(
                        callMeta.getLanguages(),
                        minMax.getName(),
                        minMax.getName()));
        groupNode.appendChild(groupNodeLableNode);

        Element inputMinNode = creator.create("input");
        creator.addAttr(inputMinNode, "ref", MINVALUEFIELDNAME);
        Element inputMinLableNode = creator.create("label");
        inputMinLableNode.setTextContent(RessourceFactory
                .getInstance().getRessource(
                        callMeta.getLanguages(), MINVALUEFIELDNAME,
                        MINVALUEFIELDNAME));
        inputMinNode.appendChild(inputMinLableNode);

        Element inputMinValueNode = creator.create("value");
        inputMinValueNode.setTextContent(min.toString());
        inputMinNode.appendChild(inputMinValueNode);

        Element inputMaxNode = creator.create("input");
        creator.addAttr(inputMaxNode, "ref", MAXVALUEFIELDNAME);
        Element inputMaxLableNode = creator.create("label");
        inputMaxLableNode.setTextContent(RessourceFactory
                .getInstance().getRessource(
                        callMeta.getLanguages(), MAXVALUEFIELDNAME,
                        MAXVALUEFIELDNAME));
        inputMaxNode.appendChild(inputMaxLableNode);

        Element inputMaxValueNode = creator.create("value");
        inputMaxValueNode.setTextContent(max.toString());
        inputMaxNode.appendChild(inputMaxValueNode);

        groupNode.appendChild(inputMinNode);
        groupNode.appendChild(inputMaxNode);

        staticNode.appendChild(groupNode);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org