]> git.argeo.org Git - lgpl/argeo-commons.git/blob - UserJcrUtils.java
b7b138eadfb5dfd9e43a7d917c31263a55383fa1
[lgpl/argeo-commons.git] / UserJcrUtils.java
1 package org.argeo.jcr;
2
3 import javax.jcr.Node;
4 import javax.jcr.RepositoryException;
5 import javax.jcr.Session;
6 import javax.jcr.query.Query;
7 import javax.jcr.query.qom.Constraint;
8 import javax.jcr.query.qom.DynamicOperand;
9 import javax.jcr.query.qom.QueryObjectModelFactory;
10 import javax.jcr.query.qom.Selector;
11 import javax.jcr.query.qom.StaticOperand;
12
13 import org.argeo.ArgeoException;
14
15 /** Utilities related to the user home and properties based on Argeo JCR model. */
16 public class UserJcrUtils {
17 /** The home base path. Not yet configurable */
18 public final static String DEFAULT_HOME_BASE_PATH = "/home";
19
20 /**
21 * Returns the home node of the user or null if none was found.
22 *
23 * @param session
24 * the session to use in order to perform the search, this can be
25 * a session with a different user ID than the one searched,
26 * typically when a system or admin session is used.
27 * @param username
28 * the username of the user
29 */
30 public static Node getUserHome(Session session, String username) {
31 try {
32 // String homePath = UserJcrUtils.getUserHomePath(username);
33 // return session.itemExists(homePath) ? session.getNode(homePath)
34 // : null;
35 // kept for example of QOM queries
36 QueryObjectModelFactory qomf = session.getWorkspace()
37 .getQueryManager().getQOMFactory();
38 Selector userHomeSel = qomf.selector(ArgeoTypes.ARGEO_USER_HOME,
39 "userHome");
40 DynamicOperand userIdDop = qomf.propertyValue(
41 userHomeSel.getSelectorName(), ArgeoNames.ARGEO_USER_ID);
42 StaticOperand userIdSop = qomf.literal(session.getValueFactory()
43 .createValue(username));
44 Constraint constraint = qomf.comparison(userIdDop,
45 QueryObjectModelFactory.JCR_OPERATOR_EQUAL_TO, userIdSop);
46 Query query = qomf.createQuery(userHomeSel, constraint, null, null);
47 return JcrUtils.querySingleNode(query);
48 } catch (RepositoryException e) {
49 throw new ArgeoException("Cannot find home for user " + username, e);
50 }
51 }
52
53 public static Node getUserProfile(Session session, String username) {
54 try {
55 QueryObjectModelFactory qomf = session.getWorkspace()
56 .getQueryManager().getQOMFactory();
57 Selector userHomeSel = qomf.selector(ArgeoTypes.ARGEO_USER_PROFILE,
58 "userProfile");
59 DynamicOperand userIdDop = qomf.propertyValue(
60 userHomeSel.getSelectorName(), ArgeoNames.ARGEO_USER_ID);
61 StaticOperand userIdSop = qomf.literal(session.getValueFactory()
62 .createValue(username));
63 Constraint constraint = qomf.comparison(userIdDop,
64 QueryObjectModelFactory.JCR_OPERATOR_EQUAL_TO, userIdSop);
65 Query query = qomf.createQuery(userHomeSel, constraint, null, null);
66 return JcrUtils.querySingleNode(query);
67 } catch (RepositoryException e) {
68 throw new ArgeoException(
69 "Cannot find profile for user " + username, e);
70 }
71 }
72
73 /** Returns the home node of the session user or null if none was found. */
74 public static Node getUserHome(Session session) {
75 String userID = session.getUserID();
76 return getUserHome(session, userID);
77 }
78
79 private UserJcrUtils() {
80 }
81 }