View Javadoc

1   /**
2    * 2010, 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.ContentManagerException;
17  import pt.digitalis.dif.utils.extensions.cms.exception.NodeNotFoundException;
18  import pt.digitalis.dif.utils.extensions.cms.exception.NodeWithNodesException;
19  
20  /**
21   * Content node definition class
22   * 
23   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
24   * @created 2010/10/06
25   */
26  public class Node {
27  
28      /** the node ACL */
29      private List<ACLEntry> accessList;
30  
31      /** The contentManager */
32      private IContentManager contentManager;
33  
34      /** The date when the document was created */
35      private Date creationDate;
36  
37      /** the creator user ID */
38      private String creatorID;
39  
40      /** The node description */
41      private String description;
42  
43      /** the full path name with all parent nodes */
44      private String fullPathName;
45  
46      /** the node unique identifier */
47      private Long id;
48  
49      /** True if the node object has already been deleted in the repository */
50      private boolean isDeleted = false;
51  
52      /**  */
53      private boolean isPublic;
54  
55      /** the node name. Unique within the node */
56      private String name;
57  
58      /** The parent node ID, if it is not a root node */
59      private Long parentNodeId;
60  
61      /** The current user */
62      private IDIFUser user;
63  
64      /**
65       * Constructor
66       * 
67       * @param parentNodeId
68       *            the parent node ID
69       * @param name
70       *            the node Name
71       * @param creatorID
72       *            the creator ID
73       * @param user
74       *            the user instantiating the Node
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       * Node is deleted in DB. Object ID property is set to null
87       * 
88       * @return T if update was succeeded
89       * @throws IdentityManagerException
90       * @throws ContentManagerException
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          // object ID is set to null
100         if (ret)
101         {
102             isDeleted = true;
103             id = null;
104         }
105 
106         return ret;
107     }
108 
109     /**
110      * Node is deleted in DB, as well has its child nodes. Object ID property is set to null
111      * 
112      * @return T if update was succeeded
113      * @throws IdentityManagerException
114      * @throws ContentManagerException
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             // never should happen, since it is a cascade delete...
130         }
131 
132         // object ID is set to null
133         id = null;
134 
135         return ret;
136     }
137 
138     /**
139      * Get ACL.
140      * 
141      * @return the accessList value
142      * @throws ContentManagerException
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      * Lazy load for content manager
156      * 
157      * @return the content manager
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      * Inspector for the 'creationDate' attribute.
169      * 
170      * @return the creationDate value
171      */
172     public Date getCreationDate()
173     {
174         return creationDate;
175     }
176 
177     /**
178      * Inspector for the 'creatorID' attribute.
179      * 
180      * @return the creatorID value
181      */
182     public String getCreatorID()
183     {
184         return creatorID;
185     }
186 
187     /**
188      * Inspector for the 'description' attribute.
189      * 
190      * @return the description value
191      */
192     public String getDescription()
193     {
194         return description;
195     }
196 
197     /**
198      * Inspector for the 'fullPathName' attribute.
199      * 
200      * @return the fullPathName value
201      */
202     public String getFullPathName()
203     {
204         return fullPathName;
205     }
206 
207     /**
208      * Inspector for the 'id' attribute.
209      * 
210      * @return the id value
211      */
212     public Long getId()
213     {
214         return id;
215     }
216 
217     /**
218      * @return the node inner nodes
219      * @throws IdentityManagerException
220      * @throws ContentManagerException
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      * @return the node content elements
232      * @throws IdentityManagerException
233      * @throws ContentManagerException
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      * Inspector for the 'name' attribute.
245      * 
246      * @return the name value
247      */
248     public String getName()
249     {
250         return name;
251     }
252 
253     /**
254      * @return the parent Node
255      * @throws IdentityManagerException
256      * @throws ContentManagerException
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      * Inspector for the 'parentNodeId' attribute.
268      * 
269      * @return the parentNodeId value
270      */
271     public Long getParentNodeId()
272     {
273         return parentNodeId;
274     }
275 
276     /**
277      * Inspector for the 'user' attribute.
278      * 
279      * @return the user value
280      */
281     public IDIFUser getUser()
282     {
283         return user;
284     }
285 
286     /**
287      * Grants Node access to group. Grant access is done immediately
288      * 
289      * @param groupId
290      *            the group to grant access
291      * @return T if grant was succeeded
292      * @throws ContentManagerException
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      * Grants Node access to user. Grant access is done immediately
315      * 
316      * @param user
317      *            the user to grant access
318      * @return T if grant was succeeded
319      * @throws ContentManagerException
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      * Inspector for the 'isDeleted' attribute.
342      * 
343      * @return the isDeleted value
344      */
345     public boolean isDeleted()
346     {
347         return isDeleted;
348     }
349 
350     /**
351      * Inspector for the 'isPublic' attribute.
352      * 
353      * @return the isPublic value
354      */
355     public boolean isPublic()
356     {
357         return isPublic;
358     }
359 
360     /**
361      * Revokes Node access to group. revoke access is done immediately
362      * 
363      * @param groupId
364      *            the group to revoke access
365      * @return T if revoke was succeeded
366      * @throws ContentManagerException
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      * Revokes Node access to group. revoke access is done immediately
391      * 
392      * @param user
393      *            the user to revoke access
394      * @return T if revoke was succeeded
395      * @throws ContentManagerException
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      * Modifier for the 'creationDate' attribute.
420      * 
421      * @param creationDate
422      *            the new creationDate value to set
423      */
424     public void setCreationDate(Date creationDate)
425     {
426         this.creationDate = creationDate;
427     }
428 
429     /**
430      * Modifier for the 'creatorID' attribute.
431      * 
432      * @param creatorID
433      *            the new creatorID value to set
434      */
435     public void setCreatorID(String creatorID)
436     {
437         this.creatorID = creatorID;
438     }
439 
440     /**
441      * Modifier for the 'description' attribute.
442      * 
443      * @param description
444      *            the new description value to set
445      */
446     public void setDescription(String description)
447     {
448         this.description = description;
449     }
450 
451     /**
452      * Modifier for the 'fullPathName' attribute.
453      * 
454      * @param fullPathName
455      *            the new fullPathName value to set
456      */
457     public void setFullPathName(String fullPathName)
458     {
459         this.fullPathName = fullPathName;
460     }
461 
462     /**
463      * Modifier for the 'id' attribute.
464      * 
465      * @param id
466      *            the new id value to set
467      */
468     public void setId(Long id)
469     {
470         this.id = id;
471     }
472 
473     /**
474      * Modifier for the 'name' attribute.
475      * 
476      * @param name
477      *            the new name value to set
478      */
479     public void setName(String name)
480     {
481         this.name = name;
482     }
483 
484     /**
485      * Modifier for the 'parentNodeId' attribute.
486      * 
487      * @param parentNodeId
488      *            the new parentNodeId value to set
489      */
490     public void setParentNodeId(long parentNodeId)
491     {
492         this.parentNodeId = parentNodeId;
493     }
494 
495     /**
496      * Modifier for the 'isPublic' attribute.
497      * 
498      * @param isPublic
499      *            the new isPublic value to set
500      */
501     public void setPublic(boolean isPublic)
502     {
503         this.isPublic = isPublic;
504     }
505 
506     /**
507      * Sets the 'user' attribute.
508      * 
509      * @param user
510      *            the user currently using the object
511      */
512     public void setUser(IDIFUser user)
513     {
514         this.user = user;
515     }
516 
517     /**
518      * Updates the Node with current object values. If the Node doesn't exist than it is inserted
519      * 
520      * @return T if update was succeeded
521      * @throws IdentityManagerException
522      * @throws ContentManagerException
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 }