1 17 package org.alfresco.repo.policy; 18 19 import java.util.Collection ; 20 import java.util.HashSet ; 21 import java.util.Set ; 22 23 import org.alfresco.service.cmr.dictionary.AssociationDefinition; 24 import org.alfresco.service.cmr.dictionary.DictionaryService; 25 import org.alfresco.service.cmr.repository.NodeRef; 26 import org.alfresco.service.namespace.QName; 27 28 29 37 public class AssociationPolicyDelegate<P extends AssociationPolicy> 38 { 39 private DictionaryService dictionary; 40 private CachedPolicyFactory<ClassFeatureBehaviourBinding, P> factory; 41 42 43 50 @SuppressWarnings ("unchecked") 51 AssociationPolicyDelegate(DictionaryService dictionary, Class <P> policyClass, BehaviourIndex<ClassFeatureBehaviourBinding> index) 52 { 53 Collection <BehaviourDefinition> definitions = index.getAll(); 56 for (BehaviourDefinition definition : definitions) 57 { 58 definition.getBehaviour().getInterface(policyClass); 59 } 60 61 this.factory = new CachedPolicyFactory<ClassFeatureBehaviourBinding, P>(policyClass, index); 64 this.dictionary = dictionary; 65 } 66 67 73 private void checkAssocType(QName assocTypeQName) throws IllegalArgumentException 74 { 75 AssociationDefinition assocDef = dictionary.getAssociation(assocTypeQName); 76 if (assocDef == null) 77 { 78 throw new IllegalArgumentException ("Association " + assocTypeQName + " has not been defined in the data dictionary"); 79 } 80 } 81 82 93 public P get(QName classQName, QName assocTypeQName) 94 { 95 return get(null, classQName, assocTypeQName); 96 } 97 98 110 public P get(NodeRef nodeRef, QName classQName, QName assocTypeQName) 111 { 112 checkAssocType(assocTypeQName); 113 return factory.create(new ClassFeatureBehaviourBinding(dictionary, nodeRef, classQName, assocTypeQName)); 114 } 115 116 123 public Collection <P> getList(QName classQName, QName assocTypeQName) 124 { 125 return getList(null, classQName, assocTypeQName); 126 } 127 128 136 public Collection <P> getList(NodeRef nodeRef, QName classQName, QName assocTypeQName) 137 { 138 checkAssocType(assocTypeQName); 139 return factory.createList(new ClassFeatureBehaviourBinding(dictionary, nodeRef, classQName, assocTypeQName)); 140 } 141 142 149 public P get(Set <QName> classQNames, QName assocTypeQName) 150 { 151 return get(null, classQNames, assocTypeQName); 152 } 153 154 162 public P get(NodeRef nodeRef, Set <QName> classQNames, QName assocTypeQName) 163 { 164 checkAssocType(assocTypeQName); 165 return factory.toPolicy(getList(nodeRef, classQNames, assocTypeQName)); 166 } 167 168 175 public Collection <P> getList(Set <QName> classQNames, QName assocTypeQName) 176 { 177 return getList(null, classQNames, assocTypeQName); 178 } 179 180 188 public Collection <P> getList(NodeRef nodeRef, Set <QName> classQNames, QName assocTypeQName) 189 { 190 checkAssocType(assocTypeQName); 191 Collection <P> policies = new HashSet <P>(); 192 for (QName classQName : classQNames) 193 { 194 P policy = factory.create(new ClassFeatureBehaviourBinding(dictionary, nodeRef, classQName, assocTypeQName)); 195 if (policy instanceof PolicyList) 196 { 197 policies.addAll(((PolicyList<P>)policy).getPolicies()); 198 } 199 else 200 { 201 policies.add(policy); 202 } 203 } 204 return policies; 205 } 206 207 } 208 | Popular Tags |