changeset 149:c69abd07bdd0

Implemented the createUser() method in the artifact database. artifacts/trunk@1374 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 03 Mar 2011 06:53:51 +0000
parents 101a52d3ad08
children 78a27a6b3f1f
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java
diffstat 2 files changed, 82 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Mar 02 18:18:16 2011 +0000
+++ b/ChangeLog	Thu Mar 03 06:53:51 2011 +0000
@@ -1,3 +1,10 @@
+2011-03-03  Ingo Weinzierl <ingo@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java:
+	  Implemented the createUser() interface method. Calling this method will
+	  trigger the backend to create a new user described in the document given
+	  in the POST request.
+
 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/ArtifactDatabaseImpl.java	Wed Mar 02 18:18:16 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Thu Mar 03 06:53:51 2011 +0000
@@ -20,6 +20,7 @@
 import de.intevation.artifacts.CallMeta;
 import de.intevation.artifacts.Service;
 import de.intevation.artifacts.ServiceFactory;
+import de.intevation.artifacts.User;
 import de.intevation.artifacts.UserFactory;
 
 import java.io.IOException;
@@ -158,6 +159,28 @@
     public static final String INVALID_ARTIFACT =
         "Invalid artifact";
 
+
+    // User constants
+
+    /**
+     * Error message issued if the creation of a user failed.
+     */
+    public static final String USER_CREATION_FAILED =
+        "Creation of user failed.";
+
+    /** XPath to figure out the name of a new user.*/
+    public static final String XPATH_USERNAME =
+        "/art:action/art:user/@name";
+
+    /** XPath to figure out the role of a new user.*/
+    public static final String XPATH_USERROLE =
+        "/art:action/art:user/art:role";
+
+    /** Error message if no username is given for user creation.*/
+    public static final String NO_USERNAME =
+        "Invalid username";
+
+
     /**
      * Inner class that implements the call context handed
      * to the methods calls describe(), feed(), etc. of the artifact.
@@ -567,6 +590,10 @@
         return (ArtifactFactory)name2factory.get(factoryName);
     }
 
+    public UserFactory getUserFactory() {
+        return userFactory;
+    }
+
     public Document createArtifactWithFactory(
         String   factoryName,
         CallMeta callMeta,
@@ -914,8 +941,54 @@
     }
 
     public Document createUser(Document data, CallMeta callMeta)
-        throws ArtifactDatabaseException {
-        throw new ArtifactDatabaseException("Not implemented, yet!");
+        throws ArtifactDatabaseException
+    {
+        UserFactory factory = getUserFactory();
+
+        if (factory == null) {
+            throw new ArtifactDatabaseException(NO_SUCH_FACTORY);
+        }
+
+        String name = XMLUtils.xpathString(
+            data, XPATH_USERNAME, ArtifactNamespaceContext.INSTANCE);
+
+        if (name == null || name.length() == 0) {
+            logger.warn("User without username not accepted!");
+            throw new ArtifactDatabaseException(NO_USERNAME);
+        }
+
+        // TODO Extract the role is contained as node in the xml document.
+        Document role = XMLUtils.newDocument();
+
+        User newUser = null;
+
+        try {
+            newUser = factory.createUser(name, role, context);
+        }
+        catch (Exception e) {
+            logger.error(e.getMessage(), e);
+            throw new ArtifactDatabaseException(USER_CREATION_FAILED);
+        }
+
+        Document result = XMLUtils.newDocument();
+
+        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
+            result,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        Element root = ec.create("result");
+
+        if (newUser != null) {
+            root.setTextContent("SUCCESS");
+        }
+        else {
+            root.setTextContent("FAILURE");
+        }
+
+        result.appendChild(root);
+
+        return result;
     }
 
     public Document deleteUser(String userId, CallMeta callMeta)

http://dive4elements.wald.intevation.org