changeset 415:d4a846ee35b8

Implement ArtifactDatabase findUser method artifacts/trunk@5262 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 24 Aug 2012 14:08:31 +0000
parents c22f3a7640d7
children 733e76413bf8
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java
diffstat 3 files changed, 66 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Aug 24 14:06:06 2012 +0000
+++ b/ChangeLog	Fri Aug 24 14:08:31 2012 +0000
@@ -1,3 +1,10 @@
+2012-08-24	Björn Ricks	<bjoern.ricks@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java,
+	  artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java:
+	  Implement findUser method. The findUser method in ArtifactDatabase
+	  converts a User object into its XML representation.
+
 2012-08-24	Björn Ricks	<bjoern.ricks@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java,
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Fri Aug 24 14:06:06 2012 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Fri Aug 24 14:08:31 2012 +0000
@@ -197,10 +197,14 @@
     public static final String XPATH_USERROLE =
         "/art:action/art:user/art:role";
 
-    /** XPath to figure out the role of a new user.*/
+    /** XPath to figure out the account of a new user.*/
     public static final String XPATH_USERACCOUNT =
         "/art:action/art:user/art:account/@name";
 
+    /** XPath to figure out the account of when searching for a user .*/
+    public static final String XPATH_USERACCOUNT_FIND =
+        "/art:action/art:account/@name";
+
     /** Error message if a specified user does not exist.*/
     public static final String NO_SUCH_USER =
         "No such user";
@@ -1235,6 +1239,56 @@
         return result;
     }
 
+    /** Search for a user. */
+    public Document findUser(Document data, CallMeta callMeta)
+        throws ArtifactDatabaseException
+    {
+        UserFactory factory = getUserFactory();
+
+        if (factory == null) {
+            throw new ArtifactDatabaseException(NO_SUCH_FACTORY);
+        }
+
+        String account = XMLUtils.xpathString(
+            data, XPATH_USERACCOUNT_FIND, ArtifactNamespaceContext.INSTANCE);
+
+        if (account == null || account.length() == 0) {
+            logger.warn("Can't find user without account!");
+            throw new ArtifactDatabaseException(NO_USERACCOUNT);
+        }
+
+        User user = backend.findUser(account, factory, context);
+
+        Document result = XMLUtils.newDocument();
+
+        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
+            result,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        Element ue = ec.create("user");
+
+        if (user != null) {
+            logger.debug(user + " user found in the backend.");
+
+            ec.addAttr(ue, "uuid", user.identifier(), true);
+            ec.addAttr(ue, "name", user.getName(), true);
+            Element ua = ec.create("account");
+            ec.addAttr(ua, "name", user.getAccount(), true);
+            ue.appendChild(ua);
+
+            Document role = user.getRole();
+
+            if (role != null) {
+                ue.appendChild(result.importNode(role.getFirstChild(), true));
+            }
+        }
+
+        result.appendChild(ue);
+
+        return result;
+    }
+
     public Document createUser(Document data, CallMeta callMeta)
         throws ArtifactDatabaseException
     {
@@ -1255,7 +1309,7 @@
         String account = XMLUtils.xpathString(
             data, XPATH_USERACCOUNT, ArtifactNamespaceContext.INSTANCE);
 
-        if (name == null || name.length() == 0) {
+        if (account == null || account.length() == 0) {
             logger.warn("User without account not accepted!");
             throw new ArtifactDatabaseException(NO_USERACCOUNT);
         }
--- a/artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java	Fri Aug 24 14:06:06 2012 +0000
+++ b/artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java	Fri Aug 24 14:08:31 2012 +0000
@@ -215,6 +215,9 @@
     Document listUsers(CallMeta callMeta)
         throws ArtifactDatabaseException;
 
+    Document findUser(Document data, CallMeta callMeta)
+        throws ArtifactDatabaseException;
+
     Document createUser(Document data, CallMeta callMeta)
         throws ArtifactDatabaseException;
 

http://dive4elements.wald.intevation.org