diff artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 147:9a64e0c1c737

Added code to load user from database. artifacts/trunk@1372 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 02 Mar 2011 18:08:37 +0000
parents 967dc552455d
children 101a52d3ad08
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Wed Mar 02 17:47:54 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Wed Mar 02 18:08:37 2011 +0000
@@ -895,12 +895,16 @@
 
     public void deleteUser(User user) {
 
-        Connection        conn  = null;
-        ResultSet         result      = null;
-        PreparedStatement stmnt       = null;
+        Connection        conn   = null;
+        ResultSet         result = null;
+        PreparedStatement stmnt  = null;
 
         String identifier = user.identifier();
 
+        if (!StringUtils.checkUUID(identifier)) {
+            return;
+        }
+
         DataSource dataSource = DBConnection.getDataSource();
         try {
             conn = dataSource.getConnection();
@@ -978,7 +982,51 @@
     }
 
     public User getUser(String identifier) {
-        // TODO: implement me!
+
+        Connection        conn   = null;
+        ResultSet         result = null;
+        PreparedStatement stmnt  = null;
+
+        if (!StringUtils.checkUUID(identifier)) {
+            logger.debug("Invalid UUID: '" + identifier + "'");
+            return null;
+        }
+
+        DataSource dataSource = DBConnection.getDataSource();
+        try {
+            conn = dataSource.getConnection();
+            stmnt = conn.prepareStatement(SQL_USERS_SELECT_ID_BY_GID);
+            stmnt.setString(1, identifier);
+            result = stmnt.executeQuery();
+            if (!result.next()) { // no such user
+                return null;
+            }
+            // omit id
+            String  name     = result.getString(2);
+            byte [] roleData = result.getBytes(3);
+
+            Document role = XMLUtils.fromByteArray(roleData);
+
+            return new DefaultUser(identifier, name, role);
+        }
+        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