When working with the Event API and Parent-Child associations there is one data object called ChildAssociationResource that is used over and over. It’s used to get to the JSON association data returned in the JMS message payload.
Here is an example payload for a Parent-Child association created event (see the Parent-Child association created event section in the Alfresco Content Services documentation):
{
"specversion": "1.0",
"type": "org.alfresco.event.assoc.child.Created",
"id": "4014bcb2-f1e6-447f-8caa-3a6219bc94ad",
"source": "/08d9b620-48de-4247-8f33-360988d3b19b",
"time": "2021-01-28T13:42:34.329162Z",
"dataschema": "https://api.alfresco.com/schema/event/repo/v1/childAssocCreated",
"datacontenttype": "application/json",
"data": {
"eventGroupId": "78da21cc-fa5a-47d1-afcb-03005229efa9",
"resource": {
"@type": "ChildAssociationResource",
"assocType": "fdk:images",
"parent": {
"id": "a4eb7684-0ffe-4bf5-b6f7-4297a6e4ee84"
},
"child": {
"id": "ceb3c804-8b32-4050-b2da-b55c47f01666"
}
}
}
}
In an event handler, being it pure Java or Spring Integration based, we can get to the payload data via the org.alfresco.event.sdk.model.v1.model.ChildAssociationResource object:
public class ParentChildAssocCreatedEventHandler implements OnChildAssocCreatedEventHandler {
public void handleEvent(final RepoEvent<DataAttributes<Resource>> repoEvent) {
ChildAssociationResource resource = (ChildAssociationResource) repoEvent.getData().getResource();
LOGGER.info("A secondary Parent-Child association of type {} was created between nodes: {} -> {}",
resource.getAssocType(), resource.getParent().getId(), resource.getChild().getId());
}
}
The resource.getParent().getId() call will return the Node ID for the parent node in the association and the resource.getChild().getId() call will return the Node ID for the child node.