ingo@126: package de.intevation.flys.artifacts.states;
ingo@126:
ingo@126: import java.util.Collection;
ingo@126: import java.util.Map;
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@126: import de.intevation.artifacts.CallContext;
ingo@126: import de.intevation.artifacts.ArtifactNamespaceContext;
ingo@126:
ingo@126: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@126:
ingo@126: import de.intevation.artifactdatabase.ProtocolUtils;
ingo@126: import de.intevation.artifactdatabase.data.StateData;
ingo@126: import de.intevation.artifactdatabase.state.AbstractState;
ingo@126:
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:
ingo@126:
ingo@126: public Element describe(
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@126: Element ui = ProtocolUtils.createArtNode(
ingo@126: creator, "dynamic", null, null);
ingo@126:
ingo@126: Map theData = getData();
ingo@126: if (theData == null) {
ingo@126: return ui;
ingo@126: }
ingo@126:
ingo@126: Collection dataItems = theData.values();
ingo@126:
ingo@126: for (StateData data: dataItems) {
ingo@126: String name = data.getName();
ingo@126: Element select = createData(creator, data, context);
ingo@126:
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@126: Element[] items = createItems(creator, name, context);
ingo@126: for (Element item: items) {
ingo@126: choices.appendChild(item);
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@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: */
ingo@126: protected abstract Element[] createItems(
ingo@126: XMLUtils.ElementCreator cr,
ingo@126: String name,
ingo@126: CallContext context);
ingo@126: }
ingo@126: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :