diff gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java @ 464:70df44021a9f

Next step of changing the concept how to use the user interface. Adapted namespaces and replaced FISArtifact with a ProxyArtifact. gnv-artifacts/trunk@525 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Sat, 09 Jan 2010 17:55:45 +0000
parents 3ddc22aab764
children b7bb66440cc8
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java	Sat Jan 09 16:58:53 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java	Sat Jan 09 17:55:45 2010 +0000
@@ -27,6 +27,8 @@
 import de.intevation.artifacts.CallContext;
 import de.intevation.artifacts.CallMeta;
 import de.intevation.gnv.artifacts.context.GNVArtifactContext;
+import de.intevation.gnv.artifacts.fis.product.Product;
+import de.intevation.gnv.artifacts.ressource.RessourceFactory;
 import de.intevation.gnv.state.DefaultInputData;
 import de.intevation.gnv.state.InputData;
 import de.intevation.gnv.state.InputValue;
@@ -41,6 +43,7 @@
 
 /**
  * @author Tim Englich <tim.englich@intevation.de>
+ * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
  * 
  */
 public abstract class GNVArtifactBase extends DefaultArtifact {
@@ -65,6 +68,18 @@
                                                               + XPATH_IDENTIFIER_REPLACE
                                                               + "']";
 
+    public static final String XPATH_STATIC_NODE = "/art:result/art:ui/art:static";
+
+    public static final String XPATH_INPUT_DATA = "/art:action/art:data/art:input";
+
+    public static final String XPATH_INCLUDE_UI = "/art:action/art:include-ui";
+
+    public static final String XPATH_TARGET_NAME = "/art:action/art:target/@name";
+
+    public static final String XPATH_OUTPUT_NAME = "/art:action/art:out/@name"; 
+
+    public static final String XPATH_OUTPUT_PARAMS = "/art:action/art:out/art:params/art:input"; 
+
     /**
      * The current State
      */
@@ -80,6 +95,12 @@
      */
     protected Collection<Transition> transitions = null;
 
+
+    /**
+     * The current product
+     */
+    protected Product product;
+
     /**
      * The Name of the Artifact
      */
@@ -183,8 +204,8 @@
     }
 
     protected String readStateName(Document document) {
-        String returnValue = Config.getStringXPath(document,
-                "action/target/@name");
+        String returnValue = XMLUtils.xpathString(
+            document, XPATH_TARGET_NAME, ArtifactNamespaceContext.INSTANCE);
         return returnValue;
     }
 
