# HG changeset patch # User Bjoern Ricks # Date 1345817311 0 # Node ID d4a846ee35b87890c4d6cbf16da79469cd67ae86 # Parent c22f3a7640d790c6645e8d9f742f4460106b4c8f Implement ArtifactDatabase findUser method artifacts/trunk@5262 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r c22f3a7640d7 -r d4a846ee35b8 ChangeLog --- 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 + + * 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 * artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java, diff -r c22f3a7640d7 -r d4a846ee35b8 artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.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); } diff -r c22f3a7640d7 -r d4a846ee35b8 artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java --- 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;