view gnv-artifacts/src/main/java/de/intevation/gnv/state/SingleInputState.java @ 1115:f953c9a559d8

Added license file and license headers. gnv-artifacts/trunk@1260 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:46:55 +0000
parents 9981452c7e75
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.state;

import de.intevation.artifacts.CallContext;

import de.intevation.gnv.artifacts.ressource.RessourceFactory;
import de.intevation.gnv.geobackend.base.Result;
import de.intevation.gnv.state.describedata.DefaultSingleValueDescribeData;
import de.intevation.gnv.state.exception.StateException;
import de.intevation.gnv.utils.InputValidator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;

/**
 * This state handles single user input. The user is allowed to select just one
 * value.
 *
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 *
 */
public class SingleInputState extends StateBase {

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

    private static final long serialVersionUID = -6169497306324917318L;

    /**
     * Constructor
     */
    public SingleInputState() {
    }


    @Override
    protected List<Object> purifyResult(Collection<Result> result, String uuid) {
        log.debug("SingleInputState.purifyResult");
        List<Object> describeData = new ArrayList<Object>();

        String value = null;
        if (result != null && result.size() == 1) {
            Result tmpItem = result.iterator().next();
            value = tmpItem.getObject("MAX").toString();
        } else {
            value = "";
        }

        describeData.add(new DefaultSingleValueDescribeData(
            this.dataName, value, getID()));

        return describeData;
    }

    /**
     * This feed method needs a collection of two InputData objects. These
     * objects' values need to be a datetime string which is turned into a Date
     * object. Afterwards, the given dates are validated. Min and max date need
     * to be in range of the min and max date retrieved by
     * {@link #getDescibeData(java.lang.String)}.
     */
    @Override
    public Document feed(
        CallContext           context,
        Collection<InputData> inputData,
        String                uuid)
    throws StateException {
        RessourceFactory resFactory = RessourceFactory.getInstance();
        Locale[] serverLocales      = resFactory.getLocales();
        Locale locale               = context.getMeta().getPreferredLocale(
            serverLocales);

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

        Iterator<InputData> it = inputData.iterator();
        InputData tmpItem      = it.next();
        InputValue inputValue  = inputValues.get(tmpItem.getName());

        if (inputValue == null) {
            String msg = resFactory.getRessource(
                locale,
                EXCEPTION_INVALID_INPUT,
                EXCEPTION_INVALID_INPUT);
            log.warn(msg);
            return feedFailure(msg);
        }

        boolean valid = InputValidator.isInputValid(
            tmpItem.getValue(), inputValue.getType());

        if (valid) {
            String[] desc = getDescriptionForInputData(tmpItem, context, uuid);
            tmpItem.setDescription(desc);

            this.inputData.put(tmpItem.getName(), tmpItem);
            return feedSuccess();
        }

        else {
            String msg = resFactory.getRessource(
                locale,
                EXCEPTION_INVALID_INPUT,
                EXCEPTION_INVALID_INPUT);
            log.warn(msg);
            return feedFailure(msg);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org