1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.core;
35
36 import info.magnolia.cms.beans.config.ContentRepository;
37 import info.magnolia.cms.security.AccessDeniedException;
38 import info.magnolia.cms.security.AccessManager;
39 import info.magnolia.cms.security.Permission;
40 import org.apache.commons.lang.StringUtils;
41 import org.apache.commons.lang.builder.ToStringBuilder;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import javax.jcr.Node;
46 import javax.jcr.PathNotFoundException;
47 import javax.jcr.PropertyIterator;
48 import javax.jcr.RepositoryException;
49 import javax.jcr.Property;
50 import java.util.Calendar;
51 import java.util.GregorianCalendar;
52 import java.util.TimeZone;
53
54
55
56
57
58 public class MetaData {
59 private static final Logger log = LoggerFactory.getLogger(MetaData.class);
60
61
62
63
64
65 public static final String TITLE = "title";
66
67 public static final String CREATION_DATE = "creationdate";
68
69 public static final String LAST_MODIFIED = "lastmodified";
70
71 public static final String LAST_ACTION = "lastaction";
72
73 public static final String AUTHOR_ID = "authorid";
74
75 public static final String ACTIVATOR_ID = "activatorid";
76
77 public static final String TEMPLATE = "template";
78
79 public static final String TEMPLATE_TYPE = "templatetype";
80
81 public static final String ACTIVATED = "activated";
82
83 public static final String DEFAULT_META_NODE = "MetaData";
84
85 public static final int ACTIVATION_STATUS_NOT_ACTIVATED = 0;
86
87 public static final int ACTIVATION_STATUS_MODIFIED = 1;
88
89 public static final int ACTIVATION_STATUS_ACTIVATED = 2;
90
91
92
93
94 private Node node;
95
96 private AccessManager accessManager;
97
98
99
100
101
102 protected MetaData(Node workingNode, AccessManager manager) {
103 try {
104 this.node = workingNode.getNode(DEFAULT_META_NODE);
105 } catch (PathNotFoundException e) {
106 try {
107 log.debug("{} does not support MetaData, check node type definition of {}", workingNode.getPath(), workingNode.getPrimaryNodeType().getName());
108 } catch (RepositoryException re) {
109
110 }
111 } catch (RepositoryException re) {
112 log.error(re.getMessage(), re);
113 }
114 this.accessManager = manager;
115 }
116
117 protected MetaData() {
118 }
119
120 public String getHandle() throws RepositoryException {
121 return this.node.getPath();
122 }
123
124 private void allowUpdate() throws AccessDeniedException {
125
126 if (node == null) {
127 return;
128 }
129 try {
130 Access.isGranted(this.accessManager, Path.getAbsolutePath(this.node.getPath()), Permission.WRITE);
131 }
132 catch (RepositoryException re) {
133 log.error(re.getMessage(), re);
134 throw new AccessDeniedException(re.getMessage());
135 }
136 }
137
138
139
140
141
142
143 public PropertyIterator getProperties() {
144 if (node == null) {
145 return null;
146 }
147 try {
148 return this.node.getProperties();
149 }
150 catch (RepositoryException re) {
151 log.error(re.getMessage(), re);
152 }
153 return null;
154 }
155
156
157
158
159
160 public String getLabel() {
161 try {
162 return this.node.getName();
163 }
164 catch (NullPointerException e) {
165 if (log.isDebugEnabled()) {
166 log.debug("MetaData has not been created or this node does not support MetaData");
167 }
168 }
169 catch (RepositoryException e) {
170 log.error(e.getMessage(), e);
171 }
172 return StringUtils.EMPTY;
173 }
174
175
176
177
178
179
180 private String getInternalPropertyName(String name) {
181 if (StringUtils.indexOf(name, ContentRepository.NAMESPACE_PREFIX + ":") != 0) {
182 return ContentRepository.NAMESPACE_PREFIX + ":" + name;
183 }
184 return name;
185 }
186
187
188
189
190
191 public String getTitle() {
192 return getStringProperty(this.getInternalPropertyName(TITLE));
193 }
194
195
196
197
198
199 public void setTitle(String value) throws AccessDeniedException {
200 allowUpdate();
201 setProperty(this.getInternalPropertyName(TITLE), value);
202 }
203
204
205
206
207 public void setCreationDate() throws AccessDeniedException {
208 allowUpdate();
209 Calendar value = new GregorianCalendar(TimeZone.getDefault());
210 setProperty(this.getInternalPropertyName(CREATION_DATE), value);
211 }
212
213
214
215
216
217 public Calendar getCreationDate() {
218 return this.getDateProperty(this.getInternalPropertyName(CREATION_DATE));
219 }
220
221
222
223
224 public void setActivated() throws AccessDeniedException {
225 allowUpdate();
226 setProperty(this.getInternalPropertyName(ACTIVATED), true);
227 }
228
229
230
231
232 public void setUnActivated() throws AccessDeniedException {
233 allowUpdate();
234 setProperty(this.getInternalPropertyName(ACTIVATED), false);
235 }
236
237
238
239
240
241 public boolean getIsActivated() {
242 return getBooleanProperty(this.getInternalPropertyName(ACTIVATED));
243 }
244
245
246
247
248 public int getActivationStatus(){
249 if (getIsActivated()) {
250 if (getModificationDate() != null && getModificationDate().after(getLastActionDate())) {
251
252 return ACTIVATION_STATUS_MODIFIED;
253 }
254 else {
255
256 return ACTIVATION_STATUS_ACTIVATED;
257 }
258 }
259 else {
260
261 return ACTIVATION_STATUS_NOT_ACTIVATED;
262 }
263 }
264
265
266
267
268 public void setLastActivationActionDate() throws AccessDeniedException {
269 allowUpdate();
270 Calendar value = new GregorianCalendar(TimeZone.getDefault());
271 setProperty(this.getInternalPropertyName(LAST_ACTION), value);
272 }
273
274
275
276
277
278 public Calendar getLastActionDate() {
279 return getDateProperty(this.getInternalPropertyName(LAST_ACTION));
280 }
281
282
283
284
285 public void setModificationDate() throws AccessDeniedException {
286 allowUpdate();
287 Calendar value = new GregorianCalendar(TimeZone.getDefault());
288 setProperty(this.getInternalPropertyName(LAST_MODIFIED), value);
289 }
290
291
292
293
294
295 public Calendar getModificationDate() {
296 return getDateProperty(this.getInternalPropertyName(LAST_MODIFIED));
297 }
298
299
300
301
302
303 public String getAuthorId() {
304 return getStringProperty(this.getInternalPropertyName(AUTHOR_ID));
305 }
306
307
308
309
310
311 public void setAuthorId(String value) throws AccessDeniedException {
312 allowUpdate();
313 setProperty(this.getInternalPropertyName(AUTHOR_ID), value);
314 }
315
316
317
318
319
320 public String getActivatorId() {
321 return getStringProperty(this.getInternalPropertyName(ACTIVATOR_ID));
322 }
323
324
325
326
327
328 public void setActivatorId(String value) throws AccessDeniedException {
329 allowUpdate();
330 setProperty(this.getInternalPropertyName(ACTIVATOR_ID), value);
331 }
332
333
334
335
336
337 public String getTemplate() {
338 return getStringProperty(this.getInternalPropertyName(TEMPLATE));
339 }
340
341
342
343
344
345 public void setTemplate(String value) throws AccessDeniedException {
346 allowUpdate();
347 setProperty(this.getInternalPropertyName(TEMPLATE), value);
348 }
349
350
351
352
353
354
355 public void setTemplateType(String value) throws AccessDeniedException {
356 allowUpdate();
357 setProperty(this.getInternalPropertyName(TEMPLATE_TYPE), value);
358 }
359
360 public void setProperty(String name, String value) throws AccessDeniedException {
361 allowUpdate();
362 name = this.getInternalPropertyName(name);
363 try {
364 this.node.getProperty(name).setValue(value);
365 }
366 catch (PathNotFoundException e) {
367 try {
368 this.node.setProperty(name, value);
369 }
370 catch (RepositoryException re) {
371 log.error(re.getMessage(), re);
372 }
373 }
374 catch (RepositoryException re) {
375 throw new AccessDeniedException(re.getMessage(), re);
376 }
377 catch (NullPointerException e) {
378 log.debug("MetaData has not been created or this node does not support MetaData. Cannot set property {}", name);
379 }
380 }
381
382 public void setProperty(String name, long value) throws AccessDeniedException {
383 allowUpdate();
384 name = this.getInternalPropertyName(name);
385 try {
386 this.node.getProperty(name).setValue(value);
387 }
388 catch (PathNotFoundException e) {
389 try {
390 this.node.setProperty(name, value);
391 }
392 catch (RepositoryException re) {
393 log.error(re.getMessage(), re);
394 }
395 }
396 catch (RepositoryException re) {
397 log.error(re.getMessage(), re);
398 throw new AccessDeniedException(re.getMessage());
399 }
400 catch (NullPointerException e) {
401 if (log.isDebugEnabled()) {
402 log.debug("MetaData has not been created or this node does not support MetaData");
403 log.debug("cannot set property - " + name);
404 }
405 }
406 }
407
408 public void setProperty(String name, double value) throws AccessDeniedException {
409 allowUpdate();
410 name = this.getInternalPropertyName(name);
411 try {
412 this.node.getProperty(name).setValue(value);
413 }
414 catch (PathNotFoundException e) {
415 try {
416 this.node.setProperty(name, value);
417 }
418 catch (RepositoryException re) {
419 log.error(re.getMessage(), re);
420 }
421 }
422 catch (RepositoryException re) {
423 log.error(re.getMessage(), re);
424 throw new AccessDeniedException(re.getMessage());
425 }
426 catch (NullPointerException e) {
427 if (log.isDebugEnabled()) {
428 log.debug("MetaData has not been created or this node does not support MetaData");
429 log.debug("cannot set property - " + name);
430 }
431 }
432 }
433
434 public void setProperty(String name, boolean value) throws AccessDeniedException {
435 allowUpdate();
436 name = this.getInternalPropertyName(name);
437 try {
438 this.node.getProperty(name).setValue(value);
439 }
440 catch (PathNotFoundException e) {
441 try {
442 this.node.setProperty(name, value);
443 }
444 catch (RepositoryException re) {
445 log.error(re.getMessage(), re);
446 }
447 }
448 catch (RepositoryException re) {
449 log.error(re.getMessage(), re);
450 throw new AccessDeniedException(re.getMessage());
451 }
452 catch (NullPointerException e) {
453 if (log.isDebugEnabled()) {
454 log.debug("MetaData has not been created or this node does not support MetaData");
455 log.debug("cannot set property - " + name);
456 }
457 }
458 }
459
460 public void setProperty(String name, Calendar value) throws AccessDeniedException {
461 allowUpdate();
462 name = this.getInternalPropertyName(name);
463 try {
464 this.node.getProperty(name).setValue(value);
465 }
466 catch (PathNotFoundException e) {
467 try {
468 this.node.setProperty(name, value);
469 }
470 catch (RepositoryException re) {
471 log.error(re.getMessage(), re);
472 }
473 }
474 catch (RepositoryException re) {
475 log.error(re.getMessage(), re);
476 throw new AccessDeniedException(re.getMessage());
477 }
478 catch (NullPointerException e) {
479 log.debug("MetaData has not been created or this node does not support MetaData. Cannot set property {}", name);
480 }
481 }
482
483 public Calendar getDateProperty(String name) {
484 name = this.getInternalPropertyName(name);
485 try {
486 final Property property = this.node.getProperty(name);
487 return property.getDate();
488 }
489 catch (PathNotFoundException re) {
490 log.debug("PathNotFoundException for property [{}] in node {}", name, this.node);
491 }
492 catch (RepositoryException re) {
493 log.error(re.getMessage(), re);
494 }
495 catch (NullPointerException e) {
496 log.debug("MetaData has not been created or this node does not support MetaData. Cannot get property {}", name);
497 }
498 return null;
499 }
500
501 public boolean getBooleanProperty(String name) {
502 name = this.getInternalPropertyName(name);
503 try {
504 final Property property = this.node.getProperty(name);
505 return property.getBoolean();
506 }
507 catch (PathNotFoundException re) {
508 log.debug("PathNotFoundException for property [{}] in node {}", name, this.node);
509 }
510 catch (RepositoryException re) {
511 log.error(re.getMessage(), re);
512 }
513 catch (NullPointerException e) {
514 log.debug("MetaData has not been created or this node does not support MetaData. Cannot get property {}", name);
515 }
516 return false;
517 }
518
519 public double getDoubleProperty(String name) {
520 name = this.getInternalPropertyName(name);
521 try {
522 final Property property = this.node.getProperty(name);
523 return property.getDouble();
524 }
525 catch (PathNotFoundException re) {
526 log.debug("PathNotFoundException for property [{}] in node {}", name, this.node);
527 }
528 catch (RepositoryException re) {
529 log.error(re.getMessage(), re);
530 }
531 catch (NullPointerException e) {
532 log.debug("MetaData has not been created or this node does not support MetaData. Cannot get property {}", name);
533 }
534 return 0d;
535 }
536
537 public long getLongProperty(String name) {
538 name = this.getInternalPropertyName(name);
539 try {
540 final Property property = this.node.getProperty(name);
541 return property.getLong();
542 }
543 catch (PathNotFoundException re) {
544 log.debug("PathNotFoundException for property [{}] in node {}", name, this.node);
545 }
546 catch (RepositoryException re) {
547 log.error(re.getMessage(), re);
548 }
549 catch (NullPointerException e) {
550 log.debug("MetaData has not been created or this node does not support MetaData. Cannot get property {}", name);
551 }
552 return 0L;
553 }
554
555
556
557
558
559
560 public String getStringProperty(String name) {
561 name = this.getInternalPropertyName(name);
562 try {
563 final Property property = this.node.getProperty(name);
564 return property.getString();
565 }
566 catch (PathNotFoundException re) {
567 log.debug("PathNotFoundException for property [{}] in node {}", name, this.node);
568 }
569 catch (RepositoryException re) {
570 log.error(re.getMessage(), re);
571 }
572 catch (NullPointerException e) {
573 log.debug("MetaData has not been created or this node does not support MetaData. Cannot get property {}", name);
574 }
575 return StringUtils.EMPTY;
576 }
577
578
579
580
581
582
583
584 public void removeProperty(String name) throws PathNotFoundException, RepositoryException {
585 this.node.getProperty(this.getInternalPropertyName(name)).remove();
586 }
587
588
589
590
591
592
593
594
595 public boolean hasProperty(String name) {
596 try {
597 return this.node.hasProperty(this.getInternalPropertyName(name));
598 }
599 catch (RepositoryException re) {
600 log.error(re.getMessage(), re);
601 }
602 return false;
603 }
604
605 public String toString() {
606 return new ToStringBuilder(this).append("title", this.getTitle())
607 .append("template", this.getTemplate())
608 .append("authorId", this.getAuthorId())
609 .append("label", this.getLabel())
610 .append("activatorId", this.getActivatorId())
611 .append("isActivated", this.getIsActivated())
612 .append("creationDate", this.getCreationDate())
613 .append("lastActionDate", this.getLastActionDate())
614 .append("modificationDate", this.getModificationDate())
615 .toString();
616 }
617
618 }