changeset 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
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java
diffstat 3 files changed, 90 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Mar 02 17:47:54 2011 +0000
+++ b/ChangeLog	Wed Mar 02 18:08:37 2011 +0000
@@ -1,3 +1,11 @@
+2011-03-02	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  Added code to load user from database.
+
+	* artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java:
+	  Added method to reconstruct XML documents from byte arrays and input streams
+
 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 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;
     }
 
--- a/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java	Wed Mar 02 17:47:54 2011 +0000
+++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java	Wed Mar 02 18:08:37 2011 +0000
@@ -8,9 +8,13 @@
 
 package de.intevation.artifacts.common.utils;
 
+import java.io.ByteArrayInputStream;
+import java.io.FileInputStream;
+import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 
 import javax.xml.namespace.NamespaceContext;
@@ -169,11 +173,29 @@
      * during loading.
      */
     public static final Document parseDocument(File file) {
+        InputStream inputStream = null;
+        try {
+            inputStream = new BufferedInputStream(new FileInputStream(file));
+            return parseDocument(inputStream);
+        }
+        catch (IOException ioe) {
+            logger.error(ioe.getLocalizedMessage(), ioe);
+        }
+        finally {
+            if (inputStream != null) {
+                try { inputStream.close(); }
+                catch (IOException ioe) {}
+            }
+        }
+        return null;
+    }
+
+    public static final Document parseDocument(InputStream inputStream) {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         factory.setNamespaceAware(true);
 
         try {
-            return factory.newDocumentBuilder().parse(file);
+            return factory.newDocumentBuilder().parse(inputStream);
         }
         catch (ParserConfigurationException pce) {
             logger.error(pce.getLocalizedMessage(), pce);
@@ -324,5 +346,12 @@
             ? baos.toByteArray()
             : null;
     }
+
+    public static Document fromByteArray(byte [] data) {
+        if (data == null) {
+            return null;
+        }
+        return parseDocument(new ByteArrayInputStream(data));
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org