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