changeset 550:68f01f10624e

Introduced helper for finding the user name for a given artifact
author gernotbelger
date Fri, 09 Feb 2018 13:26:19 +0100
parents e781b847fdca
children 5849f59e7065 61255b610929
files artifact-database/src/main/java/org/dive4elements/artifactdatabase/ArtifactDatabaseImpl.java artifact-database/src/main/java/org/dive4elements/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/org/dive4elements/artifacts/ArtifactDatabase.java
diffstat 5 files changed, 61 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/artifact-database/src/main/java/org/dive4elements/artifactdatabase/ArtifactDatabaseImpl.java	Mon Feb 22 17:25:21 2016 +0100
+++ b/artifact-database/src/main/java/org/dive4elements/artifactdatabase/ArtifactDatabaseImpl.java	Fri Feb 09 13:26:19 2018 +0100
@@ -1979,5 +1979,10 @@
             }
         });
     }
+    
+    @Override
+    public String findArtifactUser(final String artifactIdentifier) {
+        return backend.findUserName(artifactIdentifier);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifact-database/src/main/java/org/dive4elements/artifactdatabase/Backend.java	Mon Feb 22 17:25:21 2016 +0100
+++ b/artifact-database/src/main/java/org/dive4elements/artifactdatabase/Backend.java	Fri Feb 09 13:26:19 2018 +0100
@@ -133,6 +133,7 @@
     public String SQL_COLLECTIONS_TOUCH_BY_ID;
     public String SQL_COLLECTION_ITEMS_LIST_GID;
     public String SQL_ALL_ARTIFACTS;
+    public String SQL_FIND_USER_BY_ARTIFACT;
 
     /** The singleton.*/
     protected static Backend instance;
@@ -361,6 +362,7 @@
         SQL_COLLECTIONS_TOUCH_BY_ID = sql.get("collections.touch.by.id");
         SQL_COLLECTION_ITEMS_LIST_GID = sql.get("collection.items.list.gid");
         SQL_ALL_ARTIFACTS = sql.get("all.artifacts");
+        SQL_FIND_USER_BY_ARTIFACT = sql.get("find.user.by.artifact");        
     }
 
     public void addListener(BackendListener listener) {
@@ -1030,6 +1032,43 @@
 
         return exec.runRead() ? user[0] : null;
     }
+    
+    /** Find the owner of a given artifact */
+    public String findUserName(final String artifactGid) {
+
+        final String[] returnValue = new String[1];
+
+        final SQLExecutor.Instance exec = this.sqlExecutor.new Instance() {
+
+            @Override
+            public boolean doIt() throws SQLException {
+
+                prepareStatement(Backend.this.SQL_FIND_USER_BY_ARTIFACT);
+                this.stmnt.setString(1, artifactGid);
+
+                this.result = this.stmnt.executeQuery();
+
+                // final HashMap<String, LazyBackendUser> users = new HashMap<String, LazyBackendUser>();
+
+                while (this.result.next()) {
+                    // final String userIdentifier = this.result.getString(1);
+                    final String userName = this.result.getString(2);
+
+                    // We only need the name at the moment, else we could do this: User user = new LazyBackendUser(
+                    // userIdentifier, userFactory, Backend.this, context);
+                    returnValue[0] = userName;
+                    return true;
+                }
+
+                return true;
+            }
+        };
+
+        if (exec.runRead())
+            return returnValue[0];
+
+        return null;
+    }    
 
     public User [] getUsers(
         final UserFactory factory,
--- a/artifact-database/src/main/resources/sql/org-h2-driver.properties	Mon Feb 22 17:25:21 2016 +0100
+++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties	Fri Feb 09 13:26:19 2018 +0100
@@ -196,3 +196,10 @@
                 INNER JOIN collection_items ci ON c.id = ci.collection_id \
                 INNER JOIN artifacts a         ON a.id = ci.artifact_id \
         ORDER BY u_gid, c_gid
+
+find.user.by.artifact = \
+	SELECT users.id, users.name FROM users, collections, collection_items, artifacts \
+	WHERE users.id = collections.owner_id AND \
+		  collections.id = collection_items.collection_id AND \
+		  collection_items.artifact_id = artifacts.id AND \
+		  artifacts.gid = ?::uuid
--- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Mon Feb 22 17:25:21 2016 +0100
+++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Fri Feb 09 13:26:19 2018 +0100
@@ -185,3 +185,10 @@
                 INNER JOIN collection_items ci ON c.id = ci.collection_id \
                 INNER JOIN artifacts a         ON a.id = ci.artifact_id \
         ORDER BY u_gid, c_gid
+
+find.user.by.artifact = \
+	SELECT users.id, users.name FROM users, collections, collection_items, artifacts \
+	WHERE users.id = collections.owner_id AND \
+		  collections.id = collection_items.collection_id AND \
+		  collection_items.artifact_id = artifacts.id AND \
+		  artifacts.gid = ?::uuid
--- a/artifacts/src/main/java/org/dive4elements/artifacts/ArtifactDatabase.java	Mon Feb 22 17:25:21 2016 +0100
+++ b/artifacts/src/main/java/org/dive4elements/artifacts/ArtifactDatabase.java	Fri Feb 09 13:26:19 2018 +0100
@@ -290,9 +290,11 @@
             String   artifactId,
             Date     artifactCreated,
             Artifact artifact);
-    };
+    }
 
     public void loadAllArtifacts(ArtifactLoadedCallback callback)
         throws ArtifactDatabaseException;
+
+    String findArtifactUser(String artifactIdentifier);
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org