KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > importer > view > NodeContext


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.repo.importer.view;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.alfresco.model.ContentModel;
29 import org.alfresco.repo.importer.ImportNode;
30 import org.alfresco.service.cmr.dictionary.AspectDefinition;
31 import org.alfresco.service.cmr.dictionary.AssociationDefinition;
32 import org.alfresco.service.cmr.dictionary.ChildAssociationDefinition;
33 import org.alfresco.service.cmr.dictionary.ClassDefinition;
34 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
35 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
36 import org.alfresco.service.cmr.dictionary.TypeDefinition;
37 import org.alfresco.service.cmr.repository.NodeRef;
38 import org.alfresco.service.cmr.security.AccessPermission;
39 import org.alfresco.service.cmr.security.AccessStatus;
40 import org.alfresco.service.cmr.security.AuthorityType;
41 import org.alfresco.service.cmr.security.PermissionService;
42 import org.alfresco.service.namespace.QName;
43
44
45 /**
46  * Maintains state about the currently imported node.
47  *
48  * @author David Caruana
49  *
50  */

51 public class NodeContext extends ElementContext
52     implements ImportNode
53 {
54     private ParentContext parentContext;
55     private boolean isReference = false;
56     private NodeRef nodeRef;
57     private String JavaDoc importId; // unique identifier within import (optional)
58
private String JavaDoc uuid; // unique identifier within repository
59
private TypeDefinition typeDef;
60     private String JavaDoc childName;
61     private Map JavaDoc<QName, AspectDefinition> nodeAspects = new HashMap JavaDoc<QName, AspectDefinition>();
62     private Map JavaDoc<QName, ChildAssociationDefinition> nodeChildAssocs = new HashMap JavaDoc<QName, ChildAssociationDefinition>();
63     private Map JavaDoc<QName, Serializable JavaDoc> nodeProperties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
64     private Map JavaDoc<QName, DataTypeDefinition> propertyDatatypes = new HashMap JavaDoc<QName, DataTypeDefinition>();
65
66     // permissions
67
private boolean inherit = true;
68     private List JavaDoc<AccessPermission> accessControlEntries = new ArrayList JavaDoc<AccessPermission>();
69     
70
71     /**
72      * Construct
73      *
74      * @param elementName
75      * @param parentContext
76      * @param typeDef
77      */

78     public NodeContext(QName elementName, ParentContext parentContext, TypeDefinition typeDef)
79     {
80         super(elementName, parentContext.getDictionaryService(), parentContext.getImporter());
81         this.parentContext = parentContext;
82         this.typeDef = typeDef;
83         this.uuid = null;
84     }
85
86     /* (non-Javadoc)
87      * @see org.alfresco.repo.importer.ImportNode#getParentContext()
88      */

89     public ParentContext getParentContext()
90     {
91         return parentContext;
92     }
93
94     /* (non-Javadoc)
95      * @see org.alfresco.repo.importer.ImportNode#getTypeDefinition()
96      */

97     public TypeDefinition getTypeDefinition()
98     {
99         return typeDef;
100     }
101
102     /*
103      * (non-Javadoc)
104      * @see org.alfresco.repo.importer.ImportNode#isReference()
105      */

106     public boolean isReference()
107     {
108         return isReference;
109     }
110     
111     /**
112      * @param isReference true => this is a node reference
113      */

114     public void setReference(boolean isReference)
115     {
116         this.isReference = isReference;
117     }
118     
119     /**
120      * Set Type Definition
121      *
122      * @param typeDef
123      */

124     public void setTypeDefinition(TypeDefinition typeDef)
125     {
126         this.typeDef = typeDef;
127     }
128     
129     /* (non-Javadoc)
130      * @see org.alfresco.repo.importer.ImportNode#getNodeRef()
131      */

132     public NodeRef getNodeRef()
133     {
134         return nodeRef;
135     }
136     
137     /**
138      * @param nodeRef the node ref
139      */

140     public void setNodeRef(NodeRef nodeRef)
141     {
142         this.nodeRef = nodeRef;
143     }
144
145     /*
146      * (non-Javadoc)
147      * @see org.alfresco.repo.importer.ImportNode#getUUID()
148      */

149     public String JavaDoc getUUID()
150     {
151         return uuid;
152     }
153     
154     /**
155      * @param uuid uuid
156      */

157     public void setUUID(String JavaDoc uuid)
158     {
159         this.uuid = uuid;
160     }
161     
162     /*
163      * (non-Javadoc)
164      * @see org.alfresco.repo.importer.ImportNode#getImportId()
165      */

166     public String JavaDoc getImportId()
167     {
168         return importId;
169     }
170     
171     /**
172      * @param importId import scoped id
173      */

174     public void setImportId(String JavaDoc importId)
175     {
176         this.importId = importId;
177     }
178     
179     /* (non-Javadoc)
180      * @see org.alfresco.repo.importer.ImportNode#getChildName()
181      */

182     public String JavaDoc getChildName()
183     {
184         return childName;
185     }
186     
187     /**
188      * @param childName the child name
189      */

190     public void setChildName(String JavaDoc childName)
191     {
192         this.childName = childName;
193     }
194
195     /*
196      * @param inherit determines if node inherits permissions from parent
197      */

198     public void setInheritPermissions(boolean inherit)
199     {
200         this.inherit = inherit;
201     }
202     
203     /**
204      * @return true => node inherits permissions from parent
205      */

206     public boolean getInheritPermissions()
207     {
208         return this.inherit;
209     }
210     
211     /**
212      * Adds a collection property to the node
213      *
214      * @param property
215      */

216     public void addPropertyCollection(QName property)
217     {
218         // Do not import properties of sys:referenceable or cm:versionable or cm:copiedfrom
219
// TODO: Make this configurable...
220
PropertyDefinition propDef = getDictionaryService().getProperty(property);
221         ClassDefinition classDef = (propDef == null) ? null : propDef.getContainerClass();
222         if (classDef != null)
223         {
224             if (!isImportableClass(classDef.getName()))
225             {
226                 return;
227             }
228         }
229         
230         // create collection and assign to property
231
List JavaDoc<Serializable JavaDoc>values = new ArrayList JavaDoc<Serializable JavaDoc>();
232         nodeProperties.put(property, (Serializable JavaDoc)values);
233     }
234     
235     
236     /**
237      * Adds a property to the node
238      *
239      * @param property the property name
240      * @param value the property value
241      */

242     public void addProperty(QName property, String JavaDoc value)
243     {
244         // Process "special" properties
245
// TODO: Make this configurable...
246
PropertyDefinition propDef = getDictionaryService().getProperty(property);
247
248         // Process Alfresco UUID
249
if (propDef != null && propDef.getName().equals(ContentModel.PROP_NODE_UUID))
250         {
251             uuid = value;
252         }
253
254         // Do not import properties of sys:referenceable or cm:versionable
255
ClassDefinition classDef = (propDef == null) ? null : propDef.getContainerClass();
256         if (classDef != null)
257         {
258             if (!isImportableClass(classDef.getName()))
259             {
260                 return;
261             }
262         }
263         
264         // Handle single / multi-valued cases
265
Serializable JavaDoc newValue = value;
266         Serializable JavaDoc existingValue = nodeProperties.get(property);
267         if (existingValue != null)
268         {
269             if (existingValue instanceof Collection JavaDoc)
270             {
271                 // add to existing collection of values
272
((Collection JavaDoc<Serializable JavaDoc>)existingValue).add(value);
273                 newValue = existingValue;
274             }
275             else
276             {
277                 // convert single to multi-valued
278
List JavaDoc<Serializable JavaDoc>values = new ArrayList JavaDoc<Serializable JavaDoc>();
279                 values.add((String JavaDoc)existingValue);
280                 values.add(value);
281                 newValue = (Serializable JavaDoc)values;
282             }
283         }
284         nodeProperties.put(property, newValue);
285     }
286     
287     /**
288      * Adds a property datatype to the node
289      *
290      * @param property property name
291      * @param datatype property datatype
292      */

293     public void addDatatype(QName property, DataTypeDefinition datatype)
294     {
295         propertyDatatypes.put(property, datatype);
296     }
297     
298     /* (non-Javadoc)
299      * @see org.alfresco.repo.importer.ImportNode#getPropertyDatatypes()
300      */

301     public Map JavaDoc<QName, DataTypeDefinition> getPropertyDatatypes()
302     {
303         return propertyDatatypes;
304     }
305
306     /* (non-Javadoc)
307      * @see org.alfresco.repo.importer.ImportNode#getProperties()
308      */

309     public Map JavaDoc<QName, Serializable JavaDoc> getProperties()
310     {
311         return nodeProperties;
312     }
313
314     /**
315      * Adds an aspect to the node
316      *
317      * @param aspect the aspect
318      */

319     public void addAspect(AspectDefinition aspect)
320     {
321         if (isImportableClass(aspect.getName()))
322         {
323             nodeAspects.put(aspect.getName(), aspect);
324         }
325     }
326     
327     /* (non-Javadoc)
328      * @see org.alfresco.repo.importer.ImportNode#getNodeAspects()
329      */

330     public Set JavaDoc<QName> getNodeAspects()
331     {
332         return nodeAspects.keySet();
333     }
334
335     /**
336      * Adds an Access Control Entry
337      *
338      * @param accessStatus
339      * @param authority
340      * @param permission
341      */

342     public void addAccessControlEntry(AccessStatus accessStatus, String JavaDoc authority, String JavaDoc permission)
343     {
344        // Note: Map guest permission to Consumer permission - this is to handle the case where
345
// exports made against a pre 1.2 RC2 release
346
if (permission.equalsIgnoreCase("guest"))
347        {
348            permission = PermissionService.CONSUMER;
349        }
350       
351        ACE ace = new ACE();
352        ace.accessStatus = accessStatus;
353        ace.authority = authority;
354        ace.permission = permission;
355        accessControlEntries.add(ace);
356     }
357
358     /**
359      * Gets the Access Control Entries
360      *
361      * @return access control entries
362      */

363     public List JavaDoc<AccessPermission> getAccessControlEntries()
364     {
365         return accessControlEntries;
366     }
367     
368     /**
369      * Determine the type of definition (aspect, property, association) from the
370      * specified name
371      *
372      * @param defName
373      * @return the dictionary definition
374      */

375     public Object JavaDoc determineDefinition(QName defName)
376     {
377         Object JavaDoc def = determineAspect(defName);
378         if (def == null)
379         {
380             def = determineProperty(defName);
381             if (def == null)
382             {
383                 def = determineAssociation(defName);
384             }
385         }
386         return def;
387     }
388     
389     /**
390      * Determine if name referes to an aspect
391      *
392      * @param defName
393      * @return
394      */

395     public AspectDefinition determineAspect(QName defName)
396     {
397         AspectDefinition def = null;
398         if (nodeAspects.containsKey(defName) == false)
399         {
400             def = getDictionaryService().getAspect(defName);
401         }
402         return def;
403     }
404     
405     /**
406      * Determine if name refers to a property
407      *
408      * @param defName
409      * @return
410      */

411     public PropertyDefinition determineProperty(QName defName)
412     {
413         PropertyDefinition def = null;
414         if (nodeProperties.containsKey(defName) == false)
415         {
416             def = (typeDef == null) ? null : getDictionaryService().getProperty(typeDef.getName(), defName);
417             if (def == null)
418             {
419                 Set JavaDoc<AspectDefinition> allAspects = new HashSet JavaDoc<AspectDefinition>();
420                 if (typeDef != null)
421                 {
422                     allAspects.addAll(typeDef.getDefaultAspects());
423                 }
424                 allAspects.addAll(nodeAspects.values());
425                 for (AspectDefinition aspectDef : allAspects)
426                 {
427                     def = getDictionaryService().getProperty(aspectDef.getName(), defName);
428                     if (def != null)
429                     {
430                         break;
431                     }
432                 }
433             }
434         }
435         return def;
436     }
437     
438     /**
439      * Determine if name referes to an association
440      *
441      * @param defName
442      * @return
443      */

444     public AssociationDefinition determineAssociation(QName defName)
445     {
446         AssociationDefinition def = null;
447         if (nodeChildAssocs.containsKey(defName) == false)
448         {
449             def = getDictionaryService().getAssociation(defName);
450         }
451         return def;
452     }
453     
454     /**
455      * Determine if the provided class name is to be imported
456      *
457      * @param className class to check (type or aspect)
458      * @return true => import, false => ignore on import
459      */

460     private boolean isImportableClass(QName className)
461     {
462         return !(className.equals(ContentModel.ASPECT_REFERENCEABLE) ||
463                  className.equals(ContentModel.ASPECT_VERSIONABLE));
464     }
465     
466     /* (non-Javadoc)
467      * @see java.lang.Object#toString()
468      */

469     @Override JavaDoc
470     public String JavaDoc toString()
471     {
472         return "NodeContext[childName=" + getChildName() + ",type=" + (typeDef == null ? "null" : typeDef.getName()) + ",nodeRef=" + nodeRef +
473             ",aspects=" + nodeAspects.values() + ",parentContext=" + parentContext.toString() + "]";
474     }
475  
476     /**
477      * Access Control Entry
478      */

479     private class ACE implements AccessPermission
480     {
481         private AccessStatus accessStatus;
482         private String JavaDoc authority;
483         private String JavaDoc permission;
484
485         /*
486          * (non-Javadoc)
487          * @see org.alfresco.service.cmr.security.AccessPermission#getPermission()
488          */

489         public String JavaDoc getPermission()
490         {
491             return permission;
492         }
493
494         /*
495          * (non-Javadoc)
496          * @see org.alfresco.service.cmr.security.AccessPermission#getAccessStatus()
497          */

498         public AccessStatus getAccessStatus()
499         {
500             return accessStatus;
501         }
502         
503         /*
504          * (non-Javadoc)
505          * @see org.alfresco.service.cmr.security.AccessPermission#getAuthority()
506          */

507         public String JavaDoc getAuthority()
508         {
509             return authority;
510         }
511         
512         /*
513          * (non-Javadoc)
514          * @see org.alfresco.service.cmr.security.AccessPermission#getAuthorityType()
515          */

516         public AuthorityType getAuthorityType()
517         {
518             return null;
519         }
520     }
521     
522 }
523
Popular Tags