changeset 138:b90e831d3dfe

Call database to create a new user. artifacts/trunk@1363 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 02 Mar 2011 11:24:03 +0000
parents f92ad0e40df2
children 320a81983c8d
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java
diffstat 2 files changed, 78 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Mar 02 11:07:21 2011 +0000
+++ b/ChangeLog	Wed Mar 02 11:24:03 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:
+	  Call database to create a new user.
+
 2011-03-02  Ingo Weinzierl <ingo@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/UserResource.java:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Wed Mar 02 11:07:21 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Wed Mar 02 11:24:03 2011 +0000
@@ -13,6 +13,8 @@
 import de.intevation.artifacts.ArtifactSerializer;
 import de.intevation.artifacts.User;
 
+import de.intevation.artifacts.common.utils.XMLUtils;
+
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -793,7 +795,77 @@
         String   name, 
         Document role
     ) {
-        // TODO: implement me!
+        Connection        connection    = null;
+        PreparedStatement stmnt_next_id = null;
+        PreparedStatement stmnt_insert  = null;
+        ResultSet         res_id        = null;
+
+        String identifier = newIdentifier();
+
+        DataSource dataSource = DBConnection.getDataSource();
+        try {
+            connection = dataSource.getConnection();
+            try {
+                connection.setAutoCommit(false);
+
+                stmnt_next_id = connection.prepareStatement(SQL_USERS_NEXT_ID);
+                stmnt_insert  = connection.prepareStatement(SQL_USERS_INSERT);
+
+                res_id = stmnt_next_id.executeQuery();
+
+                if (!res_id.next()) {
+                    throw new RuntimeException("No id generated");
+                }
+
+                int id = res_id.getInt(1);
+
+                stmnt_insert.setInt(1, id);
+                stmnt_insert.setString(2, identifier);
+                stmnt_insert.setString(3, name);
+
+                byte [] roleData = role == null
+                    ? null
+                    : XMLUtils.toByteArray(role);
+
+                if (roleData == null) {
+                    stmnt_insert.setNull(4, Types.BIGINT);
+                }
+                else {
+                    stmnt_insert.setBytes(4, roleData);
+                }
+
+                stmnt_insert.execute();
+
+                connection.commit();
+
+                return new DefaultUser(identifier, name, role);
+            }
+            catch (SQLException sqle) {
+                connection.rollback();
+                throw sqle;
+            }
+        }
+        catch (SQLException sqle) {
+            logger.error(sqle.getLocalizedMessage(), sqle);
+        }
+        finally {
+            if (res_id != null) {
+                try { res_id.close(); }
+                catch (SQLException sqle) {}
+            }
+            if (stmnt_insert != null) {
+                try { stmnt_insert.close(); }
+                catch (SQLException sqle) {}
+            }
+            if (stmnt_next_id != null) {
+                try { stmnt_next_id.close(); }
+                catch (SQLException sqle) {}
+            }
+            if (connection != null) {
+                try { connection.close(); }
+                catch (SQLException sqle) {}
+            }
+        }
         return null;
     }
 

http://dive4elements.wald.intevation.org