KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.AccessDeniedException;
16 import info.magnolia.cms.security.AccessManager;
17 import info.magnolia.cms.security.Permission;
18
19 import java.io.InputStream JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Calendar JavaDoc;
22 import java.util.Collection JavaDoc;
23
24 import javax.jcr.Node;
25 import javax.jcr.PathNotFoundException;
26 import javax.jcr.Property;
27 import javax.jcr.PropertyIterator;
28 import javax.jcr.PropertyType;
29 import javax.jcr.RepositoryException;
30 import javax.jcr.Value;
31
32 import org.apache.commons.lang.StringUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37 /**
38  * Wrapper class for a jcr property.
39  * @author Sameer Charles
40  * @version 2.0 $Id: NodeData.java 6341 2006-09-12 09:18:27Z philipp $
41  */

42 public class NodeData extends ContentHandler {
43
44     /**
45      * Logger.
46      */

47     private static Logger log = LoggerFactory.getLogger(NodeData.class);
48
49     /**
50      * Wrapped javax.jcr.Property.
51      */

52     private Property property;
53
54     /**
55      * Wrapped javax.jcr.Node for nt:resource type
56      */

57     private Node node;
58
59     /**
60      * Empty constructor. Should NEVER be used for standard use, test only.
61      */

62     protected NodeData() {
63         // property is null
64
}
65
66     /**
67      * Constructor. Create nodeData object to work-on based on existing <code>Property</code> or
68      * <code>nt:resource</code>
69      * @param workingNode current active <code>Node</code>
70      * @param name <code>NodeData</code> name to be retrieved
71      * @param manager Access manager to be used for this object
72      */

73     protected NodeData(Node workingNode, String JavaDoc name, AccessManager manager)
74         throws PathNotFoundException,
75         RepositoryException,
76         AccessDeniedException {
77         Access.isGranted(manager, Path.getAbsolutePath(workingNode.getPath(), name), Permission.READ);
78         this.init(workingNode, name);
79         this.setAccessManager(manager);
80     }
81
82     /**
83      * Constructor. Creates a new initialized NodeData of given type
84      * @param workingNode current active <code>Node</code>
85      * @param name <code>NodeData</code> name to be created
86      * @param type
87      * @param createNew if true create a new Item
88      * @param manager Access manager to be used for this object
89      * @throws PathNotFoundException
90      * @throws RepositoryException
91      */

92     protected NodeData(Node workingNode, String JavaDoc name, int type, boolean createNew, AccessManager manager)
93         throws PathNotFoundException,
94         RepositoryException,
95         AccessDeniedException {
96         if (createNew) {
97             Access.isGranted(manager, Path.getAbsolutePath(workingNode.getPath(), name), Permission.WRITE);
98             this.init(workingNode, name, type, null);
99         }
100         else {
101             Access.isGranted(manager, Path.getAbsolutePath(workingNode.getPath(), name), Permission.READ);
102             this.init(workingNode, name);
103         }
104         this.setAccessManager(manager);
105     }
106
107     /**
108      * Constructor. Creates a new initialized NodeData
109      * @param workingNode current active <code>Node</code>
110      * @param name <code>NodeData</code> name to be created
111      * @param value Value to be set
112      * @throws PathNotFoundException
113      * @throws RepositoryException
114      */

115     protected NodeData(Node workingNode, String JavaDoc name, Value value, AccessManager manager)
116         throws PathNotFoundException,
117         RepositoryException,
118         AccessDeniedException {
119         Access.isGranted(manager, Path.getAbsolutePath(workingNode.getPath(), name), Permission.WRITE);
120         this.init(workingNode, name, value.getType(), value);
121         this.setAccessManager(manager);
122     }
123
124     /**
125      * Constructor. Creates a new initialized NodeData
126      * @param node <code>Node</code> of type nt:resource
127      */

128     public NodeData(Node node, AccessManager manager)
129         throws PathNotFoundException,
130         RepositoryException,
131         AccessDeniedException {
132         Access.isGranted(manager, Path.getAbsolutePath(node.getPath()), Permission.READ);
133         this.node = node;
134         this.property = this.node.getProperty(ItemType.JCR_DATA);
135         this.setAccessManager(manager);
136     }
137
138     /**
139      * Constructor. Creates a new initialized NodeData
140      * @param property
141      */

142     public NodeData(Property property, AccessManager manager)
143         throws PathNotFoundException,
144         RepositoryException,
145         AccessDeniedException {
146         this.property = property;
147         Access.isGranted(manager, Path.getAbsolutePath(this.property.getPath()), Permission.READ);
148         this.setAccessManager(manager);
149     }
150
151     /**
152      * create a new nt:resource node
153      * @param workingNode
154      * @param name
155      * @param type
156      * @param value
157      */

158     private void init(Node workingNode, String JavaDoc name, int type, Value value) throws PathNotFoundException,
159         RepositoryException, AccessDeniedException {
160         if (PropertyType.BINARY == type) {
161             this.node = workingNode.addNode(name, ItemType.NT_RESOURCE);
162             if (null != value) {
163                 this.property = this.node.setProperty(ItemType.JCR_DATA, value, value.getType());
164             }
165         }
166         else {
167             if (null == value) {
168                 this.property = workingNode.setProperty(name, StringUtils.EMPTY);
169             }
170             else {
171                 this.property = workingNode.setProperty(name, value, value.getType());
172             }
173         }
174     }
175
176     /**
177      * initialize this object based on existing property or nt:resource node
178      * @param workingNode
179      * @param name
180      * @throws RepositoryException
181      */

182     private void init(Node workingNode, String JavaDoc name) throws PathNotFoundException, RepositoryException,
183         AccessDeniedException {
184         try {
185             this.property = workingNode.getProperty(name);
186         }
187         catch (PathNotFoundException e) {
188             if (workingNode.hasNode(name)) {
189                 // this node data should wrap nt:resource
190
this.node = workingNode.getNode(name);
191                 this.property = this.node.getProperty(ItemType.JCR_DATA);
192             }
193             else {
194                 throw e;
195             }
196         }
197     }
198
199     /**
200      * Returns the <code>value</code> of this <code>NodeData</code>. One of type:
201      * <ul>
202      * <li><code>PropertyType.STRING</code></li>
203      * <li><code>PropertyType.DATE</code></li>
204      * <li><code>PropertyType.SOFTLINK</code></li>
205      * <li><code>PropertyType.BINARY</code></li>
206      * <li><code>PropertyType.DOUBLE</code></li>
207      * <li><code>PropertyType.LONG</code></li>
208      * <li><code>PropertyType.BOOLEAN</code></li>
209      * </ul>
210      * @return Value
211      */

212     public Value getValue() {
213         try {
214             return this.property.getValue();
215         }
216         catch (Exception JavaDoc e) {
217             if (log.isDebugEnabled()) {
218                 log.debug(e.getMessage(), e);
219             }
220             return null;
221         }
222     }
223
224     /**
225      * Returns the <code>String</code> representation of the value: decodes like breaks with the specified regular
226      * expression.
227      * @param lineBreak , regular expession
228      * @return String
229      */

230     public String JavaDoc getString(String JavaDoc lineBreak) {
231         try {
232             return this.getString().replaceAll("\n", lineBreak); //$NON-NLS-1$
233
}
234         catch (Exception JavaDoc e) {
235             return StringUtils.EMPTY;
236         }
237     }
238
239     /**
240      * Returns the <code>String</code> representation of the value.
241      * @return String
242      */

243     public String JavaDoc getString() {
244         try {
245             return this.property.getString();
246         }
247         catch (Exception JavaDoc e) {
248             return StringUtils.EMPTY;
249         }
250     }
251
252     /**
253      * Returns the <code>long</code> representation of the value:
254      * @return long
255      */

256     public long getLong() {
257         try {
258             return this.property.getLong();
259         }
260         catch (Exception JavaDoc e) {
261             return 0;
262         }
263     }
264
265     /**
266      * Returns the <code>double</code> representation of the value:
267      * @return double
268      */

269     public double getDouble() {
270         try {
271             return this.property.getDouble();
272         }
273         catch (Exception JavaDoc e) {
274             return 0;
275         }
276     }
277
278     /**
279      * Returns the <code>Calendar</code> representation of the value:
280      * @return Calendar
281      */

282     public Calendar JavaDoc getDate() {
283         try {
284             return this.property.getDate();
285         }
286         catch (Exception JavaDoc e) {
287             return null;
288         }
289     }
290
291     /**
292      * Returns the <code>boolean</code> representation of the value.
293      * @return boolean
294      */

295     public boolean getBoolean() {
296         try {
297             return this.property.getBoolean();
298         }
299         catch (Exception JavaDoc e) {
300             return false;
301         }
302     }
303
304     /**
305      * Returns the <code>InputStream</code> representation of the value:
306      * @return boolean
307      */

308     public InputStream JavaDoc getStream() {
309         try {
310             return this.property.getStream();
311         }
312         catch (Exception JavaDoc e) {
313             return null;
314         }
315     }
316
317     /**
318      * Returns the <code>type</code> of this <code>NodeData</code>. One of:
319      * <ul>
320      * <li><code>PropertyType.STRING</code></li>
321      * <li><code>PropertyType.DATE</code></li>
322      * <li><code>PropertyType.SOFTLINK</code></li>
323      * <li><code>PropertyType.BINARY</code></li>
324      * <li><code>PropertyType.DOUBLE</code></li>
325      * <li><code>PropertyType.LONG</code></li>
326      * <li><code>PropertyType.BOOLEAN</code></li>
327      * </ul>
328      * @return PropertyType
329      */

330     public int getType() {
331         if (this.property != null) {
332             try {
333                 return this.property.getType();
334             }
335             catch (Exception JavaDoc e) {
336                 log.warn("Unable to read property type for " + this.property); //$NON-NLS-1$
337
}
338         }
339         return PropertyType.UNDEFINED;
340     }
341
342     /**
343      * @return atom name
344      */

345     public String JavaDoc getName() {
346         try {
347             // check if its a nt:resource
348
if (null != this.node) {
349                 return this.node.getName();
350             }
351             return this.property.getName();
352         }
353         catch (Exception JavaDoc e) {
354             log.warn("Unable to read property name for " + this.property); //$NON-NLS-1$
355
return StringUtils.EMPTY;
356         }
357     }
358
359     /**
360      * returns size in bytes
361      * @return content length
362      */

363     public long getContentLength() {
364         try {
365             return this.property.getLength();
366         }
367         catch (RepositoryException re) {
368             log.warn("Unable to read content length for " + this.property); //$NON-NLS-1$
369
return 0;
370         }
371     }
372
373     /**
374      * Access to property at the JCR level. Available only to be available, should not be used in normal circumstances!
375      * @return Property
376      */

377     public Property getJCRProperty() {
378         return this.property;
379     }
380
381     /**
382      * set value of type <code>String</code>
383      * @param value , string to be set
384      * @throws RepositoryException
385      */

386     public void setValue(String JavaDoc value) throws RepositoryException, AccessDeniedException {
387         Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET);
388         this.property.setValue(value);
389     }
390
391     /**
392      * set value of type <code>int</code>
393      * @param value , int value to be set
394      * @throws RepositoryException
395      */

