changeset 343:542caebea773

Add functionality to get 'oldest' artifact of a collection.' artifacts/trunk@3037 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 19 Oct 2011 15:55:33 +0000
parents 9a85e0ef6e02
children be883e843539
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java artifact-database/src/main/resources/sql/org-h2-driver.properties artifact-database/src/main/resources/sql/org-postgresql-driver.properties artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java
diffstat 6 files changed, 124 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Oct 17 15:42:43 2011 +0000
+++ b/ChangeLog	Wed Oct 19 15:55:33 2011 +0000
@@ -1,3 +1,25 @@
+2011-10-19	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
+
+	Add backend and db functionality query artifact which is belongs to
+	a collection the longest (not neccessary the longest artifact itself).
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java
+	  (getCollectionsMasterArtifact): New. Access master artifact for
+	   given collection (masterartifact defined as artifact which belongs
+	   to the collection the longest).
+
+	* artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java
+	  (getCollectionsMasterArtifact): New (extended interface).
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java
+	 (getMasterArtifact): New, get UUID of oldest artifact which belongs to
+	 collection the longest.
+
+	* artifact-database/src/main/resources/sql/org-h2-driver.properties,
+	  artifact-database/src/main/resources/sql/org-postgresql-driver.properties:
+	  Added SQL query to get list of artifacts in collection sorted by the
+	  entry date.
+
 2011-10-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifacts-common/src/main/java/de/intevation/artifacts/common/utils/StringUtils.java(toUpperCase):
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Mon Oct 17 15:42:43 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Wed Oct 19 15:55:33 2011 +0000
@@ -767,6 +767,8 @@
     )
     throws ArtifactDatabaseException
     {
+        logger.debug("ArtifactDatabaseImpl.createArtifactWithFactory "
+             + factoryName);
         ArtifactFactory factory = getArtifactFactory(factoryName);
 
         if (factory == null) {
@@ -1287,6 +1289,64 @@
 
     // Collection API
 
+    public Document getCollectionsMasterArtifact(
+        String collectionId,
+        CallMeta meta)
+        throws ArtifactDatabaseException
+    {
+        Document result = XMLUtils.newDocument();
+        String masterUUID = backend.getMasterArtifact(collectionId);
+
+        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
+            result,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        ArtifactCollectionFactory acf = getArtifactCollectionFactory();
+
+        if (acf == null) {
+            throw new ArtifactDatabaseException(NO_SUCH_FACTORY);
+        }
+
+        UserFactory uf = getUserFactory();
+        if (uf == null) {
+            throw new ArtifactDatabaseException(NO_SUCH_FACTORY);
+        }
+
+        ArtifactCollection c = backend.getCollection(
+            collectionId, acf, uf, context);
+
+        if (c == null) {
+            logger.warn("No collection found with identifier: " + collectionId);
+            throw new ArtifactDatabaseException(NO_SUCH_COLLECTION);
+        }
+
+        Element root = ec.create("artifact-collection");
+        ec.addAttr(root, "name", c.getName(), true);
+        ec.addAttr(root, "uuid", c.identifier(), true);
+        ec.addAttr(root, "ttl",  String.valueOf(c.getTTL()), true);
+
+        Date creationTime = c.getCreationTime();
+        String creation   = creationTime != null
+            ? Long.toString(creationTime.getTime())
+            : "";
+
+        ec.addAttr(root, "creation", creation,  true);
+        result.appendChild(root);
+
+        if (masterUUID == null || masterUUID.length() == 0) {
+            logger.debug("No master for the collection existing.");
+            return result;
+        }
+
+        Element master = ec.create("artifact");
+        ec.addAttr(master, "uuid", masterUUID, true);
+
+        root.appendChild(master);
+
+        return result;
+    }
+
     public Document listCollections(String userId, CallMeta callMeta)
         throws ArtifactDatabaseException
     {
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Mon Oct 17 15:42:43 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Wed Oct 19 15:55:33 2011 +0000
@@ -115,6 +115,7 @@
     public String SQL_COLLECTIONS_SELECT_GID;
     public String SQL_COLLECTIONS_CREATION_TIME;
     public String SQL_COLLECTIONS_ID_BY_GID;
+    public String SQL_COLLECTIONS_OLDEST_ARTIFACT;
     public String SQL_DELETE_COLLECTION_ITEMS;
     public String SQL_DELETE_COLLECTION;
     public String SQL_COLLECTION_CHECK_ARTIFACT;
@@ -332,7 +333,8 @@
         SQL_COLLECTIONS_SELECT_ALL = sql.get("collections.select.all");
         SQL_COLLECTIONS_SELECT_GID = sql.get("collections.select.by.gid");
         SQL_COLLECTIONS_CREATION_TIME = sql.get("collection.creation.time");
-        SQL_COLLECTIONS_ID_BY_GID = sql.get("collections.id.by.gid");
+        SQL_COLLECTIONS_OLDEST_ARTIFACT = sql.get("collections.artifacts.oldest");
+        SQL_COLLECTIONS_ID_BY_GID = sql.get("collections.by.gid");
         SQL_DELETE_COLLECTION_ITEMS = sql.get("delete.collection.items");
         SQL_DELETE_COLLECTION = sql.get("delete.collection");
         SQL_COLLECTION_CHECK_ARTIFACT = sql.get("collection.check.artifact");
@@ -1258,6 +1260,32 @@
     }
 
 
+    public String getMasterArtifact(final String collectionId) {
+        if (!isValidIdentifier(collectionId)) {
+            logger.debug("Invalid collection id: '" + collectionId + "'");
+            return null;
+        }
+        final List<String> uuid = new ArrayList<String>();
+        SQLExecutor.Instance exec = sqlExecutor.new Instance() {
+            public boolean doIt() throws SQLException {
+                // Fetch masters (oldest artifact) id.
+                prepareStatement(SQL_COLLECTIONS_OLDEST_ARTIFACT);
+                stmnt.setString(1, collectionId);
+                logger.debug("getMaster.execute");
+                result = stmnt.executeQuery();
+                if (!result.next()) {
+                    logger.debug("No such collection: " + collectionId);
+                    return false;
+                }
+                logger.debug("getMasterArtifact result.getString " + result.getString(1));
+                uuid.add(result.getString(1));
+                reset();
+                return true;
+            }
+        };
+        return exec.runRead() ? uuid.get(0) : null;
+    }
+
     public boolean deleteCollection(final String collectionId) {
         if (!isValidIdentifier(collectionId)) {
             logger.debug("Invalid collection id: '" + collectionId + "'");
--- a/artifact-database/src/main/resources/sql/org-h2-driver.properties	Mon Oct 17 15:42:43 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties	Wed Oct 19 15:55:33 2011 +0000
@@ -137,6 +137,11 @@
 
 collection.creation.time=SELECT creation from collections WHERE id = ?
 
+collections.artifacts.oldest=SELECT a.gid, ci.artifact_id  \
+    FROM artifacts AS a, collection_items AS ci, collections AS c \
+    WHERE ci.collection_id = c.id AND c.gid = ?::uuid AND ci.artifact_id = a.id \
+    ORDER BY ci.creation
+
 collections.select.user= \
     SELECT c.gid, c.name, c.creation, u.gid, c.ttl FROM \
     collections c LEFT OUTER JOIN users u ON c.owner_id = u.id \
--- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Mon Oct 17 15:42:43 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Wed Oct 19 15:55:33 2011 +0000
@@ -128,6 +128,11 @@
 
 collection.creation.time=SELECT creation from collections WHERE id = ?
 
+collections.artifacts.oldest=SELECT a.gid, ci.artifact_id  \
+    FROM artifacts AS a, collection_items AS ci, collections AS c \
+    WHERE ci.collection_id = c.id AND c.gid = ?::uuid AND ci.artifact_id = a.id \
+    ORDER BY ci.creation
+
 collections.select.user= \
     SELECT c.gid, c.name, c.creation, u.gid, c.ttl FROM \
     collections c LEFT OUTER JOIN users u ON c.owner_id = u.id \
--- a/artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java	Mon Oct 17 15:42:43 2011 +0000
+++ b/artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java	Wed Oct 19 15:55:33 2011 +0000
@@ -223,6 +223,9 @@
 
     // Collection API
 
+    Document getCollectionsMasterArtifact(String collectionId, CallMeta meta)
+        throws ArtifactDatabaseException;
+
     Document listCollections(String userId, CallMeta callMeta)
         throws ArtifactDatabaseException;
 

http://dive4elements.wald.intevation.org