diff gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/fis/FISArtifact.java @ 71:e4ecf3188bdf

Integrated FIS-Artifact gnv-artifacts/trunk@62 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Thu, 10 Sep 2009 13:11:24 +0000
parents
children 504570de21fd
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/fis/FISArtifact.java	Thu Sep 10 13:11:24 2009 +0000
@@ -0,0 +1,341 @@
+/**
+ *
+ */
+package de.intevation.gnv.artifacts.fis;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import de.intevation.artifactdatabase.Config;
+import de.intevation.artifactdatabase.DefaultArtifact;
+import de.intevation.artifactdatabase.XMLUtils;
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactFactory;
+import de.intevation.gnv.artifacts.GNVArtifactBase;
+import de.intevation.gnv.artifacts.context.GNVArtifactContext;
+import de.intevation.gnv.artifacts.fis.product.DefaultProduct;
+import de.intevation.gnv.artifacts.fis.product.Product;
+import de.intevation.gnv.transition.DefaultInputData;
+import de.intevation.gnv.transition.InputData;
+import de.intevation.gnv.transition.InputValue;
+import de.intevation.gnv.transition.OutputMode;
+import de.intevation.gnv.transition.OutputTransition;
+import de.intevation.gnv.utils.ArtifactFactoryUtilities;
+import de.intevation.gnv.utils.ArtifactXMLUtilities;
+
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class FISArtifact extends DefaultArtifact {
+    
+    /**
+     * the logger, used to log exceptions and additonaly information
+     */
+    private static Logger log = Logger.getLogger(GNVArtifactBase.class);
+    /**
+     * The UID of this Class
+     */
+    private static final long serialVersionUID = 2874044542701727083L;
+    
+    /**
+     * The Identifier for the Replacement of the Artifactname
+     */
+    public static final String XPATH_IDENTIFIER_REPLACE = "IDENTIFIER";
+    
+    /**
+     * The XPATH to the XML-Fragment that should be used for the Configuration
+     */
+    public static final String XPATH_ARTIFACT_CONFIGURATION= "/artifact-database/artifacts/artifact[@name='"+XPATH_IDENTIFIER_REPLACE+"']";
+    
+    /**
+     * The Name of the Artifact
+     */
+    private String name = null;
+    
+    private Map<String, Product> products = null;
+    
+    private Artifact productArtifact = null;
+    
+    private Product current = null;
+    
+    private ArtifactXMLUtilities xmlUtilities = new ArtifactXMLUtilities();
+    
+    
+    /**
+     * @see de.intevation.artifactdatabase.DefaultArtifact#advance(org.w3c.dom.Document, java.lang.Object)
+     */
+    @Override
+    public Document advance(Document target, Object context) {
+        if (this.productArtifact == null){
+            if (this.current != null){
+                String uuid = Config.getStringXPath(target, "action/uuid/@value"); // TODO: müssen wir für das subartifact eine veränderte uuid führen?
+                String hash = Config.getStringXPath(target, "action/hash/@value");
+                this.productArtifact = this.current.getArtifactFactory().createArtifact(uuid, context);
+                Document feedDocument =  xmlUtilities.reInitDocument(this.createFeedProductArtifactDocument(uuid, hash));
+                log.debug("Feed ==> "+this.xmlUtilities.writeDocument2String(feedDocument));
+                Document descibeDocument =  xmlUtilities.reInitDocument(this.productArtifact.describe(context));
+                log.debug("Descibe ==> "+this.xmlUtilities.writeDocument2String(descibeDocument));
+                this.productArtifact.feed(feedDocument, context);
+                String targetName =  Config.getStringXPath(descibeDocument, "result/reachable-states/state/@name");
+                Document advanceDocument = xmlUtilities.reInitDocument(this.createAdvanceProductArtifactDocument(uuid, hash, targetName));
+                log.debug("Advance ==> "+this.xmlUtilities.writeDocument2String(advanceDocument));
+                return this.productArtifact.advance(advanceDocument, context);
+            }else{
+                log.warn("Artifact is not configured properly. Call feed first.");
+                // TODO Fehlerdokument erzeugen.
+            }
+            return null;
+        }else{
+            return this.productArtifact.advance(target, context);
+        }
+    }
+    private Document createAdvanceProductArtifactDocument(String uuid, String hash, String targetName){
+        Document document = XMLUtils.newDocument();
+        Element rootNode = xmlUtilities.createArtifactElement(document, "action");
+        
+        Element typeNode = xmlUtilities.createArtifactElement(document, "type");
+        typeNode.setAttribute("name", "advanve");
+        rootNode.appendChild(typeNode);
+        
+        Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid");
+        uuidNode.setAttribute("value", uuid);
+        rootNode.appendChild(uuidNode);
+        
+        Element hashNode = xmlUtilities.createArtifactElement(document, "hash");
+        hashNode.setAttribute("value", hash);
+        rootNode.appendChild(hashNode);
+        Element targetNode = xmlUtilities.createArtifactElement(document, "target");
+        targetNode.setAttribute("name", targetName);
+        rootNode.appendChild(targetNode);
+        
+        document.appendChild(rootNode);
+        return document;
+    }
+    
+    private Document createFeedProductArtifactDocument(String uuid, String hash){
+        Document document = XMLUtils.newDocument();
+        Element rootNode = xmlUtilities.createArtifactElement(document, "action");
+        
+        Element typeNode = xmlUtilities.createArtifactElement(document, "type");
+        typeNode.setAttribute("name", "feed");
+        rootNode.appendChild(typeNode);
+        
+        Element uuidNode = xmlUtilities.createArtifactElement(document, "uuid");
+        uuidNode.setAttribute("value", uuid);
+        rootNode.appendChild(uuidNode);
+        
+        Element hashNode = xmlUtilities.createArtifactElement(document, "hash");
+        hashNode.setAttribute("value", hash);
+        rootNode.appendChild(hashNode);
+        
+        Element dataNode = xmlUtilities.createArtifactElement(document, "data");
+        rootNode.appendChild(dataNode);
+        
+        
+        Collection<InputData> parameter = this.current.getParameter();
+        if (parameter != null){
+            Iterator<InputData> parameterIt = parameter.iterator();
+            while(parameterIt.hasNext()){
+                InputData inputData = parameterIt.next();
+                Element inputNode = xmlUtilities.createArtifactElement(document, "input");
+                inputNode.setAttribute("name", inputData.getName());
+                inputNode.setAttribute("value", inputData.getValue());
+                dataNode.appendChild(inputNode);
+            }
+        }
+        document.appendChild(rootNode);
+        return document;
+        
+    }
+
+    /**
+     * @see de.intevation.artifactdatabase.DefaultArtifact#describe(java.lang.Object)
+     */
+    @Override
+    public Document describe(Object context) {
+        if (this.productArtifact == null){
+            return this.createDescibeOutput();
+        }else{
+            return this.productArtifact.describe(context);
+        }
+    }
+
+    /**
+     * @see de.intevation.artifactdatabase.DefaultArtifact#feed(org.w3c.dom.Document, java.lang.Object)
+     */
+    @Override
+    public Document feed(Document target, Object context) {
+        if (this.productArtifact == null){
+            String productName = Config.getStringXPath(target, "action/data/input[@name='product']/@value");
+            if (this.products.containsKey(productName)) {
+                this.current = this.products.get(productName);
+            }else{
+                log.error("Product does not exists for "+productName);
+                // TODO: Fehlerdokument erzeugen.
+            }
+            return null;
+        }else{
+            return this.productArtifact.feed(target, context);
+        }
+    }
+
+    /**
+     * @see de.intevation.artifactdatabase.DefaultArtifact#out(org.w3c.dom.Document, java.lang.Object)
+     */
+    @Override
+    public byte[] out(Document format, Object context) {
+        byte[] returnValue = null;
+        if (this.productArtifact != null){
+            returnValue = this.productArtifact.out(format, context);
+        }
+        return returnValue;
+    }
+
+    /**
+     * Constructor
+     */
+    public FISArtifact() {
+        super();
+    }
+
+   /**
+    * @see de.intevation.artifactdatabase.DefaultArtifact#setup(java.lang.String, de.intevation.artifacts.ArtifactFactory, java.lang.Object)
+    */
+    @Override
+    public void setup(String identifier, ArtifactFactory factory, Object context) {
+        log.debug("FISArtifact.setup");
+        this.name = factory.getName();
+        super.setup(identifier,factory, context);
+        if (context instanceof GNVArtifactContext){
+            GNVArtifactContext gnvContext = (GNVArtifactContext)context;
+            Document doc = gnvContext.getConfig();
+            Node artifactNode = this.getConfigurationFragment(doc);
+            
+            NodeList products = Config.getNodeSetXPath(artifactNode,"products/product");
+           if (products != null){
+               this.products = new HashMap<String, Product>(products.getLength());
+               for (int i = 0; i < products.getLength(); i++){
+                   Node productNode = products.item(i);
+                   String productName = Config.getStringXPath(productNode, "@name");
+                   NodeList parameterNodes = Config.getNodeSetXPath(productNode, "parameters/parameter");
+                   Collection<InputData> parameter = null;
+                   if (parameterNodes != null){
+                       parameter = new ArrayList<InputData>(parameterNodes.getLength());
+                       for (int j = 0; j < parameterNodes.getLength(); j++){
+                           Node parameterNode = parameterNodes.item(j);
+                           String name = Config.getStringXPath(parameterNode, "@name");
+                           String value = Config.getStringXPath(parameterNode, "@value");
+                           parameter.add(new DefaultInputData(name, value));
+                       }
+                   }
+                   Node artifactFactoryNode = Config.getNodeXPath(productNode, "artifact-factory");
+                   ArtifactFactory artifactFactory =new ArtifactFactoryUtilities().createArtitfactFactor(doc, artifactFactoryNode);
+                   this.products.put(productName, new DefaultProduct(productName, parameter,artifactFactory));
+               }
+           }
+            
+            
+        }
+    }
+    
+    protected Node getConfigurationFragment(Document document){
+        log.debug("GNVArtifactBase.getConfigurationFragment");
+        String xpathQuery = XPATH_ARTIFACT_CONFIGURATION.replaceAll(XPATH_IDENTIFIER_REPLACE, this.name);
+        log.debug(xpathQuery);
+        return Config.getNodeXPath(document,xpathQuery);
+    }
+    
+    protected Document createDescibeOutput(){
+        log.debug("GNVArtifactBase.createDescibeOutput");
+        Document document = XMLUtils.newDocument();
+        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.createUserInterface(rootNode, document);
+        
+        return document;
+    }
+    
+    protected Element createRootNode(Document document){
+        Element rootNode = xmlUtilities.createArtifactElement(document,"result");
+        document.appendChild(rootNode);
+        return rootNode;
+    }
+    
+    protected void createHeader(Element parent, Document document, String documentType){
+        Element typeNode = xmlUtilities.createArtifactElement(document,"type");
+        typeNode.setAttribute("name", documentType);
+        parent.appendChild(typeNode);
+        
+        Element uuidNode = xmlUtilities.createArtifactElement(document,"uuid");
+        uuidNode.setAttribute("value", super.identifier);
+        parent.appendChild(uuidNode);
+        
+        Element hashNode = xmlUtilities.createArtifactElement(document,"hash");
+        hashNode.setAttribute("value", this.hash());
+        parent.appendChild(hashNode);
+
+        
+    }
+    protected void createReachableStates(Element parent,Document document){
+        Element stateNode = xmlUtilities.createArtifactElement(document,"reachable-states");
+        if (this.products != null){
+            Iterator<Product> products = this.products.values().iterator();
+            while(products.hasNext()){
+                Product product = products.next();
+                Element currentNode = xmlUtilities.createArtifactElement(document,"state");
+                currentNode.setAttribute("name", product.getName());
+                currentNode.setAttribute("description", product.getName());
+                stateNode.appendChild(currentNode);
+            }
+        }
+        parent.appendChild(stateNode);
+    }
+    
+    protected void createCurrentState(Element parent, Document document){
+        Element stateNode = xmlUtilities.createArtifactElement(document,"state");
+        stateNode.setAttribute("name", "choose-product");
+        stateNode.setAttribute("description", "Initialer Stand Auswahl des products");
+        parent.appendChild(stateNode);
+    }
+    
+    
+    protected void createModel(Element parent, Document document){
+        Element modelNode = xmlUtilities.createArtifactElement(document,"model");
+        
+        Element inputNode = xmlUtilities.createArtifactElement(document,"input");
+        inputNode.setAttribute("name", "product");
+        inputNode.setAttribute("type", "String");
+        modelNode.appendChild(inputNode);
+        
+        parent.appendChild(modelNode);
+    }
+    
+    protected void createUserInterface(Element parent, Document document){
+        Element uiNode = xmlUtilities.createArtifactElement(document,"ui");
+        
+        // TODO hier selectbox mit den produktnamen
+        
+        parent.appendChild(uiNode);
+    }
+    
+    protected void createOutputs(Element parent, Document document){
+        log.debug("GNVArtifactBase.createOutputs");
+        Element outputsNode = xmlUtilities.createArtifactElement(document,"outputs");
+        parent.appendChild(outputsNode);
+    }
+
+}

http://dive4elements.wald.intevation.org