changeset 148:101a52d3ad08

Added code to load all users from database. artifacts/trunk@1373 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 02 Mar 2011 18:18:16 +0000
parents 9a64e0c1c737
children c69abd07bdd0
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java
diffstat 2 files changed, 51 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Mar 02 18:08:37 2011 +0000
+++ b/ChangeLog	Wed Mar 02 18:18:16 2011 +0000
@@ -1,3 +1,8 @@
+2011-03-02	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  Added code to load all users from database.
+
 2011-03-02	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Wed Mar 02 18:08:37 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Wed Mar 02 18:18:16 2011 +0000
@@ -21,6 +21,8 @@
 import java.sql.SQLException;
 import java.sql.Types;
 
+import java.util.ArrayList;
+
 import javax.sql.DataSource;
 
 import org.apache.log4j.Logger;
@@ -106,7 +108,7 @@
     public static final String SQL_USERS_DELETE_COLLECTIONS =
         SQL.get("users.delete.collections");
 
-    public static final String SQL_USER_SELECT_ALL =
+    public static final String SQL_USERS_SELECT_ALL =
         SQL.get("users.select.all");
 
     public static final String SQL_USERS_COLLECTIONS =
@@ -1031,7 +1033,49 @@
     }
 
     public User [] getUsers() {
-        // TODO: implement me!
+        Connection        conn   = null;
+        ResultSet         result = null;
+        PreparedStatement stmnt  = null;
+
+        DataSource dataSource = DBConnection.getDataSource();
+        try {
+            conn   = dataSource.getConnection();
+            stmnt  = conn.prepareStatement(SQL_USERS_SELECT_ALL);
+            result = stmnt.executeQuery();
+
+            ArrayList<User> users = new ArrayList<User>();
+
+            while (result.next()) {
+                // omit id
+                String  identifier = result.getString(2);
+                String  name       = result.getString(3);
+                byte [] roleData   = result.getBytes(4);
+
+                Document role = XMLUtils.fromByteArray(roleData);
+                User     user = new DefaultUser(identifier, name, role);
+                users.add(user);
+            }
+
+            return users.toArray(new User[users.size()]);
+        }
+        catch (SQLException sqle) {
+            logger.error(sqle.getLocalizedMessage(), sqle);
+        }
+        finally {
+            if (result != null) {
+                try { result.close(); }
+                catch (SQLException sqle) {}
+            }
+            if (stmnt != null) {
+                try { stmnt.close(); }
+                catch (SQLException sqle) {}
+            }
+            if (conn != null) {
+                try { conn.close(); }
+                catch (SQLException sqle) {}
+            }
+        }
+
         return null;
     }
 }

http://dive4elements.wald.intevation.org