KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > version > common > AbstractVersionServiceImpl


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.version.common;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.alfresco.repo.policy.ClassPolicyDelegate;
26 import org.alfresco.repo.policy.PolicyComponent;
27 import org.alfresco.repo.policy.PolicyScope;
28 import org.alfresco.repo.version.VersionServicePolicies;
29 import org.alfresco.repo.version.VersionServicePolicies.BeforeCreateVersionPolicy;
30 import org.alfresco.repo.version.VersionServicePolicies.CalculateVersionLabelPolicy;
31 import org.alfresco.repo.version.VersionServicePolicies.OnCreateVersionPolicy;
32 import org.alfresco.service.cmr.dictionary.AssociationDefinition;
33 import org.alfresco.service.cmr.dictionary.ClassDefinition;
34 import org.alfresco.service.cmr.dictionary.DictionaryService;
35 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
36 import org.alfresco.service.cmr.repository.AssociationRef;
37 import org.alfresco.service.cmr.repository.ChildAssociationRef;
38 import org.alfresco.service.cmr.repository.NodeRef;
39 import org.alfresco.service.cmr.repository.NodeService;
40 import org.alfresco.service.cmr.version.Version;
41 import org.alfresco.service.cmr.version.VersionServiceException;
42 import org.alfresco.service.namespace.QName;
43 import org.alfresco.service.namespace.RegexQNamePattern;
44
45 /**
46  * Abstract version service implementation.
47  *
48  * @author Roy Wetherall
49  */