396     public void setValue(int value) throws RepositoryException, AccessDeniedException {
397         Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET);
398         this.property.setValue(value);
399     }
400
401     /**
402      * set value of type <code>long</code>
403      * @param value , long value to be set
404      * @throws RepositoryException
405      */

406     public void setValue(long value) throws RepositoryException, AccessDeniedException {
407         Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET);
408         this.property.setValue(value);
409     }
410
411     /**
412      * set value of type <code>InputStream</code>
413      * @param value , InputStream to be set
414      * @throws RepositoryException
415      */

416     public void setValue(InputStream JavaDoc value) throws RepositoryException, AccessDeniedException {
417         Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET);
418         if (this.node != null) {
419             this.property = this.node.setProperty(ItemType.JCR_DATA, value);
420         }
421         else {
422             log.error("This is not a valid Binary type, Binary NodeData must be created with PropertyType.BINARY");
423         }
424     }
425
426     /**
427      * set value of type <code>double</code>
428      * @param value , double value to be set
429      * @throws RepositoryException
430      */

431     public void setValue(double value) throws RepositoryException, AccessDeniedException {
432         Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET);
433         this.property.setValue(value);
434     }
435
436     /**
437      * set value of type <code>boolean</code>
438      * @param value , boolean value to be set
439      * @throws RepositoryException
440      */

