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 }
133
134
135
136
137 public static class Activatable {
138 public static final String NAME = MGNL_PREFIX + "activatable";
139 public static final String LAST_ACTIVATED = MGNL_PREFIX + "lastActivated";
140 public static final String LAST_ACTIVATED_BY = LAST_ACTIVATED + BY;
141 public static final String LAST_ACTIVATED_VERSION = LAST_ACTIVATED + "Version";
142 public static final String LAST_ACTIVATED_VERSION_CREATED = LAST_ACTIVATED_VERSION + "Created";
143 public static final String ACTIVATION_STATUS = MGNL_PREFIX + "activationStatus";
144
145 public static final int ACTIVATION_STATUS_NOT_ACTIVATED = 0;
146
147 public static final int ACTIVATION_STATUS_MODIFIED = 1;
148
149 public static final int ACTIVATION_STATUS_ACTIVATED = 2;
150
151
152
153
154
155
156
157
158
159 public static int getActivationStatus(Node node) throws RepositoryException {
160
161 if (!isActivated(node)) {
162
163 return ACTIVATION_STATUS_NOT_ACTIVATED;
164 }
165
166 Calendar lastModified = LastModified.getLastModified(node);
167 Calendar lastActivatedVersionCreated = null;
168
169 if (!NodeUtil.isNodeType(node, Versionable.NAME)) {
170 Node versionableNode = NodeUtil.getNearestAncestorOfType(node, Versionable.NAME);
171 if (versionableNode != null) {
172 lastActivatedVersionCreated = getLastActivatedVersionCreated(versionableNode);
173 }
174 } else {
175 lastActivatedVersionCreated = getLastActivatedVersionCreated(node);
176 }
177
178 Calendar lastActivated = lastActivatedVersionCreated != null ? lastActivatedVersionCreated : getLastActivated(node);
179
180 if (lastModified != null && lastModified.after(lastActivated)) {
181
182 return ACTIVATION_STATUS_MODIFIED;
183 }
184
185
186 return ACTIVATION_STATUS_ACTIVATED;
187 }
188
189
190
191
192 public static boolean isActivated(Node node) throws RepositoryException {
193 return node.hasProperty(ACTIVATION_STATUS) && node.getProperty(ACTIVATION_STATUS).getBoolean();
194 }
195
196
197
198
199 public static Calendar getLastActivated(Node node) throws RepositoryException {
200 return node.hasProperty(LAST_ACTIVATED) ? node.getProperty(LAST_ACTIVATED).getDate() : null;
201 }
202
203
204
205
206 public static String getLastActivatedBy(Node node) throws RepositoryException {
207 return node.hasProperty(LAST_ACTIVATED_BY) ? node.getProperty(LAST_ACTIVATED_BY).getString() : null;
208 }
209
210
211
212
213 public static String getLastActivatedVersion(Node node) throws RepositoryException {
214 return node.hasProperty(LAST_ACTIVATED_VERSION) ? node.getProperty(LAST_ACTIVATED_VERSION).getString() : null;
215 }
216
217
218
219
220 public static Calendar getLastActivatedVersionCreated(Node node) throws RepositoryException {
221 return node.hasProperty(LAST_ACTIVATED_VERSION_CREATED) ? node.getProperty(LAST_ACTIVATED_VERSION_CREATED).getDate() : null;
222 }
223
224
225
226
227 public static void update(Node node, String userName, boolean isActivated) throws RepositoryException {
228 update(node, userName, isActivated, (Version) null);
229 }
230
231
232
233
234
235 @Deprecated
236 public static void update(Node node, String userName, boolean isActivated, String versionName) throws RepositoryException {
237 node.setProperty(LAST_ACTIVATED_VERSION, versionName);
238 update(node, userName, isActivated, (Version) null);
239 }
240
241
242
243
244 public static void update(Node node, String userName, boolean isActivated, Version version) throws RepositoryException {
245 checkNodeType(node, Activatable.NAME, LAST_ACTIVATED, LAST_ACTIVATED_BY, ACTIVATION_STATUS, LAST_ACTIVATED_VERSION);
246 node.setProperty(LAST_ACTIVATED, getCurrentCalendar());
247 node.setProperty(LAST_ACTIVATED_BY, userName);
248 node.setProperty(ACTIVATION_STATUS, isActivated);
249 if (version != null) {
250 node.setProperty(LAST_ACTIVATED_VERSION, version.getName());
251 node.setProperty(LAST_ACTIVATED_VERSION_CREATED, version.getCreated());
252 }
253 }
254
255 }
256
257
258
259
260 public static class Created {
261 public static final String NAME = MGNL_PREFIX + "created";
262 public static final String CREATED = NAME;
263 public static final String CREATED_BY = CREATED + BY;
264
265
266
267
268 public static Calendar getCreated(Node node) throws RepositoryException {
269 return node.hasProperty(CREATED) ? node.getProperty(CREATED).getDate() : null;
270 }
271
272
273
274
275 public static String getCreatedBy(Node node) throws RepositoryException {
276 return node.hasProperty(CREATED_BY) ? node.getProperty(CREATED_BY).getString() : null;
277 }
278
279
280
281
282
283 public static void set(Node node) throws RepositoryException {
284 set(node, getCurrentUserName(), getCurrentCalendar());
285 }
286
287
288
289
290
291
292 public static void set(Node node, String userName, Calendar created) throws RepositoryException {
293 checkNodeType(node, NAME, CREATED, CREATED_BY);
294 node.setProperty(CREATED, created);
295 node.setProperty(CREATED_BY, userName);
296
297 LastModified.update(node, userName, created);
298 }
299 }
300
301
302
303
304 public static class Renderable {
305 public static final String NAME = MGNL_PREFIX + "renderable";
306 public static final String TEMPLATE = MGNL_PREFIX + "template";
307
308
309
310
311
312 public static String getTemplate(Node node) throws RepositoryException {
313 return node.hasProperty(TEMPLATE) ? node.getProperty(TEMPLATE).getString() : null;
314 }
315
316
317
318
319 public static void set(Node node, String template) throws RepositoryException {
320 checkNodeType(node, NAME, TEMPLATE);
321 node.setProperty(TEMPLATE, template);
322 }
323 }
324
325
326
327
328 public static class Deleted {
329 public static final String NAME = MGNL_PREFIX + "deleted";
330 public static final String DELETED = NAME;
331 public static final String DELETED_BY = DELETED + BY;
332 public static final String COMMENT = MGNL_PREFIX + "comment";
333
334
335
336
337 public static Calendar getDeleted(Node node) throws RepositoryException {
338 return node.hasProperty(DELETED) ? node.getProperty(DELETED).getDate() : null;
339 }
340
341
342
343
344 public static String getDeletedBy(Node node) throws RepositoryException {
345 return node.hasProperty(DELETED_BY) ? node.getProperty(DELETED_BY).getString() : null;
346 }
347
348
349
350
351 public static String getComment(Node node) throws RepositoryException {
352 return node.hasProperty(COMMENT) ? node.getProperty(COMMENT).getString() : null;
353 }
354
355 public static void set(Node node, String comment) throws RepositoryException {
356 checkNodeType(node, NAME, DELETED, DELETED_BY, COMMENT);
357 node.setProperty(DELETED, getCurrentCalendar());
358 node.setProperty(DELETED_BY, getCurrentUserName());
359 node.setProperty(COMMENT, comment);
360 }
361 }
362
363
364
365
366 public static class Versionable {
367 public static final String NAME = MGNL_PREFIX + "versionable";
368 public static final String COMMENT = Deleted.COMMENT;
369
370
371
372
373 public static String getComment(Node node) throws RepositoryException {
374 return node.hasProperty(COMMENT) ? node.getProperty(COMMENT).getString() : null;
375 }
376
377
378
379
380 public static void set(Node node, String comment) throws RepositoryException {
381 checkNodeType(node, NAME, COMMENT);
382 node.setProperty(COMMENT, comment);
383 }
384 }
385
386
387
388
389 public static class Folder {
390 public static final String NAME = MGNL_PREFIX + "folder";
391 }
392
393
394
395
396 public static class Resource {
397 public static final String NAME = MGNL_PREFIX + "resource";
398 }
399
400
401
402
403 public static class Content {
404 public static final String NAME = MGNL_PREFIX + "content";
405 }
406
407
408
409
410 public static class ContentNode {
411 public static final String NAME = MGNL_PREFIX + "contentNode";
412 }
413
414
415
416
417 public static class NodeData {
418 public static final String NAME = MGNL_PREFIX + "nodeData";
419 }
420
421
422
423
424 public static class Page {
425 public static final String NAME = MGNL_PREFIX + "page";
426 }
427
428
429
430
431 public static class Area {
432 public static final String NAME = MGNL_PREFIX + "area";
433 }
434
435
436
437
438 public static class Component {
439 public static final String NAME = MGNL_PREFIX + "component";
440 }
441
442
443
444
445 public static class User {
446 public static final String NAME = MGNL_PREFIX + "user";
447 }
448
449
450
451
452 public static class Role {
453 public static final String NAME = MGNL_PREFIX + "role";
454 }
455
456
457
458
459 public static class Group {
460 public static final String NAME = MGNL_PREFIX + "group";
461 }
462
463
464
465
466 public static class System {
467 public static final String NAME = MGNL_PREFIX + "reserve";
468 }
469
470
471
472
473
474
475 public static class MetaData {
476 public static final String NAME = MGNL_PREFIX + "metaData";
477 }
478
479
480
481
482 public static class HasVersion {
483 public static final String NAME = MGNL_PREFIX + "hasVersion";
484 }
485
486 protected static String getCurrentUserName() {
487 String userName = MgnlContext.getUser().getName();
488 if (MgnlContext.isSystemInstance()) {
489
490 Context ctx = ((SystemContext) MgnlContext.getInstance()).getOriginalContext();
491 if (ctx != null && ctx.getUser() != null && !userName.equals(ctx.getUser().getName())) {
492
493 return "System [" + ctx.getUser().getName() + "]";
494 }
495 }
496 return userName;
497 }
498
499 protected static Calendar getCurrentCalendar() {
500 return Calendar.getInstance();
501 }
502
503 public static void checkNodeType(Node node, String nodeType, String... propertyNames) throws RepositoryException {
504 if (!node.isNodeType(nodeType)) {
505 log.warn("Trying to set property/ies '{}' although the node '{}' with PrimaryType '{}' is not of type '{}'!", propertyNames, node.getPath(), node.getPrimaryNodeType().getName(), nodeType);
506 }
507 }
508 }