KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > repository > Node


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.bean.repository;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.faces.context.FacesContext;
27
28 import org.alfresco.service.ServiceRegistry;
29 import org.alfresco.service.cmr.repository.AssociationRef;
30 import org.alfresco.service.cmr.repository.ChildAssociationRef;
31 import org.alfresco.service.cmr.repository.NodeRef;
32 import org.alfresco.service.cmr.security.AccessStatus;
33 import org.alfresco.service.cmr.security.PermissionService;
34 import org.alfresco.service.namespace.QName;
35 import org.alfresco.service.namespace.RegexQNamePattern;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 /**
40  * Lighweight client side representation of a node held in the repository.
41  *
42  * @author gavinc
43  */

44 public class Node implements Serializable JavaDoc
45 {
46    private static final long serialVersionUID = 3544390322739034169L;
47
48    protected static Log logger = LogFactory.getLog(Node.class);
49    
50    protected NodeRef nodeRef;
51    private String JavaDoc name;
52    private QName type;
53    private String JavaDoc path;
54    private String JavaDoc id;
55    private Set JavaDoc<QName> aspects = null;
56    private Map JavaDoc<String JavaDoc, Boolean JavaDoc> permissions;
57    protected QNameNodeMap<String JavaDoc, Object JavaDoc> properties;
58    protected boolean propsRetrieved = false;
59    protected ServiceRegistry services = null;
60    
61    private boolean childAssocsRetrieved = false;
62    private QNameNodeMap childAssociations;
63    private Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, ChildAssociationRef>> childAssociationsAdded;
64    private Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, ChildAssociationRef>> childAssociationsRemoved;
65    
66    private boolean assocsRetrieved = false;
67    private QNameNodeMap associations;
68    private Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AssociationRef>> associationsAdded;
69    private Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AssociationRef>> associationsRemoved;
70    
71    /**
72     * Constructor
73     *
74     * @param nodeRef The NodeRef this Node wrapper represents
75     */

76    public Node(NodeRef nodeRef)
77    {
78       if (nodeRef == null)
79       {
80          throw new IllegalArgumentException JavaDoc("NodeRef must be supplied for creation of a Node.");
81       }
82       
83       this.nodeRef = nodeRef;
84       this.id = nodeRef.getId();
85       
86       this.properties = new QNameNodeMap<String JavaDoc, Object JavaDoc>(getServiceRegistry().getNamespaceService(), this);
87    }
88
89    /**
90     * @return All the properties known about this node.
91     */

92    public Map JavaDoc<String JavaDoc, Object JavaDoc> getProperties()
93    {
94       if (this.propsRetrieved == false)
95       {
96          Map JavaDoc<QName, Serializable JavaDoc> props = getServiceRegistry().getNodeService().getProperties(this.nodeRef);
97          
98          for (QName qname: props.keySet())
99          {
100             Serializable JavaDoc propValue = props.get(qname);
101             this.properties.put(qname.toString(), propValue);
102          }
103          
104          this.propsRetrieved = true;
105       }
106       
107       return this.properties;
108    }
109    
110    /**
111     * @return All the associations this node has as a Map, using the association
112     * type as the key
113     */

114    public final Map JavaDoc getAssociations()
115    {
116       if (this.assocsRetrieved == false)
117       {
118          associations = new QNameNodeMap(getServiceRegistry().getNamespaceService(), this);
119          
120          List JavaDoc<AssociationRef> assocs = getServiceRegistry().getNodeService().getTargetAssocs(this.nodeRef, RegexQNamePattern.MATCH_ALL);
121          
122          for (AssociationRef assocRef: assocs)
123          {
124             String JavaDoc assocName = assocRef.getTypeQName().toString();
125             
126             List JavaDoc list = (List JavaDoc)this.associations.get(assocName);
127             // create the list if this is first association with 'assocName'
128
if (list == null)
129             {
130                list = new ArrayList JavaDoc<AssociationRef>();
131                this.associations.put(assocName, list);
132             }
133             
134             // add the association to the list
135
list.add(assocRef);
136          }
137          
138          this.assocsRetrieved = true;
139       }
140       
141       return this.associations;
142    }
143    
144    /**
145     * Returns all the associations added to this node in this UI session
146     *
147     * @return Map of Maps of AssociationRefs
148     */

149    public final Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AssociationRef>> getAddedAssociations()
150    {
151       if (this.associationsAdded == null)
152       {
153          this.associationsAdded = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AssociationRef>>();
154       }
155       return this.associationsAdded;
156    }
157    
158    /**
159     * Returns all the associations removed from this node is this UI session
160     *
161     * @return Map of Maps of AssociationRefs
162     */

163    public final Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AssociationRef>> getRemovedAssociations()
164    {
165       if (this.associationsRemoved == null)
166       {
167          this.associationsRemoved = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AssociationRef>>();
168       }
169       return this.associationsRemoved;
170    }
171    
172    /**
173     * @return All the child associations this node has as a Map, using the association
174     * type as the key
175     */

176    public final Map JavaDoc getChildAssociations()
177    {
178       if (this.childAssocsRetrieved == false)
179       {
180          this.childAssociations = new QNameNodeMap(getServiceRegistry().getNamespaceService(), this);
181          
182          List JavaDoc<ChildAssociationRef> assocs = getServiceRegistry().getNodeService().getChildAssocs(this.nodeRef);
183          
184          for (ChildAssociationRef assocRef: assocs)
185          {
186             String JavaDoc assocName = assocRef.getTypeQName().toString();
187             
188             List JavaDoc list = (List JavaDoc)this.childAssociations.get(assocName);
189             // create the list if this is first association with 'assocName'
190
if (list == null)
191             {
192                list = new ArrayList JavaDoc<ChildAssociationRef>();
193                this.childAssociations.put(assocName, list);
194             }
195             
196             // add the association to the list
197
list.add(assocRef);
198          }
199          
200          this.childAssocsRetrieved = true;
201       }
202       
203       return this.childAssociations;
204    }
205    
206    /**
207     * Returns all the child associations added to this node in this UI session
208     *
209     * @return Map of Maps of ChildAssociationRefs
210     */

211    public final Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, ChildAssociationRef>> getAddedChildAssociations()
212    {
213       if (this.childAssociationsAdded == null)
214       {
215          this.childAssociationsAdded = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, ChildAssociationRef>>();
216       }
217       return this.childAssociationsAdded;
218    }
219    
220    /**
221     * Returns all the child associations removed from this node is this UI session
222     *
223     * @return Map of Maps of ChildAssociationRefs
224     */

225    public final Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, ChildAssociationRef>> getRemovedChildAssociations()
226    {
227       if (this.childAssociationsRemoved == null)
228       {
229          this.childAssociationsRemoved = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, ChildAssociationRef>>();
230       }
231       return this.childAssociationsRemoved;
232    }
233    
234    /**
235     * Register a property resolver for the named property.
236     *
237     * @param name Name of the property this resolver is for
238     * @param resolver Property resolver to register
239     */

240    public final void addPropertyResolver(String JavaDoc name, NodePropertyResolver resolver)
241    {
242       this.properties.addPropertyResolver(name, resolver);
243    }
244    
245    /**
246     * Returns if a property resolver with a specific name has been applied to the Node
247     *
248     * @param name of property resolver to look for
249     *
250     * @return true if a resolver with the name is found, false otherwise
251     */

252    public final boolean containsPropertyResolver(String JavaDoc name)
253    {
254       return this.properties.containsPropertyResolver(name);
255    }
256    
257    /**
258     * Determines whether the given property name is held by this node
259     *
260     * @param propertyName Property to test existence of
261     * @return true if property exists, false otherwise
262     */

263    public final boolean hasProperty(String JavaDoc propertyName)
264    {
265       return getProperties().containsKey(propertyName);
266    }
267
268    /**
269     * @return Returns the NodeRef this Node object represents
270     */

271    public final NodeRef getNodeRef()
272    {
273       return this.nodeRef;
274    }
275    
276    /**
277     * @return Returns the type.
278     */

279    public final QName getType()
280    {
281       if (this.type == null)
282       {
283          this.type = getServiceRegistry().getNodeService().getType(this.nodeRef);
284       }
285       
286       return type;
287    }
288    
289    /**
290     * @return The display name for the node
291     */

292    public final String JavaDoc getName()
293    {
294       if (this.name == null)
295       {
296          // try and get the name from the properties first
297
this.name = (String JavaDoc)getProperties().get("cm:name");
298          
299          // if we didn't find it as a property get the name from the association name
300
if (this.name == null)
301          {
302             this.name = getServiceRegistry().getNodeService().getPrimaryParent(this.nodeRef).getQName().getLocalName();
303          }
304       }
305       
306       return this.name;
307    }
308
309    /**
310     * @return The list of aspects applied to this node
311     */

312    public final Set JavaDoc<QName> getAspects()
313    {
314       if (this.aspects == null)
315       {
316          this.aspects = getServiceRegistry().getNodeService().getAspects(this.nodeRef);
317       }
318       
319       return this.aspects;
320    }
321    
322    /**
323     * @param aspect The aspect to test for
324     * @return true if the node has the aspect false otherwise
325     */

326    public final boolean hasAspect(QName aspect)
327    {
328       Set JavaDoc aspects = getAspects();
329       return aspects.contains(aspect);
330    }
331    
332    /**
333     * Return whether the current user has the specified access permission on this Node
334     *
335     * @param permission Permission to validate against
336     *
337     * @return true if the permission is applied to the node for this user, false otherwise
338     */

339    public final boolean hasPermission(String JavaDoc permission)
340    {
341       Boolean JavaDoc valid = null;
342       if (permissions != null)
343       {
344          valid = permissions.get(permission);
345       }
346       else
347       {
348          permissions = new HashMap JavaDoc<String JavaDoc, Boolean JavaDoc>(5, 1.0f);
349       }
350       
351       if (valid == null)
352       {
353          PermissionService service = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getPermissionService();
354          valid = Boolean.valueOf(service.hasPermission(this.nodeRef, permission) == AccessStatus.ALLOWED);
355          permissions.put(permission, valid);
356       }
357       
358       return valid.booleanValue();
359    }
360
361    /**
362     * @return The GUID for the node
363     */

364    public final String JavaDoc getId()
365    {
366       return this.id;
367    }
368
369    /**
370     * @return The path for the node
371     */

372    public final String JavaDoc getPath()
373    {
374       if (this.path == null)
375       {
376          this.path = getServiceRegistry().getNodeService().getPath(this.nodeRef).toString();
377       }
378       
379       return this.path;
380    }
381    
382    /**
383     * Resets the state of the node to force re-retrieval of the data
384     */

385    public void reset()
386    {
387       this.name = null;
388       this.type = null;
389       this.path = null;
390       this.properties.clear();
391       this.propsRetrieved = false;
392       this.aspects = null;
393       this.permissions = null;
394       
395       this.associations = null;
396       this.associationsAdded = null;
397       this.associationsRemoved = null;
398       this.assocsRetrieved = false;
399       
400       this.childAssociations = null;
401       this.childAssociationsAdded = null;
402       this.childAssociationsRemoved = null;
403       this.childAssocsRetrieved = false;
404    }
405    
406    /**
407     * Override Object.toString() to provide useful debug output
408     */

409    public String JavaDoc toString()
410    {
411       if (getServiceRegistry().getNodeService() != null)
412       {
413          if (getServiceRegistry().getNodeService().exists(nodeRef))
414          {
415             return "Node Type: " + getType() +
416                    "\nNode Properties: " + this.getProperties().toString() +
417                    "\nNode Aspects: " + this.getAspects().toString();
418          }
419          else
420          {
421             return "Node no longer exists: " + nodeRef;
422          }
423       }
424       else
425       {
426          return super.toString();
427       }
428    }
429    
430    protected ServiceRegistry getServiceRegistry()
431    {
432       if (this.services == null)
433       {
434           this.services = Repository.getServiceRegistry(FacesContext.getCurrentInstance());
435       }
436       return this.services;
437    }
438 }
439
Popular Tags