view gnv-artifacts/src/main/java/de/intevation/gnv/state/MeasurementState.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 e764cab8c885
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.base.ResultDescriptor;

import de.intevation.gnv.state.describedata.ExtendedKeyValueData;
import de.intevation.gnv.state.describedata.KeyValueDescibeData;
import de.intevation.gnv.state.describedata.NamedArrayList;
import de.intevation.gnv.state.describedata.NamedCollection;

import de.intevation.gnv.state.exception.StateException;

import de.intevation.gnv.utils.InputValidator;

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

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class MeasurementState
extends      DefaultState
{
    private static Logger logger = Logger.getLogger(MeasurementState.class);

    public static final String SQL_KEY_PARAMETERID = "PARAMETERID";

    public static final String SEPARATOR = ";";


    private class ParameterMatrix {
        private final Logger logger =
            Logger.getLogger(ParameterMatrix.class);

        private List measurements;
        private List mDescriptions;
        private List parameters;
        private boolean[][] values;

        public ParameterMatrix(Collection data, String[] parameter) {
            measurements  = new ArrayList(data.size());
            mDescriptions = new ArrayList(data.size());
            parameters    = new ArrayList(parameter.length);

            values = new boolean[data.size()][parameter.length];
            for (int i = 0; i < data.size(); i++) {
                Arrays.fill(values[i], false);
            }

            initParameters(parameter);
            initMeasurements(data);
        }

        private void initMeasurements(Collection data) {
            Iterator iter = data.iterator();
            while (iter.hasNext()) {
                ExtendedKeyValueData value = (ExtendedKeyValueData) iter.next();
                String key                = value.getKey();
                String val                = value.getValue();
                String parameter          = value.getParameter();

                int i                     = measurements.indexOf(key);
                int j                     = parameters.indexOf(parameter);
                int tmp                   = mDescriptions.indexOf(val);

                if (i < 0) {
                    measurements.add(key);
                    i = measurements.indexOf(key);
                }

                if (j < 0) {
                    logger.warn("Not a valid parameter: " + parameter);
                }

                if (tmp < 0) {
                    mDescriptions.add(val);
                    tmp = mDescriptions.indexOf(val);
                }

                if (i >= 0 && i < measurements.size() && j >= 0
                &&  j < parameters.size())
                {
                    values[i][j] = true;
                }
            }
        }

        private void initParameters(String[] parameter) {
            for (String param: parameter) {
                parameters.add(param);
            }
        }

        public int measurementSize() {
            if (measurements != null)
                return measurements.size();

            return 0;
        }

        public int parameterSize() {
            if (parameters != null)
                return parameters.size();

            return 0;
        }

        public String getMeasurement(int idx) {
            if (idx >= 0 && idx < measurements.size())
                return (String) measurements.get(idx);

            logger.warn("Index is out of bounds: " + idx);
            return "";
        }

        public String getParameter(int idx) {
            if (idx >= 0 && idx < parameters.size()) {
                return (String) parameters.get(idx);
            }

            logger.warn("Index is out of bounds: " + idx);
            return "";
        }

        public String getMDescription(int idx) {
            if (mDescriptions != null) {
                return (String) mDescriptions.get(idx);
            }

            return null;
        }

        public boolean isValid(int i, int j) {
            if (i < 0 || i > measurements.size()
            ||  j < 0 || j > parameters.size())
            {
                logger.warn("Index out of bounds: " + i + "|" + j);
                return false;
            }

            return values[i][j];
        }
    } // End of ParameterMatrix


    public MeasurementState() {
        super();
    }

    @Override
    protected NamedCollection<KeyValueDescibeData> extractKVP(
        Collection<Result> result,
        String             keyid,
        String             valueid
    ) {
        NamedCollection<KeyValueDescibeData> kvdd =
            new NamedArrayList<KeyValueDescibeData>(dataName, result.size());

        kvdd.setMultiSelect(true);

        int keyPos = -1;
        int valPos = -1;
        int parPos = -1;

        for (Result res: result) {
            if (keyPos < 0 || valPos < 0 || parPos < 0) {
                ResultDescriptor rd = res.getResultDescriptor();

                keyPos = rd.getColumnIndex(keyid);
                valPos = rd.getColumnIndex(valueid);
                parPos = rd.getColumnIndex(SQL_KEY_PARAMETERID);
            }

            kvdd.add(new ExtendedKeyValueData(
                res.getString(keyPos),
                res.getString(valPos),
                getID(),
                res.getString(parPos)));
        }

        return kvdd;
    }


    @Override
    protected void appendToDynamicNode(
        XMLUtils.ElementCreator artCreator,
        XMLUtils.ElementCreator creator,
        Document                document,
        Node                    dynamicNode,
        CallMeta                callMeta,
        Object                  o
    ) {
        NamedArrayList all       = (NamedArrayList) o;
        String name              = all.getName();
        RessourceFactory factory = RessourceFactory.getInstance();

        Element matrixNode = creator.create("group");
        Element matrixLabel = creator.create("label");
        matrixLabel.setTextContent(factory.getRessource(
            callMeta.getLanguages(), all.getName(), all.getName()));
        creator.addAttr(matrixNode, "mode", "matrix");
        matrixNode.appendChild(matrixLabel);

        InputData inputParam = inputData.get("parameterid");
        ParameterMatrix matrix = new ParameterMatrix(all, inputParam.splitValue());

        int measurements = matrix.measurementSize();
        int parameters   = matrix.parameterSize();

        for (int i = 0; i < parameters; i++) {
            Element select = creator.create("select");
            String param   = matrix.getParameter(i);
            creator.addAttr(select, "label", inputParam.getDescription(param));
            creator.addAttr(select, "ref", name);

            for (int j = 0; j < measurements; j++) {
                Element item  = creator.create("item");
                Element label = creator.create("label");
                Element value = creator.create("value");

                creator.addAttr(item, "ref", name);
                creator.addAttr(
                    item,
                    "parameter",
                    matrix.getMDescription(j));

                if (!matrix.isValid(j, i)) {
                    creator.addAttr(item, "disabled", "true");
                }
                else {
                    creator.addAttr(item, "disabled", "false");
                }

                String tmpValue = matrix.getMeasurement(j) + ";" + param;
                label.setTextContent(matrix.getMDescription(j));
                value.setTextContent(tmpValue);

                item.appendChild(label);
                item.appendChild(value);
                select.appendChild(item);
            }

            matrixNode.appendChild(select);
        }

        dynamicNode.appendChild(matrixNode);
    }


    @Override
    public Document feed(
        CallContext           context,
        Collection<InputData> input,
        String                uuid)
    throws StateException
    {
        RessourceFactory resFactory = RessourceFactory.getInstance();
        Locale[] serverLocales      = resFactory.getLocales();
        Locale locale               = context.getMeta().getPreferredLocale(
            serverLocales);

        if (input == null) {
            String msg = resFactory.getRessource(
                locale,
                EXCEPTION_NO_INPUT,
                EXCEPTION_NO_INPUT);
            logger.warn(msg);
            return feedFailure(msg);
        }

        for(InputData item: input) {
            String   name         = item.getName();
            InputValue inputValue = inputValues.get(name);

            String[] tupel = extractValuesAndParams(item.getValue());
            String   type  = inputValue.getType();

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

            if (!InputValidator.isInputValid(tupel[0], type)) {
                String msg = resFactory.getRessource(
                    locale,
                    EXCEPTION_INVALID_INPUT,
                    EXCEPTION_INVALID_INPUT);
                logger.warn(msg);
                return feedFailure(msg);
            }

            if (inputData == null) {
                inputData = new HashMap<String, InputData>();
            }

            ExtendedInputData extended = new ExtendedInputData(
                name,
                tupel[0],
                item.getObject(),
                tupel[1]);

            if (name.equals(dataName)) {
                String[] desc = getDescriptionForInputData(extended, uuid);
                extended.setDescription(desc);
            }

            inputData.put(name, extended);
        }

        return feedSuccess();
    }


    protected String[] extractValuesAndParams(String tmp) {
        String[] array = tmp.split(DefaultInputData.VALUE_SEPARATOR);

        String[] extracted = new String[2];
        for (String item: array) {
            String[] tupel = item.split(ExtendedInputData.SEPARATOR);

            if (extracted[0] == null) {
                extracted[0] = tupel[0];
            }
            else {
                extracted[0] +=
                    DefaultInputData.VALUE_SEPARATOR + tupel[0];
            }

            if (extracted[1] == null) {
                extracted[1] = tupel[1];
            }
            else {
                extracted[1] += DefaultInputData.VALUE_SEPARATOR + tupel[1];
            }
        }

        logger.debug("VALUES RESULT: " + extracted[0]);
        logger.debug("PARAMS RESULT: " + extracted[1]);

        return extracted;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org