@@ -215,8 +236,10 @@
         Document result = XMLUtils.newDocument();
         try {
             if (this.current != null) {
-                Collection<InputData> inputData = this.parseInputData(target,
-                                                                      "/action/data/input");
+                Collection<InputData> inputData = this.parseInputData(
+                    target,
+                    XPATH_INPUT_DATA);
+
                 if (!inputData.isEmpty()){
                     this.current.putInputData(inputData, super.identifier);
                     result = new ArtifactXMLUtilities().createSuccessReport(
@@ -241,6 +264,42 @@
         return result;
     }
 
+
+    /**
+     * @see de.intevation.artifactdatabase.DefaultArtifact#describe(org.w3c.dom.Document, de.intevation.artifacts.CallContext)
+     */
+    @Override
+    public Document describe(Document data, CallContext context) {
+        log.debug("GNVArtifactBase.describe");
+
+        Document document = createDescibeOutput(
+            context.getMeta(),
+            identifier,
+            getIncludeUIFromDocument(data)
+        );
+
+        // insert node for rendering product field
+        Element staticNode = (Element) XMLUtils.xpath(
+            document,
+            XPATH_STATIC_NODE,
+            XPathConstants.NODE,
+            ArtifactNamespaceContext.INSTANCE
+        );
+
+        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
+            document,
+            XMLUtils.XFORM_URL,
+            XMLUtils.XFORM_PREFIX
+        );
+
+        if (staticNode != null) {
+            Element staticUI = createSelectBox(creator, document, context);
+            staticNode.insertBefore(staticUI, staticNode.getFirstChild());
+        }
+
+        return document;
+    }
+
     /**
      * @see de.intevation.artifactdatabase.DefaultArtifact#setup(java.lang.String,
      *      java.lang.Object)
@@ -290,29 +349,38 @@
         }
     }
     
-    
 
     protected Document createDescibeOutput(CallMeta callMeta, String uuid, boolean incudeUI) {
         log.debug("GNVArtifactBase.createDescibeOutput");
         Document document = XMLUtils.newDocument();
+        
+        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
+            document,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX
+        );
         Element rootNode = this.createRootNode(document);
-        this.createHeader(rootNode, document, "describe");
-        this.createOutputs(rootNode, document);
-        this.createCurrentState(rootNode, document);
-        this.createReachableStates(rootNode, document);
-        this.createModel(rootNode, document);
+        this.createHeader(creator, rootNode, document, "describe");
+        this.createOutputs(creator, rootNode, document);
+        this.createCurrentState(creator, rootNode, document);
+        this.createReachableStates(creator, rootNode, document);
+        this.createModel(creator, rootNode, document);
         if (incudeUI){
-            this.createUserInterface(rootNode, document, callMeta, uuid);
+            this.createUserInterface(creator, rootNode, document, callMeta, uuid);
         }
+
         return document;
     }
     
     protected boolean getIncludeUIFromDocument(Document document){
-        String value = Config.getStringXPath(document, "action/include-ui");
+        String value = XMLUtils.xpathString(
+            document, XPATH_INCLUDE_UI, ArtifactNamespaceContext.INSTANCE);
+
         boolean includeUI = false;
         if (value != null){
             includeUI = Boolean.parseBoolean(value);
         }
+        log.debug("INCLUDE UI? " + includeUI);
         return includeUI;
     }
 
@@ -323,37 +391,86 @@
         return rootNode;
     }
 
-    protected void createHeader(Element parent, Document document,
-                                String documentType) {
-        Element typeNode = xmlUtilities.createArtifactElement(document, "type");
-        typeNode.setAttribute("name", documentType);
+    protected void createHeader(
+        XMLUtils.ElementCreator creator,
+        Element                 parent,
+        Document                document,
+        String                  documentType
+    ) { 
+        Element typeNode = creator.create("type");
+        creator.addAttr(typeNode, "name", documentType);
         parent.appendChild(typeNode);
 
-        Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid");
-        uuidNode.setAttribute("value", super.identifier);
+        Element uuidNode = creator.create("uuid");
+        creator.addAttr(uuidNode, "value", super.identifier);
         parent.appendChild(uuidNode);
 
-        Element hashNode = xmlUtilities.createArtifactElement(document, "hash");
-        hashNode.setAttribute("value", this.hash());
+        Element hashNode = creator.create("hash");
+        creator.addAttr(hashNode, "value", this.hash());
         parent.appendChild(hashNode);
     }
 
-    protected void createReachableStates(Element parent, Document document) {
-        Element stateNode = xmlUtilities.createArtifactElement(document,
-                "reachable-states");
+    protected Element createSelectBox(
+        XMLUtils.ElementCreator creator,
+        Document                document,
+        CallContext             context
+    ) {
+        RessourceFactory resource = RessourceFactory.getInstance();
+        CallMeta callMeta         = (CallMeta) context.getMeta();
+        String productName        = product.getName(); 
+
+        Element selectNode = creator.create("select1");
+        creator.addAttr(selectNode, "ref", "product");
+
+        Element labelNode = creator.create("label");
+        labelNode.setTextContent(
+            resource.getRessource(callMeta.getLanguages(), "product", "product")
+        );
+
+        Element choicesNode = creator.create("choices");
+
+        Element itemNode = creator.create("item");
+        creator.addAttr(itemNode, "selected", "true");
+
+        Element choiceLabel = creator.create("label");
+        choiceLabel.setTextContent(resource.getRessource(
+            callMeta.getLanguages(),
+            productName,
+            productName
+        ));
+
+        Element choiceValue = creator.create("value");
+        choiceValue.setTextContent(productName);
+
+        itemNode.appendChild(choiceLabel);
+        itemNode.appendChild(choiceValue);
+        choicesNode.appendChild(itemNode);
+
+        selectNode.appendChild(labelNode);
+        selectNode.appendChild(choicesNode);
+
+        return selectNode;
+    }
+
+    protected void createReachableStates(
+        XMLUtils.ElementCreator creator,
+        Element                 parent,
+        Document                document
+    ) {
+        Element stateNode = creator.create("reachable-states");
         if (this.current != null) {
             Iterator<Transition> transitions = this.transitions.iterator();
             while (transitions.hasNext()) {
                 Transition tmpTransition = transitions.next();
                 if (tmpTransition.getFrom().equals(current.getID()) && 
                     tmpTransition.isValid(this.current)){
-                    Element currentNode = xmlUtilities.createArtifactElement(
-                            document, "state");
-                    currentNode.setAttribute("name", tmpTransition.getTo());
+                    Element currentNode = creator.create("state");
+                    creator.addAttr(currentNode, "name", tmpTransition.getTo());
                     log.debug("Reachable State: " + tmpTransition.getTo());
-                    currentNode.setAttribute("description", 
-                                             this.states.get(tmpTransition.getTo())
-                                                             .getDescription());
+                    creator.addAttr(
+                        currentNode,
+                        "description",
+                        this.states.get(tmpTransition.getTo()).getDescription());
                     stateNode.appendChild(currentNode);
                 }
             }
@@ -361,17 +478,23 @@
         parent.appendChild(stateNode);
     }
 
-    protected void createCurrentState(Element parent, Document document) {
-        Element stateNode = xmlUtilities.createArtifactElement(document,
-                "state");
-        stateNode.setAttribute("name", this.current.getID());
-        stateNode.setAttribute("description", this.current.getDescription());
+    protected void createCurrentState(
+        XMLUtils.ElementCreator creator,
+        Element                 parent,
+        Document                document
+    ) {
+        Element stateNode = creator.create("state");
+        creator.addAttr(stateNode, "name", this.current.getID());
+        creator.addAttr(stateNode, "description", this.current.getDescription());
         parent.appendChild(stateNode);
     }
 
-    protected void createModel(Element parent, Document document) {
-        Element modelNode = xmlUtilities.createArtifactElement(document,
-                "model");
+    protected void createModel(
+        XMLUtils.ElementCreator creator,
+        Element                 parent,
+        Document                document
+    ) {
+        Element modelNode = creator.create("model");
         if (this.current != null) {
             Collection<InputValue> inputValues = this.current
                     .getRequiredInputValues();
@@ -379,10 +502,9 @@
                 Iterator<InputValue> it = inputValues.iterator();
                 while (it.hasNext()) {
                     InputValue inputValue = it.next();
-                    Element inputNode = xmlUtilities.createArtifactElement(
-                            document, "input");
-                    inputNode.setAttribute("name", inputValue.getName());
-                    inputNode.setAttribute("type", inputValue.getType());
+                    Element inputNode = creator.create("input");
+                    creator.addAttr(inputNode, "name", inputValue.getName());
+                    creator.addAttr(inputNode, "type", inputValue.getType());
                     modelNode.appendChild(inputNode);
                 }
             }
@@ -390,9 +512,14 @@
         parent.appendChild(modelNode);
     }
 
-    protected void createUserInterface(Element parent, Document document,
-                                       CallMeta callMeta, String uuid) {
-        Element uiNode = xmlUtilities.createArtifactElement(document, "ui");
+    protected void createUserInterface(
+        XMLUtils.ElementCreator creator,
+        Element                 parent,
+        Document                document,
+        CallMeta                callMeta,
+        String uuid
+    ) {
+        Element uiNode = creator.create("ui");
 
         if (this.current != null) {
             this.current.describe(document, uiNode, callMeta, uuid);
@@ -401,10 +528,13 @@
         parent.appendChild(uiNode);
     }
 
-    protected void createOutputs(Element parent, Document document) {
+    protected void createOutputs(
+        XMLUtils.ElementCreator creator,
+        Element                 parent,
+        Document                document
+    ) {
         log.debug("GNVArtifactBase.createOutputs");
-        Element outputsNode = xmlUtilities.createArtifactElement(document,
-                "outputs");
+        Element outputsNode = creator.create("outputs");
         if (this.current instanceof OutputState) {
             Collection<OutputMode> outputModes = ((OutputState) this.current)
                     .getOutputModes();
@@ -413,34 +543,32 @@
                 while (it.hasNext()) {
                     OutputMode outputMode = it.next();
                     log.debug("Write Outputnode for " + outputMode.toString());
-                    Element outputModeNode = xmlUtilities
-                            .createArtifactElement(document, "output");
-                    outputModeNode.setAttribute("name", outputMode.getName());
-                    outputModeNode.setAttribute("description", outputMode
-                            .getDescription());
-                    outputModeNode.setAttribute("mime-type", outputMode
-                            .getMimeType());
+                    Element outputModeNode = creator.create("output");
+                    creator.addAttr(
+                        outputModeNode, "name", outputMode.getName());
+                    creator.addAttr(
+                        outputModeNode, "description", outputMode.getDescription());
+                    creator.addAttr(
+                        outputModeNode, "mime-type", outputMode.getMimeType());
                     outputsNode.appendChild(outputModeNode);
 
                     Collection<InputValue> inputParameters = outputMode
                             .getInputParameters();
                     if (inputParameters != null) {
-                        Element inputParametersNode = xmlUtilities
-                                .createArtifactElement(document, "parameter");
+                        Element inputParametersNode = creator.create("parameter");
                         outputModeNode.appendChild(inputParametersNode);
                         Iterator<InputValue> it2 = inputParameters.iterator();
                         while (it2.hasNext()) {
                             InputValue inputValue = it2.next();
-                            Element inputParameterNode = xmlUtilities
-                                    .createArtifactElement(document,
-                                            "parameter");
+                            Element inputParameterNode =
+                                creator.create("parameter");
+                            creator.addAttr(
+                                inputParameterNode, "name", inputValue.getName());
+                            creator.addAttr(
+                                inputParameterNode, "type", inputValue.getType());
+                            creator.addAttr(
+                                inputParameterNode, "value", inputValue.getDefaultValue());
                             inputParametersNode.appendChild(inputParameterNode);
-                            inputParameterNode.setAttribute("name", inputValue
-                                    .getName());
-                            inputParameterNode.setAttribute("type", inputValue
-                                    .getType());
-                            inputParameterNode.setAttribute("value", inputValue
-                                    .getDefaultValue());
                         }
                     }
                 }
@@ -453,17 +581,13 @@
 
     protected Collection<InputData> parseInputData(Document document,
                                                    String xPath) {
-        log.debug("GNVArtifactBase.parseInputData");
         HashMap<String, InputData> returnValue = null;
 
-        log.debug(new ArtifactXMLUtilities().writeDocument2String(document));
-
         NodeList inputElemets = (NodeList) XMLUtils.xpath(document, xPath,
-                XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE);// Config.getNodeSetXPath(document,
-                                                                           // "");
+                XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE);
         if (inputElemets != null) {
-            returnValue = new HashMap<String, InputData>(inputElemets
-                    .getLength());
+            returnValue = new HashMap<String, InputData>();
+
             for (int i = 0; i < inputElemets.getLength(); i++) {
                 Element inputDataNode = (Element)inputElemets.item(i);
                 String name = inputDataNode.getAttribute("name");
@@ -497,7 +621,7 @@
             if (current != null && current instanceof OutputState) {
                 ((OutputState) current)
                         .out(format, this.parseInputData(
-                                format, "/action/out/params/input"),
+                                format, XPATH_OUTPUT_PARAMS),
                                 outputStream, super.identifier, context);
             }
         } catch (StateException e) {
@@ -507,7 +631,14 @@
     }
 
     protected String readOutputType(Document document) {
-        String value = Config.getStringXPath(document, "action/out/@name");
+        String value = XMLUtils.xpathString(
+            document, XPATH_OUTPUT_NAME, ArtifactNamespaceContext.INSTANCE);
         return value;
     }
+
+
+    public void setProduct(Product product) {
+        this.product = product;        
+    }
 }
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org