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