1 17 package org.alfresco.repo.node.integrity; 18 19 import java.util.List ; 20 21 import org.alfresco.service.cmr.dictionary.AssociationDefinition; 22 import org.alfresco.service.cmr.dictionary.DictionaryService; 23 import org.alfresco.service.cmr.repository.AssociationRef; 24 import org.alfresco.service.cmr.repository.ChildAssociationRef; 25 import org.alfresco.service.cmr.repository.NodeRef; 26 import org.alfresco.service.cmr.repository.NodeService; 27 import org.alfresco.service.namespace.QName; 28 import org.alfresco.service.namespace.RegexQNamePattern; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 40 public class AssocSourceMultiplicityIntegrityEvent extends AbstractIntegrityEvent 41 { 42 private static Log logger = LogFactory.getLog(AssocSourceMultiplicityIntegrityEvent.class); 43 44 45 private boolean isDelete; 46 47 public AssocSourceMultiplicityIntegrityEvent( 48 NodeService nodeService, 49 DictionaryService dictionaryService, 50 NodeRef targetNodeRef, 51 QName assocTypeQName, 52 boolean isDelete) 53 { 54 super(nodeService, dictionaryService, targetNodeRef, assocTypeQName, null); 55 this.isDelete = isDelete; 56 } 57 58 @Override 59 public boolean equals(Object obj) 60 { 61 if (!super.equals(obj)) 62 { 63 return false; 64 } 65 AssocSourceMultiplicityIntegrityEvent that = (AssocSourceMultiplicityIntegrityEvent) obj; 67 return this.isDelete == that.isDelete; 68 } 69 70 public void checkIntegrity(List <IntegrityRecord> eventResults) 71 { 72 QName assocTypeQName = getTypeQName(); 73 NodeRef targetNodeRef = getNodeRef(); 74 QName targetNodeTypeQName = getNodeType(targetNodeRef); 76 if (targetNodeTypeQName == null) 77 { 78 if (logger.isDebugEnabled()) 80 { 81 logger.debug("Ignoring integrity check - node gone: \n" + 82 " event: " + this); 83 } 84 return; 85 } 86 87 AssociationDefinition assocDef = getAssocDef(eventResults, assocTypeQName); 89 if (assocDef == null) 91 { 92 if (!isDelete) { 94 IntegrityRecord result = new IntegrityRecord( 95 "Association type does not exist: \n" + 96 " Target Node Type: " + targetNodeTypeQName + "\n" + 97 " Association Type: " + assocTypeQName); 98 eventResults.add(result); 99 return; 100 } 101 else { 103 return; 104 } 105 } 106 107 checkSourceMultiplicity(eventResults, assocDef, assocTypeQName, targetNodeRef); 109 } 110 111 115 protected void checkSourceMultiplicity( 116 List <IntegrityRecord> eventResults, 117 AssociationDefinition assocDef, 118 QName assocTypeQName, 119 NodeRef targetNodeRef) 120 { 121 boolean mandatory = assocDef.isSourceMandatory(); 123 boolean allowMany = assocDef.isSourceMany(); 124 if (!mandatory && allowMany) 126 { 127 return; 129 } 130 int actualSize = 0; 131 if (assocDef.isChild()) 132 { 133 List <ChildAssociationRef> parentAssocRefs = nodeService.getParentAssocs( 135 targetNodeRef, 136 assocTypeQName, 137 RegexQNamePattern.MATCH_ALL); 138 actualSize = parentAssocRefs.size(); 139 } 140 else 141 { 142 List <AssociationRef> sourceAssocRefs = nodeService.getSourceAssocs(targetNodeRef, assocTypeQName); 144 actualSize = sourceAssocRefs.size(); 145 } 146 if ((mandatory && actualSize == 0) || (!allowMany && actualSize > 1)) 147 { 148 String parentOrSourceStr = (assocDef.isChild() ? "child" : "target"); 149 IntegrityRecord result = new IntegrityRecord( 150 "The association " + parentOrSourceStr + " multiplicity has been violated: \n" + 151 " Association: " + assocDef + "\n" + 152 " Required " + parentOrSourceStr + " Multiplicity: " + getMultiplicityString(mandatory, allowMany) + "\n" + 153 " Actual " + parentOrSourceStr + " Multiplicity: " + actualSize); 154 eventResults.add(result); 155 } 156 } 157 } 158 | Popular Tags |