view gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxState.java @ 639:a94ed2755480

Implemented SVG export for histograms. gnv-artifacts/trunk@724 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Mar 2010 14:48:32 +0000
parents 65f09139e9b3
children dde7f51dbe1e
line wrap: on
line source
package de.intevation.gnv.state;

import de.intevation.artifactdatabase.XMLUtils;

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 Tim Englich (tim.englich@intevation.de)
 * @author Ingo Weinzierl (ingo.weinzierl@intevation.de)
 */
public class MinMaxState extends StateBase {

    /**
     * The UID of this Class
     */
    private static final long serialVersionUID = -3525903792105187408L;
    /**
     * 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 void feed(Collection<InputData> inputData, String uuid)
    throws StateException
    {
        if (inputData == null)
            return;

        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     value = tmp.getValue();
            String     name  = tmp.getName();

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

            boolean valid = iv.isInputValid(value, meta.getType());
            if (!valid) {
                String msg = "Input is not valid for this state.";
                log.error(msg);
                throw new StateException(msg);
            }

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

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

        if (min != null && max != null) {
            DescribeData values = new DefaultMinMaxDescribeData(
                dataName, min, max, getID());

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


    @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