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