]> git.argeo.org Git - lgpl/argeo-commons.git/blob - node/NodeUtils.java
Prepare next development cycle
[lgpl/argeo-commons.git] / node / NodeUtils.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.node;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
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;
34
35 /** Utilities related to Argeo model in JCR */
36 public class NodeUtils {
37 /**
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.
41 */
42 public static Repository getRepositoryByAlias(
43 RepositoryFactory repositoryFactory, String alias) {
44 try {
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 "
51 + alias, e);
52 }
53 }
54
55 /**
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.
59 */
60 public static Repository getRepositoryByUri(
61 RepositoryFactory repositoryFactory, String uri) {
62 return getRepositoryByUri(repositoryFactory, uri, null);
63 }
64
65 /**
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.
69 */
70 public static Repository getRepositoryByUri(
71 RepositoryFactory repositoryFactory, String uri, String alias) {
72 try {
73 Map<String, String> parameters = new HashMap<String, String>();
74 parameters.put(NodeConstants.JCR_REPOSITORY_URI, uri);
75 if (alias != null)
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 "
81 + uri, e);
82 }
83 }
84
85 private NodeUtils() {
86 }
87
88 /**
89 * Returns the home node of the user or null if none was found.
90 *
91 * @param session
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.
95 * @param username
96 * the username of the user
97 */
98 public static Node getUserHome(Session session, String username) {
99 try {
100 // String homePath = UserJcrUtils.getUserHomePath(username);
101 // return session.itemExists(homePath) ? session.getNode(homePath)
102 // : null;
103 // kept for example of QOM queries
104 QueryObjectModelFactory qomf = session.getWorkspace()
105 .getQueryManager().getQOMFactory();
106 Selector userHomeSel = qomf.selector(ArgeoTypes.ARGEO_USER_HOME,
107 "userHome");
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);
118 }
119 }
120
121 /**
122 * Queries one single node.
123 *
124 * @return one single node or null if none was found
125 * @throws ArgeoJcrException
126 * if more than one node was found
127 */
128 private static Node querySingleNode(Query query) {
129 NodeIterator nodeIterator;
130 try {
131 QueryResult queryResult = query.execute();
132 nodeIterator = queryResult.getNodes();
133 } catch (RepositoryException e) {
134 throw new RuntimeException("Cannot execute query " + query, e);
135 }
136 Node node;
137 if (nodeIterator.hasNext())
138 node = nodeIterator.nextNode();
139 else
140 return null;
141
142 if (nodeIterator.hasNext())
143 throw new RuntimeException("Query returned more than one node.");
144 return node;
145 }
146
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);
151 }
152
153 public static Node getUserProfile(Session session, String username) {
154 try {
155 QueryObjectModelFactory qomf = session.getWorkspace()
156 .getQueryManager().getQOMFactory();
157 Selector userHomeSel = qomf.selector(ArgeoTypes.ARGEO_USER_PROFILE,
158 "userProfile");
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);
170 }
171 }
172
173 }