diff artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java @ 273:22a90706d32d

Enables the artifact server to set the TTL of a specific collection via REST call. artifacts/trunk@2070 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 07 Jun 2011 16:27:47 +0000
parents d9a99b28a847
children e92d5944fe4b
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Tue Jun 07 11:06:54 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Tue Jun 07 16:27:47 2011 +0000
@@ -222,6 +222,12 @@
     public static final String XPATH_COLLECTION_ITEM_ATTRIBUTE =
         "/art:action/art:type/art:artifact/art:attribute";
 
+    /**
+     * XPath to figure out the time to live value for setting a new TTL.
+     */
+    public static final String XPATH_COLLECTION_TTL =
+        "/art:action/art:type/art:ttl/@value";
+
 
     /**
      * This inner class allows the deferral of writing the output
@@ -1485,6 +1491,59 @@
         return result;
     }
 
+    public Document setCollectionTTL(String uuid, Document doc, CallMeta meta)
+    throws ArtifactDatabaseException
+    {
+        Document result            = XMLUtils.newDocument();
+        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
+            result,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        Element root = ec.create("result");
+        result.appendChild(root);
+
+        String tmp = XMLUtils.xpathString(
+            doc, XPATH_COLLECTION_TTL, ArtifactNamespaceContext.INSTANCE);
+
+        logger.info("Set TTL of artifact collection '" + uuid + "' to: " + tmp);
+
+        if (tmp == null || tmp.length() == 0) {
+            logger.warn("No ttl for this collection specified.");
+            root.setTextContent(OPERATION_FAILURE);
+
+            return result;
+        }
+
+        Long ttl = null;
+        if ((tmp = tmp.toUpperCase()).equals("INF")) {
+            ttl = null;
+        }
+        else if (tmp.equals("DEFAULT")) {
+            ArtifactCollectionFactory acf = getArtifactCollectionFactory();
+            ttl = acf.timeToLiveUntouched(null, context);
+        }
+        else {
+            try {
+                ttl = Long.valueOf(tmp);
+
+                if (ttl < 0) {
+                    throw new NumberFormatException("Negative value.");
+                }
+            }
+            catch (NumberFormatException nfe) {
+                logger.error("Could not determine TTL", nfe);
+                root.setTextContent(OPERATION_FAILURE);
+                return result;
+            }
+        }
+
+        boolean success = backend.setCollectionTTL(uuid, ttl);
+        root.setTextContent(success ? OPERATION_SUCCESSFUL: OPERATION_FAILURE);
+
+        return result;
+    }
+
     public DeferredOutput outCollection(
         String   collectionId,
         Document format,

http://dive4elements.wald.intevation.org