felix@1173: package de.intevation.flys.artifacts.states;
felix@1173: 
felix@1173: import java.util.List;
felix@1173: 
felix@1173: import org.apache.log4j.Logger;
felix@1173: 
felix@1663: import org.w3c.dom.Element;
felix@1663: 
felix@1693: import de.intevation.artifacts.Artifact;
felix@1173: import de.intevation.artifacts.CallContext;
felix@1173: 
felix@1693: import de.intevation.artifactdatabase.ProtocolUtils;
felix@1173: import de.intevation.artifactdatabase.state.Facet;
felix@1173: 
felix@1663: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
felix@1663: 
felix@1173: import de.intevation.flys.artifacts.FLYSArtifact;
felix@1663: import de.intevation.flys.artifacts.model.FacetTypes;
felix@1663: import de.intevation.flys.artifacts.resources.Resources;
felix@1173: 
felix@1663: import de.intevation.flys.utils.StringUtil;
felix@1663: 
felix@1737: /**
felix@1737:  * State in which the user selects 1 to n pairs of Waterlevels and alikes.
felix@1737:  */
felix@1173: public class WaterlevelPairSelectState
felix@1173: extends      DefaultState
felix@1173: implements   FacetTypes
felix@1173: {
felix@1173:     /** The logger that is used in this state. */
felix@1693:     private static Logger logger = Logger.getLogger(
felix@1693:          WaterlevelPairSelectState.class);
felix@1173: 
felix@1663: 
felix@1737:     /** Trivial constructor. */
felix@1173:     public WaterlevelPairSelectState() {
felix@1173:     }
felix@1173: 
felix@1173: 
felix@1173:     /** Specify to display a datacage_twin_panel. */
felix@1173:     @Override
felix@1173:     protected String getUIProvider() {
felix@1173:         return "datacage_twin_panel";
felix@1173:     }
felix@1173: 
felix@1173: 
felix@1737:     /**
felix@1737:      * Overridden to do nothing.
felix@1737:      */
felix@1173:     @Override
felix@1173:     public Object computeAdvance(
felix@1173:         FLYSArtifact artifact,
felix@1173:         String       hash,
felix@1173:         CallContext  context,
felix@1173:         List<Facet>  facets,
felix@1173:         Object       old
felix@1173:     ) {
felix@1173:         //Get data and do stuff, do not calculate
felix@1737:         return "";
felix@1693:     }
felix@1693: 
felix@1693: 
felix@1693:     /**
felix@1693:      * Create elements for document (prepopulated with data, if any).
felix@1737:      * @param artifact FLYSArtifact to get data from.
felix@1737:      * @param name DataName, expceted to be "diffids".
felix@1693:      */
felix@1693:     @Override
felix@1693:     protected Element[] createItems(
felix@1694:         ElementCreator cr,
felix@1693:         Artifact    artifact,
felix@1693:         String      name,
felix@1693:         CallContext context)
felix@1693:     {
felix@1693:         logger.debug("createItems: " + name);
felix@1693:         if (name.equals("diffids")) {
felix@1693:             Element item  = ProtocolUtils.createArtNode(cr, "item", null, null);
felix@1693:             Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
felix@1693:             Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
felix@1693:             FLYSArtifact flys = (FLYSArtifact) artifact;
felix@1693:             String s = flys.getDataAsString("diffids");
felix@1693:             value.setTextContent(s);
felix@1693:             item.appendChild(label);
felix@1693:             item.appendChild(value);
felix@1693:             return new Element[] { item };
felix@1693:         }
felix@1693:         return new Element[] {};
felix@1173:     }
felix@1663: 
felix@1663: 
felix@1663:     /**
felix@1663:      * Creats the data element used for the static part of DESCRIBE document.
felix@1663:      */
felix@1663:     @Override
felix@1663:     protected Element createStaticData(
ingo@1743:         FLYSArtifact   flys,
felix@1663:         ElementCreator creator,
felix@1663:         CallContext    cc,
felix@1663:         String         name,
felix@1663:         String         value,
felix@1663:         String         type
felix@1663:     ) {
felix@1663:         Element dataElement = creator.create("data");
felix@1663:         creator.addAttr(dataElement, "name", name, true);
felix@1663:         creator.addAttr(dataElement, "type", type, true);
felix@1663: 
felix@1663:         Element itemElement = creator.create("item");
felix@1663:         creator.addAttr(itemElement, "value", value, true);
felix@1663: 
felix@1663:         String[] labels = getLabels(cc, value);
felix@1663:         Object[] obj    = new Object[] { labels[0] };
felix@1663: 
felix@1663:         // TODO own i18n
felix@1663:         String attrValue = Resources.getMsg(
felix@1663:             cc.getMeta(), "wsp.selected.string", "wsp.selected.string", obj);
felix@1663:         //I18N_STATIC_KEY, I18N_STATIC_KEY, obj);
felix@1663: 
felix@1663:         creator.addAttr(itemElement, "label", attrValue, true);
felix@1663:         dataElement.appendChild(itemElement);
felix@1663: 
felix@1663:         return dataElement;
felix@1663:     }
felix@1663: 
felix@1663: 
felix@1663:     /**
felix@1663:      * Get name to display for selected watelerlevels (for example "Q=123")
sascha@3076:      * from the CalculationResult.
felix@1663:      */
felix@1663:     public static String[] getLabels(CallContext cc, String value) {
felix@1663:         String[] recommendations = value.split("#");
felix@1663:         String displayString = "";
felix@1663: 
felix@1663:         // Walk over all selected recommendations and create label
felix@1663:         // like "W (Q=1) - W (Q=2)".
felix@1663:         for (int i = 0; i < recommendations.length; i+=2) {
felix@1663:             String[] minuendParts = StringUtil
felix@1663:                 .unbracket(recommendations[i+0])
felix@1663:                 .split(";");
felix@1737:             if(minuendParts.length >= 4) {
felix@1737:                 displayString += "(" + minuendParts[3];
felix@1737:             }
felix@1737:             else {
felix@1737:                 displayString += "([error]";
felix@1737:             }
felix@1737: 
felix@1737:             displayString += " - ";
felix@1737: 
felix@1663:             String[] subtrahendParts = StringUtil
felix@1663:                 .unbracket(recommendations[i+1])
felix@1663:                 .split(";");
felix@1737:             if(subtrahendParts.length >= 4) {
felix@1737:                 displayString += subtrahendParts[3] + ") ";
felix@1663:             }
felix@1737:             else {
felix@1737:                 displayString += "[error])";
felix@1663:             }
felix@1663:         }
felix@1663: 
felix@1663:         return new String[] { displayString };
felix@1663:     }
felix@1173: }
felix@1173: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :