diff artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java @ 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 f4b14240ffa3
children 410a1bfb9590
line wrap: on
line diff
--- 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);
         }

http://dive4elements.wald.intevation.org