Source code: com/obinary/cms/core/ContentHandler.java
1 /**
2 *
3 * Magnolia and its source-code is licensed under the LGPL.
4 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6 * you are required to provide proper attribution to obinary.
7 * If you reproduce or distribute the document without making any substantive modifications to its content,
8 * please use the following attribution line:
9 *
10 * Copyright 1993-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11 *
12 * */
13
14
15
16
17
18
19 package com.obinary.cms.core;
20
21
22 import com.obinary.cms.util.ChildrenCollector;
23 import com.obinary.cms.util.DateComparator;
24 import com.obinary.cms.util.SequenceComparator;
25 import com.obinary.cms.beans.ServerInfo;
26
27 import javax.jcr.Node;
28 import javax.jcr.ElementNotFoundException;
29 import javax.jcr.RepositoryException;
30 import java.util.Collection;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33
34 import jdsl.core.ref.ArraySequence;
35 import jdsl.core.algo.sorts.HeapSort;
36 import jdsl.core.api.ObjectIterator;
37
38
39 /**
40 * User: sameercharles
41 * Date: Jun 12, 2003
42 * Time: 08:01:17 AM
43 * @author Marcel Salathe
44 * @author Sameer Charles
45 * @version 1.0
46 */
47
48
49 public class ContentHandler implements Cloneable {
50
51 private static final String TEMPLATE = "template";
52 private static final String TITLE = "title";
53
54 public static final String SORT_BY_DATE = "date";
55 public static final String SORT_BY_NAME = "name";
56 public static final String SORT_BY_SEQUENCE = "sequence";
57
58 protected Node node;
59
60
61
62 /**
63 *
64 * package private constructor
65 *
66 */
67 ContentHandler() {
68 }
69
70
71
72 /**
73 * <p>bit by bit copy of the current object</p>
74 *
75 * @return Object cloned object
76 * @throws CloneNotSupportedException
77 */
78 public Object clone() throws CloneNotSupportedException {
79 return super.clone();
80 }
81
82
83
84 /**
85 *
86 * <p>get a handle representing path relative to the content repository</p>
87 *
88 * @throws javax.jcr.ElementNotFoundException
89 * @throws javax.jcr.RepositoryException
90 * @return String representing path (handle) of the content
91 */
92 public String getHandle() throws ElementNotFoundException, RepositoryException {
93 return this.node.getPath();
94 }
95
96
97
98 /**
99 *
100 * <p>get a handle representing path relative to the content repository with the default extension</p>
101 *
102 * @throws javax.jcr.ElementNotFoundException
103 * @throws javax.jcr.RepositoryException
104 * @return String representing path (handle) of the content
105 */
106 public String getHandleWithDefaultExtension() throws ElementNotFoundException, RepositoryException {
107 return (this.node.getPath()+"."+ServerInfo.getDefaultExtension());
108 }
109
110
111
112 /**
113 *
114 * <p>get parent content object</p>
115 *
116 * @throws javax.jcr.ElementNotFoundException
117 * @throws javax.jcr.RepositoryException
118 * @return Content representing parent node
119 */
120 public Content getParent() throws ElementNotFoundException, RepositoryException {
121 return (new Content(this.node.getParent()));
122 }
123
124
125 /**
126 * <p>gets a Collection containing all child nodes at the current level+1 level<br>
127 * By default, this collectiohn is sorted by the creation date of the nodes</p>
128 * @return Collection of content nodes
129 */
130 public Collection getChildren() {
131 //return this.getChildren(SORT_BY_DATE);
132 return this.getChildren(SORT_BY_SEQUENCE);
133 }
134
135
136 /**
137 * <p>gets a Collection containing all clild nodes at the current level+1 level</p>
138 * @param sortCriteria which can be either ContentHandler.SORT_BY_SEQUENCE (default), ContentHandler.SORT_BY_DATE or ContentHandler.SORT_BY_NAME
139 * @return Collection of content nodes
140 */
141 public Collection getChildren(String sortCriteria) {
142 Collection children = new ArrayList();
143 ChildrenCollector cc = new ChildrenCollector(children,true,false,1,ChildrenCollector.HIERARCHY_NODE);
144 try {
145 cc.visit(this.node);
146 } catch (RepositoryException re) {re.printStackTrace(); }
147 if (sortCriteria.equals("date")) {
148 children = sortByDate(children);
149 } else if (sortCriteria.equals("sequence")) {
150 children = sortBySequence(children);
151 }
152 //@todo .. needs to be fixed (implement better get children)
153 ArrayList list = ((ArrayList)children);
154 if (list.size() > 0)
155 if (((Content)list.get(0)).getTemplate().equals("CMSadmin"))
156 ((ArrayList)children).remove(0);
157 return children;
158 }
159
160
161
162 /**
163 * <p>get collection either sub pages or sub containers<br>
164 * use:<br>
165 * ChildrenCollector.SIMPLE_NODE to get sub containers<br>
166 * ChildrenCollector.HIERARCHY_NODE to get sub pages
167 * </p>
168 *
169 * @param collectionType , either hierarchy nodes (sub pages) or simple nodes (child containers)
170 * @return Collection of content nodes
171 */
172 public Collection getChildren(int collectionType) {
173 Collection children = new ArrayList();
174 ChildrenCollector cc = new ChildrenCollector(children,true,false,1,collectionType);
175 try {
176 cc.visit(this.node);
177 } catch (RepositoryException re) {re.printStackTrace(); }
178 //@todo .. needs to be fixed (implement better get children)
179 ArrayList list = ((ArrayList)children);
180 if (list.size() > 0)
181 if (((Content)list.get(0)).getTemplate().equals("CMSadmin"))
182 ((ArrayList)children).remove(0);
183 return children;
184 }
185
186
187
188 /**
189 *
190 * <p>get absolute parent object starting from the root node</p>
191 *
192 * @param digree level at which the requested node exist, relative to the ROOT node
193 * @throws javax.jcr.ElementNotFoundException
194 * @throws javax.jcr.RepositoryException
195 * @return Content representing parent node
196 */
197 public Content getAncestor(int digree) throws ElementNotFoundException, RepositoryException {
198 if (digree > this.getLevel())
199 throw new ElementNotFoundException();
200 return (new Content(this.node.getAncestor(digree)));
201 }
202
203
204
205 /**
206 *
207 * <p>Convenience method for taglib</p>
208 *
209 * @throws javax.jcr.ElementNotFoundException
210 * @throws javax.jcr.RepositoryException
211 * @return Content representing node on level 0
212 */
213 public Collection getAncestors() throws ElementNotFoundException, RepositoryException {
214 ArrayList allAncestors = new ArrayList();
215 Content currentNode = new Content(this.node);
216 int level = currentNode.getLevel();
217 while (level != 0) {
218 allAncestors.add(new Content(this.node.getAncestor(--level)));
219 }
220 return allAncestors;
221 }
222
223
224 /**
225 *
226 * <p>Convenience method for taglib</p>
227 *
228 * @throws javax.jcr.ElementNotFoundException
229 * @throws javax.jcr.RepositoryException
230 * @return Content representing node on level 0
231 */
232 public Content getAncestor0() throws ElementNotFoundException, RepositoryException {
233 return (new Content(this.node.getAncestor(0)));
234 }
235
236 /**
237 *
238 * <p>Convenience method for taglib</p>
239 *
240 * @throws javax.jcr.ElementNotFoundException
241 * @throws javax.jcr.RepositoryException
242 * @return Content representing node on level 1
243 */
244 public Content getAncestor1() throws ElementNotFoundException, RepositoryException {
245 return (new Content(this.node.getAncestor(1)));
246 }
247
248 /**
249 *
250 * <p>Convenience method for taglib</p>
251 *
252 * @throws javax.jcr.ElementNotFoundException
253 * @throws javax.jcr.RepositoryException
254 * @return Content representing node on level 2
255 */
256 public Content getAncestor2() throws ElementNotFoundException, RepositoryException {
257 return (new Content(this.node.getAncestor(2)));
258 }
259
260 /**
261 *
262 * <p>Convenience method for taglib</p>
263 *
264 * @throws javax.jcr.ElementNotFoundException
265 * @throws javax.jcr.RepositoryException
266 * @return Content representing node on level 3
267 */
268 public Content getAncestor3() throws ElementNotFoundException, RepositoryException {
269 return (new Content(this.node.getAncestor(3)));
270 }
271
272 /**
273 *
274 * <p>Convenience method for taglib</p>
275 *
276 * @throws javax.jcr.ElementNotFoundException
277 * @throws javax.jcr.RepositoryException
278 * @return Content representing node on level 4
279 */
280 public Content getAncestor4() throws ElementNotFoundException, RepositoryException {
281 return (new Content(this.node.getAncestor(4)));
282 }
283
284 /**
285 *
286 * <p>Convenience method for taglib</p>
287 *
288 * @throws javax.jcr.ElementNotFoundException
289 * @throws javax.jcr.RepositoryException
290 * @return Content representing node on level 5
291 */
292 public Content getAncestor5() throws ElementNotFoundException, RepositoryException {
293 return (new Content(this.node.getAncestor(5)));
294 }
295
296 /**
297 *
298 * <p>Convenience method for taglib</p>
299 *
300 * @throws javax.jcr.ElementNotFoundException
301 * @throws javax.jcr.RepositoryException
302 * @return Content representing node on level 6
303 */
304 public Content getAncestor6() throws ElementNotFoundException, RepositoryException {
305 return (new Content(this.node.getAncestor(6)));
306 }
307
308 /**
309 *
310 * <p>Convenience method for taglib</p>
311 *
312 * @throws javax.jcr.ElementNotFoundException
313 * @throws javax.jcr.RepositoryException
314 * @return Content representing node on level 7
315 */
316 public Content getAncestor7() throws ElementNotFoundException, RepositoryException {
317 return (new Content(this.node.getAncestor(7)));
318 }
319
320 /**
321 *
322 * <p>Convenience method for taglib</p>
323 *
324 * @throws javax.jcr.ElementNotFoundException
325 * @throws javax.jcr.RepositoryException
326 * @return Content representing node on level 8
327 */
328 public Content getAncestor8() throws ElementNotFoundException, RepositoryException {
329 return (new Content(this.node.getAncestor(8)));
330 }
331
332 /**
333 *
334 * <p>Convenience method for taglib</p>
335 *
336 * @throws javax.jcr.ElementNotFoundException
337 * @throws javax.jcr.RepositoryException
338 * @return Content representing node on level 9
339 */
340 public Content getAncestor9() throws ElementNotFoundException, RepositoryException {
341 return (new Content(this.node.getAncestor(9)));
342 }
343
344
345
346
347 /**
348 *
349 * <p>get node level from the ROOT node
350 * : FIXME implement getDepth in javax.jcr
351 * </p>
352 *
353 * @throws javax.jcr.ElementNotFoundException
354 * @throws javax.jcr.RepositoryException
355 * @return level at which current node exist, relative to the ROOT node
356 */
357 public int getLevel() throws ElementNotFoundException, RepositoryException {
358 return this.node.getPath().split("/").length - 1;
359 }
360
361
362
363 /**
364 * @return Boolean, if sub node(s) exists
365 */
366 public boolean hasChildren() {
367 return (this.getChildren(SORT_BY_NAME).size() > 0);
368 }
369
370
371 /**
372 * Convenicence method to access from JSTL
373 *
374 *
375 * @return Boolean, if sub node(s) exists
376 */
377 public boolean isHasChildren() {
378 return hasChildren();
379 }
380
381
382
383 /**
384 * @return String, template name
385 * */
386 public String getTemplate() {
387 try {
388 return this.node.getProperty(ContentHandler.TEMPLATE).getValue().getString();
389 } catch (RepositoryException re ) {return "";}
390
391 }
392
393 /**
394 * @return String, title
395 * */
396 public String getTitle() {
397 try {
398 return this.node.getProperty(ContentHandler.TITLE).getValue().getString();
399 } catch (RepositoryException re ) {return "";}
400
401 }
402
403
404
405 /**
406 * <p>gets a Collection containing all clild nodes at the current level+1 level</p>
407 * @return Collection of content nodes
408 */
409 public Collection sortByDate(Collection c) {
410 try {
411 if (c==null) return c;
412 ArraySequence as = new ArraySequence();
413 Iterator it = c.iterator();
414 while (it.hasNext()) {
415 Content content = (Content)it.next();
416 as.insertLast(content);
417 }
418 HeapSort hs = new HeapSort();
419 hs.sort(as,new DateComparator());
420 Collection sortedCollection = new ArrayList();
421 ObjectIterator oi = as.elements();
422 while (oi.hasNext()) {
423 sortedCollection.add((Content)oi.nextObject());
424 }
425 return sortedCollection;
426 }
427 catch (Exception e) {
428 e.printStackTrace();
429 return c;
430 }
431 }
432
433
434 /**
435 * <p>gets a Collection containing all clild nodes at the current level+1 level</p>
436 * @return Collection of content nodes
437 */
438 public Collection sortBySequence(Collection c) {
439 try {
440 if (c==null) return c;
441 ArraySequence as = new ArraySequence();
442 Iterator it = c.iterator();
443 while (it.hasNext()) {
444 Content content = (Content)it.next();
445 as.insertLast(content);
446 }
447 HeapSort hs = new HeapSort();
448 hs.sort(as,new SequenceComparator());
449 Collection sortedCollection = new ArrayList();
450 ObjectIterator oi = as.elements();
451 while (oi.hasNext()) {
452 sortedCollection.add(oi.nextObject());
453 }
454 return sortedCollection;
455 }
456 catch (Exception e) {
457 e.printStackTrace();
458 return c;
459 }
460 }
461
462
463
464 /**
465 * <p>checks for the allowed access rights</p>
466 *
467 * @param permissions as defined in javax.jcr.Permission
468 * @return true is the current user has specified access on this node.
469 */
470 public boolean isGranted(long permissions) {
471 return this.node.isGranted(permissions);
472 }
473
474
475 }