diff artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java @ 32:c2d53bd30ab8

Re-factored artifact API for better integration of background processing. artifacts/trunk@78 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 13 Sep 2009 14:50:53 +0000
parents 22b03d5c84c5
children 9935e1c928de
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java	Sat Sep 12 10:45:28 2009 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java	Sun Sep 13 14:50:53 2009 +0000
@@ -10,6 +10,7 @@
 import org.restlet.ext.xml.DomRepresentation;
 
 import de.intevation.artifacts.ArtifactDatabase;
+import de.intevation.artifacts.ArtifactDatabaseException;
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactNamespaceContext;
 
@@ -60,22 +61,46 @@
         ArtifactDatabase db = (ArtifactDatabase)getContext()
             .getAttributes().get("database");
 
-        Artifact artifact = db.getArtifact(identifier);
+        try {
+            return new DomRepresentation(
+                MediaType.APPLICATION_XML,
+                db.describe(identifier));
+        }
+        catch (ArtifactDatabaseException adbe) {
+            Response response = getResponse();
+            response.setStatus(
+                Status.CLIENT_ERROR_NOT_FOUND, adbe.getMessage());
+            return new EmptyRepresentation();
+        }
+    }
 
-        if (artifact == null) {
+    protected Representation dispatch(
+        String           identifier,
+        String           action,
+        Document         source,
+        ArtifactDatabase db
+    ) {
+        Document out = null;
+
+        try {
+            if (action.equals(FEED)) {
+                out = db.feed(identifier, source);
+            }
+            else if (action.equals(ADVANCE)) {
+                out = db.advance(identifier, source);
+            }
+            else {
+                throw new ArtifactDatabaseException(NO_SUCH_ACTION_MESSAGE);
+            }
+        }
+        catch (ArtifactDatabaseException adbe) {
             Response response = getResponse();
-            response.setStatus(Status.CLIENT_ERROR_NOT_FOUND, NO_ARTIFACT_FOUND);
+            response.setStatus(
+                Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage());
             return new EmptyRepresentation();
         }
 
-        Document description = artifact.describe(db.getArtifactContext());
-
-        if (logger.isDebugEnabled()) {
-            logger.debug("out document: " + description);
-        }
-
-        return new DomRepresentation(
-            MediaType.APPLICATION_XML, description);
+        return new DomRepresentation(MediaType.APPLICATION_XML, out);
     }
 
     @Post
@@ -103,52 +128,17 @@
 
         if (action == null || action.length() == 0) {
             Response response = getResponse();
-            response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MESSAGE);
-            return new EmptyRepresentation();
-        }
-
-        int actionType = -1;
-
-             if (FEED   .equals(action)) actionType = 0;
-        else if (ADVANCE.equals(action)) actionType = 1;
-        else {
-            Response response = getResponse();
-            response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, NO_SUCH_ACTION_MESSAGE);
+            response.setStatus(
+                Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MESSAGE);
             return new EmptyRepresentation();
         }
 
         String identifier = (String)request.getAttributes().get("uuid");
 
-        if (logger.isDebugEnabled()) {
-            logger.debug("looking for artifact id '" + identifier + "'");
-        }
-
         ArtifactDatabase db = (ArtifactDatabase)getContext()
             .getAttributes().get("database");
 
-        Artifact artifact = db.getArtifact(identifier);
-
-        if (artifact == null) {
-            Response response = getResponse();
-            response.setStatus(Status.CLIENT_ERROR_NOT_FOUND, NO_ARTIFACT_FOUND);
-            return new EmptyRepresentation();
-        }
-
-        Document document = null;
-
-        switch (actionType) {
-            case 0:
-                document = artifact.feed(inputDocument, db.getArtifactContext());
-                break;
-            case 1:
-                document = artifact.advance(inputDocument, db.getArtifactContext());
-                break;
-            default:
-                // should not happen
-                return new EmptyRepresentation();
-        }
-
-        return new DomRepresentation(MediaType.APPLICATION_XML, document);
+        return dispatch(identifier, action, inputDocument, db);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org