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