KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > policy > PolicyScope


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.policy;
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 org.alfresco.service.cmr.repository.ChildAssociationRef;
27 import org.alfresco.service.cmr.repository.AssociationRef;
28 import org.alfresco.service.namespace.QName;
29
30 /**
31  * Policy scope.
32  * <p>
33  * Helper often used by policies which require information
34  * about a node to be gathered, for example onCopy or onCreateVersion.
35  *
36  * @author Roy Wetherall
37  */

38 public class PolicyScope extends AspectDetails
39 {
40     /**
41      * The aspects
42      */

43     protected Map JavaDoc<QName, AspectDetails> aspectCopyDetails = new HashMap JavaDoc<QName, AspectDetails>();
44     
45     /**
46      * Constructor
47      *
48      * @param classRef the class reference
49      */

50     public PolicyScope(QName classRef)
51     {
52         super(classRef);
53     }
54     
55     /**
56      * Add a property
57      *
58      * @param classRef the class reference
59      * @param qName the qualified name of the property
60      * @param value the value of the property
61      */

62     public void addProperty(QName classRef, QName qName, Serializable JavaDoc value)
63     {
64         if (classRef.equals(this.classRef) == true)
65         {
66             addProperty(qName, value);
67         }
68         else
69         {
70             AspectDetails aspectDetails = this.aspectCopyDetails.get(classRef);
71             if (aspectDetails == null)
72             {
73                 // Add the aspect
74
aspectDetails = addAspect(classRef);
75             }
76             aspectDetails.addProperty(qName, value);
77         }
78     }
79     
80     /**
81      * Removes a property from the list
82      *
83      * @param classRef the class reference
84      * @param qName the qualified name
85      */

86     public void removeProperty(QName classRef, QName qName)
87     {
88         if (classRef.equals(this.classRef) == true)
89         {
90             removeProperty(qName);
91         }
92         else
93         {
94             AspectDetails aspectDetails = this.aspectCopyDetails.get(classRef);
95             if (aspectDetails != null)
96             {
97                 aspectDetails.removeProperty(qName);
98             }
99         }
100     }
101     
102     /**
103      * Get the properties
104      *
105      * @param classRef the class ref
106      * @return the properties that should be copied
107      */

108     public Map JavaDoc<QName, Serializable JavaDoc> getProperties(QName classRef)
109     {
110         Map JavaDoc<QName, Serializable JavaDoc> result = null;
111         if (classRef.equals(this.classRef) == true)
112         {
113             result = getProperties();
114         }
115         else
116         {
117             AspectDetails aspectDetails = this.aspectCopyDetails.get(classRef);
118             if (aspectDetails != null)
119             {
120                 result = aspectDetails.getProperties();
121             }
122         }
123         
124         return result;
125     }
126     
127     /**
128      * Adds a child association
129      *
130      * @param classRef
131      * @param qname
132      * @param childAssocRef
133      */

134     public void addChildAssociation(QName classRef, ChildAssociationRef childAssocRef)
135     {
136         if (classRef.equals(this.classRef) == true)
137         {
138             addChildAssociation(childAssocRef);
139         }
140         else
141         {
142             AspectDetails aspectDetails = this.aspectCopyDetails.get(classRef);
143             if (aspectDetails == null)
144             {
145                 // Add the aspect
146
aspectDetails = addAspect(classRef);
147             }
148             aspectDetails.addChildAssociation(childAssocRef);
149         }
150     }
151     
152     /**
153      *
154      * @param classRef
155      * @param childAssocRef
156      * @param alwaysTraverseAssociation
157      */

158     public void addChildAssociation(QName classRef, ChildAssociationRef childAssocRef, boolean alwaysTraverseAssociation)
159     {
160         if (classRef.equals(this.classRef) == true)
161         {
162             addChildAssociation(childAssocRef, alwaysTraverseAssociation);
163         }
164         else
165         {
166             AspectDetails aspectDetails = this.aspectCopyDetails.get(classRef);
167             if (aspectDetails == null)
168             {
169                 // Add the aspect
170
aspectDetails = addAspect(classRef);
171             }
172             aspectDetails.addChildAssociation(childAssocRef, alwaysTraverseAssociation);
173         }
174     }
175     
176     /**
177      * Get a child association
178      *
179      * @param classRef
180      * @return
181      */

182     public List JavaDoc<ChildAssociationRef> getChildAssociations(QName classRef)
183     {
184         List JavaDoc<ChildAssociationRef> result = null;
185         if (classRef.equals(this.classRef) == true)
186         {
187             result = getChildAssociations();
188         }
189         else
190         {
191             AspectDetails aspectDetails = this.aspectCopyDetails.get(classRef);
192             if (aspectDetails != null)
193             {
194                 result = aspectDetails.getChildAssociations();
195             }
196         }
197         
198         return result;
199     }
200     
201     public boolean isChildAssociationRefAlwaysTraversed(QName classRef, ChildAssociationRef childAssocRef)
202     {
203         boolean result = false;
204         if (classRef.equals(this.classRef) == true)
205         {
206             result = isChildAssociationRefAlwaysTraversed(childAssocRef);
207         }
208         else
209         {
210             AspectDetails aspectDetails = this.aspectCopyDetails.get(classRef);
211             if (aspectDetails != null)
212             {
213                 result = aspectDetails.isChildAssociationRefAlwaysTraversed(childAssocRef);
214             }
215         }
216         
217         return result;
218     }
219     
220     /**
221      * Add an association
222      *
223      * @param classRef
224      * @param qname
225      * @param nodeAssocRef
226      */

227     public void addAssociation(QName classRef, AssociationRef nodeAssocRef)
228     {
229         if (classRef.equals(this.classRef) == true)
230         {
231             addAssociation(nodeAssocRef);
232         }
233         else
234         {
235             AspectDetails aspectDetails = this.aspectCopyDetails.get(classRef);
236             if (aspectDetails == null)
237             {
238                 // Add the aspect
239
aspectDetails = addAspect(classRef);
240             }
241             aspectDetails.addAssociation(nodeAssocRef);
242         }
243     }
244     
245
246     
247     /**
248      * Get associations
249      *
250      * @param classRef
251      * @return
252      */

253     public List JavaDoc<AssociationRef> getAssociations(QName classRef)
254     {
255         List JavaDoc<AssociationRef> result = null;
256         if (classRef.equals(this.classRef) == true)
257         {
258             result = getAssociations();
259         }
260         else
261         {
262             AspectDetails aspectDetails = this.aspectCopyDetails.get(classRef);
263             if (aspectDetails != null)
264             {
265                 result = aspectDetails.getAssociations();
266             }
267         }
268         
269         return result;
270     }
271     
272     /**
273      * Add an aspect
274      *
275      * @param aspect the aspect class reference
276      * @return the apsect copy details (returned as a helper)
277      */

278     public AspectDetails addAspect(QName aspect)
279     {
280         AspectDetails result = new AspectDetails(aspect);
281         this.aspectCopyDetails.put(aspect, result);
282         return result;
283     }
284     
285     /**
286      * Removes an aspect from the list
287      *
288      * @param aspect the aspect class reference
289      */

290     public void removeAspect(QName aspect)
291     {
292         this.aspectCopyDetails.remove(aspect);
293     }
294     
295     /**
296      * Gets a list of the aspects
297      *
298      * @return a list of aspect to copy
299      */

300     public Set JavaDoc<QName> getAspects()
301     {
302         return this.aspectCopyDetails.keySet();
303     }
304 }
305
306 /**
307  * Aspect details class.
308  * <p>
309  * Contains the details of an aspect this can be used for copying or versioning.
310  *
311  * @author Roy Wetherall
312  */