441     public void setValue(boolean value) throws RepositoryException, AccessDeniedException {
442         Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET);
443         this.property.setValue(value);
444     }
445
446     /**
447      * set value of type <code>Calendar</code>
448      * @param value , Calendar value to be set
449      * @throws RepositoryException
450      */

451     public void setValue(Calendar JavaDoc value) throws RepositoryException, AccessDeniedException {
452         Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET);
453         this.property.setValue(value);
454     }
455
456     /**
457      * set value of type <code>Value</code>
458      * @param value
459      * @throws RepositoryException
460      */

461     public void setValue(Value value) throws RepositoryException, AccessDeniedException {
462         Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET);
463         this.property.setValue(value);
464     }
465
466     /**
467      * set attribute, available only if NodeData is of type <code>Binary</code>
468      * @param name
469      * @param value
470      * @throws RepositoryException
471      * @throws AccessDeniedException
472      * @throws UnsupportedOperationException if its not a Binary type
473      */

474     public void setAttribute(String JavaDoc name, String JavaDoc value) throws RepositoryException, AccessDeniedException,
475         UnsupportedOperationException JavaDoc {
476         Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET);
477         if (null == this.node) {
478             throw new UnsupportedOperationException JavaDoc("Attributes are only supported for BINARY type");
479         }
480         this.node.setProperty(name, value);
481     }
482
483     /**
484      * set attribute, available only if NodeData is of type <code>Binary</code>
485      * @param name
486      * @param value
487      * @throws RepositoryException
488      * @throws AccessDeniedException
489      * @throws UnsupportedOperationException if its not a Binary type
490      */

