1
2
3
4
5
6 package pt.digitalis.dif.utils.extensions.cms;
7
8 import java.util.ArrayList;
9 import java.util.Date;
10 import java.util.List;
11
12 import pt.digitalis.dif.controller.security.objects.IDIFUser;
13 import pt.digitalis.dif.exception.security.IdentityManagerException;
14 import pt.digitalis.dif.ioc.DIFIoCRegistry;
15 import pt.digitalis.dif.utils.extensions.cms.exception.AlreadyDeletedException;
16 import pt.digitalis.dif.utils.extensions.cms.exception.ContentManagerException;
17 import pt.digitalis.dif.utils.extensions.cms.exception.NodeNotFoundException;
18 import pt.digitalis.dif.utils.extensions.cms.exception.NodeWithNodesException;
19
20
21
22
23
24
25
26 public class Node {
27
28
29 private List<ACLEntry> accessList;
30
31
32 private IContentManager contentManager;
33
34
35 private Date creationDate;
36
37
38 private String creatorID;
39
40
41 private String description;
42
43
44 private String fullPathName;
45
46
47 private Long id;
48
49
50 private boolean isDeleted = false;
51
52
53 private boolean isPublic;
54
55
56 private String name;
57
58
59 private Long parentNodeId;
60
61
62 private IDIFUser user;
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public Node(Long parentNodeId, String name, String creatorID, IDIFUser user)
77 {
78 this.creatorID = creatorID;
79 this.name = name;
80 this.parentNodeId = parentNodeId;
81 this.user = user;
82 this.creationDate = new Date();
83 }
84
85
86
87
88
89
90
91
92 public boolean delete() throws IdentityManagerException, ContentManagerException
93 {
94 if (isDeleted)
95 throw new AlreadyDeletedException();
96
97 boolean ret = getContentManager().deleteNode(id, user);
98
99
100 if (ret)
101 {
102 isDeleted = true;
103 id = null;
104 }
105
106 return ret;
107 }
108
109
110
111
112
113
114
115
116 public boolean deleteCascade() throws IdentityManagerException, ContentManagerException
117 {
118 if (isDeleted)
119 throw new AlreadyDeletedException();
120
121 boolean ret = false;
122
123 try
124 {
125 ret = getContentManager().deleteNode(id, user, true);
126 }
127 catch (NodeWithNodesException e)
128 {
129
130 }
131
132
133 id = null;
134
135 return ret;
136 }
137
138
139
140
141
142
143
144 public List<ACLEntry> getACL() throws ContentManagerException
145 {
146 if (accessList == null)
147 {
148 accessList = getContentManager().getNodeACL(id);
149 }
150
151 return accessList;
152 }
153
154
155
156
157
158
159 private IContentManager getContentManager()
160 {
161 if (contentManager == null)
162 contentManager = DIFIoCRegistry.getRegistry().getImplementation(IContentManager.class);
163
164 return contentManager;
165 }
166
167
168
169
170
171
172 public Date getCreationDate()
173 {
174 return creationDate;
175 }
176
177
178
179
180
181
182 public String getCreatorID()
183 {
184 return creatorID;
185 }
186
187
188
189
190
191
192 public String getDescription()
193 {
194 return description;
195 }
196
197
198
199
200
201
202 public String getFullPathName()
203 {
204 return fullPathName;
205 }
206
207
208
209
210
211
212 public Long getId()
213 {
214 return id;
215 }
216
217
218
219
220
221
222 public List<Node> getInnerNodes() throws IdentityManagerException, ContentManagerException
223 {
224 if (isDeleted)
225 throw new AlreadyDeletedException();
226
227 return getContentManager().getNodesByParentNode(id, user);
228 }
229
230
231
232
233
234
235 public List<ContentItem> getItems() throws IdentityManagerException, ContentManagerException
236 {
237 if (isDeleted)
238 throw new AlreadyDeletedException();
239
240 return getContentManager().getContentByParentNode(id, user);
241 }
242
243
244
245
246
247
248 public String getName()
249 {
250 return name;
251 }
252
253
254
255
256
257
258 public Node getParentNode() throws IdentityManagerException, ContentManagerException
259 {
260 if (isDeleted)
261 throw new AlreadyDeletedException();
262
263 return getContentManager().getNodeById(parentNodeId, user);
264 }
265
266
267
268
269
270
271 public Long getParentNodeId()
272 {
273 return parentNodeId;
274 }
275
276
277
278
279
280
281 public IDIFUser getUser()
282 {
283 return user;
284 }
285
286
287
288
289
290
291
292
293
294 public boolean grantAccessGroup(String groupId) throws ContentManagerException
295 {
296 ACLEntry entry = null;
297
298 if (isDeleted)
299 throw new AlreadyDeletedException();
300
301 boolean ret = getContentManager().grantNodeAccessToGroup(id, groupId);
302
303 if (ret && accessList != null)
304 {
305 entry = new ACLEntry();
306 entry.setGroupID(groupId);
307 accessList.add(entry);
308 }
309
310 return ret;
311 }
312
313
314
315
316
317
318
319
320
321 public boolean grantAccessUser(IDIFUser user) throws ContentManagerException
322 {
323 ACLEntry entry = null;
324
325 if (isDeleted)
326 throw new AlreadyDeletedException();
327
328 boolean ret = getContentManager().grantNodeAccessToUser(id, user);
329
330 if (ret && accessList != null)
331 {
332 entry = new ACLEntry();
333 entry.setUserID(user.getID());
334 accessList.add(entry);
335 }
336
337 return ret;
338 }
339
340
341
342
343
344
345 public boolean isDeleted()
346 {
347 return isDeleted;
348 }
349
350
351
352
353
354
355 public boolean isPublic()
356 {
357 return isPublic;
358 }
359
360
361
362
363
364
365
366
367
368 public boolean revokeAccessGroup(String groupId) throws ContentManagerException
369 {
370 if (isDeleted)
371 throw new AlreadyDeletedException();
372
373 boolean ret = getContentManager().revokeNodeAccessToGroup(id, groupId);
374
375 if (ret && accessList != null)
376 {
377 List<ACLEntry> newList = new ArrayList<ACLEntry>();
378 for (ACLEntry entry: accessList)
379 {
380 if (entry.isUserEntry() || !entry.getGroupID().equals(groupId))
381 newList.add(entry);
382 }
383 accessList = newList;
384 }
385
386 return ret;
387 }
388
389
390
391
392
393
394
395
396
397 public boolean revokeAccessUser(IDIFUser user) throws ContentManagerException
398 {
399 if (isDeleted)
400 throw new AlreadyDeletedException();
401
402 boolean ret = getContentManager().revokeNodeAccessToUser(id, user);
403
404 if (ret && accessList != null)
405 {
406 List<ACLEntry> newList = new ArrayList<ACLEntry>();
407 for (ACLEntry entry: accessList)
408 {
409 if (entry.isGroupEntry() || !entry.getUserID().equals(user.getID()))
410 newList.add(entry);
411 }
412 accessList = newList;
413 }
414
415 return ret;
416 }
417
418
419
420
421
422
423
424 public void setCreationDate(Date creationDate)
425 {
426 this.creationDate = creationDate;
427 }
428
429
430
431
432
433
434
435 public void setCreatorID(String creatorID)
436 {
437 this.creatorID = creatorID;
438 }
439
440
441
442
443
444
445
446 public void setDescription(String description)
447 {
448 this.description = description;
449 }
450
451
452
453
454
455
456
457 public void setFullPathName(String fullPathName)
458 {
459 this.fullPathName = fullPathName;
460 }
461
462
463
464
465
466
467
468 public void setId(Long id)
469 {
470 this.id = id;
471 }
472
473
474
475
476
477
478
479 public void setName(String name)
480 {
481 this.name = name;
482 }
483
484
485
486
487
488
489
490 public void setParentNodeId(long parentNodeId)
491 {
492 this.parentNodeId = parentNodeId;
493 }
494
495
496
497
498
499
500
501 public void setPublic(boolean isPublic)
502 {
503 this.isPublic = isPublic;
504 }
505
506
507
508
509
510
511
512 public void setUser(IDIFUser user)
513 {
514 this.user = user;
515 }
516
517
518
519
520
521
522
523
524 public boolean update() throws IdentityManagerException, ContentManagerException
525 {
526 Node node = null;
527 if (isDeleted || id == null)
528 {
529 node = getContentManager().addNode(this);
530 id = node.getId();
531 }
532 else
533 {
534 try
535 {
536 node = getContentManager().updateNode(this);
537 }
538 catch (NodeNotFoundException e)
539 {
540 node = getContentManager().addNode(this);
541 id = node.getId();
542 }
543 }
544 return true;
545 }
546
547 }