changeset 26:83a059c204f8

Connected /create with artifact db. artifacts/trunk@61 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 10 Sep 2009 12:29:57 +0000
parents ccc6aae25585
children 75bdaf900473
files Changelog artifact-database/doc/data/create-artifact.xml artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifact.java artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateResource.java artifacts/src/main/java/de/intevation/artifacts/ArtifactNamespaceContext.java contrib/run.sh
diffstat 9 files changed, 160 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/Changelog	Thu Sep 10 09:23:53 2009 +0000
+++ b/Changelog	Thu Sep 10 12:29:57 2009 +0000
@@ -1,4 +1,31 @@
-2009-09-09	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+2009-09-10	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java:
+	Added method to get strings from XML documents via XPath.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifact.java:
+	Used XMLUtils to create new XML documents.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java:
+	Forgot to store backend in instance variable.
+
+	* artifacts/src/main/java/de/intevation/artifacts/ArtifactNamespaceContext.java:
+	Added static instance of this class.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateResource.java:
+	Forward call to ArtifactDatabase, do some error handling.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	Treat problems with database errors more gracefully.
+
+	* contrib/run.sh: Added dependencies to common-pool and common-dbcp.
+
+	* artifact-database/doc/data/create-artifact.xml: New. example data set to
+	create an artifact. Usage
+
+	$ curl --data-binary @doc/data/create-artifact.xml -D - http://localhost:8181/create
+
+2009-09-10	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/FactoriesResource.java:
 	Removed needless import.
@@ -7,7 +34,7 @@
 	artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java:
 	Moved some XPath stuff from Config into XMLUtils.
 
-2009-09-09	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+2009-09-10	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifacts/src/main/java/de/intevation/artifacts/Artifact.java(setup): Added
 	callback parameter in form of the creating factory.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/doc/data/create-artifact.xml	Thu Sep 10 12:29:57 2009 +0000
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<art:action
+     xmlns:art="http://www.intevation.de/2009/artifacts">
+     <art:type name="create"/>
+     <art:factory name="dummy-1"/>
+</art:action>
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Thu Sep 10 09:23:53 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Thu Sep 10 12:29:57 2009 +0000
@@ -33,6 +33,8 @@
         }
 
         context = bootstrap.getContext();
