changeset 7:fe6a64545552

Support for creating an Artifact integrated gnv/trunk@83 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Mon, 14 Sep 2009 13:46:25 +0000
parents 6592580ae823
children 65ff6fcfee0c
files gnv/Changelog gnv/pom.xml gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/Artifact.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactFactory.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactObject.java gnv/src/main/java/de/intevation/gnv/propertiesreader/PropertiesReaderFactory.java gnv/src/main/java/de/intevation/gnv/servlet/GNVActionServlet.java
diffstat 9 files changed, 202 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/gnv/Changelog	Fri Sep 11 15:25:43 2009 +0000
+++ b/gnv/Changelog	Mon Sep 14 13:46:25 2009 +0000
@@ -1,3 +1,20 @@
+2009-09-14  Tim Englich  <tim.englich@intevation.de>
+
+    * src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java Edited:
+      All Methods for doing the create-Request and reading the Result-Document implemented.
+    * src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java (createNewArtifact) Edited:
+      Add Method for creating a new Artifact in the ArtifactDatabase
+      
+    * src/main/java/de/intevation/gnv/artifactdatabase/objects/Artifact.java (Artifact): 
+      Added new Class for the Representation of an Artifact
+    * src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactFactory.java (getHash) Edited,
+    * src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactObject.java (getHash) Edited
+      getHash Method added. Now it will be possible to read the Hash of an Artifact send by the Artifactdatabase. 
+    * src/main/java/de/intevation/gnv/propertiesreader/PropertiesReaderFactory.java (getInstance) Edited:
+      Bug Fixed. factory will now be instantiated.
+    * src/main/java/de/intevation/gnv/servlet/GNVActionServlet.java Edited,
+      Integration of temporal Basic Log4J Support.
+      
 2009-09-11  Tim Englich  <tim.englich@intevation.de>
     * pom.xml Further Dependencies and Plugins added
     Deleted some utdated Files
--- a/gnv/pom.xml	Fri Sep 11 15:25:43 2009 +0000
+++ b/gnv/pom.xml	Mon Sep 14 13:46:25 2009 +0000
@@ -121,6 +121,9 @@
                  <m2eclipse>true</m2eclipse>
                  <downloadSources>true</downloadSources>
                  <wtpversion>1.5</wtpversion>
+                 <additionalProjectFacets>
+                       <jst.web.xdoclet>1.2.3</jst.web.xdoclet>
+                 </additionalProjectFacets>
              </configuration>
       </plugin>
     </plugins>
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java	Fri Sep 11 15:25:43 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java	Mon Sep 14 13:46:25 2009 +0000
@@ -22,4 +22,13 @@
      */
     public Collection<ArtifactObject> getArtifactFactories() throws ArtifactDatabaseClientException;
 
+    /**
+     * b
+     * @param artifactFactory
+     * @return
+     * @throws ArtifactDatabaseClientException
+     */
+    public ArtifactObject createNewArtifact(ArtifactObject artifactFactory) throws ArtifactDatabaseClientException;
+
+
 }
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Fri Sep 11 15:25:43 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Mon Sep 14 13:46:25 2009 +0000
@@ -30,12 +30,14 @@
 import org.restlet.data.Request;
 import org.restlet.data.Response;
 import org.restlet.representation.Representation;
+import org.restlet.representation.StringRepresentation;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
 
 import de.intevation.gnv.artifactdatabase.client.exception.ArtifactDatabaseClientException;
+import de.intevation.gnv.artifactdatabase.objects.Artifact;
 import de.intevation.gnv.artifactdatabase.objects.ArtifactFactory;
 import de.intevation.gnv.artifactdatabase.objects.ArtifactObject;
 import de.intevation.gnv.util.XMLUtils;
@@ -46,6 +48,16 @@
  */
 public class DefaultArtifactDatabaseClient implements ArtifactDatabaseClient {
     /**
+     * The URI of the namespace of the artifacts.
+     */
+    public final static String NAMESPACE_URI = "http://www.intevation.de/2009/artifacts";
+    
+    /**
+     * The XML prefix for the artifacts namespace.
+     */
+    public final static String NAMESPACE_PREFIX = "art";
+
+    /**
      * the logger, used to log exceptions and additonaly information
      */
     private static Logger log = Logger.getLogger(DefaultArtifactDatabaseClient.class);
@@ -119,6 +131,20 @@
         return output.getStream();
     }
     
+    /**
+     * @throws IOException
+     */
+    private InputStream doPostRequest(String requestUrl, Document requestBody) throws IOException {
+        Client client = new Client(Protocol.HTTP);
+        Request request = new Request(Method.POST, requestUrl);
+        String documentBody = new XMLUtils().writeDocument2String(requestBody);
+        Representation representation = new StringRepresentation(documentBody);
+        request.setEntity(representation);
+        Response response = client.handle(request);
+        Representation output = response.getEntity();
+        return output.getStream();
+    }
+    
     private synchronized void initialize(){
         if (!initialized){
             this.artifactDatabases = new HashMap<String, String>();
@@ -127,6 +153,68 @@
         }
         
     }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#createNewArtifact(de.intevation.gnv.artifactdatabase.objects.ArtifactObject)
