1 17 package org.alfresco.repo.node; 18 19 import java.io.Serializable ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.HashSet ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import org.alfresco.error.AlfrescoRuntimeException; 28 import org.alfresco.model.ContentModel; 29 import org.alfresco.repo.domain.PropertyValue; 30 import org.alfresco.repo.node.NodeServicePolicies.BeforeAddAspectPolicy; 31 import org.alfresco.repo.node.NodeServicePolicies.BeforeCreateChildAssociationPolicy; 32 import org.alfresco.repo.node.NodeServicePolicies.BeforeCreateNodePolicy; 33 import org.alfresco.repo.node.NodeServicePolicies.BeforeCreateStorePolicy; 34 import org.alfresco.repo.node.NodeServicePolicies.BeforeDeleteChildAssociationPolicy; 35 import org.alfresco.repo.node.NodeServicePolicies.BeforeDeleteNodePolicy; 36 import org.alfresco.repo.node.NodeServicePolicies.BeforeRemoveAspectPolicy; 37 import org.alfresco.repo.node.NodeServicePolicies.BeforeUpdateNodePolicy; 38 import org.alfresco.repo.node.NodeServicePolicies.OnAddAspectPolicy; 39 import org.alfresco.repo.node.NodeServicePolicies.OnCreateAssociationPolicy; 40 import org.alfresco.repo.node.NodeServicePolicies.OnCreateChildAssociationPolicy; 41 import org.alfresco.repo.node.NodeServicePolicies.OnCreateNodePolicy; 42 import org.alfresco.repo.node.NodeServicePolicies.OnCreateStorePolicy; 43 import org.alfresco.repo.node.NodeServicePolicies.OnDeleteAssociationPolicy; 44 import org.alfresco.repo.node.NodeServicePolicies.OnDeleteChildAssociationPolicy; 45 import org.alfresco.repo.node.NodeServicePolicies.OnDeleteNodePolicy; 46 import org.alfresco.repo.node.NodeServicePolicies.OnRemoveAspectPolicy; 47 import org.alfresco.repo.node.NodeServicePolicies.OnUpdateNodePolicy; 48 import org.alfresco.repo.node.NodeServicePolicies.OnUpdatePropertiesPolicy; 49 import org.alfresco.repo.policy.AssociationPolicyDelegate; 50 import org.alfresco.repo.policy.ClassPolicyDelegate; 51 import org.alfresco.repo.policy.PolicyComponent; 52 import org.alfresco.repo.search.Indexer; 53 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 54 import org.alfresco.service.cmr.dictionary.DictionaryException; 55 import org.alfresco.service.cmr.dictionary.PropertyDefinition; 56 import org.alfresco.service.cmr.repository.AssociationRef; 57 import org.alfresco.service.cmr.repository.ChildAssociationRef; 58 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 59 import org.alfresco.service.cmr.repository.NodeRef; 60 import org.alfresco.service.cmr.repository.NodeService; 61 import org.alfresco.service.cmr.repository.StoreRef; 62 import org.alfresco.service.cmr.repository.datatype.TypeConversionException; 63 import org.alfresco.service.namespace.QName; 64 import org.alfresco.service.namespace.QNamePattern; 65 import org.alfresco.service.namespace.RegexQNamePattern; 66 import org.alfresco.util.GUID; 67 68 79 public abstract class AbstractNodeServiceImpl implements NodeService 80 { 81 82 private String uuid; 83 84 private PolicyComponent policyComponent; 85 86 89 private ClassPolicyDelegate<BeforeCreateStorePolicy> beforeCreateStoreDelegate; 90 private ClassPolicyDelegate<OnCreateStorePolicy> onCreateStoreDelegate; 91 private ClassPolicyDelegate<BeforeCreateNodePolicy> beforeCreateNodeDelegate; 92 private ClassPolicyDelegate<OnCreateNodePolicy> onCreateNodeDelegate; 93 private ClassPolicyDelegate<BeforeUpdateNodePolicy> beforeUpdateNodeDelegate; 94 private ClassPolicyDelegate<OnUpdateNodePolicy> onUpdateNodeDelegate; 95 private ClassPolicyDelegate<OnUpdatePropertiesPolicy> onUpdatePropertiesDelegate; 96 private ClassPolicyDelegate<BeforeDeleteNodePolicy> beforeDeleteNodeDelegate; 97 private ClassPolicyDelegate<OnDeleteNodePolicy> onDeleteNodeDelegate; 98 private ClassPolicyDelegate<BeforeAddAspectPolicy> beforeAddAspectDelegate; 99 private ClassPolicyDelegate<OnAddAspectPolicy> onAddAspectDelegate; 100 private ClassPolicyDelegate<BeforeRemoveAspectPolicy> beforeRemoveAspectDelegate; 101 private ClassPolicyDelegate<OnRemoveAspectPolicy> onRemoveAspectDelegate; 102 private AssociationPolicyDelegate<BeforeCreateChildAssociationPolicy> beforeCreateChildAssociationDelegate; 103 private AssociationPolicyDelegate<OnCreateChildAssociationPolicy> onCreateChildAssociationDelegate; 104 private AssociationPolicyDelegate<BeforeDeleteChildAssociationPolicy> beforeDeleteChildAssociationDelegate; 105 private AssociationPolicyDelegate<OnDeleteChildAssociationPolicy> onDeleteChildAssociationDelegate; 106 private AssociationPolicyDelegate<OnCreateAssociationPolicy> onCreateAssociationDelegate; 107 private AssociationPolicyDelegate<OnDeleteAssociationPolicy> onDeleteAssociationDelegate; 108 109 114 protected AbstractNodeServiceImpl(PolicyComponent policyComponent) 115 { 116 this.uuid = GUID.generate(); 117 this.policyComponent = policyComponent; 118 } 119 120 123 public boolean equals(Object obj) 124 { 125 if (obj == null) 126 { 127 return false; 128 } 129 else if (!(obj instanceof AbstractNodeServiceImpl)) 130 { 131 return false; 132 } 133 AbstractNodeServiceImpl that = (AbstractNodeServiceImpl) obj; 134 return this.uuid.equals(that.uuid); 135 } 136 137 140 public int hashCode() 141 { 142 return uuid.hashCode(); 143 } 144 145 149 public void init() 150 { 151 beforeCreateStoreDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeCreateStorePolicy.class); 153 onCreateStoreDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnCreateStorePolicy.class); 154 beforeCreateNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeCreateNodePolicy.class); 155 onCreateNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnCreateNodePolicy.class); 156 beforeUpdateNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeUpdateNodePolicy.class); 157 onUpdateNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnUpdateNodePolicy.class); 158 onUpdatePropertiesDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnUpdatePropertiesPolicy.class); 159 beforeDeleteNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeDeleteNodePolicy.class); 160 onDeleteNodeDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnDeleteNodePolicy.class); 161 162 beforeAddAspectDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeAddAspectPolicy.class); 163 onAddAspectDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnAddAspectPolicy.class); 164 beforeRemoveAspectDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.BeforeRemoveAspectPolicy.class); 165 onRemoveAspectDelegate = policyComponent.registerClassPolicy(NodeServicePolicies.OnRemoveAspectPolicy.class); 166 167 beforeCreateChildAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.BeforeCreateChildAssociationPolicy.class); 168 onCreateChildAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.OnCreateChildAssociationPolicy.class); 169 beforeDeleteChildAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.BeforeDeleteChildAssociationPolicy.class); 170 onDeleteChildAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.OnDeleteChildAssociationPolicy.class); 171 172 onCreateAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.OnCreateAssociationPolicy.class); 173 onDeleteAssociationDelegate = policyComponent.registerAssociationPolicy(NodeServicePolicies.OnDeleteAssociationPolicy.class); 174 } 175 176 180 protected void invokeBeforeCreateStore(QName nodeTypeQName, StoreRef storeRef) 181 { 182 NodeServicePolicies.BeforeCreateStorePolicy policy = this.beforeCreateStoreDelegate.get(nodeTypeQName); 183 policy.beforeCreateStore(nodeTypeQName, storeRef); 184 } 185 186 189 protected void invokeOnCreateStore(NodeRef rootNodeRef) 190 { 191 Set <QName> qnames = getTypeAndAspectQNames(rootNodeRef); 193 NodeServicePolicies.OnCreateStorePolicy policy = onCreateStoreDelegate.get(qnames); 195 policy.onCreateStore(rootNodeRef); 196 } 197 198 202 protected void invokeBeforeCreateNode(NodeRef parentNodeRef, QName assocTypeQName, QName assocQName, QName childNodeTypeQName) 203 { 204 NodeServicePolicies.BeforeCreateNodePolicy policy = beforeCreateNodeDelegate.get(parentNodeRef, childNodeTypeQName); 206 policy.beforeCreateNode(parentNodeRef, assocTypeQName, assocQName, childNodeTypeQName); 207 } 208 209 212 protected void invokeOnCreateNode(ChildAssociationRef childAssocRef) 213 { 214 NodeRef childNodeRef = childAssocRef.getChildRef(); 215 Set <QName> qnames = getTypeAndAspectQNames(childNodeRef); 217 NodeServicePolicies.OnCreateNodePolicy policy = onCreateNodeDelegate.get(childNodeRef, qnames); 219 policy.onCreateNode(childAssocRef); 220 } 221 222 225 protected void invokeBeforeUpdateNode(NodeRef nodeRef) 226 { 227 Set <QName> qnames = getTypeAndAspectQNames(nodeRef); 229 NodeServicePolicies.BeforeUpdateNodePolicy policy = beforeUpdateNodeDelegate.get(nodeRef, qnames); 231 policy.beforeUpdateNode(nodeRef); 232 } 233 234 237 protected void invokeOnUpdateNode(NodeRef nodeRef) 238 { 239 Set <QName> qnames = getTypeAndAspectQNames(nodeRef); 241 NodeServicePolicies.OnUpdateNodePolicy policy = onUpdateNodeDelegate.get(nodeRef, qnames); 243 policy.onUpdateNode(nodeRef); 244 } 245 246 249 protected void invokeOnUpdateProperties( 250 NodeRef nodeRef, 251 Map <QName, Serializable > before, 252 Map <QName, Serializable > after) 253 { 254 Set <QName> qnames = getTypeAndAspectQNames(nodeRef); 256 NodeServicePolicies.OnUpdatePropertiesPolicy policy = onUpdatePropertiesDelegate.get(nodeRef, qnames); 258 policy.onUpdateProperties(nodeRef, before, after); 259 } 260 261 264 protected void invokeBeforeDeleteNode(NodeRef nodeRef) 265 { 266 Set <QName> qnames = getTypeAndAspectQNames(nodeRef); 268 NodeServicePolicies.BeforeDeleteNodePolicy policy = beforeDeleteNodeDelegate.get(nodeRef, qnames); 270 policy.beforeDeleteNode(nodeRef); 271 } 272 273 276 protected void invokeOnDeleteNode(ChildAssociationRef childAssocRef, QName childNodeTypeQName, Set <QName> childAspectQnames) 277 { 278 Set <QName> qnames = new HashSet <QName>(childAspectQnames.size() + 1); 280 qnames.addAll(childAspectQnames); 281 qnames.add(childNodeTypeQName); 282 283 NodeServicePolicies.OnDeleteNodePolicy policy = onDeleteNodeDelegate.get(childAssocRef.getChildRef(), qnames); 285 policy.onDeleteNode(childAssocRef); 286 } 287 288 292 protected void invokeBeforeAddAspect(NodeRef nodeRef, QName aspectTypeQName) 293 { 294 NodeServicePolicies.BeforeAddAspectPolicy policy = beforeAddAspectDelegate.get(nodeRef, aspectTypeQName); 295 policy.beforeAddAspect(nodeRef, aspectTypeQName); 296 } 297 298 301 protected void invokeOnAddAspect(NodeRef nodeRef, QName aspectTypeQName) 302 { 303 NodeServicePolicies.OnAddAspectPolicy policy = onAddAspectDelegate.get(nodeRef, aspectTypeQName); 304 policy.onAddAspect(nodeRef, aspectTypeQName); 305 } 306 307 311 protected void invokeBeforeRemoveAspect(NodeRef nodeRef, QName aspectTypeQName) 312 { 313 NodeServicePolicies.BeforeRemoveAspectPolicy policy = beforeRemoveAspectDelegate.get(nodeRef, aspectTypeQName); 314 policy.beforeRemoveAspect(nodeRef, aspectTypeQName); 315 } 316 317 321 protected void invokeOnRemoveAspect(NodeRef nodeRef, QName aspectTypeQName) 322 { 323 NodeServicePolicies.OnRemoveAspectPolicy policy = onRemoveAspectDelegate.get(nodeRef, aspectTypeQName); 324 policy.onRemoveAspect(nodeRef, aspectTypeQName); 325 } 326 327 331 protected void invokeBeforeCreateChildAssociation(NodeRef parentNodeRef, NodeRef childNodeRef, QName assocTypeQName, QName assocQName) 332 { 333 Set <QName> qnames = getTypeAndAspectQNames(parentNodeRef); 335 NodeServicePolicies.BeforeCreateChildAssociationPolicy policy = beforeCreateChildAssociationDelegate.get(parentNodeRef, qnames, assocTypeQName); 337 policy.beforeCreateChildAssociation(parentNodeRef, childNodeRef, assocTypeQName, assocQName); 338 } 339 340 343 protected void invokeOnCreateChildAssociation(ChildAssociationRef childAssocRef) 344 { 345 NodeRef parentNodeRef = childAssocRef.getParentRef(); 347 QName assocTypeQName = childAssocRef.getTypeQName(); 348 Set <QName> qnames = getTypeAndAspectQNames(parentNodeRef); 350 NodeServicePolicies.OnCreateChildAssociationPolicy policy = onCreateChildAssociationDelegate.get(parentNodeRef, qnames, assocTypeQName); 352 policy.onCreateChildAssociation(childAssocRef); 353 } 354 355 358 protected void invokeBeforeDeleteChildAssociation(ChildAssociationRef childAssocRef) 359 { 360 NodeRef parentNodeRef = childAssocRef.getParentRef(); 361 QName assocTypeQName = childAssocRef.getTypeQName(); 362 Set <QName> qnames = getTypeAndAspectQNames(parentNodeRef); 364 NodeServicePolicies.BeforeDeleteChildAssociationPolicy policy = beforeDeleteChildAssociationDelegate.get(parentNodeRef, qnames, assocTypeQName); 366 policy.beforeDeleteChildAssociation(childAssocRef); 367 } 368 369 372 protected void invokeOnDeleteChildAssociation(ChildAssociationRef childAssocRef) 373 { 374 NodeRef parentNodeRef = childAssocRef.getParentRef(); 375 QName assocTypeQName = childAssocRef.getTypeQName(); 376 Set <QName> qnames = getTypeAndAspectQNames(parentNodeRef); 378 NodeServicePolicies.OnDeleteChildAssociationPolicy policy = onDeleteChildAssociationDelegate.get(parentNodeRef, qnames, assocTypeQName); 380 policy.onDeleteChildAssociation(childAssocRef); 381 } 382 383 386 protected void invokeOnCreateAssociation(AssociationRef nodeAssocRef) 387 { 388 NodeRef sourceNodeRef = nodeAssocRef.getSourceRef(); 389 QName assocTypeQName = nodeAssocRef.getTypeQName(); 390 Set <QName> qnames = getTypeAndAspectQNames(sourceNodeRef); 392 NodeServicePolicies.OnCreateAssociationPolicy policy = onCreateAssociationDelegate.get(sourceNodeRef, qnames, assocTypeQName); 394 policy.onCreateAssociation(nodeAssocRef); 395 } 396 397 400 protected void invokeOnDeleteAssociation(AssociationRef nodeAssocRef) 401 { 402 NodeRef sourceNodeRef = nodeAssocRef.getSourceRef(); 403 QName assocTypeQName = nodeAssocRef.getTypeQName(); 404 Set <QName> qnames = getTypeAndAspectQNames(sourceNodeRef); 406 NodeServicePolicies.OnDeleteAssociationPolicy policy = onDeleteAssociationDelegate.get(sourceNodeRef, qnames, assocTypeQName); 408 policy.onDeleteAssociation(nodeAssocRef); 409 } 410 411 419 protected Set <QName> getTypeAndAspectQNames(NodeRef nodeRef) 420 { 421 Set <QName> qnames = null; 422 try 423 { 424 Set <QName> aspectQNames = getAspects(nodeRef); 425 QName typeQName = getType(nodeRef); 426 qnames = new HashSet <QName>(aspectQNames.size() + 1); 427 qnames.addAll(aspectQNames); 428 qnames.add(typeQName); 429 } 430 catch (InvalidNodeRefException e) 431 { 432 qnames = Collections.emptySet(); 433 } 434 return qnames; 436 } 437 438 445 protected String generateGuid(Map <QName, Serializable > preCreationProperties) 446 { 447 String uuid = (String ) preCreationProperties.get(ContentModel.PROP_NODE_UUID); 448 if (uuid == null) 449 { 450 uuid = GUID.generate(); 451 } 452 else 453 { 454 preCreationProperties.remove(ContentModel.PROP_NODE_UUID); 456 } 457 return uuid; 459 } 460 461 470 protected void removeReferencableProperties(Map <QName, Serializable > properties) 471 { 472 properties.remove(ContentModel.PROP_STORE_PROTOCOL); 473 properties.remove(ContentModel.PROP_STORE_IDENTIFIER); 474 properties.remove(ContentModel.PROP_NODE_UUID); 475 } 476 477 490 protected void addReferencableProperties(NodeRef nodeRef, Map <QName, Serializable > properties) 491 { 492 properties.put(ContentModel.PROP_STORE_PROTOCOL, nodeRef.getStoreRef().getProtocol()); 493 properties.put(ContentModel.PROP_STORE_IDENTIFIER, nodeRef.getStoreRef().getIdentifier()); 494 properties.put(ContentModel.PROP_NODE_UUID, nodeRef.getId()); 495 if (properties.get(ContentModel.PROP_NAME) == null) 497 { 498 properties.put(ContentModel.PROP_NAME, nodeRef.getId()); 499 } 500 } 501 502 508 public List <ChildAssociationRef> getParentAssocs(NodeRef nodeRef) throws InvalidNodeRefException 509 { 510 return getParentAssocs(nodeRef, RegexQNamePattern.MATCH_ALL, RegexQNamePattern.MATCH_ALL); 511 } 512 513 519 public final List <ChildAssociationRef> getChildAssocs(NodeRef nodeRef) throws InvalidNodeRefException 520 { 521 return getChildAssocs(nodeRef, RegexQNamePattern.MATCH_ALL, RegexQNamePattern.MATCH_ALL); 522 } 523 524 539 protected PropertyValue makePropertyValue(PropertyDefinition propertyDef, Serializable value) 540 { 541 QName propertyTypeQName = null; 543 if (propertyDef == null) { 545 propertyTypeQName = DataTypeDefinition.ANY; 547 } 548 else 549 { 550 propertyTypeQName = propertyDef.getDataType().getName(); 551 boolean isMultiValued = propertyDef.isMultiValued(); 553 if (isMultiValued && !(value instanceof Collection )) 554 { 555 if (value != null) 556 { 557 value = (Serializable ) Collections.singletonList(value); 560 } 561 } 562 else if (!isMultiValued && (value instanceof Collection )) 563 { 564 throw new DictionaryException("A single-valued property may not be a collection: \n" + 565 " Property: " + propertyDef + "\n" + 566 " Value: " + value); 567 } 568 } 569 try 570 { 571 PropertyValue propertyValue = new PropertyValue(propertyTypeQName, value); 572 return propertyValue; 574 } 575 catch (TypeConversionException e) 576 { 577 throw new TypeConversionException( 578 "The property value is not compatible with the type defined for the property: \n" + 579 " property: " + (propertyDef == null ? "unknown" : propertyDef) + "\n" + 580 " value: " + value + "\n" + 581 " value type: " + value.getClass(), 582 e); 583 } 584 } 585 586 594 protected Serializable makeSerializableValue(PropertyDefinition propertyDef, PropertyValue propertyValue) 595 { 596 if (propertyValue == null) 597 { 598 return null; 599 } 600 QName propertyTypeQName = null; 602 if (propertyDef == null) 603 { 604 propertyTypeQName = DataTypeDefinition.ANY; 606 } 607 else 608 { 609 propertyTypeQName = propertyDef.getDataType().getName(); 610 } 611 try 612 { 613 Serializable value = propertyValue.getValue(propertyTypeQName); 614 return value; 616 } 617 catch (TypeConversionException e) 618 { 619 throw new TypeConversionException( 620 "The property value is not compatible with the type defined for the property: \n" + 621 " property: " + (propertyDef == null ? "unknown" : propertyDef) + "\n" + 622 " property value: " + propertyValue, 623 e); 624 } 625 } 626 } 627 | Popular Tags |