]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.server.jcr/newsrc/org/argeo/jcr/UserJcrUtils.java
1758802f85679c7334f1d60c24d7b68f82b06020
[lgpl/argeo-commons.git] / org.argeo.server.jcr / newsrc / org / argeo / jcr / UserJcrUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.jcr;
17
18 import javax.jcr.Node;
19 import javax.jcr.RepositoryException;
20 import javax.jcr.Session;
21 import javax.jcr.query.Query;
22 import javax.jcr.query.qom.Constraint;
23 import javax.jcr.query.qom.DynamicOperand;
24 import javax.jcr.query.qom.QueryObjectModelFactory;
25 import javax.jcr.query.qom.Selector;
26 import javax.jcr.query.qom.StaticOperand;
27
28 import org.argeo.ArgeoException;
29
30 /** Utilities related to the user home and properties based on Argeo JCR model. */
31 public class UserJcrUtils {
32 /** The home base path. Not yet configurable */
33 public final static String DEFAULT_HOME_BASE_PATH = "/home";
34
35 /**
36 * Returns the home node of the user or null if none was found.
37 *
38 * @param session
39 * the session to use in order to perform the search, this can be
40 * a session with a different user ID than the one searched,
41 * typically when a system or admin session is used.
42 * @param username
43 * the username of the user
44 */
45 public static Node getUserHome(Session session, String username) {
46 try {
47 // String homePath = UserJcrUtils.getUserHomePath(username);
48 // return session.itemExists(homePath) ? session.getNode(homePath)
49 // : null;
50 // kept for example of QOM queries
51 QueryObjectModelFactory qomf = session.getWorkspace()
52 .getQueryManager().getQOMFactory();
53 Selector userHomeSel = qomf.selector(ArgeoTypes.ARGEO_USER_HOME,
54 "userHome");
55 DynamicOperand userIdDop = qomf.propertyValue(
56 userHomeSel.getSelectorName(), ArgeoNames.ARGEO_USER_ID);
57 StaticOperand userIdSop = qomf.literal(session.getValueFactory()
58 .createValue(username));
59 Constraint constraint = qomf.comparison(userIdDop,
60 QueryObjectModelFactory.JCR_OPERATOR_EQUAL_TO, userIdSop);
61 Query query = qomf.createQuery(userHomeSel, constraint, null, null);
62 return JcrUtils.querySingleNode(query);
63 } catch (RepositoryException e) {
64 throw new ArgeoException("Cannot find home for user " + username, e);
65 }
66 }
67
68 public static Node getUserProfile(Session session, String username) {
69 try {
70 QueryObjectModelFactory qomf = session.getWorkspace()
71 .getQueryManager().getQOMFactory();
72 Selector userHomeSel = qomf.selector(ArgeoTypes.ARGEO_USER_PROFILE,
73 "userProfile");
74 DynamicOperand userIdDop = qomf.propertyValue(
75 userHomeSel.getSelectorName(), ArgeoNames.ARGEO_USER_ID);
76 StaticOperand userIdSop = qomf.literal(session.getValueFactory()
77 .createValue(username));
78 Constraint constraint = qomf.comparison(userIdDop,
79 QueryObjectModelFactory.JCR_OPERATOR_EQUAL_TO, userIdSop);
80 Query query = qomf.createQuery(userHomeSel, constraint, null, null);
81 return JcrUtils.querySingleNode(query);
82 } catch (RepositoryException e) {
83 throw new ArgeoException(
84 "Cannot find profile for user " + username, e);
85 }
86 }
87
88 /** Returns the home node of the session user or null if none was found. */
89 public static Node getUserHome(Session session) {
90 String userID = session.getUserID();
91 return getUserHome(session, userID);
92 }
93
94 private UserJcrUtils() {
95 }
96 }