+
+        this.backend = backend;
     }
 
     public String [] getArtifactFactoryNames() {
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Thu Sep 10 09:23:53 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Thu Sep 10 12:29:57 2009 +0000
@@ -176,9 +176,14 @@
         Long ttl = factory.timeToLiveUntouched(
             artifact, context);
 
-        int id = insertDatabase(uuid, ttl);
-
-        return new ArtifactProxy(artifact, id, true);
+        try {
+            int id = insertDatabase(uuid, ttl);
+            return new ArtifactProxy(artifact, id, true);
+        }
+        catch (Exception e) {
+            logger.error(e.getLocalizedMessage(), e);
+        }
+        return null;
     }
 
     protected Artifact getArtifactByUUID(UUID uuid) {
@@ -345,73 +350,87 @@
     }
 
     public void touch(ArtifactProxy proxy) {
-        logger.info("touch: " + proxy);
+
         if (proxy.isUnwritten()) {
             store(proxy);
             return;
         }
-        Connection        connection  = null;
-        PreparedStatement stmnt_touch = null;
-        DataSource        dataSource  = DBConnection.getDataSource();
+
         try {
-            connection = dataSource.getConnection();
+            Connection        connection  = null;
+            PreparedStatement stmnt_touch = null;
+            DataSource        dataSource  = DBConnection.getDataSource();
             try {
-                connection.setAutoCommit(false);
-                stmnt_touch = connection.prepareStatement(SQL_UPDATE);
-                stmnt_touch.setInt(1, proxy.getId());
-                stmnt_touch.execute();
-                connection.commit();
+                connection = dataSource.getConnection();
+                try {
+                    connection.setAutoCommit(false);
+                    stmnt_touch = connection.prepareStatement(SQL_UPDATE);
+                    stmnt_touch.setInt(1, proxy.getId());
+                    stmnt_touch.execute();
+                    connection.commit();
+                }
+                catch (SQLException sqle) {
+                    connection.rollback();
+                }
             }
             catch (SQLException sqle) {
-                connection.rollback();
+                logger.error(sqle.getLocalizedMessage(), sqle);
+            }
+            finally {
+                if (stmnt_touch != null) {
+                    try { stmnt_touch.close(); }
+                    catch (SQLException sqle) {}
+                }
+                if (connection != null) {
+                    try { connection.close(); }
+                    catch (SQLException sqle) {}
+                }
             }
         }
-        catch (SQLException sqle) {
-            logger.error(sqle.getLocalizedMessage(), sqle);
-        }
-        finally {
-            if (stmnt_touch != null) {
-                try { stmnt_touch.close(); }
-                catch (SQLException sqle) {}
-            }
-            if (connection != null) {
-                try { connection.close(); }
-                catch (SQLException sqle) {}
-            }
+        catch (Exception e) {
+            logger.error(e.getLocalizedMessage(), e);
         }
     }
 
     public void store(ArtifactProxy proxy) {
-        logger.info("store: " + proxy);
-        Connection        connection   = null;
-        PreparedStatement stmnt_update = null;
-        DataSource        dataSource   = DBConnection.getDataSource();
+
         try {
-            connection = dataSource.getConnection();
+            Connection        connection   = null;
+            PreparedStatement stmnt_update = null;
+            DataSource        dataSource   = DBConnection.getDataSource();
             try {
-                connection.setAutoCommit(false);
-                stmnt_update = connection.prepareStatement(SQL_UPDATE);
-                stmnt_update.setInt(1, proxy.getId());
-                stmnt_update.setBytes(2, proxy.toBytes());
-                stmnt_update.execute();
-                connection.commit();
+                connection = dataSource.getConnection();
+                try {
+                    connection.setAutoCommit(false);
+                    stmnt_update = connection.prepareStatement(SQL_UPDATE);
+                    stmnt_update.setInt(2, proxy.getId());
+
+                    byte [] bytes = proxy.toBytes();
+
+                    stmnt_update.setBytes(1, bytes);
+                    stmnt_update.execute();
+                    connection.commit();
+                }
+                catch (SQLException sqle) {
+                    connection.rollback();
+                }
             }
             catch (SQLException sqle) {
-                connection.rollback();
+                logger.error(sqle.getLocalizedMessage(), sqle);
+            }
+            finally {
+                if (stmnt_update != null) {
+                    try { stmnt_update.close(); }
+                    catch (SQLException sqle) {}
+                }
+                if (connection != null) {
+                    try { connection.close(); }
+                    catch (SQLException sqle) {}
+                }
             }
         }
-        catch (SQLException sqle) {
-            logger.error(sqle.getLocalizedMessage(), sqle);
-        }
-        finally {
-            if (stmnt_update != null) {
-                try { stmnt_update.close(); }
-                catch (SQLException sqle) {}
-            }
-            if (connection != null) {
-                try { connection.close(); }
-                catch (SQLException sqle) {}
-            }
+        catch (Exception e) {
+            logger.error(e.getLocalizedMessage(), e);
         }
     }
 
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifact.java	Thu Sep 10 09:23:53 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifact.java	Thu Sep 10 12:29:57 2009 +0000
@@ -2,9 +2,6 @@
 
 import org.w3c.dom.Document;
 
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactFactory;
 
@@ -23,19 +20,6 @@
     public DefaultArtifact() {
     }
 
-    protected Document newDocument() {
-        try {
-            return DocumentBuilderFactory
-                .newInstance()
-                .newDocumentBuilder()
-                .newDocument();
-        }
-        catch (ParserConfigurationException pce) {
-            logger.error(pce.getLocalizedMessage(), pce);
-        }
-        return null;
-    }
-
     public String identifier() {
         return this.identifier;
     }
@@ -45,15 +29,15 @@
     }
 
     public Document describe(Object context) {
-        return newDocument();
+        return XMLUtils.newDocument();
     }
 
     public Document advance(Document target, Object context) {
-        return newDocument();
+        return XMLUtils.newDocument();
     }
 
     public Document feed(Document target, Object context) {
-        return newDocument();
+        return XMLUtils.newDocument();
     }
 
     public byte [] out(Document format, Object context) {
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java	Thu Sep 10 09:23:53 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java	Thu Sep 10 12:29:57 2009 +0000
@@ -13,6 +13,7 @@
 import javax.xml.xpath.XPathFactory;
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathConstants;
 
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
@@ -83,6 +84,12 @@
         return xpath(root, query, returnTyp, null);
     }
 
+    public static final String xpathString(
+        Object root, String query, NamespaceContext namespaceContext
+    ) {
+        return (String)xpath(root, query, XPathConstants.STRING, namespaceContext);
+    }
+
     public static final Object xpath(
         Object           root,
         String           query,
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateResource.java	Thu Sep 10 09:23:53 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateResource.java	Thu Sep 10 12:29:57 2009 +0000
@@ -22,6 +22,10 @@
 
 import java.io.IOException;
 
+import de.intevation.artifacts.ArtifactNamespaceContext;
+import de.intevation.artifacts.ArtifactDatabase;
+import de.intevation.artifacts.Artifact;
+
 /**
  * @author Sascha L. Teichmann (sascha.teichmann@intevation)
  */
@@ -32,6 +36,12 @@
 
     public static final String PATH = "/create";
 
+    public static final String XPATH_FACTORY = "/action/factory/@name";
+
+    public static final String NO_FACTORY_MESSAGE = "No factory given";
+
+    public static final String NO_ARTIFACT_CREATED = "No artifact created";
+
     @Post
     public Representation represent() {
 
@@ -50,8 +60,35 @@
             response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
             return new EmptyRepresentation();
         }
+
+        String factory = XMLUtils.xpathString(
+            inputDocument,
+            XPATH_FACTORY,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (factory == null || factory.length() == 0) {
+            Response response = getResponse();
+            response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, NO_FACTORY_MESSAGE);
+            return new EmptyRepresentation();
+        }
+
+        if (logger.isDebugEnabled()) {
+            logger.debug("Create artifact with factory '" + factory + "'");
+        }
+
+        ArtifactDatabase db = (ArtifactDatabase)getContext()
+            .getAttributes().get("database");
+
+        Artifact artifact = db.createArtifactWithFactory(factory);
+
+        if (artifact == null) {
+            Response response = getResponse();
+            response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, NO_ARTIFACT_CREATED);
+            return new EmptyRepresentation();
+        }
         
-        Document outputDocument = XMLUtils.newDocument();
+        Document outputDocument = artifact.describe(
+            db.getArtifactContext());
 
         return new DomRepresentation(
             MediaType.APPLICATION_XML, outputDocument);
--- a/artifacts/src/main/java/de/intevation/artifacts/ArtifactNamespaceContext.java	Thu Sep 10 09:23:53 2009 +0000
+++ b/artifacts/src/main/java/de/intevation/artifacts/ArtifactNamespaceContext.java	Thu Sep 10 12:29:57 2009 +0000
@@ -22,6 +22,9 @@
      */
     public final static String NAMESPACE_PREFIX = "art";
 
+    public static final ArtifactNamespaceContext INSTANCE =
+        new ArtifactNamespaceContext();
+
     public ArtifactNamespaceContext() {
     }
 
--- a/contrib/run.sh	Thu Sep 10 09:23:53 2009 +0000
+++ b/contrib/run.sh	Thu Sep 10 12:29:57 2009 +0000
@@ -3,13 +3,12 @@
 RESTLET_XML=`find -L ~/.m2 -name org\.restlet.ext.xml-\*M\*.jar`
 H2=`find -L ~/.m2 -name h2-\*.jar`
 LOG4J=`find -L ~/.m2 -name log4j-1.2.13\*.jar`
-echo $RESTLET
-echo $LOG4J
-echo $H2
+DBCP=`find -L ~/.m2 -name commons-dbcp-\*.jar`
+POOL=`find -L ~/.m2 -name commons-pool-1.5\*.jar`
 DIR=`dirname $0`/..
 CLASSPATH=$DIR/artifact-database/target/classes
 CLASSPATH=$CLASSPATH:$DIR/artifacts/target/classes
 CLASSPATH=$CLASSPATH:$RESTLET_CORE:$RESTLET_XML
-CLASSPATH=$CLASSPATH:$LOG4J:$H2
+CLASSPATH=$CLASSPATH:$LOG4J:$H2:$DBCP:$POOL
 export CLASSPATH
 java "$@"

http://dive4elements.wald.intevation.org