]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.node.api/src/org/argeo/node/NodeUtils.java
Minimal documentation in Markdown format.
[lgpl/argeo-commons.git] / org.argeo.node.api / src / org / argeo / 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#CN} in order to simplify it and
40 * protect against future API changes.
41 */
42 public static Repository getRepositoryByAlias(RepositoryFactory repositoryFactory, String alias) {
43 try {
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,
49 e);
50 }
51 }
52
53 /**
54 * Wraps the call to the repository factory based on parameter
55 * {@link NodeConstants#LABELED_URI} in order to simplify it and
56 * protect against future API changes.
57 */
58 public static Repository getRepositoryByUri(RepositoryFactory repositoryFactory, String uri) {
59 return getRepositoryByUri(repositoryFactory, uri, null);
60 }
61
62 /**
63 * Wraps the call to the repository factory based on parameter
64 * {@link NodeConstants#LABELED_URI} in order to simplify it and
65 * protect against future API changes.
66 */
67 public static Repository getRepositoryByUri(RepositoryFactory repositoryFactory, String uri, String alias) {
68 try {
69 Map<String, String> parameters = new HashMap<String, String>();
70 parameters.put(NodeConstants.LABELED_URI, uri);
71 if (alias != null)
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);
76 }
77 }
78
79 private NodeUtils() {
80 }
81
82 /**
83 * Returns the home node of the user or null if none was found.
84 *
85 * @param session
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.
89 * @param username
90 * the username of the user
91 */
92 public static Node getUserHome(Session session, String username) {
93 try {
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);
103 }
104 }
105
106 /**
107 * Returns the home node of the user or null if none was found.
108 *
109 * @param session
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.
113 * @param cn
114 * the name of the group
115 */
116 public static Node getGroupHome(Session session, String cn) {
117 try {
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);
127 }
128 }
129
130 /**
131 * Queries one single node.
132 *
133 * @return one single node or null if none was found
134 * @throws ArgeoJcrException
135 * if more than one node was found
136 */
137 private static Node querySingleNode(Query query) {
138 NodeIterator nodeIterator;
139 try {
140 QueryResult queryResult = query.execute();
141 nodeIterator = queryResult.getNodes();
142 } catch (RepositoryException e) {
143 throw new RuntimeException("Cannot execute query " + query, e);
144 }
145 Node node;
146 if (nodeIterator.hasNext())
147 node = nodeIterator.nextNode();
148 else
149 return null;
150
151 if (nodeIterator.hasNext())
152 throw new RuntimeException("Query returned more than one node.");
153 return node;
154 }
155
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);
160 }
161
162 public static String getDataPath(String cn, Node node) throws RepositoryException {
163 assert node != null;
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();
167 }
168 }