view gnv-artifacts/src/main/java/de/intevation/gnv/state/MeasurementState.java @ 636:22cc12b1622c

Take parent states only to describe static user interface part - not the current state itself. gnv-artifacts/trunk@718 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 01 Mar 2010 14:15:33 +0000
parents 58c32df1a44d
children 1946c4e9a878
line wrap: on
line source
package de.intevation.gnv.state;

import de.intevation.artifacts.CallMeta;
import de.intevation.artifactdatabase.XMLUtils;

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.transition.DefaultTransition;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
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 Ingo Weinzierl (ingo.weinzierl@intevation.de)
 */
public class MeasurementState
extends      DefaultState
{
    private static Logger logger = Logger.getLogger(MeasurementState.class);

    public static final String SQL_KEY_PARAMETERID = "PARAMETERID";


    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) {
            int count = parameter.length;
            for (int i = 0; i < count; i++) {
                parameters.add(parameter[i]);
            }
        }

        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()) {
                logger.debug("############### PARAM: " + parameters.get(idx));
                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 < measurements; i++) {
            Element select = creator.create("select");
            creator.addAttr(select, "label", matrix.getMDescription(i));

            for (int j = 0; j < parameters; 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",
                    inputParam.getDescription(matrix.getParameter(j)));

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

                label.setTextContent(matrix.getMDescription(i));
                value.setTextContent(matrix.getMeasurement(i));

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

            matrixNode.appendChild(select);
        }

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

http://dive4elements.wald.intevation.org