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 AssocTargetMultiplicityIntegrityEvent extends AbstractIntegrityEvent 41 { 42 private static Log logger = LogFactory.getLog(AssocTargetMultiplicityIntegrityEvent.class); 43 44 45 private boolean isDelete; 46 47 public AssocTargetMultiplicityIntegrityEvent( 48 NodeService nodeService, 49 DictionaryService dictionaryService, 50 NodeRef sourceNodeRef, 51 QName assocTypeQName, 52 boolean isDelete) 53 { 54 super(nodeService, dictionaryService, sourceNodeRef, 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 AssocTargetMultiplicityIntegrityEvent that = (AssocTargetMultiplicityIntegrityEvent) obj; 67 return this.isDelete == that.isDelete; 68 } 69 70 public void checkIntegrity(List <IntegrityRecord> eventResults) 71 { 72 QName assocTypeQName = getTypeQName(); 73 NodeRef sourceNodeRef = getNodeRef(); 74 QName sourceNodeTypeQName = getNodeType(sourceNodeRef); 76 if (sourceNodeTypeQName == 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 " Source Node Type: " + sourceNodeTypeQName + "\n" + 97 " Association Type: " + assocTypeQName); 98 eventResults.add(result); 99 return; 100 } 101 else { 103 return; 104 } 105 } 106 107 checkTargetMultiplicity(eventResults, assocDef, assocTypeQName, sourceNodeRef); 109 } 110 111 115 protected void checkTargetMultiplicity( 116 List <IntegrityRecord> eventResults, 117 AssociationDefinition assocDef, 118 QName assocTypeQName, 119 NodeRef sourceNodeRef) 120 { 121 boolean mandatory = assocDef.isTargetMandatory(); 123 boolean allowMany = assocDef.isTargetMany(); 124 if (!mandatory && allowMany) 126 { 127 return; 129 } 130 int actualSize = 0; 131 if (assocDef.isChild()) 132 { 133 List <ChildAssociationRef> childAssocRefs = nodeService.getChildAssocs( 135 sourceNodeRef, 136 assocTypeQName, 137 RegexQNamePattern.MATCH_ALL); 138 actualSize = childAssocRefs.size(); 139 } 140 else 141 { 142 List <AssociationRef> targetAssocRefs = nodeService.getTargetAssocs(sourceNodeRef, assocTypeQName); 144 actualSize = targetAssocRefs.size(); 145 } 146 if ((mandatory && actualSize == 0) || (!allowMany && actualSize > 1)) 147 { 148 String childOrTargetStr = (assocDef.isChild() ? "child" : "target"); 149 IntegrityRecord result = new IntegrityRecord( 150 "The association " + childOrTargetStr + " multiplicity has been violated: \n" + 151 " Association: " + assocDef + "\n" + 152 " Required " + childOrTargetStr + " Multiplicity: " + getMultiplicityString(mandatory, allowMany) + "\n" + 153 " Actual " + childOrTargetStr + " Multiplicity: " + actualSize); 154 eventResults.add(result); 155 } 156 } 157 } 158 | Popular Tags |