View Javadoc

1   /**
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      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          super();
69  
70          this.creationDate = new Date();
71          this.user = user;
72      }
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      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       * 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         if (isDeleted)
103             throw new AlreadyDeletedException();
104 
105         boolean ret = getContentManager().deleteContent(id, user);
106 
107         // object ID is set to null
108         if (ret)
109         {
110             isDeleted = true;
111             id = null;
112         }
113 
114         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         if (accessList == null)
126         {
127 
128             accessList = getContentManager().getContentItemACL(id);
129         }
130 
131         return accessList;
132     }
133 
134     /**
135      * Inspector for the 'content' attribute.
136      * 
137      * @return the content value
138      */
139     public String getContent()
140     {
141         return content;
142     }
143 
144     /**
145      * Lazy load for content manager
146      * 
147      * @return the content manager
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      * Inspector for the 'creationDate' attribute.
159      * 
160      * @return the creationDate value
161      */
162     public Date getCreationDate()
163     {
164         return creationDate;
165     }
166 
167     /**
168      * Inspector for the 'creatorID' attribute.
169      * 
170      * @return the creatorID value
171      */
172     public String getCreatorID()
173     {
174         return creatorID;
175     }
176 
177     /**
178      * Inspector for the 'description' attribute.
179      * 
180      * @return the description value
181      */
182     public String getDescription()
183     {
184         return description;
185     }
186 
187     /**
188      * Inspector for the 'id' attribute.
189      * 
190      * @return the id value
191      */
192     public String getId()
193     {
194         return id;
195     }
196 
197     /**
198      * Inspector for the 'name' attribute.
199      * 
200      * @return the name value
201      */
202     public String getName()
203     {
204         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         if (isDeleted)
215             throw new AlreadyDeletedException();
216 
217         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         return parentNodeId;
228     }
229 
230     /**
231      * Inspector for the 'userId' attribute.
232      * 
233      * @return the userId value
234      */
235     public IDIFUser getUser()
236     {
237         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         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      * 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         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      * Inspector for the 'isDeleted' attribute.
296      * 
297      * @return the isDeleted value
298      */
299     public boolean isDeleted()
300     {
301         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         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      * 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         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      * 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         this.content = content;
371     }
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         this.creationDate = creationDate;
382     }
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         this.creatorID = creatorID;
393     }
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         this.description = description;
404     }
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         this.id = id;
415     }
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         this.name = name;
426     }
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         this.user = user;
437     }
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         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 }