491     public void setAttribute(String JavaDoc name, Calendar JavaDoc value) throws RepositoryException, AccessDeniedException,
492         UnsupportedOperationException JavaDoc {
493         Access.isGranted(this.accessManager, Path.getAbsolutePath(this.getHandle()), Permission.SET);
494         if (null == this.node) {
495             throw new UnsupportedOperationException JavaDoc("Attributes are only supported for BINARY type");
496         }
497         this.node.setProperty(name, value);
498     }
499
500     /**
501      * get attribute, available only if NodeData is of type <code>Binary</code>
502      * @param name
503      * @return string value
504      */

505     public String JavaDoc getAttribute(String JavaDoc name) {
506         if (null == this.node) {
507             return "";
508         }
509         try {
510             return this.node.getProperty(name).getString();
511         }
512         catch (RepositoryException re) {
513             if (log.isDebugEnabled()) {
514                 log.debug("Attribute [ " + name + " ] not set");
515             }
516             return "";
517         }
518     }
519
520     /**
521      * get all attribute names
522      * @return collection of attrubute names
523      * @throws RepositoryException
524      */

525     public Collection JavaDoc getAttributeNames() throws RepositoryException {
526         Collection JavaDoc names = new ArrayList JavaDoc();
527         if (this.node == null) {
528             if (log.isDebugEnabled()) {
529                 log.debug("Attributes are only supported for BINARY type");
530             }
531             return names;
532         }
533         PropertyIterator properties = this.node.getProperties();
534         while (properties.hasNext()) {
535             String JavaDoc name = properties.nextProperty().getName();
536             if (!name.equalsIgnoreCase(ItemType.JCR_DATA)) {
537                 names.add(name);
538             }
539         }
540         return names;
541     }
542
543     /**
544      * checks if the atom exists in the repository
545      * @return boolean
546      */

547     public boolean isExist() {
548         return (this.property != null);
549     }
550
551     /**
552      * get a handle representing path relative to the content repository
553      * @return String representing path (handle) of the content
554      */

555     public String JavaDoc getHandle() {
556         try {
557             if (null != this.node) {
558                 return this.node.getPath();
559             }
560             return this.property.getPath();
561         }
562         catch (RepositoryException e) {
563             log.error("Failed to get handle"); //$NON-NLS-1$
564
log.error(e.getMessage(), e);
565             return StringUtils.EMPTY;
566         }
567     }
568
569     /**
570      * Persists all changes to the repository if valiation succeds
571      * @throws RepositoryException
572      */

573     public void save() throws RepositoryException {
574         this.property.getSession().save();
575     }
576
577     /**
578      * checks for the allowed access rights
579      * @param permissions as defined in javax.jcr.Permission
580      * @return true is the current user has specified access on this node.
581      */

582     public boolean isGranted(long permissions) {
583         try {
584             Access.isGranted(this.accessManager, Path.getAbsolutePath(property.getPath()), permissions);
585             return true;
586         }
587         catch (RepositoryException re) {
588             log.error(re.getMessage(), re);
589         }
590         return false;
591     }
592
593     /**
594      * Remove this path
595      * @throws RepositoryException
596      */

597     public void delete() throws RepositoryException {
598         Access.isGranted(this.accessManager, Path.getAbsolutePath(this.property.getPath()), Permission.REMOVE);
599         if (null != this.node) {
600             this.node.remove();
601         }
602         else {
603             this.property.remove();
604         }
605     }
606
607     /**
608      * Refreshes current node keeping all changes
609      * @throws RepositoryException
610      * @see javax.jcr.Node#refresh(boolean)
611      */

612     public void refresh(boolean keepChanges) throws RepositoryException {
613         this.property.refresh(keepChanges);
614     }
615
616 }
617
Popular Tags