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
.node
;
18 import java
.util
.HashMap
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.NodeIterator
;
23 import javax
.jcr
.Repository
;
24 import javax
.jcr
.RepositoryException
;
25 import javax
.jcr
.RepositoryFactory
;
26 import javax
.jcr
.Session
;
27 import javax
.jcr
.query
.Query
;
28 import javax
.jcr
.query
.QueryResult
;
29 import javax
.jcr
.query
.qom
.Constraint
;
30 import javax
.jcr
.query
.qom
.DynamicOperand
;
31 import javax
.jcr
.query
.qom
.QueryObjectModelFactory
;
32 import javax
.jcr
.query
.qom
.Selector
;
33 import javax
.jcr
.query
.qom
.StaticOperand
;
35 /** Utilities related to Argeo model in JCR */
36 public class NodeUtils
{
38 * Wraps the call to the repository factory based on parameter
39 * {@link NodeConstants#JCR_REPOSITORY_ALIAS} in order to simplify it and
40 * protect against future API changes.
42 public static Repository
getRepositoryByAlias(RepositoryFactory repositoryFactory
, String alias
) {
44 Map
<String
, String
> parameters
= new HashMap
<String
, String
>();
45 parameters
.put(NodeConstants
.CN
, alias
);
46 return repositoryFactory
.getRepository(parameters
);
47 } catch (RepositoryException e
) {
48 throw new RuntimeException("Unexpected exception when trying to retrieve repository with alias " + alias
,
54 * Wraps the call to the repository factory based on parameter
55 * {@link NodeConstants#JCR_REPOSITORY_URI} in order to simplify it and
56 * protect against future API changes.
58 public static Repository
getRepositoryByUri(RepositoryFactory repositoryFactory
, String uri
) {
59 return getRepositoryByUri(repositoryFactory
, uri
, null);
63 * Wraps the call to the repository factory based on parameter
64 * {@link NodeConstants#JCR_REPOSITORY_URI} in order to simplify it and
65 * protect against future API changes.
67 public static Repository
getRepositoryByUri(RepositoryFactory repositoryFactory
, String uri
, String alias
) {
69 Map
<String
, String
> parameters
= new HashMap
<String
, String
>();
70 parameters
.put(NodeConstants
.LABELED_URI
, uri
);
72 parameters
.put(NodeConstants
.CN
, alias
);
73 return repositoryFactory
.getRepository(parameters
);
74 } catch (RepositoryException e
) {
75 throw new RuntimeException("Unexpected exception when trying to retrieve repository with uri " + uri
, e
);
83 * Returns the home node of the user or null if none was found.
86 * the session to use in order to perform the search, this can be
87 * a session with a different user ID than the one searched,
88 * typically when a system or admin session is used.
90 * the username of the user
92 public static Node
getUserHome(Session session
, String username
) {
94 QueryObjectModelFactory qomf
= session
.getWorkspace().getQueryManager().getQOMFactory();
95 Selector sel
= qomf
.selector(NodeTypes
.NODE_USER_HOME
, "sel");
96 DynamicOperand dop
= qomf
.propertyValue(sel
.getSelectorName(), NodeNames
.LDAP_UID
);
97 StaticOperand sop
= qomf
.literal(session
.getValueFactory().createValue(username
));
98 Constraint constraint
= qomf
.comparison(dop
, QueryObjectModelFactory
.JCR_OPERATOR_EQUAL_TO
, sop
);
99 Query query
= qomf
.createQuery(sel
, constraint
, null, null);
100 return querySingleNode(query
);
101 } catch (RepositoryException e
) {
102 throw new RuntimeException("Cannot find home for user " + username
, e
);
107 * Returns the home node of the user or null if none was found.
110 * the session to use in order to perform the search, this can be
111 * a session with a different user ID than the one searched,
112 * typically when a system or admin session is used.
114 * the username of the user
116 public static Node
getGroupHome(Session session
, String cn
) {
118 QueryObjectModelFactory qomf
= session
.getWorkspace().getQueryManager().getQOMFactory();
119 Selector sel
= qomf
.selector(NodeTypes
.NODE_GROUP_HOME
, "sel");
120 DynamicOperand dop
= qomf
.propertyValue(sel
.getSelectorName(), NodeNames
.LDAP_CN
);
121 StaticOperand sop
= qomf
.literal(session
.getValueFactory().createValue(cn
));
122 Constraint constraint
= qomf
.comparison(dop
, QueryObjectModelFactory
.JCR_OPERATOR_EQUAL_TO
, sop
);
123 Query query
= qomf
.createQuery(sel
, constraint
, null, null);
124 return querySingleNode(query
);
125 } catch (RepositoryException e
) {
126 throw new RuntimeException("Cannot find home for user " + cn
, e
);
131 * Queries one single node.
133 * @return one single node or null if none was found
134 * @throws ArgeoJcrException
135 * if more than one node was found
137 private static Node
querySingleNode(Query query
) {
138 NodeIterator nodeIterator
;
140 QueryResult queryResult
= query
.execute();
141 nodeIterator
= queryResult
.getNodes();
142 } catch (RepositoryException e
) {
143 throw new RuntimeException("Cannot execute query " + query
, e
);
146 if (nodeIterator
.hasNext())
147 node
= nodeIterator
.nextNode();
151 if (nodeIterator
.hasNext())
152 throw new RuntimeException("Query returned more than one node.");
156 /** Returns the home node of the session user or null if none was found. */
157 public static Node
getUserHome(Session session
) {
158 String userID
= session
.getUserID();
159 return getUserHome(session
, userID
);
162 public static String
getDataPath(String cn
, Node node
) throws RepositoryException
{
164 StringBuilder buf
= new StringBuilder(NodeConstants
.PATH_DATA
);
165 return buf
.append('/').append(cn
).append('/').append(node
.getSession().getWorkspace().getName())
166 .append(node
.getPath()).toString();
169 // public static Node getUserProfile(Session session, String username) {
171 // QueryObjectModelFactory qomf = session.getWorkspace()
172 // .getQueryManager().getQOMFactory();
173 // Selector userHomeSel = qomf.selector(ArgeoTypes.ARGEO_USER_PROFILE,
175 // DynamicOperand userIdDop = qomf.propertyValue(
176 // userHomeSel.getSelectorName(), ArgeoNames.ARGEO_USER_ID);
177 // StaticOperand userIdSop = qomf.literal(session.getValueFactory()
178 // .createValue(username));
179 // Constraint constraint = qomf.comparison(userIdDop,
180 // QueryObjectModelFactory.JCR_OPERATOR_EQUAL_TO, userIdSop);
181 // Query query = qomf.createQuery(userHomeSel, constraint, null, null);
182 // return querySingleNode(query);
183 // } catch (RepositoryException e) {
184 // throw new RuntimeException(
185 // "Cannot find profile for user " + username, e);