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.ChildAssociationDefinition; 23 import org.alfresco.service.cmr.dictionary.DictionaryService; 24 import org.alfresco.service.cmr.repository.ChildAssociationRef; 25 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 26 import org.alfresco.service.cmr.repository.NodeRef; 27 import org.alfresco.service.cmr.repository.NodeService; 28 import org.alfresco.service.namespace.QName; 29 import org.alfresco.service.namespace.RegexQNamePattern; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 38 public class AssocTargetRoleIntegrityEvent extends AbstractIntegrityEvent 39 { 40 private static Log logger = LogFactory.getLog(AssocTargetRoleIntegrityEvent.class); 41 42 public AssocTargetRoleIntegrityEvent( 43 NodeService nodeService, 44 DictionaryService dictionaryService, 45 NodeRef sourceNodeRef, 46 QName assocTypeQName, 47 QName assocName) 48 { 49 super(nodeService, dictionaryService, sourceNodeRef, assocTypeQName, assocName); 50 } 51 52 public void checkIntegrity(List <IntegrityRecord> eventResults) 53 { 54 NodeRef sourceNodeRef = getNodeRef(); 55 QName assocTypeQName = getTypeQName(); 56 QName assocQName = getQName(); 57 58 AssociationDefinition assocDef = getAssocDef(eventResults, assocTypeQName); 60 if (assocDef == null) 62 { 63 IntegrityRecord result = new IntegrityRecord( 64 "Association type does not exist: \n" + 65 " Association Type: " + assocTypeQName); 66 eventResults.add(result); 67 return; 68 } 69 70 if (assocQName == null) 72 { 73 throw new IllegalArgumentException ("The association qualified name must be supplied"); 74 } 75 if (!assocDef.isChild()) 76 { 77 throw new UnsupportedOperationException ("This operation is only relevant to child associations"); 78 } 79 ChildAssociationDefinition childAssocDef = (ChildAssociationDefinition) assocDef; 80 81 checkAssocQNameRegex(eventResults, childAssocDef, assocQName); 83 checkAssocQNameDuplicate(eventResults, childAssocDef, sourceNodeRef, assocQName); 84 } 85 86 89 protected void checkAssocQNameRegex( 90 List <IntegrityRecord> eventResults, 91 ChildAssociationDefinition assocDef, 92 QName assocQName) 93 { 94 QName assocRoleQName = assocDef.getTargetRoleName(); 96 if (assocRoleQName != null) 97 { 98 RegexQNamePattern rolePattern = new RegexQNamePattern(assocRoleQName.toString()); 100 if (!rolePattern.isMatch(assocQName)) 101 { 102 IntegrityRecord result = new IntegrityRecord( 103 "The association name does not match the allowed role names: \n" + 104 " Association: " + assocDef + "\n" + 105 " Allowed roles: " + rolePattern + "\n" + 106 " Name assigned: " + assocRoleQName); 107 eventResults.add(result); 108 } 109 } 110 } 111 112 115 protected void checkAssocQNameDuplicate( 116 List <IntegrityRecord> eventResults, 117 ChildAssociationDefinition assocDef, 118 NodeRef sourceNodeRef, 119 QName assocQName) 120 { 121 if (assocDef.getDuplicateChildNamesAllowed()) 122 { 123 return; 125 } 126 QName assocTypeQName = assocDef.getName(); 127 try 129 { 130 List <ChildAssociationRef> childAssocs = nodeService.getChildAssocs(sourceNodeRef, assocTypeQName, assocQName); 131 if (childAssocs.size() > 1) 133 { 134 IntegrityRecord result = new IntegrityRecord( 135 "Duplicate child associations are not allowed: \n" + 136 " Association: " + assocDef + "\n" + 137 " Name: " + assocQName); 138 eventResults.add(result); 139 } 140 } 141 catch (InvalidNodeRefException e) 142 { 143 } 145 } 146 } 147 | Popular Tags |