KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > version > VersionableAspect


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;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.alfresco.model.ContentModel;
23 import org.alfresco.repo.action.executer.CreateVersionActionExecuter;
24 import org.alfresco.repo.content.ContentServicePolicies;
25 import org.alfresco.repo.policy.Behaviour;
26 import org.alfresco.repo.policy.JavaBehaviour;
27 import org.alfresco.repo.policy.PolicyComponent;
28 import org.alfresco.repo.policy.PolicyScope;
29 import org.alfresco.repo.rule.RuntimeRuleService;
30 import org.alfresco.service.cmr.action.Action;
31 import org.alfresco.service.cmr.action.ActionService;
32 import org.alfresco.service.cmr.repository.NodeRef;
33 import org.alfresco.service.cmr.repository.NodeService;
34 import org.alfresco.service.cmr.repository.StoreRef;
35 import org.alfresco.service.cmr.rule.Rule;
36 import org.alfresco.service.cmr.rule.RuleService;
37 import org.alfresco.service.namespace.NamespaceService;
38 import org.alfresco.service.namespace.QName;
39
40 /**
41  * Class containing behaviour for the versionable aspect
42  *
43  * @author Roy Wetherall
44  */

45 public class VersionableAspect implements ContentServicePolicies.OnContentUpdatePolicy
46 {
47     /**
48      * The policy component
49      */

50     private PolicyComponent policyComponent;
51     
52     /**
53      * The node service
54      */

55     private NodeService nodeService;
56     
57     /**
58      * The rule service
59      */

60     private RuleService ruleService;
61     
62     /**
63      * The action service
64      */

65     private ActionService actionService;
66     
67     /**
68      * The rule used to create versions
69      */

70     private Rule rule;
71
72     /**
73      * Auto version behaviour
74      */

75     private Behaviour autoVersionBehaviour;
76     
77     /**
78      * Set the policy component
79      *
80      * @param policyComponent the policy component
81      */

82     public void setPolicyComponent(PolicyComponent policyComponent)
83     {
84         this.policyComponent = policyComponent;
85     }
86     
87     /**
88      * Set the rule service
89      *
90      * @param ruleService the rule service
91      */

92     public void setRuleService(RuleService ruleService)
93     {
94         this.ruleService = ruleService;
95     }
96     
97     /**
98      * Set the action service
99      *
100      * @param actionService the action service
101      */

102     public void setActionService(ActionService actionService)
103     {
104         this.actionService = actionService;
105     }
106     
107     /**
108      * Set the node service
109      *
110      * @param nodeService the node service
111      */

112     public void setNodeService(NodeService nodeService)
113     {
114         this.nodeService = nodeService;
115     }
116     
117     /**
118      * Initialise the versionable aspect policies
119      */

120     public void init()
121     {
122         this.policyComponent.bindClassBehaviour(
123                 QName.createQName(NamespaceService.ALFRESCO_URI, "onAddAspect"),
124                 ContentModel.ASPECT_VERSIONABLE,
125                 new JavaBehaviour(this, "onAddAspect"));
126         autoVersionBehaviour = new JavaBehaviour(this, "onContentUpdate");
127         this.policyComponent.bindClassBehaviour(
128                 ContentServicePolicies.ON_CONTENT_UPDATE,
129                 ContentModel.ASPECT_VERSIONABLE,
130                 autoVersionBehaviour);
131         
132         // Register the copy behaviour
133
this.policyComponent.bindClassBehaviour(
134                 QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyNode"),
135                 ContentModel.ASPECT_VERSIONABLE,
136                 new JavaBehaviour(this, "onCopy"));
137     }
138     
139     /**
140      * OnCopy behaviour implementation for the version aspect.
141      * <p>
142      * Ensures that the propety values of the version aspect are not copied onto
143      * the destination node.
144      *
145      * @see org.alfresco.repo.copy.CopyServicePolicies.OnCopyNodePolicy#onCopyNode(QName, NodeRef, StoreRef, boolean, PolicyScope)
146      */

147     public void onCopy(
148             QName sourceClassRef,
149             NodeRef sourceNodeRef,
150             StoreRef destinationStoreRef,
151             boolean copyToNewNode,
152             PolicyScope copyDetails)
153     {
154         // Add the version aspect, but do not copy the version label
155
copyDetails.addAspect(ContentModel.ASPECT_VERSIONABLE);
156         copyDetails.addProperty(
157                 ContentModel.ASPECT_VERSIONABLE,
158                 ContentModel.PROP_AUTO_VERSION,
159                 this.nodeService.getProperty(sourceNodeRef, ContentModel.PROP_AUTO_VERSION));
160     }
161     
162     /**
163      * OnCreateVersion behaviour for the version aspect
164      * <p>
165      * Ensures that the version aspect and it proerties are 'frozen' as part of
166      * the versioned state.
167      *
168      * @param classRef the class reference
169      * @param versionableNode the versionable node reference
170      * @param versionProperties the version properties
171      * @param nodeDetails the details of the node to be versioned
172      */

173     public void onCreateVersion(
174             QName classRef,
175             NodeRef versionableNode,
176             Map JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties,
177             PolicyScope nodeDetails)
178     {
179         // Do nothing since we do not what to freeze any of the version
180
// properties
181
}
182  
183     
184     /**
185      * On add aspect policy behaviour
186      *
187      * @param nodeRef
188      * @param aspectTypeQName
189      */

190     @SuppressWarnings JavaDoc("unchecked")
191     public void onAddAspect(NodeRef nodeRef, QName aspectTypeQName)
192     {
193         if (aspectTypeQName.equals(ContentModel.ASPECT_VERSIONABLE) == true)
194         {
195             boolean initialVersion = true;
196             Boolean JavaDoc value = (Boolean JavaDoc)this.nodeService.getProperty(nodeRef, ContentModel.PROP_INITIAL_VERSION);
197             if (value != null)
198             {
199                 initialVersion = value.booleanValue();
200             }
201             // else this means that the default vlaue has not been set the versionable aspect we applied pre-1.2
202

203             if (initialVersion == true)
204             {
205                 // Queue create version action
206
queueCreateVersionAction(nodeRef);
207             }
208         }
209     }
210     
211     /**
212      * On content update policy bahaviour
213      *
214      * @param nodeRef the node reference
215      */

216     public void onContentUpdate(NodeRef nodeRef, boolean newContent)
217     {
218         if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE) == true)
219         {
220             // Determine whether the node is auto versionable or not
221
boolean autoVersion = false;
222             Boolean JavaDoc value = (Boolean JavaDoc)this.nodeService.getProperty(nodeRef, ContentModel.PROP_AUTO_VERSION);
223             if (value != null)
224             {
225                 // If the value is not null then
226
autoVersion = value.booleanValue();
227             }
228             // else this means that the default value has not been set and the versionable aspect was applied pre-1.1
229

230             if (autoVersion == true)
231             {
232                 // Queue create version action
233
queueCreateVersionAction(nodeRef);
234             }
235         }
236     }
237     
238     /**
239      * Enable the auto version behaviour
240      *
241      */

242     public void enableAutoVersion()
243     {
244         this.autoVersionBehaviour.enable();
245     }
246     
247     /**
248      * Disable the auto version behaviour
249      *
250      */

251     public void disableAutoVersion()
252     {
253         this.autoVersionBehaviour.disable();
254     }
255     
256     /**
257      * Queue create version action
258      *
259      * @param nodeRef the node reference
260      */

261     private void queueCreateVersionAction(NodeRef nodeRef)
262     {
263         if (this.rule == null)
264         {
265             // Create the version action rule
266
this.rule = this.ruleService.createRule("inbound");
267             Action action = this.actionService.createAction(CreateVersionActionExecuter.NAME);
268             this.rule.addAction(action);
269         }
270         
271         // Stash the rule pending execution at the end of the transaction
272
((RuntimeRuleService)this.ruleService).addRulePendingExecution(nodeRef, nodeRef, this.rule, true);
273     }
274 }
275
Popular Tags