tim@52: /** tim@52: * tim@52: */ tim@52: package de.intevation.gnv.artifacts; tim@52: tim@56: import java.util.Collection; tim@52: import java.util.HashMap; tim@54: import java.util.Iterator; tim@52: import java.util.Map; tim@52: tim@52: import org.apache.log4j.Logger; tim@52: import org.w3c.dom.Document; tim@54: import org.w3c.dom.Element; tim@52: import org.w3c.dom.Node; tim@52: import org.w3c.dom.NodeList; tim@52: tim@52: import de.intevation.artifactdatabase.Config; tim@52: import de.intevation.artifactdatabase.DefaultArtifact; tim@52: import de.intevation.gnv.artifacts.context.GNVArtifactContext; tim@56: import de.intevation.gnv.transition.InputValue; tim@52: import de.intevation.gnv.transition.Transition; tim@52: import de.intevation.gnv.transition.TransitionFactory; tim@52: tim@52: /** tim@52: * @author Tim Englich tim@52: * tim@52: */ tim@52: public abstract class GNVArtifactBase extends DefaultArtifact { tim@52: /** tim@52: * the logger, used to log exceptions and additonaly information tim@52: */ tim@52: private static Logger log = Logger.getLogger(GNVArtifactBase.class); tim@52: /** tim@52: * The UID of this Class tim@52: */ tim@52: private static final long serialVersionUID = -8907096744400741458L; tim@52: tim@54: /** tim@54: * The Identifier for the Replacement of the Artifactname tim@54: */ tim@52: public static final String XPATH_IDENTIFIER_REPLACE = "IDENTIFIER"; tim@54: tim@52: /** tim@52: * The XPATH to the XML-Fragment that should be used for the Configuration tim@52: */ tim@52: public static final String XPATH_ARTIFACT_CONFIGURATION= "/artifact-database/artifacts/artifact[@name='"+XPATH_IDENTIFIER_REPLACE+"']"; tim@52: tim@54: tim@54: /** tim@54: * The current Transition tim@54: */ tim@52: protected Transition current = null; tim@52: tim@54: /** tim@54: * The Transitions that can be used tim@54: */ tim@52: protected Map transitions = null; tim@52: tim@54: /** tim@54: * The Name of the Artifact tim@54: */ tim@52: protected String name = null; tim@54: tim@52: /** tim@52: * Constructor tim@52: */ tim@52: public GNVArtifactBase() { tim@52: super(); tim@52: } tim@52: tim@52: protected Node getConfigurationFragment(Document document){ tim@52: log.debug("GNVArtifactBase.getConfigurationFragment"); tim@52: String xpathQuery = XPATH_ARTIFACT_CONFIGURATION.replaceAll(XPATH_IDENTIFIER_REPLACE, this.name); tim@52: log.debug(xpathQuery); tim@52: return Config.getNodeXPath(document,xpathQuery); tim@52: } tim@52: tim@52: /** tim@52: * @see de.intevation.artifactdatabase.DefaultArtifact#setup(java.lang.String, java.lang.Object) tim@52: */ tim@52: @Override tim@52: public void setup(String identifier, Object context) { tim@52: log.debug("GNVArtifactBase.setup"); tim@52: super.setup(identifier, context); tim@52: if (context instanceof GNVArtifactContext){ tim@52: GNVArtifactContext gnvContext = (GNVArtifactContext)context; tim@52: Document doc = gnvContext.getConfig(); tim@52: Node artifactNode = this.getConfigurationFragment(doc); tim@52: NodeList transitionList = Config.getNodeSetXPath(artifactNode, "transitions/transition"); tim@52: this.transitions = new HashMap(transitionList.getLength()); tim@52: for (int i = 0 ; i < transitionList.getLength(); i++){ tim@52: Transition tmpTransition = TransitionFactory.getInstance().createTransition(transitionList.item(i)); tim@52: if (tmpTransition != null){ tim@52: this.transitions.put(tmpTransition.getID(), tmpTransition); tim@52: if (this.current == null){ tim@52: this.current = tmpTransition; tim@52: } tim@52: } tim@52: } tim@52: tim@52: } tim@52: } tim@52: tim@54: tim@54: protected Document createDescibeOutput(){ tim@54: log.debug("GNVArtifactBase.createDescibeOutput"); tim@54: Document document = super.newDocument(); tim@54: Element rootNode = this.createRootNode(document); tim@54: this.createHeader(rootNode, document, "describe"); tim@54: this.createCurrentState(rootNode, document); tim@54: this.createReachableStates(rootNode, document); tim@54: this.createModel(rootNode, document); tim@54: this.createUserInterface(rootNode, document); tim@54: tim@54: return document; tim@54: } tim@54: tim@54: protected Element createRootNode(Document document){ tim@54: Element rootNode = createElement(document,"result"); tim@54: document.appendChild(rootNode); tim@54: return rootNode; tim@54: } tim@54: tim@54: protected void createHeader(Element parent, Document document, String documentType){ tim@54: Element typeNode = createElement(document,"type"); tim@54: typeNode.setAttribute("name", documentType); tim@54: parent.appendChild(typeNode); tim@54: tim@54: Element uuidNode = createElement(document,"uuid"); tim@54: uuidNode.setAttribute("value", super.identifier); tim@54: parent.appendChild(uuidNode); tim@54: tim@54: Element hashNode = createElement(document,"hash"); tim@54: hashNode.setAttribute("value", this.hash()); tim@54: parent.appendChild(hashNode); tim@54: tim@54: tim@54: } tim@54: protected void createReachableStates(Element parent,Document document){ tim@54: Element stateNode = createElement(document,"reachable-states"); tim@54: if (this.current != null){ tim@54: Iterator states = this.current.reachableTransitions().iterator(); tim@54: while(states.hasNext()){ tim@54: String value = states.next(); tim@54: Element currentNode = createElement(document,"state"); tim@54: currentNode.setAttribute("name", value); tim@54: currentNode.setAttribute("description", transitions.get(value).getDescription()); tim@54: stateNode.appendChild(currentNode); tim@54: } tim@54: } tim@54: parent.appendChild(stateNode); tim@54: } tim@54: tim@54: protected void createCurrentState(Element parent, Document document){ tim@54: Element stateNode = createElement(document,"state"); tim@54: stateNode.setAttribute("name", this.current.getID()); tim@54: stateNode.setAttribute("description", this.current.getDescription()); tim@54: parent.appendChild(stateNode); tim@54: } tim@54: tim@54: tim@54: protected void createModel(Element parent, Document document){ tim@54: Element modelNode = createElement(document,"model"); tim@56: if (this.current != null){ tim@56: Collection inputValues = this.current.getRequiredInputValues(); tim@56: if (inputValues != null){ tim@56: Iterator it = inputValues.iterator(); tim@56: while(it.hasNext()){ tim@56: InputValue inputValue = it.next(); tim@56: Element inputNode = createElement(document,"input"); tim@56: inputNode.setAttribute("name", inputValue.getName()); tim@56: inputNode.setAttribute("type", inputValue.getType()); tim@56: modelNode.appendChild(inputNode); tim@56: } tim@56: } tim@56: } tim@54: parent.appendChild(modelNode); tim@54: } tim@54: tim@54: protected void createUserInterface(Element parent, Document document){ tim@54: Element uiNode = createElement(document,"ui"); tim@54: tim@54: // TODO mit leben füllen. tim@54: tim@54: parent.appendChild(uiNode); tim@54: } tim@54: tim@54: protected void createOutputs(Element parent, Document document){ tim@54: Element outputsNode = createElement(document,"outputs"); tim@54: tim@54: // TODO mit leben füllen. tim@54: tim@54: parent.appendChild(outputsNode); tim@54: } tim@54: tim@54: /** tim@54: * @param document tim@54: * @return tim@54: */ tim@54: private Element createElement(Document document, String name) { tim@54: Element node = document.createElementNS(DefaultArtifact.NAMESPACE_URI, name); tim@54: node.setPrefix(DefaultArtifact.NAMESPACE_PREFIX); tim@54: return node; tim@54: } tim@54: tim@52: tim@52: }