KickJava   Java API By Example, From Geeks To Geeks.

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


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.DictionaryService;
24 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
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 PropertyPolicyDelegate<P extends PropertyPolicy>
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*/ PropertyPolicyDelegate(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 property type
69      *
70      * @param assocTypeQName
71      * @throws IllegalArgumentException
72      */

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

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

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

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

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

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

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

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

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