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(
43 RepositoryFactory repositoryFactory
, String alias
) {
45 Map
<String
, String
> parameters
= new HashMap
<String
, String
>();
46 parameters
.put(NodeConstants
.JCR_REPOSITORY_ALIAS
, alias
);
47 return repositoryFactory
.getRepository(parameters
);
48 } catch (RepositoryException e
) {
49 throw new RuntimeException(
50 "Unexpected exception when trying to retrieve repository with alias "
56 * Wraps the call to the repository factory based on parameter
57 * {@link NodeConstants#JCR_REPOSITORY_URI} in order to simplify it and
58 * protect against future API changes.
60 public static Repository
getRepositoryByUri(
61 RepositoryFactory repositoryFactory
, String uri
) {
62 return getRepositoryByUri(repositoryFactory
, uri
, null);
66 * Wraps the call to the repository factory based on parameter
67 * {@link NodeConstants#JCR_REPOSITORY_URI} in order to simplify it and
68 * protect against future API changes.
70 public static Repository
getRepositoryByUri(
71 RepositoryFactory repositoryFactory
, String uri
, String alias
) {
73 Map
<String
, String
> parameters
= new HashMap
<String
, String
>();
74 parameters
.put(NodeConstants
.JCR_REPOSITORY_URI
, uri
);
76 parameters
.put(NodeConstants
.JCR_REPOSITORY_ALIAS
, alias
);
77 return repositoryFactory
.getRepository(parameters
);
78 } catch (RepositoryException e
) {
79 throw new RuntimeException(
80 "Unexpected exception when trying to retrieve repository with uri "
89 * Returns the home node of the user or null if none was found.
92 * the session to use in order to perform the search, this can be
93 * a session with a different user ID than the one searched,
94 * typically when a system or admin session is used.
96 * the username of the user
98 public static Node
getUserHome(Session session
, String username
) {
100 // String homePath = UserJcrUtils.getUserHomePath(username);
101 // return session.itemExists(homePath) ? session.getNode(homePath)
103 // kept for example of QOM queries
104 QueryObjectModelFactory qomf
= session
.getWorkspace()
105 .getQueryManager().getQOMFactory();
106 Selector userHomeSel
= qomf
.selector(ArgeoTypes
.ARGEO_USER_HOME
,
108 DynamicOperand userIdDop
= qomf
.propertyValue(
109 userHomeSel
.getSelectorName(), ArgeoNames
.ARGEO_USER_ID
);
110 StaticOperand userIdSop
= qomf
.literal(session
.getValueFactory()
111 .createValue(username
));
112 Constraint constraint
= qomf
.comparison(userIdDop
,
113 QueryObjectModelFactory
.JCR_OPERATOR_EQUAL_TO
, userIdSop
);
114 Query query
= qomf
.createQuery(userHomeSel
, constraint
, null, null);
115 return querySingleNode(query
);
116 } catch (RepositoryException e
) {
117 throw new RuntimeException("Cannot find home for user " + username
, e
);
122 * Queries one single node.
124 * @return one single node or null if none was found
125 * @throws ArgeoJcrException
126 * if more than one node was found
128 private static Node
querySingleNode(Query query
) {
129 NodeIterator nodeIterator
;
131 QueryResult queryResult
= query
.execute();
132 nodeIterator
= queryResult
.getNodes();
133 } catch (RepositoryException e
) {
134 throw new RuntimeException("Cannot execute query " + query
, e
);
137 if (nodeIterator
.hasNext())
138 node
= nodeIterator
.nextNode();
142 if (nodeIterator
.hasNext())
143 throw new RuntimeException("Query returned more than one node.");
147 /** Returns the home node of the session user or null if none was found. */
148 public static Node
getUserHome(Session session
) {
149 String userID
= session
.getUserID();
150 return getUserHome(session
, userID
);
153 public static Node
getUserProfile(Session session
, String username
) {
155 QueryObjectModelFactory qomf
= session
.getWorkspace()
156 .getQueryManager().getQOMFactory();
157 Selector userHomeSel
= qomf
.selector(ArgeoTypes
.ARGEO_USER_PROFILE
,
159 DynamicOperand userIdDop
= qomf
.propertyValue(
160 userHomeSel
.getSelectorName(), ArgeoNames
.ARGEO_USER_ID
);
161 StaticOperand userIdSop
= qomf
.literal(session
.getValueFactory()
162 .createValue(username
));
163 Constraint constraint
= qomf
.comparison(userIdDop
,
164 QueryObjectModelFactory
.JCR_OPERATOR_EQUAL_TO
, userIdSop
);
165 Query query
= qomf
.createQuery(userHomeSel
, constraint
, null, null);
166 return querySingleNode(query
);
167 } catch (RepositoryException e
) {
168 throw new RuntimeException(
169 "Cannot find profile for user " + username
, e
);