1 17 package org.alfresco.repo.version.common; 18 19 import java.io.Serializable ; 20 import java.util.Collection ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Set ; 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 50 public abstract class AbstractVersionServiceImpl 51 { 52 55 protected NodeService nodeService ; 56 57 60 protected PolicyComponent policyComponent; 61 62 65 protected DictionaryService dictionaryService; 66 67 70 private ClassPolicyDelegate<BeforeCreateVersionPolicy> beforeCreateVersionDelegate; 71 private ClassPolicyDelegate<OnCreateVersionPolicy> onCreateVersionDelegate; 72 private ClassPolicyDelegate<CalculateVersionLabelPolicy> calculateVersionLabelDelegate; 73 74 79 public void setNodeService(NodeService nodeService) 80 { 81 this.nodeService = nodeService; 82 } 83 84 89 public void setPolicyComponent(PolicyComponent policyComponent) 90 { 91 this.policyComponent = policyComponent; 92 } 93 94 99 public void setDictionaryService(DictionaryService dictionaryService) 100 { 101 this.dictionaryService = dictionaryService; 102 } 103 104 107 public void initialise() 108 { 109 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 120 protected void invokeBeforeCreateVersion(NodeRef nodeRef) 121 { 122 QName nodeTypeQName = nodeService.getType(nodeRef); 124 this.beforeCreateVersionDelegate.get(nodeTypeQName).beforeCreateVersion(nodeRef); 125 Set <QName> nodeAspectQNames = nodeService.getAspects(nodeRef); 127 this.beforeCreateVersionDelegate.get(nodeAspectQNames).beforeCreateVersion(nodeRef); 128 } 129 130 134 protected void invokeOnCreateVersion( 135 NodeRef nodeRef, 136 Map <String , Serializable > versionProperties, 137 PolicyScope nodeDetails) 138 { 139 QName classRef = this.nodeService.getType(nodeRef); 141 invokeOnCreateVersion(classRef, nodeRef, versionProperties, nodeDetails); 142 143 Collection <QName> aspects = this.nodeService.getAspects(nodeRef); 145 for (QName aspect : aspects) 146 { 147 invokeOnCreateVersion(aspect, nodeRef, versionProperties, nodeDetails); 148 } 149 150 } 151 152 160 private void invokeOnCreateVersion( 161 QName classRef, 162 NodeRef nodeRef, 163 Map <String , Serializable > versionProperties, 164 PolicyScope nodeDetails) 165 { 166 Collection <OnCreateVersionPolicy> policies = this.onCreateVersionDelegate.getList(classRef); 167 if (policies.size() == 0) 168 { 169 defaultOnCreateVersion( 171 classRef, 172 nodeRef, 173 versionProperties, 174 nodeDetails); 175 } 176 else 177 { 178 for (VersionServicePolicies.OnCreateVersionPolicy policy : policies) 180 { 181 policy.onCreateVersion( 182 classRef, 183 nodeRef, 184 versionProperties, 185 nodeDetails); 186 } 187 } 188 } 189 190 198 protected void defaultOnCreateVersion( 199 QName classRef, 200 NodeRef nodeRef, 201 Map <String , Serializable > versionProperties, 202 PolicyScope nodeDetails) 203 { 204 ClassDefinition classDefinition = this.dictionaryService.getClass(classRef); 205 if (classDefinition != null) 206 { 207 Map <QName,PropertyDefinition> propertyDefinitions = classDefinition.getProperties(); 209 for (QName propertyName : propertyDefinitions.keySet()) 210 { 211 Serializable propValue = this.nodeService.getProperty(nodeRef, propertyName); 212 nodeDetails.addProperty(classRef, propertyName, propValue); 213 } 214 215 Map <QName, AssociationDefinition> assocDefs = classDefinition.getAssociations(); 217 218 if (classDefinition.isContainer()) 220 { 221 List <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 List <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 252 protected String invokeCalculateVersionLabel( 253 QName classRef, 254 Version preceedingVersion, 255 int versionNumber, 256 Map <String , Serializable >versionProperties) 257 { 258 String versionLabel = null; 259 260 Collection <CalculateVersionLabelPolicy> behaviours = this.calculateVersionLabelDelegate.getList(classRef); 261 if (behaviours.size() == 0) 262 { 263 versionLabel = Integer.toString(versionNumber); 265 } 266 else if (behaviours.size() == 1) 267 { 268 CalculateVersionLabelPolicy[] arr = behaviours.toArray(new CalculateVersionLabelPolicy[]{}); 270 versionLabel = arr[0].calculateVersionLabel(classRef, preceedingVersion, versionNumber, versionProperties); 271 } 272 else 273 { 274 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 |