bjoern@3936: package de.intevation.flys.artifacts;
bjoern@3936: 
bjoern@3936: import java.util.ArrayList;
bjoern@4158: import java.util.Collection;
bjoern@3936: import java.util.List;
bjoern@3936: 
bjoern@4158: import org.apache.log4j.Logger;
bjoern@4158: 
bjoern@4158: import org.w3c.dom.Document;
bjoern@4158: import org.w3c.dom.Element;
bjoern@4158: 
bjoern@4158: import de.intevation.artifacts.CallContext;
bjoern@4158: import de.intevation.artifacts.ArtifactNamespaceContext;
bjoern@4158: 
bjoern@4158: import de.intevation.artifacts.common.utils.XMLUtils;
bjoern@4158: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
bjoern@4158: 
bjoern@4158: import de.intevation.artifactdatabase.ProtocolUtils;
bjoern@4158: import de.intevation.artifactdatabase.data.StateData;
bjoern@3936: import de.intevation.artifactdatabase.state.State;
bjoern@3936: 
bjoern@4157: import de.intevation.flys.artifacts.states.StaticState;
bjoern@4157: 
bjoern@3936: /**
bjoern@3936:  * A abstract baseclass for Artifacts which are using only one static state.
bjoern@3936:  *
bjoern@3936:  * This class is intended to be used without the config/stateengine to generate
bjoern@3936:  * the static state.
bjoern@3936:  *
bjoern@3936:  * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
bjoern@3936:  */
bjoern@3936: public abstract class AbstractStaticStateArtifact extends StaticFLYSArtifact {
bjoern@3936: 
bjoern@4157:     private transient StaticState staticstate;
bjoern@3936: 
bjoern@4158:     private static final Logger logger =
bjoern@4158:         Logger.getLogger(AbstractStaticStateArtifact.class);
bjoern@4158: 
bjoern@3936:     /**
bjoern@3936:      * Get a list containing the one and only State.
bjoern@3936:      * @param  context ignored.
bjoern@3936:      * @return list with one and only state.
bjoern@3936:      */
bjoern@3936:     @Override
bjoern@3936:     protected List<State> getStates(Object context) {
bjoern@3936:         ArrayList<State> states = new ArrayList<State>();
bjoern@3936:         states.add(getStaticState());
bjoern@3936:         return states;
bjoern@3936:     }
bjoern@3936: 
bjoern@3936: 
bjoern@3936:     /**
bjoern@3936:      * Get the "current" state.
bjoern@3936:      * @param cc ignored.
bjoern@3936:      * @return always the set static state.
bjoern@3936:      */
bjoern@3936:     @Override
bjoern@3936:     public State getCurrentState(Object cc) {
bjoern@3936:         return getStaticState();
bjoern@3936:     }
bjoern@3936: 
bjoern@3936:     /**
bjoern@3936:      * A child class must override this method to set its static state
bjoern@3936:      */
bjoern@3936:     protected abstract void initStaticState();
bjoern@3936: 
bjoern@4157:     protected void setStaticState(StaticState state) {
bjoern@3936:         this.staticstate = state;
bjoern@3936:     }
bjoern@3936: 
bjoern@4157:     protected StaticState getStaticState() {
bjoern@3936:         if (staticstate == null) {
bjoern@3936:             initStaticState();
bjoern@3936:         }
bjoern@3936:         return staticstate;
bjoern@3936:     }
bjoern@3936: 
bjoern@3936:     /**
bjoern@3936:      * Get the state.
bjoern@3936:      * @param context ignored.
bjoern@3936:      * @param stateID ignored.
bjoern@3936:      * @return the state.
bjoern@3936:      */
bjoern@3936:     @Override
bjoern@3936:     protected State getState(Object context, String stateID) {
bjoern@3936:         return getStaticState();
bjoern@3936:     }
bjoern@4158: 
bjoern@4158:     @Override
bjoern@4158:     public Document describe(Document data, CallContext cc) {
bjoern@4158:         logger.debug("Describe artifact: " + identifier());
bjoern@4158: 
bjoern@4158:         Document desc = XMLUtils.newDocument();
bjoern@4158: 
bjoern@4158:         ElementCreator creator = new ElementCreator(
bjoern@4158:             desc,
bjoern@4158:             ArtifactNamespaceContext.NAMESPACE_URI,
bjoern@4158:             ArtifactNamespaceContext.NAMESPACE_PREFIX);
bjoern@4158: 
bjoern@4158:         Element root = ProtocolUtils.createRootNode(creator);
bjoern@4158:         desc.appendChild(root);
bjoern@4158: 
bjoern@4158:         Element name = ProtocolUtils.createArtNode(
bjoern@4158:             creator, "name",
bjoern@4158:             new String[] { "value" },
bjoern@4158:             new String[] { getName() });
bjoern@4158: 
bjoern@4158:         root.appendChild(name);
bjoern@4158:         root.appendChild(createOutputModes(cc, desc, creator));
bjoern@4158: 
bjoern@4158:         ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash());
bjoern@4158: 
bjoern@4158:         // Add the data to an anonymous state.
bjoern@4158:         Collection<StateData> datas = getAllData();
bjoern@4158:         if (datas.size() > 0) {
bjoern@4158:             Element ui = creator.create("ui");
bjoern@4158:             Element staticE = creator.create("static");
bjoern@4158: 
bjoern@4158:             StaticState current = getStaticState();
bjoern@4158:             Element state = current.describeStatic(this, desc, root, cc, null);
bjoern@4158:             staticE.appendChild(state);
bjoern@4158: 
bjoern@4158:             for (StateData dataItem : datas) {
bjoern@4158:                 Element itemelent = creator.create("data");
bjoern@4158:                 creator.addAttr(itemelent, "name", dataItem.getName(), true);
bjoern@4158:                 creator.addAttr(itemelent, "type", dataItem.getType(), true);
bjoern@4158:                 state.appendChild(itemelent);
bjoern@4158:                 Element valuement = creator.create("item");
bjoern@4158:                 creator.addAttr(valuement, "label", dataItem.getDescription(), true);
bjoern@4158:                 creator.addAttr(valuement, "value", dataItem.getValue().toString(), true);
bjoern@4158:                 itemelent.appendChild(valuement);
bjoern@4158:             }
bjoern@4158: 
bjoern@4158:             ui.appendChild(staticE);
bjoern@4158:             root.appendChild(ui);
bjoern@4158:         }
bjoern@4158: 
bjoern@4158:         return desc;
bjoern@4158:     }
bjoern@3936: }