50 public abstract class AbstractVersionServiceImpl
51 {
52     /**
53      * The common node service
54      */

55     protected NodeService nodeService ;
56     
57     /**
58      * Policy component
59      */

60     protected PolicyComponent policyComponent;
61     
62     /**
63      * The dictionary service
64      */

65     protected DictionaryService dictionaryService;
66     
67     /**
68      * Policy delegates
69      */

70     private ClassPolicyDelegate<BeforeCreateVersionPolicy> beforeCreateVersionDelegate;
71     private ClassPolicyDelegate<OnCreateVersionPolicy> onCreateVersionDelegate;
72     private ClassPolicyDelegate<CalculateVersionLabelPolicy> calculateVersionLabelDelegate;
73     
74     /**
75      * Sets the general node service
76      *
77      * @param nodeService the node service
78      */

79     public void setNodeService(NodeService nodeService)
80     {
81         this.nodeService = nodeService;
82     }
83     
84     /**
85      * Sets the policy component
86      *
87      * @param policyComponent the policy component
88      */

89     public void setPolicyComponent(PolicyComponent policyComponent)
90     {
91         this.policyComponent = policyComponent;
92     }
93         
94     /**
95      * Sets the dictionary service
96      *
97      * @param dictionaryService the dictionary service
98      */

99     public void setDictionaryService(DictionaryService dictionaryService)
100     {
101         this.dictionaryService = dictionaryService;
102     }
103     
104     /**
105      * Initialise method
106      */

107     public void initialise()
108     {
109         // Register the policies
110
this.beforeCreateVersionDelegate = this.policyComponent.registerClassPolicy(VersionServicePolicies.BeforeCreateVersionPolicy.class);
111         this.onCreateVersionDelegate = this.policyComponent.registerClassPolicy(VersionServicePolicies.OnCreateVersionPolicy.class);
112         this.calculateVersionLabelDelegate = this.policyComponent.registerClassPolicy(VersionServicePolicies.CalculateVersionLabelPolicy.class);
113     }
114     
115     /**
116      * Invokes the before create version policy behaviour
117      *
118      * @param nodeRef the node being versioned
119      */

120     protected void invokeBeforeCreateVersion(NodeRef nodeRef)
121     {
122         // invoke for node type
123
QName nodeTypeQName = nodeService.getType(nodeRef);
124         this.beforeCreateVersionDelegate.get(nodeTypeQName).beforeCreateVersion(nodeRef);
125         // invoke for node aspects
126
Set JavaDoc<QName> nodeAspectQNames = nodeService.getAspects(nodeRef);
127         this.beforeCreateVersionDelegate.get(nodeAspectQNames).beforeCreateVersion(nodeRef);
128     }
129     
130     /**
131      * Invoke the on create version policy behaviour
132      *
133      */

134     protected void invokeOnCreateVersion(
135             NodeRef nodeRef,
136             Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties,
137             PolicyScope nodeDetails)
138     {
139         // Sort out the policies for the node type
140
QName classRef = this.nodeService.getType(nodeRef);
141         invokeOnCreateVersion(classRef, nodeRef, versionProperties, nodeDetails);
142         
143         // Sort out the policies for the aspects
144
Collection JavaDoc<QName> aspects = this.nodeService.getAspects(nodeRef);
145         for (QName aspect : aspects)
146         {
147             invokeOnCreateVersion(aspect, nodeRef, versionProperties, nodeDetails);
148         }
149         
150     }
151     
152     /**
153      * Invokes the on create version policy behaviour for a given type
154      *
155      * @param classRef
156      * @param nodeDetails
157      * @param nodeRef
158      * @param versionProperties
159      */

160     private void invokeOnCreateVersion(
161             QName classRef,
162             NodeRef nodeRef,
163             Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties,
164             PolicyScope nodeDetails)
165     {
166         Collection JavaDoc<OnCreateVersionPolicy> policies = this.onCreateVersionDelegate.getList(classRef);
167         if (policies.size() == 0)
168         {
169             // Call the default implementation
170
defaultOnCreateVersion(
171                     classRef,
172                     nodeRef,
173                     versionProperties,
174                     nodeDetails);
175         }
176         else
177         {
178             // Call the policy definitions
179
for (VersionServicePolicies.OnCreateVersionPolicy policy : policies)
180             {
181                 policy.onCreateVersion(
182                         classRef,
183                         nodeRef,
184                         versionProperties,
185                         nodeDetails);
186             }
187         }
188     }
189     
190     /**
191      * Default implementation of the on create version policy. Called if no behaviour is registered for the
192      * policy for the specified type.
193      *
194      * @param nodeRef
195      * @param versionProperties
196      * @param nodeDetails
197      */

198     protected void defaultOnCreateVersion(
199             QName classRef,
200             NodeRef nodeRef,
201             Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties,
202             PolicyScope nodeDetails)
203     {
204         ClassDefinition classDefinition = this.dictionaryService.getClass(classRef);
205         if (classDefinition != null)
206         {
207             // Copy the properties
208
Map JavaDoc<QName,PropertyDefinition> propertyDefinitions = classDefinition.getProperties();
209             for (QName propertyName : propertyDefinitions.keySet())
210             {
211                 Serializable JavaDoc propValue = this.nodeService.getProperty(nodeRef, propertyName);
212                 nodeDetails.addProperty(classRef, propertyName, propValue);
213             }
214             
215             // Version the associations (child and target)
216
Map JavaDoc<QName, AssociationDefinition> assocDefs = classDefinition.getAssociations();
217
218             // TODO: Need way of getting child assocs of a given type
219
if (classDefinition.isContainer())
220             {
221                 List JavaDoc<ChildAssociationRef> childAssocRefs = this.nodeService.getChildAssocs(nodeRef);
222                 for (ChildAssociationRef childAssocRef : childAssocRefs)
223                 {
224                     if (assocDefs.containsKey(childAssocRef.getTypeQName()))
225                     {
226                         nodeDetails.addChildAssociation(classDefinition.getName(), childAssocRef);
227                     }
228                 }
229             }
230             
231             // TODO: Need way of getting assocs of a given type
232
List JavaDoc<AssociationRef> nodeAssocRefs = this.nodeService.getTargetAssocs(nodeRef, RegexQNamePattern.MATCH_ALL);
233             for (AssociationRef nodeAssocRef : nodeAssocRefs)
234             {
235                 if (assocDefs.containsKey(nodeAssocRef.getTypeQName()))
236                 {
237                     nodeDetails.addAssociation(classDefinition.getName(), nodeAssocRef);
238                 }
239             }
240         }
241     }
242     
243     /**
244      * Invoke the calculate version label policy behaviour
245      *
246      * @param classRef
247      * @param preceedingVersion
248      * @param versionNumber
249      * @param versionProperties
250      * @return
251      */

252     protected String JavaDoc invokeCalculateVersionLabel(
253             QName classRef,
254             Version preceedingVersion,
255             int versionNumber,
256             Map JavaDoc<String JavaDoc, Serializable JavaDoc>versionProperties)
257     {
258         String JavaDoc versionLabel = null;
259         
260         Collection JavaDoc<CalculateVersionLabelPolicy> behaviours = this.calculateVersionLabelDelegate.getList(classRef);
261         if (behaviours.size() == 0)
262         {
263             // Default the version label to the version numbder
264
versionLabel = Integer.toString(versionNumber);
265         }
266         else if (behaviours.size() == 1)
267         {
268             // Call the policy behaviour
269
CalculateVersionLabelPolicy[] arr = behaviours.toArray(new CalculateVersionLabelPolicy[]{});
270             versionLabel = arr[0].calculateVersionLabel(classRef, preceedingVersion, versionNumber, versionProperties);
271         }
272         else
273         {
274             // Error since we can only deal with a single caculate version label policy
275
throw new VersionServiceException("More than one CalculateVersionLabelPolicy behaviour has been registered for the type " + classRef.toString());
276         }
277         
278         return versionLabel;
279     }
280      
281 }
282
Popular Tags