1 17 package org.alfresco.repo.version; 18 19 import java.io.Serializable ; 20 import java.util.Map ; 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 45 public class VersionableAspect implements ContentServicePolicies.OnContentUpdatePolicy 46 { 47 50 private PolicyComponent policyComponent; 51 52 55 private NodeService nodeService; 56 57 60 private RuleService ruleService; 61 62 65 private ActionService actionService; 66 67 70 private Rule rule; 71 72 75 private Behaviour autoVersionBehaviour; 76 77 82 public void setPolicyComponent(PolicyComponent policyComponent) 83 { 84 this.policyComponent = policyComponent; 85 } 86 87 92 public void setRuleService(RuleService ruleService) 93 { 94 this.ruleService = ruleService; 95 } 96 97 102 public void setActionService(ActionService actionService) 103 { 104 this.actionService = actionService; 105 } 106 107 112 public void setNodeService(NodeService nodeService) 113 { 114 this.nodeService = nodeService; 115 } 116 117 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 this.policyComponent.bindClassBehaviour( 134 QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyNode"), 135 ContentModel.ASPECT_VERSIONABLE, 136 new JavaBehaviour(this, "onCopy")); 137 } 138 139 147 public void onCopy( 148 QName sourceClassRef, 149 NodeRef sourceNodeRef, 150 StoreRef destinationStoreRef, 151 boolean copyToNewNode, 152 PolicyScope copyDetails) 153 { 154 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 173 public void onCreateVersion( 174 QName classRef, 175 NodeRef versionableNode, 176 Map <String , Serializable > versionProperties, 177 PolicyScope nodeDetails) 178 { 179 } 182 183 184 190 @SuppressWarnings ("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 value = (Boolean )this.nodeService.getProperty(nodeRef, ContentModel.PROP_INITIAL_VERSION); 197 if (value != null) 198 { 199 initialVersion = value.booleanValue(); 200 } 201 203 if (initialVersion == true) 204 { 205 queueCreateVersionAction(nodeRef); 207 } 208 } 209 } 210 211 216 public void onContentUpdate(NodeRef nodeRef, boolean newContent) 217 { 218 if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE) == true) 219 { 220 boolean autoVersion = false; 222 Boolean value = (Boolean )this.nodeService.getProperty(nodeRef, ContentModel.PROP_AUTO_VERSION); 223 if (value != null) 224 { 225 autoVersion = value.booleanValue(); 227 } 228 230 if (autoVersion == true) 231 { 232 queueCreateVersionAction(nodeRef); 234 } 235 } 236 } 237 238 242 public void enableAutoVersion() 243 { 244 this.autoVersionBehaviour.enable(); 245 } 246 247 251 public void disableAutoVersion() 252 { 253 this.autoVersionBehaviour.disable(); 254 } 255 256 261 private void queueCreateVersionAction(NodeRef nodeRef) 262 { 263 if (this.rule == null) 264 { 265 this.rule = this.ruleService.createRule("inbound"); 267 Action action = this.actionService.createAction(CreateVersionActionExecuter.NAME); 268 this.rule.addAction(action); 269 } 270 271 ((RuntimeRuleService)this.ruleService).addRulePendingExecution(nodeRef, nodeRef, this.rule, true); 273 } 274 } 275 | Popular Tags |