ingo@126: package de.intevation.flys.artifacts.states; ingo@126: ingo@669: import java.text.NumberFormat; ingo@624: import java.util.Iterator; ingo@669: import java.util.Locale; ingo@126: import java.util.Map; sascha@697: import java.util.List; ingo@126: ingo@126: import org.apache.log4j.Logger; ingo@126: ingo@126: import org.w3c.dom.Document; ingo@126: import org.w3c.dom.Element; ingo@126: import org.w3c.dom.Node; ingo@126: ingo@306: import de.intevation.artifacts.Artifact; ingo@142: import de.intevation.artifacts.ArtifactNamespaceContext; ingo@126: import de.intevation.artifacts.CallContext; ingo@142: import de.intevation.artifacts.CallMeta; ingo@126: ingo@126: import de.intevation.artifacts.common.utils.XMLUtils; ingo@126: ingo@126: import de.intevation.artifactdatabase.ProtocolUtils; sascha@697: ingo@126: import de.intevation.artifactdatabase.data.StateData; sascha@697: ingo@126: import de.intevation.artifactdatabase.state.AbstractState; sascha@697: import de.intevation.artifactdatabase.state.Facet; ingo@126: ingo@624: import de.intevation.flys.artifacts.FLYSArtifact; sascha@697: ingo@126: import de.intevation.flys.artifacts.resources.Resources; ingo@126: ingo@126: ingo@126: /** ingo@126: * @author Ingo Weinzierl ingo@126: */ ingo@126: public abstract class DefaultState extends AbstractState { ingo@126: ingo@126: /** The logger that is used in this class.*/ ingo@126: private static Logger logger = Logger.getLogger(DefaultState.class); ingo@126: sascha@697: public static enum ComputeType { sascha@697: FEED, ADVANCE sascha@697: } ingo@126: ingo@624: protected StateData getData(FLYSArtifact artifact, String name) { ingo@624: return artifact.getData(name); ingo@624: } ingo@624: ingo@624: ingo@134: public Element describeStatic( ingo@624: Artifact artifact, ingo@134: Document document, ingo@134: Node root, ingo@134: CallContext context, ingo@134: String uuid) ingo@134: { ingo@134: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( ingo@134: document, ingo@134: ArtifactNamespaceContext.NAMESPACE_URI, ingo@134: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@134: ingo@142: CallMeta meta = context.getMeta(); ingo@142: ingo@142: String label = Resources.getMsg(meta, getID(), getID()); ingo@136: Element ui = ProtocolUtils.createArtNode( ingo@134: creator, "state", ingo@136: new String[] { "name", "uiprovider", "label" }, ingo@136: new String[] { getID(), getUIProvider(), label }); ingo@134: ingo@134: Map theData = getData(); ingo@134: if (theData == null) { ingo@134: return ui; ingo@134: } ingo@134: ingo@624: Iterator iter = theData.keySet().iterator(); ingo@624: FLYSArtifact flys = (FLYSArtifact) artifact; ingo@134: ingo@624: while (iter.hasNext()) { ingo@624: String name = iter.next(); ingo@624: StateData data = getData(flys, name); ingo@624: ingo@624: String value = data != null ? (String) data.getValue() : null; ingo@142: ingo@379: if (value == null) { ingo@379: continue; ingo@379: } ingo@379: ingo@142: logger.debug("Append element '" + name + "' (" + value + ")"); ingo@134: ingo@134: Element dataElement = creator.create("data"); ingo@134: creator.addAttr(dataElement, "name", name, true); ingo@134: creator.addAttr(dataElement, "type", data.getType(), true); ingo@134: ingo@134: Element itemElement = creator.create("item"); ingo@142: creator.addAttr(itemElement, "value", value, true); ingo@669: ingo@669: String attrValue = ""; ingo@669: try { ingo@669: // XXX A better way to format the output would be to use the ingo@669: // 'type' value if the data objects. ingo@669: double doubleVal = Double.valueOf(value); ingo@669: Locale l = Resources.getLocale(meta); ingo@669: NumberFormat nf = NumberFormat.getInstance(l); ingo@669: ingo@669: attrValue = nf.format(doubleVal); ingo@669: } ingo@669: catch (NumberFormatException nfe) { ingo@669: attrValue = Resources.getMsg(meta, value, value); ingo@669: } ingo@669: ingo@669: creator.addAttr(itemElement, "label", attrValue, true); ingo@134: ingo@134: dataElement.appendChild(itemElement); ingo@134: ui.appendChild(dataElement); ingo@134: } ingo@134: ingo@134: return ui; ingo@134: } ingo@134: ingo@134: ingo@126: public Element describe( ingo@306: Artifact artifact, ingo@126: Document document, ingo@126: Node root, ingo@126: CallContext context, ingo@126: String uuid) ingo@126: { ingo@126: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( ingo@126: document, ingo@126: ArtifactNamespaceContext.NAMESPACE_URI, ingo@126: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@126: ingo@135: Element ui = null; ingo@135: String uiprovider = getUIProvider(); ingo@135: if (uiprovider != null) { ingo@135: ui = ProtocolUtils.createArtNode( ingo@135: creator, "dynamic", ingo@135: new String[] { "uiprovider" }, ingo@135: new String[] { uiprovider }); ingo@135: } ingo@135: else { ingo@135: ui = ProtocolUtils.createArtNode(creator, "dynamic", null, null); ingo@135: } ingo@126: ingo@126: Map theData = getData(); ingo@126: if (theData == null) { ingo@126: return ui; ingo@126: } ingo@126: ingo@624: Iterator iter = theData.keySet().iterator(); ingo@624: FLYSArtifact flys = (FLYSArtifact) artifact; ingo@126: ingo@624: while (iter.hasNext()) { ingo@624: String name = iter.next(); ingo@624: StateData data = getData(flys, name); ingo@624: ingo@624: data = data != null ? data : getData(name); ingo@624: ingo@313: Element select = createData(creator, artifact, data, context); ingo@126: ingo@630: String defValue = (String) data.getValue(); ingo@630: String defDesc = null; ingo@630: ingo@630: if (defValue != null && defValue.length() > 0) { ingo@630: defDesc = Resources.getMsg( ingo@630: context.getMeta(), ingo@630: defValue, ingo@630: defValue); ingo@630: } ingo@630: ingo@630: if (defValue != null && defDesc != null) { ingo@630: creator.addAttr(select, "defaultValue", defValue, true); ingo@630: creator.addAttr(select, "defaultLabel", defDesc, true); ingo@630: } ingo@630: ingo@126: Element choices = ProtocolUtils.createArtNode( ingo@126: creator, "choices", null, null); ingo@126: ingo@126: select.appendChild(choices); ingo@126: ui.appendChild(select); ingo@126: ingo@313: Element[] items = createItems(creator, artifact, name, context); sascha@635: if (items != null) { sascha@635: for (Element item: items) { sascha@635: choices.appendChild(item); sascha@635: } ingo@126: } ingo@126: } ingo@126: ingo@126: return ui; ingo@126: } ingo@126: ingo@126: ingo@126: /** ingo@126: * This method creates the root node that contains the list of selectable ingo@126: * items. ingo@126: * ingo@126: * @param cr The ElementCreator. ingo@126: * @param name The name of the amount of data. ingo@126: * ingo@126: * @return the root node of the item list. ingo@126: */ ingo@126: protected Element createData( ingo@126: XMLUtils.ElementCreator cr, ingo@313: Artifact artifact, ingo@126: StateData data, ingo@126: CallContext context) ingo@126: { ingo@126: Element select = ProtocolUtils.createArtNode( ingo@126: cr, "select", null, null); ingo@129: cr.addAttr(select, "name", data.getName(), true); ingo@126: ingo@126: Element label = ProtocolUtils.createArtNode( ingo@126: cr, "label", null, null); ingo@126: ingo@129: select.appendChild(label); ingo@129: ingo@126: label.setTextContent(Resources.getMsg( ingo@126: context.getMeta(), ingo@126: getID(), ingo@126: getID())); ingo@126: ingo@126: return select; ingo@126: } ingo@126: ingo@126: ingo@126: /** ingo@126: * This method creates a list of items. These items represent the amount of ingo@126: * input data that is possible for this state. ingo@126: * ingo@126: * @param cr The ElementCreator. ingo@126: * @param name The name of the amount of data. ingo@126: * ingo@126: * @return a list of items. ingo@126: */ sascha@660: protected Element[] createItems( ingo@126: XMLUtils.ElementCreator cr, ingo@313: Artifact artifact, ingo@126: String name, sascha@660: CallContext context sascha@660: ) { sascha@660: return null; sascha@660: } ingo@135: ingo@135: ingo@322: /** ingo@322: * This method validates the inserted data and returns true, if everything ingo@322: * was correct, otherwise an exception is thrown. ingo@322: * ingo@322: * @param artifact A reference to the owner artifact. ingo@322: * @param context The CallContext object. ingo@322: * ingo@322: * @return true, if everything was fine. ingo@322: */ ingo@322: public boolean validate(Artifact artifact, CallContext context) ingo@322: throws IllegalArgumentException ingo@322: { ingo@322: return true; ingo@322: } ingo@322: ingo@322: ingo@135: protected String getUIProvider() { ingo@135: return null; ingo@135: } ingo@687: sascha@697: public Object computeAdvance( sascha@697: FLYSArtifact artifact, sascha@697: String hash, sascha@742: CallContext context, sascha@742: List facets, sascha@697: Object old sascha@697: ) { sascha@697: return null; sascha@697: } ingo@687: sascha@697: public Object computeFeed( sascha@697: FLYSArtifact artifact, sascha@697: String hash, sascha@697: CallContext context, sascha@742: List facets, sascha@697: Object old sascha@697: ) { ingo@687: return null; ingo@687: } ingo@126: } ingo@126: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :