2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.jcr
;
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
;
28 import org
.argeo
.ArgeoException
;
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";
36 * Returns the home node of the user or null if none was found.
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.
43 * the username of the user
45 public static Node
getUserHome(Session session
, String username
) {
47 // String homePath = UserJcrUtils.getUserHomePath(username);
48 // return session.itemExists(homePath) ? session.getNode(homePath)
50 // kept for example of QOM queries
51 QueryObjectModelFactory qomf
= session
.getWorkspace()
52 .getQueryManager().getQOMFactory();
53 Selector userHomeSel
= qomf
.selector(ArgeoTypes
.ARGEO_USER_HOME
,
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
);
68 public static Node
getUserProfile(Session session
, String username
) {
70 QueryObjectModelFactory qomf
= session
.getWorkspace()
71 .getQueryManager().getQOMFactory();
72 Selector userHomeSel
= qomf
.selector(ArgeoTypes
.ARGEO_USER_PROFILE
,
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
);
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
);
94 private UserJcrUtils() {