view gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxState.java @ 793:2047f318e964

Added missing vim control comments. gnv-artifacts/trunk@875 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 31 Mar 2010 10:56:39 +0000
parents c4156275c1e1
children a5526908f92f
line wrap: on
line source
package de.intevation.gnv.state;

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;

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;

/**
 * @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();
    }

    /**
     * @see de.intevation.gnv.state.StateBase#purifyResult(java.util.Collection,
     *      java.lang.String)
     */
    @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;
    }


    /**
     * @see de.intevation.gnv.state.StateBase#feed(java.util.Collection,
     *      java.lang.String)
     */
    @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 = iv.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 (!iv.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