KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.config.ContentRepository;
16 import info.magnolia.cms.security.AccessDeniedException;
17 import info.magnolia.cms.security.AccessManager;
18 import info.magnolia.cms.security.Permission;
19
20 import java.util.Calendar JavaDoc;
21 import java.util.GregorianCalendar JavaDoc;
22 import java.util.TimeZone JavaDoc;
23
24 import javax.jcr.Node;
25 import javax.jcr.PathNotFoundException;
26 import javax.jcr.PropertyIterator;
27 import javax.jcr.RepositoryException;
28
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.commons.lang.builder.ToStringBuilder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 /**
36  * $Id: MetaData.java 6341 2006-09-12 09:18:27Z philipp $
37  */

38 public class MetaData {
39
40     /**
41      * Top level atoms viewed as metadata of the specified content these must be set by the authoring system itself, but
42      * could be changed via custom templates if neccessary.
43      */

44     public static final String JavaDoc TITLE = "title"; //$NON-NLS-1$
45

46     public static final String JavaDoc CREATION_DATE = "creationdate"; //$NON-NLS-1$
47

48     public static final String JavaDoc LAST_MODIFIED = "lastmodified"; //$NON-NLS-1$
49

50     public static final String JavaDoc LAST_ACTION = "lastaction"; //$NON-NLS-1$
51

52     public static final String JavaDoc AUTHOR_ID = "authorid"; //$NON-NLS-1$
53

54     public static final String JavaDoc ACTIVATOR_ID = "activatorid"; //$NON-NLS-1$
55

56     public static final String JavaDoc TEMPLATE = "template"; //$NON-NLS-1$
57

58     public static final String JavaDoc TEMPLATE_TYPE = "templatetype"; //$NON-NLS-1$
59

60     public static final String JavaDoc ACTIVATED = "activated"; //$NON-NLS-1$
61

62     public static final String JavaDoc SEQUENCE_POS = "sequenceposition"; //$NON-NLS-1$
63

64     public static final String JavaDoc DEFAULT_META_NODE = "MetaData"; //$NON-NLS-1$
65

66     public static final long SEQUENCE_POS_COEFFICIENT = 1000;
67
68     /**
69      * Logger.
70      */

71     private static Logger log = LoggerFactory.getLogger(MetaData.class);
72
73     /**
74      * meta data node
75      */

76     private Node node;
77
78     private AccessManager accessManager;
79
80     /**
81      * Package private constructor
82      * @param workingNode current <code>Node</code> on which <code>MetaData</code> is requested
83      */

84     MetaData(Node workingNode, AccessManager manager) {
85         this.setMetaNode(workingNode, DEFAULT_META_NODE);
86         this.setAccessManager(manager);
87     }
88
89     public String JavaDoc getHandle() throws RepositoryException {
90         return this.node.getPath();
91     }
92
93     public void setAccessManager(AccessManager manager) {
94         this.accessManager = manager;
95     }
96
97     private void allowUpdate() throws AccessDeniedException {
98         // if node is null, MetaData has not been created and allowUpdate can abort silently
99
if (node == null) {
100             return;
101         }
102         try {
103             Access.isGranted(this.accessManager, Path.getAbsolutePath(this.node.getPath()), Permission.WRITE);
104         }
105         catch (RepositoryException re) {
106             log.error(re.getMessage(), re);
107             throw new AccessDeniedException(re.getMessage());
108         }
109     }
110
111     /**
112      * MetaData should be created by the repository implementation based on the node type definition
113      * @param workingNode
114      * @param name
115      */

116     private void setMetaNode(Node workingNode, String JavaDoc name) {
117         try {
118             this.node = workingNode.getNode(name);
119         }
120         catch (PathNotFoundException e) {
121             if (log.isDebugEnabled()) {
122                 try {
123                     log.debug(workingNode.getPath() + " does not support MetaData");
124                     log.debug("check node type definition of " + workingNode.getPrimaryNodeType().getName());
125                 }
126                 catch (RepositoryException re) {
127                     // should never come here
128
}
129             }
130         }
131         catch (RepositoryException re) {
132             log.error(re.getMessage(), re);
133         }
134     }
135
136     /**
137      * Get all meta data properties
138      * @return property iterator
139      */

140     public PropertyIterator getProperties() {
141         if (node == null) {
142             return null;
143         }
144         try {
145             return this.node.getProperties();
146         }
147         catch (RepositoryException re) {
148             log.error(re.getMessage(), re);
149         }
150         return null;
151     }
152
153     /**
154      * Part of metadata, same as name of actual storage node. This value is unique at the hierarchy level context.
155      * @return String value of the requested metadata
156      */

157     public String JavaDoc getLabel() {
158         try {
159             return this.node.getName();
160         }
161         catch (NullPointerException JavaDoc e) {
162             if (log.isDebugEnabled()) {
163                 log.debug("MetaData has not been created or this node does not support MetaData"); //$NON-NLS-1$
164
}
165         }
166         catch (RepositoryException e) {
167             log.error(e.getMessage(), e);
168         }
169         return StringUtils.EMPTY;
170     }
171
172     /**
173      * get property name with the prefix
174      * @param name
175      * @return name with namespace prefix
176      */

177     private String JavaDoc getInternalPropertyName(String JavaDoc name) {
178         if (StringUtils.indexOf(name, ContentRepository.NAMESPACE_PREFIX + ":") != 0) {
179             return ContentRepository.NAMESPACE_PREFIX + ":" + name;
180         }
181         return name;
182     }
183
184     /**
185      * Part of metadata , could be used as html header.
186      * @return String value of the requested metadata
187      */

188     public String JavaDoc getTitle() {
189         return getStringProperty(this.getInternalPropertyName(TITLE));
190     }
191
192     /**
193      * Part of metadata, could be used as html header.
194      * @param value
195      */

196     public void setTitle(String JavaDoc value) throws AccessDeniedException {
197         allowUpdate();
198         setProperty(this.getInternalPropertyName(TITLE), value);
199     }
200
201     /**
202      * Part of metadata, adds creation date of the current node
203      */

204     public void setCreationDate() throws AccessDeniedException {
205         allowUpdate();
206         Calendar JavaDoc value = new GregorianCalendar JavaDoc(TimeZone.getDefault());
207         setProperty(this.getInternalPropertyName(CREATION_DATE), value);
208     }
209
210     /**
211      * Part of metadata, get creation date of the current node.
212      * @return Calendar
213      */

214     public Calendar JavaDoc getCreationDate() {
215         return this.getDateProperty(this.getInternalPropertyName(CREATION_DATE));
216     }
217
218     /**
219      * Part of metadata, adds activated status of the current node
220      */

221     public void setActivated() throws AccessDeniedException {
222         allowUpdate();
223         setProperty(this.getInternalPropertyName(ACTIVATED), true);
224     }
225
226     /**
227      * Part of metadata, adds activated status of the current node.
228      */

229     public void setUnActivated() throws AccessDeniedException {
230         allowUpdate();
231         setProperty(this.getInternalPropertyName(ACTIVATED), false);
232     }
233
234     /**
235      * Part of metadata, get last activated status of the current node
236      * @return Calendar
237      */

238     public boolean getIsActivated() {
239         return getBooleanProperty(this.getInternalPropertyName(ACTIVATED));
240     }
241
242     /**
243      * Part of metadata, adds activated date of the current node
244      */

245     public void setLastActivationActionDate() throws AccessDeniedException {
246         allowUpdate();
247         Calendar JavaDoc value = new GregorianCalendar JavaDoc(TimeZone.getDefault());
248         setProperty(this.getInternalPropertyName(LAST_ACTION), value);
249     }
250
251     /**
252      * Part of metadata, get last activated/de- date of the current node
253      * @return Calendar
254      */

255     public Calendar JavaDoc getLastActionDate() {
256         return getDateProperty(this.getInternalPropertyName(LAST_ACTION));
257     }
258
259     /**
260      * Part of metadata, adds modification date of the current node
261      */

262     public void setModificationDate() throws AccessDeniedException {
263         allowUpdate();
264         Calendar JavaDoc value = new GregorianCalendar JavaDoc(TimeZone.getDefault());
265         setProperty(this.getInternalPropertyName(LAST_MODIFIED), value);
266     }
267
268     /**
269      * Part of metadata, get last modified date of the current node
270      * @return Calendar
271      */

272     public Calendar JavaDoc getModificationDate() {
273         return getDateProperty(this.getInternalPropertyName(LAST_MODIFIED));
274     }
275
276     /**
277      * Part of metadata, last known author of this node.
278      * @return String value of the requested metadata
279      */

280     public String JavaDoc getAuthorId() {
281         return getStringProperty(this.getInternalPropertyName(AUTHOR_ID));
282     }
283
284     /**
285      * Part of metadata, current logged-in author who did some action on this page.
286      * @param value
287      */

288     public void setAuthorId(String JavaDoc value) throws AccessDeniedException {
289         allowUpdate();
290         setProperty(this.getInternalPropertyName(AUTHOR_ID), value);
291     }
292
293     /**
294      * Part of metadata, last known activator of this node.
295      * @return String value of the requested metadata
296      */

297     public String JavaDoc getActivatorId() {
298         return getStringProperty(this.getInternalPropertyName(ACTIVATOR_ID));
299     }
300
301     /**
302      * Part of metadata, current logged-in author who last activated this page
303      * @param value
304      */

305     public void setActivatorId(String JavaDoc value) throws AccessDeniedException {
306         allowUpdate();
307         setProperty(this.getInternalPropertyName(ACTIVATOR_ID), value);
308     }
309
310     /**
311      * Part of metadata, template which will be used to render content of this node.
312      * @return String value of the requested metadata
313      */

314     public String JavaDoc getTemplate() {
315         return getStringProperty(this.getInternalPropertyName(TEMPLATE));
316     }
317
318     /**
319      * Part of metadata, template which will be used to render content of this node
320      * @param value
321      */

322     public void setTemplate(String JavaDoc value) throws AccessDeniedException {
323         allowUpdate();
324         setProperty(this.getInternalPropertyName(TEMPLATE), value);
325     }
326
327     /**
328      * Part of metadata, template type : JSP - Servlet - _xxx_
329      * @param value
330      */

331     public void setTemplateType(String JavaDoc value) throws AccessDeniedException {
332         allowUpdate();
333         setProperty(this.getInternalPropertyName(TEMPLATE_TYPE), value);
334     }
335
336     /**
337      * @param name
338      * @param value
339      */

340     public void setProperty(String JavaDoc name, String JavaDoc value) throws AccessDeniedException {
341         allowUpdate();
342         name = this.getInternalPropertyName(name);
343         try {
344             this.node.getProperty(name).setValue(value);
345         }
346         catch (PathNotFoundException e) {
347             try {
348                 this.node.setProperty(name, value);
349             }
350             catch (RepositoryException re) {
351                 log.error(re.getMessage(), re);
352             }
353         }
354         catch (RepositoryException re) {
355             throw new AccessDeniedException(re.getMessage(), re);
356         }
357         catch (NullPointerException JavaDoc e) {
358             if (log.isDebugEnabled()) {
359                 log.debug("MetaData has not been created or this node does not support MetaData"); //$NON-NLS-1$
360
log.debug("cannot set property - " + name); //$NON-NLS-1$
361
}
362         }
363     }
364
365     /**
366      * @param name
367      * @param value
368      */

369     public void setProperty(String JavaDoc name, long value) throws AccessDeniedException {
370         allowUpdate();
371         name = this.getInternalPropertyName(name);
372         try {
373             this.node.getProperty(name).setValue(value);
374         }
375         catch (PathNotFoundException e) {
376             try {
377                 this.node.setProperty(name, value);
378             }
379             catch (RepositoryException re) {
380                 log.error(re.getMessage(), re);
381             }
382         }
383         catch (RepositoryException re) {
384             log.error(re.getMessage(), re);
385             throw new AccessDeniedException(re.getMessage());
386         }
387         catch (NullPointerException JavaDoc e) {
388             if (log.isDebugEnabled()) {
389                 log.debug("MetaData has not been created or this node does not support MetaData"); //$NON-NLS-1$
390
log.debug("cannot set property - " + name); //$NON-NLS-1$
391
}
392         }
393     }
394
395     /**
396      * @param name
397      * @param value
398      */

399     public void setProperty(String JavaDoc name, double value) throws AccessDeniedException {
400         allowUpdate();
401         name = this.getInternalPropertyName(name);
402         try {
403             this.node.getProperty(name).setValue(value);
404         }
405         catch (PathNotFoundException e) {
406             try {
407                 this.node.setProperty(name, value);
408             }
409             catch (RepositoryException re) {
410                 log.error(re.getMessage(), re);
411             }
412         }
413         catch (RepositoryException re) {
414             log.error(re.getMessage(), re);
415             throw new AccessDeniedException(re.getMessage());
416         }
417         catch (NullPointerException JavaDoc e) {
418             if (log.isDebugEnabled()) {
419                 log.debug("MetaData has not been created or this node does not support MetaData"); //$NON-NLS-1$
420
log.debug("cannot set property - " + name); //$NON-NLS-1$
421
}
422         }
423     }
424
425     /**
426      * @param name
427      * @param value
428      */

429     public void setProperty(String JavaDoc name, boolean value) throws AccessDeniedException {
430         allowUpdate();
431         name = this.getInternalPropertyName(name);
432         try {
433             this.node.getProperty(name).setValue(value);
434         }
435         catch (PathNotFoundException e) {
436             try {
437                 this.node.setProperty(name, value);
438             }
439             catch (RepositoryException re) {
440                 log.error(re.getMessage(), re);
441             }
442         }
443         catch (RepositoryException re) {
444             log.error(re.getMessage(), re);
445             throw new AccessDeniedException(re.getMessage());
446         }
447         catch (NullPointerException JavaDoc e) {
448             if (log.isDebugEnabled()) {
449                 log.debug("MetaData has not been created or this node does not support MetaData"); //$NON-NLS-1$
450
log.debug("cannot set property - " + name); //$NON-NLS-1$
451
}
452         }
453     }
454
455     /**
456      * @param name
457      * @param value
458      */

459     public void setProperty(String JavaDoc name, Calendar JavaDoc value) throws AccessDeniedException {
460         allowUpdate();
461         name = this.getInternalPropertyName(name);
462         try {
463             this.node.getProperty(name).setValue(value);
464         }
465         catch (PathNotFoundException e) {
466             try {
467                 this.node.setProperty(name, value);
468             }
469             catch (RepositoryException re) {
470                 log.error(re.getMessage(), re);
471             }
472         }
473         catch (RepositoryException re) {
474             log.error(re.getMessage(), re);
475             throw new AccessDeniedException(re.getMessage());
476         }
477         catch (NullPointerException JavaDoc e) {
478             if (log.isDebugEnabled()) {
479                 log.debug("MetaData has not been created or this node does not support MetaData"); //$NON-NLS-1$
480
log.debug("cannot set property - " + name); //$NON-NLS-1$
481
}
482         }
483     }
484
485     /**
486      * @param name
487      */

488     public Calendar JavaDoc getDateProperty(String JavaDoc name) {
489         name = this.getInternalPropertyName(name);
490         try {
491             return this.node.getProperty(name).getDate();
492         }
493         catch (PathNotFoundException re) {
494             if (log.isDebugEnabled()) {
495                 log.debug("PathNotFoundException for property [" + name + "] in node " + this.node); //$NON-NLS-1$ //$NON-NLS-2$
496
}
497         }
498         catch (RepositoryException re) {
499             log.error(re.getMessage(), re);
500         }
501         catch (NullPointerException JavaDoc e) {
502             if (log.isDebugEnabled()) {
503                 log.debug("MetaData has not been created or this node does not support MetaData"); //$NON-NLS-1$
504
log.debug("cannot get property - " + name); //$NON-NLS-1$
505
}
506         }
507         return null;
508     }
509
510     /**
511      * @param name
512      */

513     public boolean getBooleanProperty(String JavaDoc name) {
514         name = this.getInternalPropertyName(name);
515         try {
516             return this.node.getProperty(name).getBoolean();
517         }
518         catch (PathNotFoundException re) {
519             if (log.isDebugEnabled()) {
520                 log.debug("PathNotFoundException for property [" + name + "] in node " + this.node); //$NON-NLS-1$ //$NON-NLS-2$
521
}
522         }
523         catch (RepositoryException re) {
524             log.error(re.getMessage(), re);
525         }
526         catch (NullPointerException JavaDoc e) {
527             if (log.isDebugEnabled()) {
528                 log.debug("MetaData has not been created or this node does not support MetaData"); //$NON-NLS-1$
529
log.debug("cannot get property - " + name); //$NON-NLS-1$
530
}
531         }
532         return false;
533     }
534
535     /**
536      * @param name
537      */

538     public double getDoubleProperty(String JavaDoc name) {
539         name = this.getInternalPropertyName(name);
540         try {
541             return this.node.getProperty(name).getDouble();
542         }
543         catch (PathNotFoundException re) {
544             if (log.isDebugEnabled()) {
545                 log.debug("PathNotFoundException for property [" + name + "] in node " + this.node); //$NON-NLS-1$ //$NON-NLS-2$
546
}
547         }
548         catch (RepositoryException re) {
549             log.error(re.getMessage(), re);
550         }
551         catch (NullPointerException JavaDoc e) {
552             if (log.isDebugEnabled()) {
553                 log.debug("MetaData has not been created or this node does not support MetaData"); //$NON-NLS-1$
554
log.debug("cannot get property - " + name); //$NON-NLS-1$
555
}
556         }
557         return 0d;
558     }
559
560     /**
561      * @param name
562      */

563     public long getLongProperty(String JavaDoc name) {
564         name = this.getInternalPropertyName(name);
565         try {
566             return this.node.getProperty(name).getLong();
567         }
568         catch (PathNotFoundException re) {
569             if (log.isDebugEnabled()) {
570                 log.debug("PathNotFoundException for property [" + name + "] in node " + this.node); //$NON-NLS-1$ //$NON-NLS-2$
571
}
572         }
573         catch (RepositoryException re) {
574             log.error(re.getMessage(), re);
575         }
576         catch (NullPointerException JavaDoc e) {
577             if (log.isDebugEnabled()) {
578                 log.debug("MetaData has not been created or this node does not support MetaData"); //$NON-NLS-1$
579
log.debug("cannot get property - " + name); //$NON-NLS-1$
580
}
581         }
582         return 0L;
583     }
584
585     /**
586      * Returns a String property. If the property does not exist, this will return an empty String.
587      * @param name
588      * @return the property value, never null
589      */

590     public String JavaDoc getStringProperty(String JavaDoc name) {
591         name = this.getInternalPropertyName(name);
592         try {
593             return this.node.getProperty(name).getString();
594         }
595         catch (PathNotFoundException re) {
596             if (log.isDebugEnabled()) {
597                 log.debug("PathNotFoundException for property [" + name + "] in node " + this.node); //$NON-NLS-1$ //$NON-NLS-2$
598
}
599         }
600         catch (RepositoryException re) {
601             log.error(re.getMessage(), re);
602         }
603         catch (NullPointerException JavaDoc e) {
604             if (log.isDebugEnabled()) {
605                 log.debug("MetaData has not been created or this node does not support MetaData"); //$NON-NLS-1$
606
log.debug("cannot get property - " + name); //$NON-NLS-1$
607
}
608         }
609         return StringUtils.EMPTY;
610     }
611
612     /**
613      * remove specified property
614      * @param name of the property to be removed
615      * @throws PathNotFoundException if property does not exist
616      * @throws RepositoryException if unable to remove
617      */

618     public void removeProperty(String JavaDoc name) throws PathNotFoundException, RepositoryException {
619         this.node.getProperty(this.getInternalPropertyName(name)).remove();
620     }
621
622     /**
623      * check if property exist
624      * @param name
625      * @return true if the specified property exist
626      */

627     public boolean hasProperty(String JavaDoc name) {
628         try {
629             return this.node.hasProperty(this.getInternalPropertyName(name));
630         }
631         catch (RepositoryException re) {
632             log.error(re.getMessage(), re);
633         }
634         return false;
635     }
636
637     /**
638      * @see java.lang.Object#toString()
639      */

640     public String JavaDoc toString() {
641         return new ToStringBuilder(this).append("title", this.getTitle()) //$NON-NLS-1$
642
.append("template", this.getTemplate()) //$NON-NLS-1$
643
.append("authorId", this.getAuthorId()) //$NON-NLS-1$
644
.append("label", this.getLabel()) //$NON-NLS-1$
645
.append("activatorId", this.getActivatorId()) //$NON-NLS-1$
646
.append("isActivated", this.getIsActivated()) //$NON-NLS-1$
647
.append("creationDate", this.getCreationDate()) //$NON-NLS-1$
648
.append("lastActionDate", this.getLastActionDate()) //$NON-NLS-1$
649
.append("modificationDate", this.getModificationDate()) //$NON-NLS-1$
650
.toString();
651     }
652
653 }
654
Popular Tags