1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.core;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.jcr.Item;
23 import javax.jcr.Node;
24 import javax.jcr.NodeIterator;
25 import javax.jcr.Session;
26
27 import org.exoplatform.services.cms.link.ItemLinkAware;
28 import org.exoplatform.services.jcr.core.ManageableRepository;
29 import org.exoplatform.services.jcr.ext.common.SessionProvider;
30 import org.exoplatform.services.log.ExoLogger;
31 import org.exoplatform.services.log.Log;
32 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
33
34
35
36
37
38
39
40
41 public class NodeLocation extends ItemLocation {
42
43
44 private static final Log LOG = ExoLogger.getLogger(NodeLocation.class.getName());
45
46 private List<NodeLocation> children_;
47
48 public List<NodeLocation> getChildren() {
49 return children_;
50 }
51
52 public void setChildren(List<NodeLocation> children) {
53 children_ = children;
54 }
55
56 public boolean hasChildren() {
57 return children_ != null && children_.size() > 0;
58 }
59
60
61
62 public NodeLocation() {
63 super();
64 }
65
66
67
68
69
70
71
72
73
74
75 public NodeLocation(final String repository, final String workspace, final String path, final String uuid,
76 final boolean isSystem) {
77 super(repository, workspace, path, uuid, isSystem);
78 }
79
80
81
82
83
84
85
86
87
88 public NodeLocation(final String repository, final String workspace, final String path, final String uuid ) {
89 super(repository, workspace, path, uuid, false);
90 }
91
92
93
94
95
96
97
98
99 public NodeLocation(final String repository, final String workspace, final String path) {
100 super(repository, workspace, path, null, false);
101 }
102
103
104
105
106
107 public NodeLocation(ItemLocation location) {
108 super(location);
109 }
110
111
112
113
114
115
116
117
118 public static final NodeLocation getNodeLocationByExpression(final String exp) {
119 String[] temp = split(exp, ":");
120 if (temp.length >= 3 && temp[2].indexOf("/") == 0) {
121 String repository = temp[0];
122 String workspace = temp[1];
123 String nodepath = exp.substring(repository.length() + workspace.length() + 2);
124 return new NodeLocation(repository, workspace, nodepath);
125 }
126 throw new IllegalArgumentException("Invalid expression: " + exp
127 + ". An valid expression has pattern repository:workspace:path");
128 }
129
130
131
132
133
134
135
136
137 public static final NodeLocation getNodeLocationByNode(final Node node) {
138 try {
139 ItemLocation itemLocation = ItemLocation.getItemLocationByItem(node);
140 return new NodeLocation(itemLocation);
141 } catch (Exception e) {
142 return null;
143 }
144 }
145
146
147
148
149
150
151
152
153 public static final Node getNodeByLocation(final NodeLocation nodeLocation) {
154 try {
155 Item item = ItemLocation.getItemByLocation(nodeLocation);
156 return (Node)item;
157 } catch (Exception e) {
158 return null;
159 }
160 }
161
162
163
164
165
166
167 @SuppressWarnings("unchecked")
168 public static final List getNodeListByLocationList(final List locationList) {
169 List ret = new ArrayList();
170 try {
171 ManageableRepository repository = WCMCoreUtils.getRepository();
172 SessionProvider systemSessionProvider = WCMCoreUtils.getSystemSessionProvider();
173 SessionProvider sessionProvider = WCMCoreUtils.getUserSessionProvider();
174 String systemWorkspace = repository.getConfiguration().getSystemWorkspaceName();
175 Session session = null;
176 Node node;
177 for (Object obj : locationList)
178 if (obj instanceof NodeLocation) {
179 node = null;
180 try {
181 NodeLocation location = (NodeLocation)obj;
182 session = (systemWorkspace.equals(location.getWorkspace()) || location.isSystemSession)?
183 systemSessionProvider.getSession(location.getWorkspace(), repository) :
184 sessionProvider.getSession(location.getWorkspace(), repository);
185
186 node = location.getUUID() != null ? session.getNodeByUUID(location.getUUID()) :
187 (Node)session.getItem(location.getPath());
188 ret.add(node);
189 } catch (Exception e) {
190 if (LOG.isDebugEnabled()) {
191 LOG.debug(e.getMessage(), e);
192 }
193 }
194 } else {
195 ret.add(obj);
196 }
197 } catch (Exception e) {
198 return ret;
199 }
200 return ret;
201 }
202
203
204
205
206
207
208 @SuppressWarnings("unchecked")
209 public static final List getLocationsByNodeList(final List nodeList) {
210 List ret = new ArrayList();
211 for(Object obj : nodeList)
212 if (obj instanceof ItemLinkAware) {
213 ret.add(obj);
214 } else if (obj instanceof Node) {
215 NodeLocation location = getNodeLocationByNode((Node)obj);
216 if (location != null)
217 ret.add(location);
218 } else {
219 ret.add(obj);
220 }
221 return ret;
222 }
223
224
225
226
227
228
229 public static final List<NodeLocation> getLocationsByIterator(final NodeIterator nodeIterator) {
230 List<NodeLocation> ret = new ArrayList<NodeLocation>();
231 while (nodeIterator.hasNext()) {
232 NodeLocation location = getNodeLocationByNode(nodeIterator.nextNode());
233 if (location != null)
234 ret.add(location);
235 }
236 return ret;
237 }
238
239
240
241
242
243
244
245
246 public static final Node getNodeByExpression(final String expression) {
247 return getNodeByLocation(getNodeLocationByExpression(expression));
248 }
249
250
251
252
253
254
255
256 public static final String getExpressionByNode(final Node node) {
257 NodeLocation location = NodeLocation.getNodeLocationByNode(node);
258 return mergeString(location.getRepository(), location.getWorkspace(), location.getPath());
259 }
260
261
262
263
264
265
266
267 public static final String getExpressionByNodeLocation(final NodeLocation location) {
268 return mergeString(location.getRepository(), location.getWorkspace(), location.getPath());
269 }
270
271
272
273
274 public String toString() {
275 return mergeString(repository, workspace, path);
276 }
277
278
279
280
281
282
283
284
285
286 private static String mergeString(String repository, String workspace, String path) {
287 StringBuffer buffer = new StringBuffer();
288 buffer.append(repository)
289 .append(":")
290 .append(workspace)
291 .append(":")
292 .append(path);
293 return buffer.toString();
294 }
295
296 public boolean equals(Object obj) {
297 if (obj == null || !(obj instanceof NodeLocation)) return false;
298 NodeLocation location2 = (NodeLocation)obj;
299 return equalsString(this.repository, location2.getRepository()) &&
300 equalsString(this.getWorkspace(), location2.getWorkspace()) &&
301 (equalsString(this.getPath(), location2.getPath()) ||
302 equalsString(this.getUUID(), location2.getUUID()));
303 }
304
305 public boolean equalsString(String st1, String st2) {
306 if (st1 == null && st2 == null) return true;
307 if (st1 == null || st2 == null) return false;
308 return st1.equals(st2);
309 }
310
311 public int hashCode() {
312 return (repository == null ? 0 : repository.hashCode()) +
313 (workspace == null ? 0 : workspace.hashCode()) +
314 (uuid == null ? 0 : uuid.hashCode()) +
315 (path == null ? 0 : path.hashCode());
316 }
317
318 private static final String[] split(String s, String ch) {
319 int maxLength = 3;
320 String[] ss = new String[maxLength];
321 int prev = 0;
322 int i=0;
323 while(true) {
324 int next = s.indexOf(ch, prev);
325 if (next == -1 || i == maxLength - 1) {
326 ss[i] = s.substring(prev);
327 break;
328 }
329 ss[i++] = s.substring(prev, next);
330 prev = next+1;
331 }
332
333 return ss;
334 }
335 }