KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > core > version > ContentVersion


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.version;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ItemType;
17 import info.magnolia.cms.core.NodeData;
18 import info.magnolia.cms.security.AccessDeniedException;
19 import info.magnolia.cms.security.AccessManager;
20 import info.magnolia.cms.security.Permission;
21 import info.magnolia.cms.util.Rule;
22
23 import java.util.Calendar JavaDoc;
24 import java.util.Collection JavaDoc;
25
26 import javax.jcr.PathNotFoundException;
27 import javax.jcr.RepositoryException;
28 import javax.jcr.Value;
29 import javax.jcr.lock.Lock;
30 import javax.jcr.lock.LockException;
31 import javax.jcr.nodetype.NodeType;
32 import javax.jcr.version.Version;
33 import javax.jcr.version.VersionHistory;
34 import javax.jcr.version.VersionIterator;
35
36 import org.apache.commons.lang.StringUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41 /**
42  * @author Sameer Charles
43  * $Id: ContentVersion.java 6430 2006-09-20 11:25:35Z scharles $
44  */

45 public class ContentVersion extends Content {
46
47     /**
48      * Logger.
49      */

50     private static Logger log = LoggerFactory.getLogger(ContentVersion.class);
51
52     /**
53      * user who created this version
54      */

55     public static final String JavaDoc VERSION_USER = "versionUser"; //$NON-NLS-1$
56

57     /**
58      * name of the base node
59      */

60     public static final String JavaDoc NAME = "name";
61
62     /**
63      * version node (nt:version)
64      */

65     private Version state;
66
67     /**
68      * base content
69      */

70     private Content base;
71
72     /**
73      * Rule used to create this version
74      */

75     private Rule rule;
76
77     /**
78      * package private constructor
79      * @param thisVersion
80      * @param base content on which this version is based on
81      * @throws RepositoryException
82      */

83     public ContentVersion(Version thisVersion, Content base) throws RepositoryException {
84         if (thisVersion == null) {
85             throw new RepositoryException("Failed to get ContentVersion, version does not exist");
86         }
87         this.state = thisVersion;
88         this.base = base;
89         this.init();
90     }
91
92     /**
93      * Set frozen node of this version as working node
94      * @throws RepositoryException
95      */

96     private void init() throws RepositoryException {
97         this.setNode(this.state.getNode(ItemType.JCR_FROZENNODE));
98         try {
99             if (!StringUtils.equalsIgnoreCase(this.state.getName(), VersionManager.ROOT_VERSION)) {
100                 this.rule = VersionManager.getInstance().getUsedFilter(this);
101             }
102         }
103         catch (Exception JavaDoc e) {
104             log.error(e.getMessage(), e);
105         }
106         if (this.rule == null) {
107             log.info("failed to get filter used for creating this version, use open filter");
108             this.rule = new Rule();
109         }
110     }
111
112     /**
113      * Get creation date of this version
114      * @throws RepositoryException
115      * @return creation date as calendar
116      */

117     public Calendar JavaDoc getCreated() throws RepositoryException {
118         return this.state.getCreated();
119     }
120
121     /**
122      * Return the name of the version represented by this object
123      * @return the versions name
124      * @throws RepositoryException
125      */

126     public String JavaDoc getVersionLabel() throws RepositoryException {
127         return this.state.getName();
128     }
129
130     /**
131      * Get containing version history
132      * @throws RepositoryException
133      * @return version history associated to this version
134      */

135     public VersionHistory getContainingHistory() throws RepositoryException {
136         return this.state.getContainingHistory();
137     }
138
139     /**
140      * The original name of the node.
141      */

142     public String JavaDoc getName() {
143         try {
144             return VersionManager.getInstance().getSystemNode(this).getNodeData(NAME).getString();
145         }
146         catch (RepositoryException re) {
147             log.error("Failed to retrieve name from version system node", re);
148             return "";
149         }
150     }
151
152     /**
153      * The name of the user who created this version
154      */

155     public String JavaDoc getUserName() {
156         try {
157             return VersionManager.getInstance().getSystemNode(this).getNodeData(VERSION_USER).getString();
158         }
159         catch (RepositoryException re) {
160             log.error("Failed to retrieve user from version system node", re);
161             return "";
162         }
163     }
164
165     /**
166      * get original path of this versioned content
167      */

168     public String JavaDoc getHandle() {
169         return this.base.getHandle();
170     }
171
172     /**
173      * create Content node under the current node with the specified name
174      * @param name of the node to be created as <code>Content</code>
175      * @return newly created <node>Content </node>
176      * @throws info.magnolia.cms.security.AccessDeniedException if the current session does not have sufficient access
177      * rights to complete the operation
178      */

179     public Content createContent(String JavaDoc name) throws AccessDeniedException {
180         throw new AccessDeniedException("Not allowed to write on version preview");
181     }
182
183     /**
184      * create Content node under the current node with the specified name
185      * @param name of the node to be created as <code>Content</code>
186      * @param contentType JCR node type as configured
187      * @return newly created <node>Content </node>
188      * @throws info.magnolia.cms.security.AccessDeniedException if the current session does not have sufficient access
189      * rights to complete the operation
190      */

191     public Content createContent(String JavaDoc name, String JavaDoc contentType) throws AccessDeniedException {
192         throw new AccessDeniedException("Not allowed to write on version preview");
193     }
194
195     /**
196      * Create Content node under the current node with the specified name.
197      * @param name of the node to be created as <code>Content</code>
198      * @param contentType ItemType
199      * @return newly created <node>Content </node>
200      * @throws info.magnolia.cms.security.AccessDeniedException if the current session does not have sufficient access
201      * rights to complete the operation
202      */

203     public Content createContent(String JavaDoc name, ItemType contentType) throws AccessDeniedException {
204         throw new AccessDeniedException("Not allowed to write on version preview");
205     }
206
207     /**
208      * create top level NodeData object
209      * @param name to be created
210      * @return NodeData requested <code>NodeData</code> object
211      * @throws info.magnolia.cms.security.AccessDeniedException if the current session does not have sufficient access
212      * rights to complete the operation
213      */

214     public NodeData createNodeData(String JavaDoc name) throws AccessDeniedException {
215         throw new AccessDeniedException("Not allowed to write on version preview");
216     }
217
218     /**
219      * Create NodeData with the given value and type.
220      * @param name to be created
221      * @param value to be set initially
222      * @param type propertyType
223      * @return NodeData requested <code>NodeData</code> object
224      * @throws info.magnolia.cms.security.AccessDeniedException if the current session does not have sufficient access
225      * rights to complete the operation
226      */

227     public NodeData createNodeData(String JavaDoc name, Value value, int type) throws AccessDeniedException {
228         throw new AccessDeniedException("Not allowed to write on version preview");
229     }
230
231     /**
232      * Create NodeData with the given value and type.
233      * @param name to be created
234      * @param value to be set initially
235      * @return NodeData requested <code>NodeData</code> object
236      * @throws info.magnolia.cms.security.AccessDeniedException if the current session does not have sufficient access
237      * rights to complete the operation
238      */

239     public NodeData createNodeData(String JavaDoc name, Value value) throws AccessDeniedException {
240         throw new AccessDeniedException("Not allowed to write on version preview");
241     }
242
243     /**
244      * create top level NodeData object
245      * @param name to be created
246      * @param type propertyType
247      * @return NodeData requested <code>NodeData</code> object
248      * @throws info.magnolia.cms.security.AccessDeniedException if the current session does not have sufficient access
249      * rights to complete the operation
250      */

251     public NodeData createNodeData(String JavaDoc name, int type) throws AccessDeniedException {
252         throw new AccessDeniedException("Not allowed to write on version preview");
253     }
254
255     /**
256      * delete NodeData with the specified name
257      * @throws javax.jcr.RepositoryException if an error occurs
258      */

259     public void deleteNodeData(String JavaDoc name) throws RepositoryException {
260         throw new AccessDeniedException("Not allowed to write on version preview");
261     }
262
263     /**
264      * you could call this method anytime to update working page properties - Modification date & Author ID
265      * @throws info.magnolia.cms.security.AccessDeniedException if the current session does not have sufficient access
266      * rights to complete the operation
267      */

268     public void updateMetaData() throws AccessDeniedException {
269         throw new AccessDeniedException("Not allowed to write on version preview");
270     }
271
272     /**
273      * gets a Collection containing all child nodes of the same NodeType as "this" object.
274      * @return Collection of content objects
275      */

276     public Collection JavaDoc getChildren() {
277         try {
278             if (this.rule.isAllowed(this.base.getNodeTypeName())) {
279                 return super.getChildren();
280             }
281         }
282         catch (RepositoryException re) {
283             log.error(re.getMessage(), re);
284         }
285         return this.base.getChildren();
286     }
287
288     /**
289      * Get collection of specified content type
290      * @param contentType JCR node type as configured
291      * @return Collection of content nodes
292      */

293     public Collection JavaDoc getChildren(String JavaDoc contentType) {
294         if (this.rule.isAllowed(contentType)) {
295             return super.getChildren(contentType);
296         }
297         return this.base.getChildren(contentType);
298     }
299
300     /**
301      * Get collection of specified content type
302      * @param contentType ItemType
303      * @return Collection of content nodes
304      */

305     public Collection JavaDoc getChildren(ItemType contentType) {
306         return this.getChildren(contentType.getSystemName());
307     }
308
309     /**
310      * Get collection of specified content type.
311      * @param contentType JCR node type as configured
312      * @param namePattern
313      * @return Collection of content nodes
314      */

315     public Collection JavaDoc getChildren(String JavaDoc contentType, String JavaDoc namePattern) {
316         if (this.rule.isAllowed(contentType)) {
317             return super.getChildren(contentType, namePattern);
318         }
319         return this.base.getChildren(contentType, namePattern);
320     }
321
322     /**
323      * @return Boolean, if sub node(s) exists
324      */

325     public boolean hasChildren() {
326         return (this.getChildren().size() > 0);
327     }
328
329     /**
330      * @param contentType JCR node type as configured
331      * @return Boolean, if sub <code>collectionType</code> exists
332      */

333     public boolean hasChildren(String JavaDoc contentType) {
334         return (this.getChildren(contentType).size() > 0);
335     }
336
337     /**
338      * get parent content object
339      * @return Content representing parent node
340      * @throws javax.jcr.PathNotFoundException
341      * @throws info.magnolia.cms.security.AccessDeniedException if the current session does not have sufficient access
342      * rights to complete the operation
343      * @throws javax.jcr.RepositoryException if an error occurs
344      */

345     public Content getParent() throws PathNotFoundException, RepositoryException, AccessDeniedException {
346         return this.base.getParent();
347     }
348
349     /**
350      * get absolute parent object starting from the root node
351      * @param digree level at which the requested node exist, relative to the ROOT node
352      * @return Content representing parent node
353      * @throws info.magnolia.cms.security.AccessDeniedException if the current session does not have sufficient access
354      * rights to complete the operation
355      * @throws javax.jcr.RepositoryException if an error occurs
356      */

357     public Content getAncestor(int digree) throws PathNotFoundException, RepositoryException, AccessDeniedException {
358         return this.base.getAncestor(digree);
359     }
360
361     /**
362      * Convenience method for taglib
363      * @return Content representing node on level 0
364      * @throws javax.jcr.RepositoryException if an error occurs
365      */

366     public Collection JavaDoc getAncestors() throws PathNotFoundException, RepositoryException {
367         return this.base.getAncestors();
368     }
369
370     /**
371      * get node level from the ROOT node : FIXME implement getDepth in javax.jcr
372      * @return level at which current node exist, relative to the ROOT node
373      * @throws javax.jcr.PathNotFoundException
374      * @throws javax.jcr.RepositoryException if an error occurs
375      */

376     public int getLevel() throws PathNotFoundException, RepositoryException {
377         return this.base.getLevel();
378     }
379
380     /**
381      * move current node to the specified location above the named <code>beforename</code>
382      * @param srcName where current node has to be moved
383      * @param beforeName name of the node before the current node has to be placed
384      * @throws javax.jcr.RepositoryException if an error occurs
385      */

386     public void orderBefore(String JavaDoc srcName, String JavaDoc beforeName) throws RepositoryException {
387         throw new AccessDeniedException("Not allowed to write on version preview");
388     }
389
390     /**
391      * This method returns the index of this node within the ordered set of its same-name sibling nodes. This index is
392      * the one used to address same-name siblings using the square-bracket notation, e.g., /a[3]/b[4]. Note that the
393      * index always starts at 1 (not 0), for compatibility with XPath. As a result, for nodes that do not have
394      * same-name-siblings, this method will always return 1.
395      * @return The index of this node within the ordered set of its same-name sibling nodes.
396      * @throws javax.jcr.RepositoryException if an error occurs
397      */

398     public int getIndex() throws RepositoryException {
399         return this.base.getIndex();
400     }
401
402     /**
403      * returns primary node type definition of the associated Node of this object
404      * @throws RepositoryException if an error occurs
405      */

406     public NodeType getNodeType() throws RepositoryException {
407         log.warn("This is a Version node, it will always return NT_FROZEN as node type.");
408         log.warn("Use getNodeTypeName to retrieve base node primary type");
409         return super.getNodeType();
410     }
411
412     /**
413      * Restores this node to the state defined by the version with the specified versionName.
414      * @param versionName
415      * @param removeExisting
416      * @throws javax.jcr.RepositoryException if an error occurs
417      */

418     public void restore(String JavaDoc versionName, boolean removeExisting) throws RepositoryException {
419         throw new AccessDeniedException("Not allowed to write on version preview");
420     }
421
422     /**
423      * Restores this node to the state defined by the specified version.
424      * @param version
425      * @param removeExisting
426      * @throws javax.jcr.RepositoryException if an error occurs
427      */

428     public void restore(Version version, boolean removeExisting) throws RepositoryException {
429         throw new AccessDeniedException("Not allowed to write on version preview");
430     }
431
432     /**
433      * Restores the specified version to relPath, relative to this node.
434      * @param version
435      * @param relPath
436      * @param removeExisting
437      * @throws javax.jcr.RepositoryException if an error occurs
438      */

439     public void restore(Version version, String JavaDoc relPath, boolean removeExisting) throws RepositoryException {
440         throw new AccessDeniedException("Not allowed to write on version preview");
441     }
442
443     /**
444      * Restores this node to the state recorded in the version specified by versionLabel.
445      * @param versionLabel
446      * @param removeExisting
447      * @throws javax.jcr.RepositoryException if an error occurs
448      */

449     public void restoreByLabel(String JavaDoc versionLabel, boolean removeExisting) throws RepositoryException {
450         throw new AccessDeniedException("Not allowed to write on version preview");
451     }
452
453     /**
454      * add version leaving the node checked out
455      * @throws javax.jcr.RepositoryException if an error occurs
456      */

457     public Version addVersion() throws RepositoryException {
458         throw new AccessDeniedException("Not allowed to add version on version preview");
459     }
460
461     /**
462      * add version leaving the node checked out
463      * @param rule to be used to collect content
464      * @throws javax.jcr.RepositoryException if an error occurs
465      * @see info.magnolia.cms.util.Rule
466      */

467     public Version addVersion(Rule rule) throws RepositoryException {
468         throw new AccessDeniedException("Not allowed to add version on version preview");
469     }
470
471     /**
472      * Returns <code>true</code> if this <code>Item</code> has been saved but has subsequently been modified through
473      * the current session and therefore the state of this item as recorded in the session differs from the state of
474      * this item as saved. Within a transaction, <code>isModified</code> on an <code>Item</code> may return
475      * <code>false</code> (because the <code>Item</code> has been saved since the modification) even if the
476      * modification in question is not in persistent storage (because the transaction has not yet been committed). <p/>
477      * Note that in level 1 (that is, read-only) implementations, this method will always return <code>false</code>.
478      * @return <code>true</code> if this item is modified; <code>false</code> otherwise.
479      */

480     public boolean isModified() {
481         log.error("Not valid for version");
482         return false;
483     }
484
485     /**
486      * @return version history
487      * @throws javax.jcr.RepositoryException if an error occurs
488      */

489     public VersionHistory getVersionHistory() throws RepositoryException {
490         throw new AccessDeniedException("Not allowed to read VersionHistory of Version");
491     }
492
493     /**
494      * @return Version iterator retreived from version history
495      * @throws javax.jcr.RepositoryException if an error occurs
496      */

497     public VersionIterator getAllVersions() throws RepositoryException {
498         throw new AccessDeniedException("Not allowed to get VersionIterator of Version");
499     }
500
501     /**
502      * get the current base version of this node
503      * @return base ContentVersion
504      * @throws javax.jcr.UnsupportedRepositoryOperationException
505      * @throws javax.jcr.RepositoryException
506      */

507     public ContentVersion getBaseVersion() throws RepositoryException {
508         throw new AccessDeniedException("Not allowed to get base version of Version");
509     }
510
511     /**
512      * get content view over the jcr version object
513      * @param version
514      * @return version object wrapped in ContentVersion
515      * @see info.magnolia.cms.core.version.ContentVersion
516      */

517     public ContentVersion getVersionedContent(Version version) throws RepositoryException {
518         throw new AccessDeniedException("Not allowed to get preview of Version itself");
519     }
520
521     /**
522      * get content view over the jcr version object
523      * @param versionName
524      * @return version object wrapped in ContentVersion
525      * @see info.magnolia.cms.core.version.ContentVersion
526      */

527     public ContentVersion getVersionedContent(String JavaDoc versionName) throws RepositoryException {
528         throw new AccessDeniedException("Not allowed to get preview of Version itself");
529     }
530
531     /**
532      * Persists all changes to the repository if validation succeds
533      * @throws javax.jcr.RepositoryException if an error occurs
534      */

535     public void save() throws RepositoryException {
536         throw new AccessDeniedException("Not allowed to write on version preview");
537     }
538
539     /**
540      * checks for the allowed access rights
541      * @param permissions as defined in javax.jcr.Permission
542      * @return true is the current user has specified access on this node.
543      */

544     public boolean isGranted(long permissions) {
545         return (permissions & Permission.READ) == permissions;
546     }
547
548     /**
549      * Remove this path
550      * @throws javax.jcr.RepositoryException if an error occurs
551      */

552     public void delete() throws RepositoryException {
553         throw new AccessDeniedException("Not allowed to write on version preview");
554     }
555
556     /**
557      * Remove specified path
558      * @throws javax.jcr.RepositoryException if an error occurs
559      */

560     public void delete(String JavaDoc path) throws RepositoryException {
561         throw new AccessDeniedException("Not allowed to write on version preview");
562     }
563
564     /**
565      * UUID of the node refrenced by this object
566      * @return uuid
567      */

568     public String JavaDoc getUUID() {
569         return this.base.getUUID();
570     }
571
572     /**
573      * add specified mixin type if allowed
574      * @param type mixin type to be added
575      * @throws javax.jcr.RepositoryException if an error occurs
576      */

577     public void addMixin(String JavaDoc type) throws RepositoryException {
578         throw new AccessDeniedException("Not allowed to write on version preview");
579     }
580
581     /**
582      * Removes the specified mixin node type from this node. Also removes mixinName from this node's jcr:mixinTypes
583      * property. <b>The mixin node type removal takes effect on save</b>.
584      * @param type , mixin type to be removed
585      * @throws javax.jcr.RepositoryException if an error occurs
586      */

587     public void removeMixin(String JavaDoc type) throws RepositoryException {
588         throw new AccessDeniedException("Not allowed to write on version preview");
589     }
590
591     /**
592      * places a lock on this object
593      * @param isDeep if true this lock will apply to this node and all its descendants; if false, it applies only to
594      * this node.
595      * @param isSessionScoped if true, this lock expires with the current session; if false it expires when explicitly
596      * or automatically unlocked for some other reason.
597      * @return A Lock object containing a lock token.
598      * @throws javax.jcr.lock.LockException if this node is already locked or <code>isDeep</code> is true and a
599      * descendant node of this node already holds a lock.
600      * @throws javax.jcr.RepositoryException if an error occurs
601      * @see javax.jcr.Node#lock(boolean, boolean)
602      */

603     public Lock lock(boolean isDeep, boolean isSessionScoped) throws LockException, RepositoryException {
604         throw new AccessDeniedException("Lock not supported on version preview");
605     }
606
607     /**
608      * places a lock on this object
609      * @param isDeep if true this lock will apply to this node and all its descendants; if false, it applies only to
610      * this node.
611      * @param isSessionScoped if true, this lock expires with the current session; if false it expires when explicitly
612      * or automatically unlocked for some other reason.
613      * @param yieldFor number of milliseconds for which this method will try to get a lock
614      * @return A Lock object containing a lock token.
615      * @throws javax.jcr.lock.LockException if this node is already locked or <code>isDeep</code> is true and a
616      * descendant node of this node already holds a lock.
617      * @throws javax.jcr.RepositoryException if an error occurs
618      * @see javax.jcr.Node#lock(boolean, boolean)
619      */

620     public Lock lock(boolean isDeep, boolean isSessionScoped, long yieldFor) throws LockException, RepositoryException {
621         throw new AccessDeniedException("Lock not supported on version preview");
622     }
623
624     /**
625      * Returns the Lock object that applies to this node. This may be either a lock on this node itself or a deep lock
626      * on a node above this node.
627      * @throws javax.jcr.lock.LockException If no lock applies to this node, a LockException is thrown.
628      * @throws javax.jcr.RepositoryException if an error occurs
629      */

630     public Lock getLock() throws LockException, RepositoryException {
631         throw new AccessDeniedException("Lock not supported on version preview");
632     }
633
634     /**
635      * Removes the lock on this node. Also removes the properties jcr:lockOwner and jcr:lockIsDeep from this node. These
636      * changes are persisted automatically; <b>there is no need to call save</b>.
637      * @throws javax.jcr.lock.LockException if either does not currently hold a lock, or holds a lock for which this
638      * Session does not have the correct lock token
639      * @throws javax.jcr.RepositoryException if an error occurs
640      */

641     public void unlock() throws LockException, RepositoryException {
642         throw new AccessDeniedException("Lock not supported on version preview");
643     }
644
645     /**
646      * Returns true if this node holds a lock; otherwise returns false. To hold a lock means that this node has actually
647      * had a lock placed on it specifically, as opposed to just having a lock apply to it due to a deep lock held by a
648      * node above.
649      * @return a boolean
650      * @throws javax.jcr.RepositoryException if an error occurs
651      */

652     public boolean holdsLock() throws RepositoryException {
653         throw new AccessDeniedException("Lock not supported on version preview");
654     }
655
656     /**
657      * Returns true if this node is locked either as a result of a lock held by this node or by a deep lock on a node
658      * above this node; otherwise returns false.
659      * @return a boolean
660      * @throws javax.jcr.RepositoryException if an error occurs
661      */

662     public boolean isLocked() throws RepositoryException {
663         throw new AccessDeniedException("Lock not supported on version preview");
664     }
665
666     /**
667      * Set access manager for this object
668      * @param manager
669      */

670     public void setAccessManager(AccessManager manager) {
671         log.error("Not allowed to set access manager on Version preview");
672     }
673
674     /**
675      * Get access manager if previously set for this object
676      * @return AccessManager
677      */

678     public AccessManager getAccessManager() {
679         return this.base.getAccessManager();
680     }
681
682 }
683
Popular Tags