+     */
+    public ArtifactObject createNewArtifact(ArtifactObject artifactFactory)
+            throws ArtifactDatabaseClientException {
+        
+        try {
+            XMLUtils xmlUtils = new XMLUtils();
+            Document request = this.createCreateRequestBody(artifactFactory.getId());
+            String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl();
+            InputStream is = this.doPostRequest(url+"/create", request);
+            Document result = xmlUtils.readDocument(is);
+            // TODO: Fehleranalyse des Dokumentes
+            log.debug(xmlUtils.writeDocument2String(result));
+            return this.getArtifact(result);
+        } catch (IOException e) {
+            log.error(e,e);
+            throw new ArtifactDatabaseClientException(e);
+        }
+    }
     
+    
+    private ArtifactObject getArtifact(Document document){
+        XMLUtils xmlUtils = new XMLUtils(); 
+        String uuid = xmlUtils.getStringXPath(document, "/result/uuid/@value");
+        String hash = xmlUtils.getStringXPath(document, "/result/hash/@value");
+        log.info("NEW Artifact: "+uuid+" / "+hash);
+        return new Artifact(uuid, hash);
+    }
+    
+    
+    
+    
+    private Document createCreateRequestBody(String artifactFactoryName){
+        Document document = new XMLUtils().newDocument();
+        Node rootNode  = this.createRootNode(document);
+        Element typeNode = this.createArtifactElement(document, "type");
+        typeNode.setAttribute("name", "create");
+        rootNode.appendChild(typeNode);
+             
+        Element factoyNode = this.createArtifactElement(document, "factory");
+        factoyNode.setAttribute("name", artifactFactoryName);
+        rootNode.appendChild(factoyNode);
+        
+        return document;
+    }
+    
+    private Element createRootNode(Document document){
+        Element rootNode = this.createArtifactElement(document,"action");
+        document.appendChild(rootNode);
+        return rootNode;
+    }
+    
+    /**
+     * @param document
+     * @return
+     */
+    private Element createArtifactElement(Document document, String name) {
+        Element node = document.createElementNS(NAMESPACE_URI, name);
+        node.setPrefix(NAMESPACE_PREFIX);
+        return node;
+    }
    
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/Artifact.java	Mon Sep 14 13:46:25 2009 +0000
@@ -0,0 +1,65 @@
+/**
+ *
+ */
+package de.intevation.gnv.artifactdatabase.objects;
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class Artifact implements ArtifactObject {
+
+    private String id = null;
+    private String hash = null;
+    private boolean selected = false;
+    /**
+     * Constructor
+     */
+    public Artifact(String uuid, String hash) {
+        
+        this.id = uuid;
+        this.hash = hash;
+    }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#getHash()
+     */
+    public String getHash() {
+        return this.hash;
+    }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#getDescription()
+     */
+    public String getDescription() {
+        return this.id;
+    }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#getId()
+     */
+    public String getId() {
+        return this.id;
+    }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#getName()
+     */
+    public String getName() {
+        return this.id;
+    }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#isSelected()
+     */
+    public boolean isSelected() {
+        return this.selected;
+    }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#setSelected(boolean)
+     */
+    public void setSelected(boolean selected) {
+        this.selected = selected;
+    }
+
+}
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactFactory.java	Fri Sep 11 15:25:43 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactFactory.java	Mon Sep 14 13:46:25 2009 +0000
@@ -74,4 +74,12 @@
         return description;
     }
 
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#getHash()
+     */
+    public String getHash() {
+        return null;
+    }
+
 }
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactObject.java	Fri Sep 11 15:25:43 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactObject.java	Mon Sep 14 13:46:25 2009 +0000
@@ -17,5 +17,7 @@
     
     public String getName();
     public String getDescription();
+    
+    public String getHash();
 
 }
--- a/gnv/src/main/java/de/intevation/gnv/propertiesreader/PropertiesReaderFactory.java	Fri Sep 11 15:25:43 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/propertiesreader/PropertiesReaderFactory.java	Mon Sep 14 13:46:25 2009 +0000
@@ -45,7 +45,7 @@
      */
     public static PropertiesReaderFactory getInstance(){
         if (instance == null){
-            instance = null;
+            instance = new PropertiesReaderFactory();
         }
         return instance;
     }
--- a/gnv/src/main/java/de/intevation/gnv/servlet/GNVActionServlet.java	Fri Sep 11 15:25:43 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/servlet/GNVActionServlet.java	Mon Sep 14 13:46:25 2009 +0000
@@ -6,6 +6,7 @@
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 
+import org.apache.log4j.BasicConfigurator;
 import org.apache.log4j.Logger;
 import org.apache.struts.action.ActionServlet;
 
@@ -20,8 +21,13 @@
     /**
      * the logger, used to log exceptions and additonaly information
      */
-    private static Logger log = Logger.getLogger(GNVActionServlet.class);
-
+    private static Logger log = null;
+    
+    static {
+        BasicConfigurator.configure();
+        log = Logger.getLogger(GNVActionServlet.class);
+    }
+    
     /**
      * The UID of this Class
      */

http://dive4elements.wald.intevation.org