view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WaterlevelPairSelectState.java @ 1723:690037105542

Added new Artifact and Facet to access 'static' data via WKmsFactory. flys-artifacts/trunk@3005 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 18 Oct 2011 12:56:12 +0000
parents 2b9f0ca1449b
children d26911cac2e4
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import java.util.List;

import org.apache.log4j.Logger;

import org.w3c.dom.Element;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;

import de.intevation.artifactdatabase.ProtocolUtils;
import de.intevation.artifactdatabase.state.Facet;

import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.model.CalculationResult;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.WQKms;
import de.intevation.flys.artifacts.resources.Resources;

import de.intevation.flys.utils.FLYSUtils;
import de.intevation.flys.utils.StringUtil;


public class WaterlevelPairSelectState
extends      DefaultState
implements   FacetTypes
{
    /** The logger that is used in this state. */
    private static Logger logger = Logger.getLogger(
         WaterlevelPairSelectState.class);


    // TODO fix (state.winfo.waterlevel)
    // own id is state.winfo.waterlevel_pair_select
    public static final String WINFO_WDIFFERENCES_STATE_ID
        = "state.winfo.w_differences";

    public WaterlevelPairSelectState() {
    }


    /** Specify to display a datacage_twin_panel. */
    @Override
    protected String getUIProvider() {
        return "datacage_twin_panel";
    }


    @Override
    public Object computeAdvance(
        FLYSArtifact artifact,
        String       hash,
        CallContext  context,
        List<Facet>  facets,
        Object       old
    ) {
        //Get data and do stuff, do not calculate
        return old;
    }



    /**
     * Create elements for document (prepopulated with data, if any).
     */
    @Override
    protected Element[] createItems(
        ElementCreator cr,
        Artifact    artifact,
        String      name,
        CallContext context)
    {
        logger.debug("createItems: " + name);
        if (name.equals("diffids")) {
            Element item  = ProtocolUtils.createArtNode(cr, "item", null, null);
            Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
            Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
            FLYSArtifact flys = (FLYSArtifact) artifact;
            String s = flys.getDataAsString("diffids");
            value.setTextContent(s);
            item.appendChild(label);
            item.appendChild(value);
            return new Element[] { item };
        }
        return new Element[] {};
    }


    /**
     * Creats the data element used for the static part of DESCRIBE document.
     */
    @Override
    protected Element createStaticData(
        ElementCreator creator,
        CallContext    cc,
        String         name,
        String         value,
        String         type
    ) {
        Element dataElement = creator.create("data");
        creator.addAttr(dataElement, "name", name, true);
        creator.addAttr(dataElement, "type", type, true);

        Element itemElement = creator.create("item");
        creator.addAttr(itemElement, "value", value, true);

        String[] labels = getLabels(cc, value);
        Object[] obj    = new Object[] { labels[0] };

        // TODO own i18n
        String attrValue = Resources.getMsg(
            cc.getMeta(), "wsp.selected.string", "wsp.selected.string", obj);
        //I18N_STATIC_KEY, I18N_STATIC_KEY, obj);

        creator.addAttr(itemElement, "label", attrValue, true);
        dataElement.appendChild(itemElement);

        return dataElement;
    }


    // TODO merge implementations with WaterlevelSelectState
    /**
     * Get name to display for selected watelerlevels (for example "Q=123")
     * from the CalculationResult. 
     */
    public static String[] getLabels(CallContext cc, String value) {
        String[] recommendations = value.split("#");
        String displayString = "";

        // Walk over all selected recommendations and create label
        // like "W (Q=1) - W (Q=2)".
        for (int i = 0; i < recommendations.length; i+=2) {
            String[] minuendParts = StringUtil
                .unbracket(recommendations[i+0])
                .split(";");
            String[] subtrahendParts = StringUtil
                .unbracket(recommendations[i+1])
                .split(";");

            FLYSArtifact artifact1 = FLYSUtils.getArtifact(minuendParts[0], cc);
    
            CalculationResult rawData1 = (CalculationResult) artifact1.compute(
                cc,
                null,
                "state.winfo.waterlevel",
                ComputeType.ADVANCE,
                false);
            WQKms[] wqkms1 = (WQKms[]) rawData1.getData();
    
            int idx1 = -1;
            try {
                idx1 = Integer.parseInt(minuendParts[2]);
            }
            catch (NumberFormatException nfe) { /* do nothing */ }

            FLYSArtifact artifact2 = FLYSUtils.getArtifact(subtrahendParts[0], cc);
    
            CalculationResult rawData2 = (CalculationResult) artifact2.compute(
                cc,
                null,
                "state.winfo.waterlevel",
                ComputeType.ADVANCE,
                false);
            WQKms[] wqkms2 = (WQKms[]) rawData2.getData();
    
            int idx2 = -1;
            try {
                idx2 = Integer.parseInt(subtrahendParts[2]);
            }
            catch (NumberFormatException nfe) { /* do nothing */ }

            displayString += "(" + StringUtil.wWrap(wqkms1[idx1].getName()) 
                + " - " + StringUtil.wWrap(wqkms2[idx2].getName()) + ")";
        }

        return new String[] { displayString };
    }

}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org