Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cmlewis/1ded5447f47786d52347 to your computer and use it in GitHub Desktop.
Save cmlewis/1ded5447f47786d52347 to your computer and use it in GitHub Desktop.
Various methods using Alfresco's RegexQNamePattern to get associations
private static List<String> ASSOC_NAMES_TO_EXTRACT = Arrays.asList(new String[]{"imageRef","thumbnailRef"});
/**
* Gets a list of associations of type my:imageRef and my:thumbnailRef, given an Alfresco node;
* Implementation is not optimal
* @param nodeRef The Alfresco NodeRef that contains the associations we want to extract
* @return a List of associations of type my:imageRef and my:thumbnailRef
*/
public List<AssociationRef> getAssociations(NodeRef nodeRef) {
List<AssociationRef> associations = serviceRegistry.getNodeService().getTargetAssocs(nodeRef, RegexQNamePattern.MATCH_ALL);
List<AssociationRef> toReturn = new ArrayList<AssociationRef>();
// extract only my:imageRef and my:thumbnailRef assocs
for(AssociationRef association : associations) {
QName assocQname = association.getTypeQName();
if (ASSOC_NAMES_TO_EXTRACT.contains(assocQname.getLocalName())) {
toReturn.add(association);
}
}
return toReturn;
}
private static final RegexQNamePattern IMAGE_ASSOCIATIONS = new RegexQNamePattern(
".*thumbnailRef$|.*imageRef$");
/**
* Gets a list of associations of type my:imageRef and my:thumbnailRef, given an Alfresco node;
* @param nodeRef The Alfresco NodeRef that contains the associations we want to extract
* @return a List of associations of type my:imageRef and my:thumbnailRef
*/
public List<AssociationRef> getAssociations(NodeRef nodeRef) {
return serviceRegistry.getNodeService().getTargetAssocs(nodeRef, IMAGE_ASSOCIATIONS);
}
/**
* I am assuming that your custom Alfresco contentModel defines the following namespace
* <namespace uri="http:/session.it/alfresco/model/content/1.0" prefix="my"/>
*/
static final RegexQNamePattern ALL_MY_ASSOCIATIONS =
new RegexQNamePattern("^\\{http:\\/session.it\\/alfresco\\/model\\/content\\/1.0\\}.*");

Various methods using Alfresco's RegexQNamePattern to get associations using:

  1. RegexQnamePattern.MATCH_ALL to get all associations
  2. Getting associations by name without a namespace
  3. Getting all associations in a particular namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment