diff artifact-database/src/main/java/org/dive4elements/artifactdatabase/Backend.java @ 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 3b1e48d22ce0
children 584591f8203c
line wrap: on
line diff
--- 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,

http://dive4elements.wald.intevation.org