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.ClassDefinition; 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 36 public class ClassPolicyDelegate<P extends ClassPolicy> 37 { 38 private DictionaryService dictionary; 39 private CachedPolicyFactory<ClassBehaviourBinding, P> factory; 40 41 42 49 @SuppressWarnings ("unchecked") 50 ClassPolicyDelegate(DictionaryService dictionary, Class <P> policyClass, BehaviourIndex<ClassBehaviourBinding> index) 51 { 52 Collection <BehaviourDefinition> definitions = index.getAll(); 55 for (BehaviourDefinition definition : definitions) 56 { 57 definition.getBehaviour().getInterface(policyClass); 58 } 59 60 this.factory = new CachedPolicyFactory<ClassBehaviourBinding, P>(policyClass, index); 63 this.dictionary = dictionary; 64 } 65 66 67 77 public P get(QName classQName) 78 { 79 return get(null, classQName); 80 } 81 82 89 public P get(NodeRef nodeRef, QName classQName) 90 { 91 ClassDefinition classDefinition = dictionary.getClass(classQName); 92 if (classDefinition == null) 93 { 94 throw new IllegalArgumentException ("Class " + classQName + " has not been defined in the data dictionary"); 95 } 96 return factory.create(new ClassBehaviourBinding(dictionary, nodeRef, classQName)); 97 } 98 99 105 public Collection <P> getList(QName classQName) 106 { 107 return getList(null, classQName); 108 } 109 110 117 public Collection <P> getList(NodeRef nodeRef, QName classQName) 118 { 119 ClassDefinition classDefinition = dictionary.getClass(classQName); 120 if (classDefinition == null) 121 { 122 throw new IllegalArgumentException ("Class " + classQName + " has not been defined in the data dictionary"); 123 } 124 return factory.createList(new ClassBehaviourBinding(dictionary, nodeRef, classQName)); 125 } 126 127 134 public P get(Set <QName> classQNames) 135 { 136 return get(null, classQNames); 137 } 138 139 147 public P get(NodeRef nodeRef, Set <QName> classQNames) 148 { 149 return factory.toPolicy(getList(nodeRef, classQNames)); 150 } 151 152 158 public Collection <P> getList(Set <QName> classQNames) 159 { 160 return getList(null, classQNames); 161 } 162 163 169 public Collection <P> getList(NodeRef nodeRef, Set <QName> classQNames) 170 { 171 Collection <P> policies = new HashSet <P>(); 172 for (QName classQName : classQNames) 173 { 174 P policy = factory.create(new ClassBehaviourBinding(dictionary, nodeRef, classQName)); 175 if (policy instanceof PolicyList) 176 { 177 policies.addAll(((PolicyList<P>)policy).getPolicies()); 178 } 179 else 180 { 181 policies.add(policy); 182 } 183 } 184 return policies; 185 } 186 } 187 | Popular Tags |