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.ContentItemNotFoundException;
17 import pt.digitalis.dif.utils.extensions.cms.exception.ContentManagerException;
18
19
20
21
22
23
24
25 public class ContentItem {
26
27
28 private List<ACLEntry> accessList;
29
30
31 private String content;
32
33
34 private IContentManager contentManager;
35
36
37 private Date creationDate;
38
39
40 private String creatorID;
41
42
43 private String description;
44
45
46 private String id;
47
48
49 private boolean isDeleted = false;
50
51
52 private String name;
53
54
55 private Long parentNodeId;
56
57
58 private IDIFUser user;
59
60
61
62
63
64
65
66 public ContentItem(IDIFUser user)
67 {
68 super();
69
70 this.creationDate = new Date();
71 this.user = user;
72 }
73
74
75
76
77
78
79
80
81
82
83
84 public ContentItem(Long parentNodeId, String name, String creatorID, IDIFUser user)
85 {
86 this.creatorID = creatorID;
87 this.name = name;
88 this.parentNodeId = parentNodeId;
89 this.creationDate = new Date();
90 this.user = user;
91 }
92
93
94
95
96
97
98
99
100 public boolean delete() throws IdentityManagerException, ContentManagerException
101 {
102 if (isDeleted)
103 throw new AlreadyDeletedException();
104
105 boolean ret = getContentManager().deleteContent(id, user);
106
107
108 if (ret)
109 {
110 isDeleted = true;
111 id = null;
112 }
113
114 return ret;
115 }
116
117
118
119
120
121
122
123 public List<ACLEntry> getACL() throws ContentManagerException
124 {
125 if (accessList == null)
126 {
127
128 accessList = getContentManager().getContentItemACL(id);
129 }
130
131 return accessList;
132 }
133
134
135
136
137
138
139 public String getContent()
140 {
141 return content;
142 }
143
144
145
146
147
148
149 private IContentManager getContentManager()
150 {
151 if (contentManager == null)
152 contentManager = DIFIoCRegistry.getRegistry().getImplementation(IContentManager.class);
153
154 return contentManager;
155 }
156
157
158
159
160
161
162 public Date getCreationDate()
163 {
164 return creationDate;
165 }
166
167
168
169
170
171
172 public String getCreatorID()
173 {
174 return creatorID;
175 }
176
177
178
179
180
181
182 public String getDescription()
183 {
184 return description;
185 }
186
187
188
189
190
191
192 public String getId()
193 {
194 return id;
195 }
196
197
198
199
200
201
202 public String getName()
203 {
204 return name;
205 }
206
207
208
209
210
211
212 public Node getParentNode() throws IdentityManagerException, ContentManagerException
213 {
214 if (isDeleted)
215 throw new AlreadyDeletedException();
216
217 return getContentManager().getNodeById(parentNodeId, user);
218 }
219
220
221
222
223
224
225 public Long getParentNodeId()
226 {
227 return parentNodeId;
228 }
229
230
231
232
233
234
235 public IDIFUser getUser()
236 {
237 return user;
238 }
239
240
241
242
243
244
245
246
247
248 public boolean grantAccessGroup(String groupId) throws ContentManagerException
249 {
250 ACLEntry entry = null;
251
252 if (isDeleted)
253 throw new AlreadyDeletedException();
254
255 boolean ret = getContentManager().grantContentAccessToGroup(id, groupId);
256
257 if (ret && accessList != null)
258 {
259 entry = new ACLEntry();
260 entry.setGroupID(groupId);
261 accessList.add(entry);
262 }
263
264 return ret;
265 }
266
267
268
269
270
271
272
273
274
275 public boolean grantAccessUser(IDIFUser user) throws ContentManagerException
276 {
277 ACLEntry entry = null;
278
279 if (isDeleted)
280 throw new AlreadyDeletedException();
281
282 boolean ret = getContentManager().grantContentAccessToUser(id, user);
283
284 if (ret && accessList != null)
285 {
286 entry = new ACLEntry();
287 entry.setUserID(user.getID());
288 accessList.add(entry);
289 }
290
291 return ret;
292 }
293
294
295
296
297
298
299 public boolean isDeleted()
300 {
301 return isDeleted;
302 }
303
304
305
306
307
308
309
310
311
312 public boolean revokeAccessGroup(String groupId) throws ContentManagerException
313 {
314 if (isDeleted)
315 throw new AlreadyDeletedException();
316
317 boolean ret = getContentManager().revokeContentAccessToGroup(id, groupId);
318
319 if (ret && accessList != null)
320 {
321 List<ACLEntry> newList = new ArrayList<ACLEntry>();
322 for (ACLEntry entry: accessList)
323 {
324 if (entry.isUserEntry() || !entry.getGroupID().equals(groupId))
325 newList.add(entry);
326 }
327 accessList = newList;
328 }
329
330 return ret;
331 }
332
333
334
335
336
337
338
339
340
341 public boolean revokeAccessUser(IDIFUser user) throws ContentManagerException
342 {
343 if (isDeleted)
344 throw new AlreadyDeletedException();
345
346 boolean ret = getContentManager().revokeContentAccessToUser(id, user);
347
348 if (ret && accessList != null)
349 {
350 List<ACLEntry> newList = new ArrayList<ACLEntry>();
351 for (ACLEntry entry: accessList)
352 {
353 if (entry.isGroupEntry() || !entry.getUserID().equals(user.getID()))
354 newList.add(entry);
355 }
356 accessList = newList;
357 }
358
359 return ret;
360 }
361
362
363
364
365
366
367
368 public void setContent(String content)
369 {
370 this.content = content;
371 }
372
373
374
375
376
377
378
379 public void setCreationDate(Date creationDate)
380 {
381 this.creationDate = creationDate;
382 }
383
384
385
386
387
388
389
390 public void setCreatorID(String creatorID)
391 {
392 this.creatorID = creatorID;
393 }
394
395
396
397
398
399
400
401 public void setDescription(String description)
402 {
403 this.description = description;
404 }
405
406
407
408
409
410
411
412 public void setId(String id)
413 {
414 this.id = id;
415 }
416
417
418
419
420
421
422
423 public void setName(String name)
424 {
425 this.name = name;
426 }
427
428
429
430
431
432
433
434 public void setUserId(IDIFUser user)
435 {
436 this.user = user;
437 }
438
439
440
441
442
443
444
445
446 public boolean update() throws IdentityManagerException, ContentManagerException
447 {
448 ContentItem contentItem = null;
449 if (isDeleted || id == null)
450 {
451 contentItem = getContentManager().addContent(this);
452 id = contentItem.getId();
453 }
454 else
455 {
456 try
457 {
458 contentItem = getContentManager().updateContent(this);
459 }
460 catch (ContentItemNotFoundException e)
461 {
462 contentItem = getContentManager().addContent(this);
463 id = contentItem.getId();
464 }
465 }
466 return true;
467 }
468
469 }