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.security.AccessManager;
37 import info.magnolia.jcr.util.NodeTypes;
38 import info.magnolia.jcr.util.PropertyUtil;
39 import info.magnolia.repository.RepositoryConstants;
40
41 import java.util.Calendar;
42 import java.util.GregorianCalendar;
43 import java.util.Map;
44 import java.util.TimeZone;
45 import java.util.concurrent.ConcurrentHashMap;
46
47 import javax.jcr.Node;
48 import javax.jcr.PathNotFoundException;
49 import javax.jcr.Property;
50 import javax.jcr.RepositoryException;
51
52 import org.apache.commons.lang3.StringUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56
57
58
59
60
61
62
63
64
65 public class MetaData {
66 private static final Logger log = LoggerFactory.getLogger(MetaData.class);
67
68
69
70
71
72
73
74
75
76 public static final String TITLE = "title";
77
78
79
80
81 public static final String CREATION_DATE = "creationdate";
82
83
84
85
86 public static final String LAST_MODIFIED = "lastmodified";
87
88
89
90
91 public static final String LAST_ACTION = "lastaction";
92
93
94
95
96 public static final String AUTHOR_ID = "authorid";
97
98
99
100
101 public static final String ACTIVATOR_ID = "activatorid";
102
103
104
105
106
107
108 public static final String TEMPLATE = "template";
109
110
111
112
113 public static final String TEMPLATE_TYPE = "templatetype";
114
115
116
117
118 public static final String ACTIVATED = "activated";
119
120
121
122
123
124
125 public static final String DEFAULT_META_NODE = "MetaData";
126
127
128
129
130 public static final int ACTIVATION_STATUS_NOT_ACTIVATED = NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED;
131
132
133
134
135 public static final int ACTIVATION_STATUS_MODIFIED = NodeTypes.Activatable.ACTIVATION_STATUS_MODIFIED;
136
137
138
139
140 public static final int ACTIVATION_STATUS_ACTIVATED = NodeTypes.Activatable.ACTIVATION_STATUS_ACTIVATED;
141
142
143
144
145 private Node node;
146
147
148
149
150
151
152 protected MetaData(Node workingNode, AccessManager ignoredAccessManager) {
153 this(workingNode);
154 }
155
156
157
158
159 public MetaData(Node workingNode) {
160 this.node = workingNode;
161 }
162
163
164
165
166
167 private static Map<String, String> propertyMappings = new ConcurrentHashMap<String, String>();
168
169 static {
170 propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + CREATION_DATE, NodeTypes.Created.CREATED);
171 propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + LAST_MODIFIED, NodeTypes.LastModified.LAST_MODIFIED);
172 propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + LAST_ACTION, NodeTypes.Activatable.LAST_ACTIVATED);
173 propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + AUTHOR_ID, NodeTypes.LastModified.LAST_MODIFIED_BY);
174 propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + ACTIVATOR_ID, NodeTypes.Activatable.LAST_ACTIVATED_BY);
175 propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + TEMPLATE, NodeTypes.Renderable.TEMPLATE);
176 propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":" + ACTIVATED, NodeTypes.Activatable.ACTIVATION_STATUS);
177 propertyMappings.put(RepositoryConstants.NAMESPACE_PREFIX + ":comment", NodeTypes.Versionable.COMMENT);
178 }
179
180
181
182
183
184
185 private String getInternalPropertyName(String name) {
186 if (StringUtils.indexOf(name, ":") < 0) {
187 name = RepositoryConstants.NAMESPACE_PREFIX + ":" + name;
188 }
189
190 String newName = propertyMappings.get(name);
191
192 if (newName == null) {
193 throw new IllegalArgumentException("Unsupported meta data property: " + name);
194 }
195
196 return newName;
197 }
198
199
200
201
202
203 public String getTitle() {
204 return getStringProperty(TITLE);
205 }
206
207
208
209
210
211
212 public void setTitle(String value) {
213 setProperty(TITLE, value);
214 }
215
216
217
218
219
220
221 public void setCreationDate() {
222 Calendar value = new GregorianCalendar(TimeZone.getDefault());
223 setProperty(CREATION_DATE, value);
224 }
225
226
227
228
229
230
231 public Calendar getCreationDate() {
232 return this.getDateProperty(CREATION_DATE);
233 }
234
235
236
237
238
239
240 public void setActivated() {
241 setProperty(ACTIVATED, true);
242 }
243
244
245
246
247
248
249 public void setUnActivated() {
250 setProperty(ACTIVATED, false);
251 }
252
253
254
255
256
257
258 public boolean getIsActivated() {
259 return getBooleanProperty(ACTIVATED);
260 }
261
262
263
264
265
266
267 public int getActivationStatus() {
268 if (getIsActivated()) {
269 if (getModificationDate() != null && getModificationDate().after(getLastActionDate())) {
270
271 return ACTIVATION_STATUS_MODIFIED;
272 }
273
274 return ACTIVATION_STATUS_ACTIVATED;
275 }
276
277 return ACTIVATION_STATUS_NOT_ACTIVATED;
278 }
279
280
281
282
283
284
285 public void setLastActivationActionDate() {
286 Calendar value = new GregorianCalendar(TimeZone.getDefault());
287 setProperty(LAST_ACTION, value);
288 }
289
290
291
292
293
294
295 public Calendar getLastActionDate() {
296 return getDateProperty(LAST_ACTION);
297 }
298
299
300
301
302
303
304 public void setModificationDate() {
305 Calendar value = new GregorianCalendar(TimeZone.getDefault());
306 setProperty(LAST_MODIFIED, value);
307 }
308
309
310
311
312
313
314
315 public Calendar getModificationDate() {
316 Calendar modDate = getDateProperty(LAST_MODIFIED);
317 if (modDate == null) {
318 modDate = getCreationDate();
319 }
320 return modDate;
321 }
322
323
324
325
326
327
328 public String getAuthorId() {
329 return getStringProperty(AUTHOR_ID);
330 }
331
332
333
334
335
336
337 public void setAuthorId(String value) {
338 setProperty(AUTHOR_ID, value);
339 }
340
341
342
343
344
345
346 public String getActivatorId() {
347 return getStringProperty(ACTIVATOR_ID);
348 }
349
350
351
352
353
354
355 public void setActivatorId(String value) {
356 setProperty(ACTIVATOR_ID, value);
357 }
358
359
360
361
362
363
364 public String getTemplate() {
365 return getStringProperty(TEMPLATE);
366 }
367
368
369
370
371
372
373 public void setTemplate(String value) {
374 setProperty(TEMPLATE, value);
375 }
376
377 public void setProperty(String name, String value) {
378 setJCRProperty(name, value);
379 }
380
381 public void setProperty(String name, long value) {
382 setJCRProperty(name, value);
383 }
384
385 public void setProperty(String name, double value) {
386 setJCRProperty(name, value);
387 }
388
389 public void setProperty(String name, boolean value) {
390 setJCRProperty(name, value);
391 }
392
393 public void setProperty(String name, Calendar value) {
394 setJCRProperty(name, value);
395 }
396
397 private void setJCRProperty(String name, Object value) {
398 final String propName = this.getInternalPropertyName(name);
399 try {
400 PropertyUtil.setProperty(node, propName, value);
401 } catch (RepositoryException re) {
402 log.error(re.getMessage(), re);
403 }
404 }
405
406 public boolean getBooleanProperty(String name) {
407 try {
408 final Property property = getJCRProperty(name);
409 if (property != null) {
410 return property.getBoolean();
411 }
412 } catch (RepositoryException re) {
413 log.error(re.getMessage(), re);
414 }
415 return false;
416 }
417
418 public double getDoubleProperty(String name) {
419 try {
420 final Property property = getJCRProperty(name);
421 if (property != null) {
422 return property.getDouble();
423 }
424 } catch (RepositoryException re) {
425 log.error(re.getMessage(), re);
426 }
427 return 0d;
428 }
429
430 public long getLongProperty(String name) {
431 try {
432 final Property property = getJCRProperty(name);
433 if (property != null) {
434 return property.getLong();
435 }
436 } catch (RepositoryException re) {
437 log.error(re.getMessage(), re);
438 }
439 return 0L;
440 }
441
442 public String getStringProperty(String name) {
443 try {
444 final Property property = getJCRProperty(name);
445 if (property != null) {
446 return property.getString();
447 }
448 } catch (RepositoryException re) {
449 log.error(re.getMessage(), re);
450 }
451 return StringUtils.EMPTY;
452 }
453
454 public Calendar getDateProperty(String name) {
455 try {
456 final Property property = getJCRProperty(name);
457 if (property != null) {
458 return property.getDate();
459 }
460 } catch (RepositoryException re) {
461 log.error(re.getMessage(), re);
462 }
463 return null;
464 }
465
466
467
468
469
470
471
472
473 public void removeProperty(String name) throws PathNotFoundException, RepositoryException {
474 this.node.getProperty(this.getInternalPropertyName(name)).remove();
475 }
476
477 private Property getJCRProperty(String name) throws RepositoryException {
478 final String propName = this.getInternalPropertyName(name);
479 try {
480 return node.getProperty(propName);
481 } catch (PathNotFoundException re) {
482 log.debug("PathNotFoundException for property [{}] in node {}", propName, node);
483 }
484 return null;
485 }
486 }