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