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@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;
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@134: public Element describeStatic(
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@134: Collection dataItems = theData.values();
ingo@134:
ingo@134: for (StateData data: dataItems) {
ingo@142: String name = data.getName();
ingo@142: String value = (String) data.getValue();
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@142: creator.addAttr(
ingo@142: itemElement,
ingo@142: "label",
ingo@142: Resources.getMsg(meta, value, value),
ingo@142: 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@126: Collection dataItems = theData.values();
ingo@126:
ingo@126: for (StateData data: dataItems) {
ingo@126: String name = data.getName();
ingo@313: Element select = createData(creator, artifact, 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@313: Element[] items = createItems(creator, artifact, 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@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: */
ingo@126: protected abstract Element[] createItems(
ingo@126: XMLUtils.ElementCreator cr,
ingo@313: Artifact artifact,
ingo@126: String name,
ingo@126: CallContext context);
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@126: }
ingo@126: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :