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.jcr.util;
35
36 import info.magnolia.context.Context;
37 import info.magnolia.context.MgnlContext;
38 import info.magnolia.context.SystemContext;
39
40 import java.util.Calendar;
41
42 import javax.jcr.Node;
43 import javax.jcr.RepositoryException;
44 import javax.jcr.version.Version;
45
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49
50
51
52 public class NodeTypes {
53
54 private static final Logger log = LoggerFactory.getLogger(NodeTypes.class);
55
56
57
58
59 public static final String MGNL_PREFIX = "mgnl:";
60
61
62
63
64 public static final String JCR_PREFIX = "jcr:";
65
66
67
68
69 public static final String REP_PREFIX = "rep:";
70
71
72
73
74 private static final String BY = "By";
75
76
77
78
79 public static class LastModified {
80 public static final String NAME = MGNL_PREFIX + "lastModified";
81 public static final String LAST_MODIFIED = NAME;
82 public static final String LAST_MODIFIED_BY = LAST_MODIFIED + BY;
83
84
85
86
87
88 public static Calendar getLastModified(Node node) throws RepositoryException {
89 return node.hasProperty(LAST_MODIFIED) ? node.getProperty(LAST_MODIFIED).getDate() : Created.getCreated(node);
90 }
91
92
93
94
95
96 public static String getLastModifiedBy(Node node) throws RepositoryException {
97 return node.hasProperty(LAST_MODIFIED_BY) ? node.getProperty(LAST_MODIFIED_BY).getString() : Created.getCreatedBy(node);
98 }
99
100
101
102
103
104
105
106 public static void update(Node node) throws RepositoryException {
107 update(node, getCurrentCalendar());
108 }
109
110
111
112
113
114
115
116 public static void update(Node node, Calendar lastModified) throws RepositoryException {
117 update(node, getCurrentUserName(), lastModified);
118 }
119
120
121
122
123
124
125
126 public static void update(Node node, String userName, Calendar lastModified) throws RepositoryException {
127 checkNodeType(node, LastModified.NAME, LAST_MODIFIED, LAST_MODIFIED_BY);
128 node.setProperty(LAST_MODIFIED, lastModified);
129 node.setProperty(LAST_MODIFIED_BY, userName);
130 }
131
132 public static void remove(Node node) throws RepositoryException {
133 if (node.hasProperty(LAST_MODIFIED)) {
134 node.getProperty(LAST_MODIFIED).remove();
135 }
136 if (node.hasProperty(LAST_MODIFIED_BY)) {
137 node.getProperty(LAST_MODIFIED_BY).remove();
138 }
139 }
140 }
141
142
143
144
145 public static class Activatable {
146 public static final String NAME = MGNL_PREFIX + "activatable";
147 public static final String LAST_ACTIVATED = MGNL_PREFIX + "lastActivated";
148 public static final String LAST_ACTIVATED_BY = LAST_ACTIVATED + BY;
149 public static final String LAST_ACTIVATED_VERSION = LAST_ACTIVATED + "Version";
150 public static final String LAST_ACTIVATED_VERSION_CREATED = LAST_ACTIVATED_VERSION + "Created";
151 public static final String ACTIVATION_STATUS = MGNL_PREFIX + "activationStatus";
152
153 public static final int ACTIVATION_STATUS_NOT_ACTIVATED = 0;
154
155 public static final int ACTIVATION_STATUS_MODIFIED = 1;
156
157 public static final int ACTIVATION_STATUS_ACTIVATED = 2;
158
159
160
161
162
163
164
165
166
167 public static int getActivationStatus(Node node) throws RepositoryException {
168
169 if (!isActivated(node)) {
170
171 return ACTIVATION_STATUS_NOT_ACTIVATED;
172 }
173
174 Calendar lastModified = LastModified.getLastModified(node);
175 Calendar lastActivatedVersionCreated = null;
176
177 if (!NodeUtil.isNodeType(node, Versionable.NAME)) {
178 Node versionableNode = NodeUtil.getNearestAncestorOfType(node, Versionable.NAME);
179 if (versionableNode != null) {
180 lastActivatedVersionCreated = getLastActivatedVersionCreated(versionableNode);
181 }
182 } else {
183 lastActivatedVersionCreated = getLastActivatedVersionCreated(node);
184 }
185
186 Calendar lastActivated = lastActivatedVersionCreated != null ? lastActivatedVersionCreated : getLastActivated(node);
187
188 if (lastModified != null && lastModified.after(lastActivated)) {
189
190 return ACTIVATION_STATUS_MODIFIED;
191 }
192
193
194 return ACTIVATION_STATUS_ACTIVATED;
195 }
196
197
198
199
200 public static boolean isActivated(Node node) throws RepositoryException {
201 return node.hasProperty(ACTIVATION_STATUS) && node.getProperty(ACTIVATION_STATUS).getBoolean();
202 }
203
204
205
206
207 public static Calendar getLastActivated(Node node) throws RepositoryException {
208 return node.hasProperty(LAST_ACTIVATED) ? node.getProperty(LAST_ACTIVATED).getDate() : null;
209 }
210
211
212
213
214 public static String getLastActivatedBy(Node node) throws RepositoryException {
215 return node.hasProperty(LAST_ACTIVATED_BY) ? node.getProperty(LAST_ACTIVATED_BY).getString() : null;
216 }
217
218
219
220
221 public static String getLastActivatedVersion(Node node) throws RepositoryException {
222 return node.hasProperty(LAST_ACTIVATED_VERSION) ? node.getProperty(LAST_ACTIVATED_VERSION).getString() : null;
223 }
224
225
226
227
228 public static Calendar getLastActivatedVersionCreated(Node node) throws RepositoryException {
229 return node.hasProperty(LAST_ACTIVATED_VERSION_CREATED) ? node.getProperty(LAST_ACTIVATED_VERSION_CREATED).getDate() : null;
230 }
231
232
233
234
235 public static void update(Node node, String userName, boolean isActivated) throws RepositoryException {
236 update(node, userName, isActivated, (Version) null);
237 }
238
239
240
241
242
243 @Deprecated
244 public static void update(Node node, String userName, boolean isActivated, String versionName) throws RepositoryException {
245 node.setProperty(LAST_ACTIVATED_VERSION, versionName);
246 update(node, userName, isActivated, (Version) null);
247 }
248
249
250
251
252 public static void update(Node node, String userName, boolean isActivated, Version version) throws RepositoryException {
253 checkNodeType(node, Activatable.NAME, LAST_ACTIVATED, LAST_ACTIVATED_BY, ACTIVATION_STATUS, LAST_ACTIVATED_VERSION);
254 node.setProperty(LAST_ACTIVATED, getCurrentCalendar());
255 node.setProperty(LAST_ACTIVATED_BY, userName);
256 node.setProperty(ACTIVATION_STATUS, isActivated);
257 if (version != null) {
258 node.setProperty(LAST_ACTIVATED_VERSION, version.getName());
259 node.setProperty(LAST_ACTIVATED_VERSION_CREATED, version.getCreated());
260 } else {
261
262
263 node.setProperty(LAST_ACTIVATED_VERSION, (String) null);
264 node.setProperty(LAST_ACTIVATED_VERSION_CREATED, (String) null);
265 }
266 }
267
268 public static void remove(Node node) throws RepositoryException {
269 if (node.hasProperty(ACTIVATION_STATUS)) {
270 node.getProperty(ACTIVATION_STATUS).remove();
271 }
272 if (node.hasProperty(LAST_ACTIVATED)) {
273 node.getProperty(LAST_ACTIVATED).remove();
274 }
275 if (node.hasProperty(LAST_ACTIVATED_BY)) {
276 node.getProperty(LAST_ACTIVATED_BY).remove();
277 }
278 if (node.hasProperty(LAST_ACTIVATED_VERSION)) {
279 node.getProperty(LAST_ACTIVATED_VERSION).remove();
280 }
281 if (node.hasProperty(LAST_ACTIVATED_VERSION_CREATED)) {
282 node.getProperty(LAST_ACTIVATED_VERSION_CREATED).remove();
283 }
284 }
285 }
286
287
288
289
290 public static class Created {
291 public static final String NAME = MGNL_PREFIX + "created";
292 public static final String CREATED = NAME;
293 public static final String CREATED_BY = CREATED + BY;
294
295
296
297
298 public static Calendar getCreated(Node node) throws RepositoryException {
299 return node.hasProperty(CREATED) ? node.getProperty(CREATED).getDate() : null;
300 }
301
302
303
304
305 public static String getCreatedBy(Node node) throws RepositoryException {
306 return node.hasProperty(CREATED_BY) ? node.getProperty(CREATED_BY).getString() : null;
307 }
308
309
310
311
312
313 public static void set(Node node) throws RepositoryException {
314 set(node, getCurrentUserName(), getCurrentCalendar());
315 }
316
317
318
319
320
321
322 public static void set(Node node, String userName, Calendar created) throws RepositoryException {
323 checkNodeType(node, NAME, CREATED, CREATED_BY);
324 node.setProperty(CREATED, created);
325 node.setProperty(CREATED_BY, userName);
326
327 LastModified.update(node, userName, created);
328 }
329 }
330
331
332
333
334 public static class Renderable {
335 public static final String NAME = MGNL_PREFIX + "renderable";
336 public static final String TEMPLATE = MGNL_PREFIX + "template";
337
338
339
340
341
342 public static String getTemplate(Node node) throws RepositoryException {
343 return node.hasProperty(TEMPLATE) ? node.getProperty(TEMPLATE).getString() : null;
344 }
345
346
347
348
349 public static void set(Node node, String template) throws RepositoryException {
350 checkNodeType(node, NAME, TEMPLATE);
351 node.setProperty(TEMPLATE, template);
352 }
353 }
354
355
356
357
358 public static class Deleted {
359 public static final String NAME = MGNL_PREFIX + "deleted";
360 public static final String DELETED = NAME;
361 public static final String DELETED_BY = DELETED + BY;
362 public static final String COMMENT = MGNL_PREFIX + "comment";
363
364
365
366
367 public static Calendar getDeleted(Node node) throws RepositoryException {
368 return node.hasProperty(DELETED) ? node.getProperty(DELETED).getDate() : null;
369 }
370
371
372
373
374 public static String getDeletedBy(Node node) throws RepositoryException {
375 return node.hasProperty(DELETED_BY) ? node.getProperty(DELETED_BY).getString() : null;
376 }
377
378
379
380
381 public static String getComment(Node node) throws RepositoryException {
382 return node.hasProperty(COMMENT) ? node.getProperty(COMMENT).getString() : null;
383 }
384
385 public static void set(Node node, String comment) throws RepositoryException {
386 checkNodeType(node, NAME, DELETED, DELETED_BY, COMMENT);
387 node.setProperty(DELETED, getCurrentCalendar());
388 node.setProperty(DELETED_BY, getCurrentUserName());
389 node.setProperty(COMMENT, comment);
390 }
391 }
392
393
394
395
396 public static class Versionable {
397 public static final String NAME = MGNL_PREFIX + "versionable";
398 public static final String COMMENT = Deleted.COMMENT;
399
400
401
402
403 public static String getComment(Node node) throws RepositoryException {
404 return node.hasProperty(COMMENT) ? node.getProperty(COMMENT).getString() : null;
405 }
406
407
408
409
410 public static void set(Node node, String comment) throws RepositoryException {
411 checkNodeType(node, NAME, COMMENT);
412 node.setProperty(COMMENT, comment);
413 }
414 }
415
416
417
418
419 public static class Folder {
420 public static final String NAME = MGNL_PREFIX + "folder";
421 }
422
423
424
425
426 public static class Resource {
427 public static final String NAME = MGNL_PREFIX + "resource";
428 }
429
430
431
432
433 public static class Content {
434 public static final String NAME = MGNL_PREFIX + "content";
435 }
436
437
438
439
440 public static class ContentNode {
441 public static final String NAME = MGNL_PREFIX + "contentNode";
442 }
443
444
445
446
447 public static class NodeData {
448 public static final String NAME = MGNL_PREFIX + "nodeData";
449 }
450
451
452
453
454 public static class Page {
455 public static final String NAME = MGNL_PREFIX + "page";
456 }
457
458
459
460
461 public static class Area {
462 public static final String NAME = MGNL_PREFIX + "area";
463 }
464
465
466
467
468 public static class Component {
469 public static final String NAME = MGNL_PREFIX + "component";
470 }
471
472
473
474
475 public static class User {
476 public static final String NAME = MGNL_PREFIX + "user";
477 }
478
479
480
481
482 public static class Role {
483 public static final String NAME = MGNL_PREFIX + "role";
484 }
485
486
487
488
489 public static class Group {
490 public static final String NAME = MGNL_PREFIX + "group";
491 }
492
493
494
495
496 public static class System {
497 public static final String NAME = MGNL_PREFIX + "reserve";
498 }
499
500
501
502
503
504
505 public static class MetaData {
506 public static final String NAME = MGNL_PREFIX + "metaData";
507 }
508
509
510
511
512 public static class HasVersion {
513 public static final String NAME = MGNL_PREFIX + "hasVersion";
514 }
515
516 protected static String getCurrentUserName() {
517 String userName = MgnlContext.getUser().getName();
518 if (MgnlContext.isSystemInstance()) {
519
520 Context ctx = ((SystemContext) MgnlContext.getInstance()).getOriginalContext();
521 if (ctx != null && ctx.getUser() != null && !userName.equals(ctx.getUser().getName())) {
522
523 return "System [" + ctx.getUser().getName() + "]";
524 }
525 }
526 return userName;
527 }
528
529 protected static Calendar getCurrentCalendar() {
530 return Calendar.getInstance();
531 }
532
533 public static void checkNodeType(Node node, String nodeType, String... propertyNames) throws RepositoryException {
534 if (!node.isNodeType(nodeType)) {
535 log.warn("Trying to set property/ies '{}' although the node '{}' with PrimaryType '{}' is not of type '{}'!", propertyNames, node.getPath(), node.getPrimaryNodeType().getName(), nodeType);
536 }
537 }
538 }