KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > core > HierarchyManager


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-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.core;
14
15 import info.magnolia.cms.core.search.QueryManager;
16 import info.magnolia.cms.security.AccessDeniedException;
17 import info.magnolia.cms.security.AccessManager;
18 import info.magnolia.cms.security.Authenticator;
19 import info.magnolia.cms.security.Permission;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import javax.jcr.ItemNotFoundException;
25 import javax.jcr.Node;
26 import javax.jcr.PathNotFoundException;
27 import javax.jcr.RepositoryException;
28 import javax.jcr.Workspace;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import org.apache.commons.lang.StringUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36 /**
37  * User: sameercharles Date: Sept 23, 2004 Time: 1:42:48 PM
38  * @author Sameer Charles $Id:HierarchyManager.java 2719 2006-04-27 14:38:44Z scharles $
39  */

40 public class HierarchyManager {
41
42     /**
43      * Logger.
44      */

45     private static Logger log = LoggerFactory.getLogger(HierarchyManager.class);
46
47     /**
48      * root of this hierarchy
49      */

50     private Node startPage;
51
52     /**
53      * workspacer for this hierarchy
54      */

55     private Workspace workSpace;
56
57     /**
58      * user who created this hierarchy
59      */

60     private String JavaDoc userID;
61
62     /**
63      * access manager for this hierarchy
64      */

65     private AccessManager accessManager;
66
67     /**
68      * query manager for this hierarchy
69      */

70     private QueryManager queryManager;
71
72     /**
73      * constructor
74      */

75     public HierarchyManager() {
76         this.userID = "anonymous"; //$NON-NLS-1$
77
}
78
79     public HierarchyManager(String JavaDoc userID) {
80         this.userID = userID;
81     }
82
83     /**
84      * constructor
85      */

86     public HierarchyManager(HttpServletRequest JavaDoc request) {
87         this.userID = Authenticator.getUserId(request);
88     }
89
90     /**
91      * sets start page of the current working repository
92      * @throws javax.jcr.PathNotFoundException
93      * @throws javax.jcr.RepositoryException
94      * @see HierarchyManager#init(javax.jcr.Node)
95      * @deprecated instead use init(Node rootNode)
96      */

97     public void setStartPage(Node rootNode) throws PathNotFoundException, RepositoryException {
98         this.startPage = rootNode;
99         this.workSpace = this.startPage.getSession().getWorkspace();
100     }
101
102     /**
103      * initialize hierarchy manager and sets default workspace
104      * @throws javax.jcr.PathNotFoundException
105      * @throws javax.jcr.RepositoryException
106      */

107     public void init(Node rootNode) throws PathNotFoundException, RepositoryException {
108         this.startPage = rootNode;
109         this.workSpace = this.startPage.getSession().getWorkspace();
110     }
111
112     /**
113      * initialize hierarchy manager and sets default workspace
114      * @throws javax.jcr.PathNotFoundException
115      * @throws javax.jcr.RepositoryException
116      */

117     public void init(Node rootNode, AccessManager manager) throws PathNotFoundException, RepositoryException {
118         this.startPage = rootNode;
119         this.workSpace = this.startPage.getSession().getWorkspace();
120         this.accessManager = manager;
121     }
122
123     /**
124      * Set access manager for this hierarchy
125      * @param accessManager
126      */

127     public void setAccessManager(AccessManager accessManager) {
128         this.accessManager = accessManager;
129     }
130
131     /**
132      * Get access manager
133      * @return accessmanager attached to this hierarchy
134      */

135     public AccessManager getAccessManager() {
136         return this.accessManager;
137     }
138
139     /**
140      * Set query manager for this hierarchy
141      * @param queryManager
142      */

143     public void setQueryManager(QueryManager queryManager) {
144         this.queryManager = queryManager;
145     }
146
147     public QueryManager getQueryManager() {
148         return this.queryManager;
149     }
150
151     /**
152      * creates a new content page
153      * @param path parent handle under which new page has to be created
154      * @param label page name to be created
155      * @return Content newly created hierarchy node
156      * @throws javax.jcr.PathNotFoundException
157      * @throws javax.jcr.RepositoryException
158      */

159     public Content createPage(String JavaDoc path, String JavaDoc label) throws PathNotFoundException, RepositoryException,
160         AccessDeniedException {
161         Content newPage = (new Content(
162             this.startPage,
163             this.getNodePath(path, label),
164             ItemType.CONTENT.getSystemName(),
165             this.accessManager));
166         this.setMetaData(newPage.getMetaData());
167         return newPage;
168     }
169
170     /**
171      * creates contentNode of type <b>contentType </b> contentType must be defined in item type definition of magnolia
172      * as well as JCR implementation
173      * @param path absolute (primary) path to this <code>Node</code>
174      * @param label page name
175      * @param contentType , JCR node type as configured
176      * @throws PathNotFoundException
177      * @throws RepositoryException
178      * @throws AccessDeniedException
179      */

180     public Content createContent(String JavaDoc path, String JavaDoc label, String JavaDoc contentType) throws PathNotFoundException,
181         RepositoryException, AccessDeniedException {
182         Content newPage = new Content(this.startPage, this.getNodePath(path, label), contentType, this.accessManager);
183         setMetaData(newPage.getMetaData());
184         return newPage;
185     }
186
187     private String JavaDoc getNodePath(String JavaDoc path, String JavaDoc label) {
188         if (StringUtils.isEmpty(path) || (path.equals("/"))) { //$NON-NLS-1$
189
return label;
190         }
191         return getNodePath(path + "/" + label); //$NON-NLS-1$
192
}
193
194     private String JavaDoc getNodePath(String JavaDoc path) {
195         if (path != null && path.startsWith("/")) { //$NON-NLS-1$
196
return path.replaceFirst("/", StringUtils.EMPTY); //$NON-NLS-1$
197
}
198         return path;
199     }
200
201     /**
202      * Helper method to set page properties, create page calls this method. you could call this method anytime to create
203      * working page properties
204      */

205     public void setMetaData(MetaData md, String JavaDoc template) throws RepositoryException, AccessDeniedException {
206         md.setTemplate(template);
207         setMetaData(md);
208     }
209
210     /**
211      * Helper method to set page properties, create page calls this method. you could call this method anytime to create
212      * working page properties
213      */

214     public void setMetaData(MetaData md) throws RepositoryException, AccessDeniedException {
215         md.setCreationDate();
216         md.setModificationDate();
217         md.setAuthorId(this.userID);
218         md.setTitle(StringUtils.EMPTY);
219     }
220
221     /**
222      * Helper method to set page properties, get page calls this method. you could call this method anytime to update
223      * working page properties
224      */

225     public void updateMetaData(MetaData md) throws RepositoryException, AccessDeniedException {
226         md.setModificationDate();
227         md.setAuthorId(this.userID);
228     }
229
230     /**
231      * returns the page specified by path in the parameter
232      * @param path handle of the page to be initialized
233      * @return Content hierarchy node
234      * @throws javax.jcr.PathNotFoundException
235      * @throws javax.jcr.RepositoryException
236      * @deprecated use getContent(String path) instead
237      */

238     public Content getPage(String JavaDoc path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
239         return this.getContent(path);
240     }
241
242     /**
243      * get content object of the requested URI
244      * @param path of the content to be initialized
245      * @return Content
246      * @throws javax.jcr.PathNotFoundException
247      * @throws javax.jcr.RepositoryException
248      */

249     public Content getContent(String JavaDoc path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
250         if (path.equals("/")) { //$NON-NLS-1$
251
return this.getRoot();
252         }
253         return (new Content(this.startPage, getNodePath(path), this.accessManager));
254     }
255
256     /**
257      * Like getContent() but creates the node if not yet existing. Attention save is not called!
258      * @param path the path of the node
259      * @param create true if the node should get created
260      * @param type the node type of the created node
261      * @return the node
262      * @throws AccessDeniedException
263      * @throws RepositoryException
264      */

265     public Content getContent(String JavaDoc path, boolean create, ItemType type) throws AccessDeniedException,
266         RepositoryException {
267         Content node;
268         try {
269             node = getContent(path);
270         }
271         catch (PathNotFoundException e) {
272             if (create) {
273                 node = this.createContent(StringUtils.substringBeforeLast(path, "/"), StringUtils.substringAfterLast(
274                     path,
275                     "/"), type.toString());
276             }
277             else {
278                 throw e;
279             }
280         }
281         return node;
282     }
283
284     /**
285      * get content node object of the requested URI
286      * @param path of the content (container / containerlist) to be initialized
287      * @return ContentNode
288      * @throws javax.jcr.PathNotFoundException
289      * @throws javax.jcr.RepositoryException
290      * @deprecated use getContent(String path) instead
291      */

292     public Content getContentNode(String JavaDoc path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
293         return new Content(this.startPage, getNodePath(path), this.accessManager);
294     }
295
296     /**
297      * get NodeData object of the requested URI
298      * @param path of the atom to be initialized
299      * @return NodeData
300      * @throws javax.jcr.PathNotFoundException
301      * @throws javax.jcr.RepositoryException
302      */

303     public NodeData getNodeData(String JavaDoc path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
304         String JavaDoc nodePath = getNodePath(path);
305         if (StringUtils.isEmpty(nodePath)) {
306             return null;
307         }
308
309         return new NodeData(this.startPage, nodePath, this.accessManager);
310     }
311
312     /**
313      * returns the first page with a given template name that is found in tree that starts from the page given py the
314      * path (including this page)
315      * @param path handle of the page from where the search should start
316      * @param templateName template name to search for
317      * @return first Content hierarchy node that has the specified template name assigned
318      * @throws javax.jcr.PathNotFoundException
319      * @throws javax.jcr.RepositoryException
320      */

321     public Content getPage(String JavaDoc path, String JavaDoc templateName) throws PathNotFoundException, RepositoryException,
322         AccessDeniedException {
323         Content page = getContent(path);
324         if (page.getTemplate().equals(templateName)) {
325             return page;
326         }
327         Content pageToBeFound = null;
328         try {
329             if (page.hasChildren()) {
330                 Collection JavaDoc children = page.getChildren(ItemType.CONTENT.getSystemName());
331                 Iterator JavaDoc iterator = children.iterator();
332                 while (iterator.hasNext()) {
333                     Content child = (Content) iterator.next();
334                     if (child.getTemplate().equals(templateName)) {
335                         return child;
336                     }
337                     if (child.hasChildren()) {
338                         pageToBeFound = getPage(child.getHandle(), templateName);
339                     }
340                     if (pageToBeFound != null) {
341                         return pageToBeFound;
342                     }
343                 }
344             }
345         }
346         catch (Exception JavaDoc e) {
347             log.error("Failed to get - " + path); //$NON-NLS-1$
348
log.error(e.getMessage(), e);
349         }
350         return pageToBeFound;
351     }
352
353     /**
354      * removes specified path, it can be either node or property
355      * @param path to be removed
356      * @throws javax.jcr.PathNotFoundException
357      * @throws javax.jcr.RepositoryException
358      * @throws AccessDeniedException
359      */

360     public void delete(String JavaDoc path) throws PathNotFoundException, RepositoryException, AccessDeniedException {
361         if (this.isNodeData(path)) {
362             this.getNodeData(makeRelative(path)).delete();
363         }
364         else {
365             this.startPage.getNode(makeRelative(path)).remove();
366         }
367
368     }
369
370     private String JavaDoc makeRelative(String JavaDoc path) {
371         return StringUtils.stripStart(path, "/"); //$NON-NLS-1$
372
}
373
374     /**
375      * @return startPage of the current working repository-workspace
376      */

377     public Content getRoot() throws RepositoryException, AccessDeniedException {
378         return (new Content(this.startPage, this.accessManager));
379     }
380
381     /**
382      * Checks if the requested resource is a page (hierarchy Node).
383      * @param path of the requested content
384      * @return boolean true is the requested content is a Hierarchy Node todo remove this method, instead use
385      * (getContent(PATH) is NodeType)
386      */

387     public boolean isPage(String JavaDoc path) throws AccessDeniedException {
388         Access.isGranted(this.accessManager, path, Permission.READ);
389
390         String JavaDoc nodePath = getNodePath(path);
391         if (StringUtils.isEmpty(nodePath)) {
392             return false;
393         }
394
395         try {
396             Node n = this.startPage.getNode(nodePath);
397             return (n.isNodeType(ItemType.CONTENT.getSystemName()));
398         }
399         catch (RepositoryException re) {
400             // ignore, not existing?
401
}
402         return false;
403     }
404
405     /**
406      * check is either the node or property exists with the specified path
407      * @param path
408      */

409     public boolean isExist(String JavaDoc path) {
410         try {
411             Access.isGranted(this.accessManager, path, Permission.READ);
412         }
413         catch (AccessDeniedException e) {
414             log.error(e.getMessage());
415             return false;
416         }
417         boolean isExist = false;
418         try {
419             isExist = this.workSpace.getSession().itemExists(path);
420         }
421         catch (RepositoryException re) {
422             log.error("Exception caught", re);
423         }
424         return isExist;
425     }
426
427     /**
428      * Evaluate primary node type of the node at the given path.
429      */

430     public boolean isNodeType(String JavaDoc path, String JavaDoc type) {
431         try {
432             Node n = this.startPage.getNode(getNodePath(path));
433             return n.isNodeType(type);
434         }
435         catch (RepositoryException re) {
436             log.error(re.getMessage());
437             log.debug(re.getMessage(), re);
438         }
439         return false;
440     }
441
442     /**
443      * Evaluate primary node type of the node at the given path.
444      */

445     public boolean isNodeType(String JavaDoc path, ItemType type) {
446         return isNodeType(path, type.getSystemName());
447     }
448
449     /**
450      * checks if the requested resource is an NodeData (Property)
451      * @param path of the requested NodeData
452      * @return boolean true is the requested content is an NodeData
453      */

454     public boolean isNodeData(String JavaDoc path) throws AccessDeniedException {
455         Access.isGranted(this.accessManager, path, Permission.READ);
456         boolean result = false;
457         String JavaDoc nodePath = getNodePath(path);
458         if (StringUtils.isEmpty(nodePath)) {
459             return false;
460         }
461         try {
462             result = this.startPage.hasProperty(nodePath);
463             if (!result) {
464                 // check if its a nt:resource
465
result = this.startPage.hasProperty(nodePath + "/" + ItemType.JCR_DATA);
466             }
467         }
468         catch (RepositoryException e) {
469             // ignore, no property
470
}
471         return result;
472     }
473
474     /**
475      * This method can be used to retrieve Content which has UUID assigned to it, in other words only those nodes which
476      * has mixin type mix:referenceable.
477      * @param uuid
478      */

479     public Content getContentByUUID(String JavaDoc uuid) throws ItemNotFoundException, RepositoryException,
480         AccessDeniedException {
481         return new Content(this.startPage.getSession().getNodeByUUID(uuid), this.accessManager);
482     }
483
484     /**
485      * gets currently used workspace for this hierarchy manager
486      */

487     public Workspace getWorkspace() {
488         return this.workSpace;
489     }
490
491     /**
492      * move content to the specified location
493      * @param source source node path
494      * @param destination node where the node has to be moved
495      * @throws javax.jcr.PathNotFoundException
496      * @throws javax.jcr.RepositoryException
497      */

498     public void moveTo(String JavaDoc source, String JavaDoc destination) throws PathNotFoundException, RepositoryException,
499         AccessDeniedException {
500         Access.isGranted(this.accessManager, source, Permission.REMOVE);
501         Access.isGranted(this.accessManager, destination, Permission.WRITE);
502         this.workSpace.move(source, destination);
503     }
504
505     /**
506      * copy content to the specified location
507      * @param source source node path
508      * @param destination node where the node has to be copied
509      * @throws javax.jcr.PathNotFoundException
510      * @throws javax.jcr.RepositoryException
511      */

512     public void copyTo(String JavaDoc source, String JavaDoc destination) throws PathNotFoundException, RepositoryException,
513         AccessDeniedException {
514         Access.isGranted(this.accessManager, source, Permission.READ);
515         Access.isGranted(this.accessManager, destination, Permission.WRITE);
516         this.workSpace.copy(source, destination);
517     }
518
519     /**
520      * Persists all changes to the repository if validation succeds
521      * @throws RepositoryException
522      */

523     public void save() throws RepositoryException {
524         try {
525             this.startPage.getSession().save();
526         }
527         catch (RepositoryException re) {
528             log.error(re.getMessage(), re);
529             throw re;
530         }
531     }
532
533     /**
534      * Returns true if the session has pending (unsaved) changes.
535      */

536     public boolean hasPendingChanges() throws RepositoryException {
537         return this.startPage.getSession().hasPendingChanges();
538     }
539
540     /**
541      * Refreshes this session
542      * @param keepChanges
543      * @throws RepositoryException
544      * @see javax.jcr.Session#refresh(boolean)
545      */

546     public void refresh(boolean keepChanges) throws RepositoryException {
547         this.workSpace.getSession().refresh(keepChanges);
548     }
549
550 }
551
Popular Tags