313 /*package*/ class AspectDetails
314 {
315     /**
316      * The properties
317      */

318     protected Map JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
319     
320     /**
321      * The child associations
322      */

323     protected List JavaDoc<ChildAssociationRef> childAssocs = new ArrayList JavaDoc<ChildAssociationRef>();
324     
325     /**
326      * The target associations
327      */

328     protected List JavaDoc<AssociationRef> targetAssocs = new ArrayList JavaDoc<AssociationRef>();
329     
330     /**
331      * The class ref of the aspect
332      */

333     protected QName classRef;
334
335     /**
336      * Map of assocs that will always be traversed
337      */

338     protected Map JavaDoc<ChildAssociationRef, ChildAssociationRef> alwaysTraverseMap = new HashMap JavaDoc<ChildAssociationRef, ChildAssociationRef>();
339
340     /**
341      * Constructor
342      *
343      * @param classRef the class ref
344      */

345     public AspectDetails(QName classRef)
346     {
347         this.classRef = classRef;
348     }
349     
350     /**
351      * Add a property to the list
352      *
353      * @param qName the qualified name of the property
354      * @param value the value of the property
355      */

356     public void addProperty(QName qName, Serializable JavaDoc value)
357     {
358         this.properties.put(qName, value);
359     }
360     
361     /**
362      * Remove a property from the list
363      *
364      * @param qName the qualified name of the property
365      */

366     public void removeProperty(QName qName)
367     {
368         this.properties.remove(qName);
369     }
370     
371     /**
372      * Gets the map of properties
373      *
374      * @return map of property names and values
375      */

376     public Map JavaDoc<QName, Serializable JavaDoc> getProperties()
377     {
378         return properties;
379     }
380     
381     /**
382      * Add a child association
383      *
384      * @param childAssocRef the child association reference
385      */

386     protected void addChildAssociation(ChildAssociationRef childAssocRef)
387     {
388         this.childAssocs.add(childAssocRef);
389     }
390     
391     /**
392      * Add a child association
393      *
394      * @param childAssocRef the child assoc reference
395      * @param alwaysDeepCopy indicates whether the assoc should always be traversed
396      */

397     protected void addChildAssociation(ChildAssociationRef childAssocRef, boolean alwaysTraverseAssociation)
398     {
399         addChildAssociation(childAssocRef);
400         
401         if (alwaysTraverseAssociation == true)
402         {
403             // Add to the list of deep copy child associations
404
this.alwaysTraverseMap.put(childAssocRef, childAssocRef);
405         }
406     }
407     
408     /**
409      * Indicates whether a child association ref is always traversed or not
410      *
411      * @param childAssocRef the child association reference
412      * @return true if the assoc is always traversed, false otherwise
413      */

414     protected boolean isChildAssociationRefAlwaysTraversed(ChildAssociationRef childAssocRef)
415     {
416         return this.alwaysTraverseMap.containsKey(childAssocRef);
417     }
418     
419     /**
420      * Gets the child associations to be copied
421      *
422      * @return map containing the child associations to be copied
423      */

424     public List JavaDoc<ChildAssociationRef> getChildAssociations()
425     {
426         return this.childAssocs;
427     }
428     
429     /**
430      * Adds an association to be copied
431      *
432      * @param qname the qualified name of the association
433      * @param nodeAssocRef the association reference
434      */

435     protected void addAssociation(AssociationRef nodeAssocRef)
436     {
437         this.targetAssocs.add(nodeAssocRef);
438     }
439     
440     /**
441      * Gets the map of associations to be copied
442      *
443      * @return a map conatining the associations to be copied
444      */

445     public List JavaDoc<AssociationRef> getAssociations()
446     {
447         return this.targetAssocs;
448     }
449 }
Popular Tags