KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.alfresco.service.cmr.dictionary.AssociationDefinition;
24 import org.alfresco.service.cmr.dictionary.DictionaryService;
25 import org.alfresco.service.cmr.repository.NodeRef;
26 import org.alfresco.service.namespace.QName;
27
28
29 /**
30  * Delegate for a Class Feature-level (Property and Association) Policies. Provides
31  * access to Policy Interface implementations which invoke the appropriate bound behaviours.
32  *
33  * @author David Caruana
34  *
35  * @param <P> the policy interface
36  */

37 public class AssociationPolicyDelegate<P extends AssociationPolicy>
38 {
39     private DictionaryService dictionary;
40     private CachedPolicyFactory<ClassFeatureBehaviourBinding, P> factory;
41
42
43     /**
44      * Construct.
45      *
46      * @param dictionary the dictionary service
47      * @param policyClass the policy interface class
48      * @param index the behaviour index to query against
49      */

50     @SuppressWarnings JavaDoc("unchecked")
51     /*package*/ AssociationPolicyDelegate(DictionaryService dictionary, Class JavaDoc<P> policyClass, BehaviourIndex<ClassFeatureBehaviourBinding> index)
52     {
53         // Get list of all pre-registered behaviours for the policy and
54
// ensure they are valid.
55
Collection JavaDoc<BehaviourDefinition> definitions = index.getAll();
56         for (BehaviourDefinition definition : definitions)
57         {
58             definition.getBehaviour().getInterface(policyClass);
59         }
60
61         // Rely on cached implementation of policy factory
62
// Note: Could also use PolicyFactory (without caching)
63
this.factory = new CachedPolicyFactory<ClassFeatureBehaviourBinding, P>(policyClass, index);
64         this.dictionary = dictionary;
65     }
66     
67     /**
68      * Ensures the validity of the given assoc type
69      *
70      * @param assocTypeQName
71      * @throws IllegalArgumentException
72      */

73     private void checkAssocType(QName assocTypeQName) throws IllegalArgumentException JavaDoc
74     {
75         AssociationDefinition assocDef = dictionary.getAssociation(assocTypeQName);
76         if (assocDef == null)
77         {
78             throw new IllegalArgumentException JavaDoc("Association " + assocTypeQName + " has not been defined in the data dictionary");
79         }
80     }
81
82     /**
83      * Gets the Policy implementation for the specified Class and Association
84      *
85      * When multiple behaviours are bound to the policy for the class feature, an
86      * aggregate policy implementation is returned which invokes each policy
87      * in turn.
88      *
89      * @param classQName the class qualified name
90      * @param assocTypeQName the association type qualified name
91      * @return the policy
92      */

93     public P get(QName classQName, QName assocTypeQName)
94     {
95         return get(null, classQName, assocTypeQName);
96     }
97
98     /**
99      * Gets the Policy implementation for the specified Class and Association
100      *
101      * When multiple behaviours are bound to the policy for the class feature, an
102      * aggregate policy implementation is returned which invokes each policy
103      * in turn.
104      *
105      * @param nodeRef the node reference
106      * @param classQName the class qualified name
107      * @param assocTypeQName the association type qualified name
108      * @return the policy
109      */

110     public P get(NodeRef nodeRef, QName classQName, QName assocTypeQName)
111     {
112         checkAssocType(assocTypeQName);
113         return factory.create(new ClassFeatureBehaviourBinding(dictionary, nodeRef, classQName, assocTypeQName));
114     }
115     
116     /**
117      * Gets the collection of Policy implementations for the specified Class and Association
118      *
119      * @param classQName the class qualified name
120      * @param assocTypeQName the association type qualified name
121      * @return the collection of policies
122      */

123     public Collection JavaDoc<P> getList(QName classQName, QName assocTypeQName)
124     {
125         return getList(null, classQName, assocTypeQName);
126     }
127
128     /**
129      * Gets the collection of Policy implementations for the specified Class and Association
130      *
131      * @param nodeRef the node reference
132      * @param classQName the class qualified name
133      * @param assocTypeQName the association type qualified name
134      * @return the collection of policies
135      */

136     public Collection JavaDoc<P> getList(NodeRef nodeRef, QName classQName, QName assocTypeQName)
137     {
138         checkAssocType(assocTypeQName);
139         return factory.createList(new ClassFeatureBehaviourBinding(dictionary, nodeRef, classQName, assocTypeQName));
140     }
141
142     /**
143      * Gets a <tt>Policy</tt> for all the given Class and Association
144      *
145      * @param classQNames the class qualified names
146      * @param assocTypeQName the association type qualified name
147      * @return Return the policy
148      */

149     public P get(Set JavaDoc<QName> classQNames, QName assocTypeQName)
150     {
151         return get(null, classQNames, assocTypeQName);
152     }
153     
154     /**
155      * Gets a <tt>Policy</tt> for all the given Class and Association
156      *
157      * @param nodeRef the node reference
158      * @param classQNames the class qualified names
159      * @param assocTypeQName the association type qualified name
160      * @return Return the policy
161      */

162     public P get(NodeRef nodeRef, Set JavaDoc<QName> classQNames, QName assocTypeQName)
163     {
164         checkAssocType(assocTypeQName);
165         return factory.toPolicy(getList(nodeRef, classQNames, assocTypeQName));
166     }
167
168     /**
169      * Gets the <tt>Policy</tt> instances for all the given Classes and Associations
170      *
171      * @param classQNames the class qualified names
172      * @param assocTypeQName the association type qualified name
173      * @return Return the policies
174      */

175     public Collection JavaDoc<P> getList(Set JavaDoc<QName> classQNames, QName assocTypeQName)
176     {
177         return getList(null, classQNames, assocTypeQName);
178     }
179
180     /**
181      * Gets the <tt>Policy</tt> instances for all the given Classes and Associations
182      *
183      * @param nodeRef the node reference
184      * @param classQNames the class qualified names
185      * @param assocTypeQName the association type qualified name
186      * @return Return the policies
187      */

188     public Collection JavaDoc<P> getList(NodeRef nodeRef, Set JavaDoc<QName> classQNames, QName assocTypeQName)
189     {
190         checkAssocType(assocTypeQName);
191         Collection JavaDoc<P> policies = new HashSet JavaDoc<P>();
192         for (QName classQName : classQNames)
193         {
194             P policy = factory.create(new ClassFeatureBehaviourBinding(dictionary, nodeRef, classQName, assocTypeQName));
195             if (policy instanceof PolicyList)
196             {
197                 policies.addAll(((PolicyList<P>)policy).getPolicies());
198             }
199             else
200             {
201                 policies.add(policy);
202             }
203         }
204         return policies;
205     }
206
207 }
208
Popular Tags