Peer Association Deleted Event - Alfresco Content Services - 23.4 - 23.4 - Ready - Alfresco - external

Alfresco Content Services

Platform
Alfresco
Product
Alfresco Content Services
Release
23.4
License

This event is fired whenever a peer association is deleted, such as via the the DELETE nodes/{sourceId}/targets. For more information, see Deleting Associations.

ReST API. The full name of this event is org.alfresco.event.assoc.peer.Deleted.

Here is an example payload for this event type:

{
  "specversion": "1.0",
  "type": "org.alfresco.event.assoc.peer.Deleted",
  "id": "630a2b78-5832-42ad-89e0-bafaed73df3d",
  "source": "/08d9b620-48de-4247-8f33-360988d3b19b",
  "time": "2021-02-01T10:39:37.329006Z",
  "dataschema": "https://api.alfresco.com/schema/event/repo/v1/peerAssocDeleted",
  "datacontenttype": "application/json",
  "data": {
    "eventGroupId": "27e7f158-707b-48ee-87ca-efd247e6cbb7",
    "resource": {
      "@type": "PeerAssociationResource",
      "assocType": "fdk:reviews",
      "source": {
        "id": "a4eb7684-0ffe-4bf5-b6f7-4297a6e4ee84"
      },
      "target": {
        "id": "f826ac49-0262-48af-8f63-f87eb7007078"
      }
    }
  }
}

Using the Node Browser (see Using the Node Browser) the following NodeRefs were resolved as follows:

  "source": {
    "id": "a4eb7684-0ffe-4bf5-b6f7-4297a6e4ee84"  /app:company_home/cm:My_x0020_Gadgets/cm:My_x0020_Gadget  
  },
  "target": {
    "id": "f826ac49-0262-48af-8f63-f87eb7007078"  /sys:archivedItem/gadget-review.txt

The event payload is telling us that a peer association of type fdk:reviews (i.e. data.resource.assocType) was deleted between a gadget file My Gadget (i.e. data.resource.source) and a gadget review gadget-review.txt (i.e. data.resource.target).

Note: When you use the Node Browser to look for the deleted gadget-review.txt file (i.e. with ID f826ac49-0262-48af-8f63-f87eb7007078) you have to search in the archive://SpacesStore store to find it. This store contains soft deleted files.

When subscribing to the org.alfresco.event.assoc.peer.Deleted event it’s possible to filter out anything that is of no interest. So for example, if you are only interested in associations of type fdk:reviews it would be easy to configure this.

SDK5 - Plain Java

The following code shows how this can be done with SDK 5 and plain Java event handlers:

import org.alfresco.event.sdk.handling.filter.AssocTypeFilter;
import org.alfresco.event.sdk.handling.filter.EventFilter;
import org.alfresco.event.sdk.handling.handler.OnPeerAssocDeletedEventHandler;
import org.alfresco.event.sdk.model.v1.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
* Sample event handler to demonstrate reacting to a peer-2-peer assoc being deleted.
*/
@Component
public class Peer2PeerAssocDeletedEventHandler implements OnPeerAssocDeletedEventHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(Peer2PeerAssocDeletedEventHandler.class);

    public void handleEvent(final RepoEvent<DataAttributes<Resource>> repoEvent) {
        PeerAssociationResource resource = (PeerAssociationResource) repoEvent.getData().getResource();
        LOGGER.info("A Peer-Peer association was deleted: Source {} -> Target {}", resource.getSource().getId(),
                resource.getTarget().getId());
    }

    public EventFilter getEventFilter() {
        return AssocTypeFilter.of("fdk:reviews"); // Make sure the Peer-Peer association is of type FDK Reviews
    }
}

This code uses the org.alfresco.event.sdk.handling.filter.AssocTypeFilter event filter to specify what type of Peer-2-Peer association we are interested in.

For more information about how to extract all the properties from the message payload see the PeerAssociationResource information in Software Development Kits (SDK).

To create an SDK event handler project that uses plain Java event handlers follow the instructions on pure Java event handlers in Software Development Kits (SDK).

SDK5 - Spring Integration

The following code shows how this can be done with SDK 5 and Spring Integration event handlers:

package org.alfresco.tutorial.events;

import org.alfresco.event.sdk.handling.filter.AssocTypeFilter;
import org.alfresco.event.sdk.handling.filter.EventTypeFilter;
import org.alfresco.event.sdk.integration.EventChannels;
import org.alfresco.event.sdk.integration.filter.IntegrationEventFilter;
import org.alfresco.event.sdk.model.v1.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.dsl.IntegrationFlowAdapter;
import org.springframework.integration.dsl.IntegrationFlowDefinition;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * Spring Integration based event handler that will execute code when a peer-2-peer assoc is being deleted.
 */
@Component
public class Peer2PeerAssocDeletedFlow extends IntegrationFlowAdapter {
    private static final Logger LOGGER = LoggerFactory.getLogger(Peer2PeerAssocDeletedFlow.class);

    // Use builder to create an integration flow based on alfresco.events.main.channel event channel
    @Override
    protected IntegrationFlowDefinition<?> buildFlow() {
        return from(EventChannels.MAIN) // Listen to events coming from the Alfresco events channel
                .filter(IntegrationEventFilter.of(EventTypeFilter.PEER_ASSOC_DELETED)) // Filter events and select only Peer2Peer assoc deleted events
                .filter(IntegrationEventFilter.of(AssocTypeFilter.of("fdk:reviews"))) // Make sure the Peer2Peer association is of type FDK Reviews
                .handle(t -> handleEvent(t)); // Handle event with a bit of logging
    }

    private void handleEvent(Message message) {
        RepoEvent<DataAttributes<Resource>> repoEvent = (RepoEvent<DataAttributes<Resource>>)message.getPayload();
        PeerAssociationResource resource = (PeerAssociationResource) repoEvent.getData().getResource();
        LOGGER.info("A Peer-Peer association was deleted: Source {} -> Target {}", resource.getSource().getId(),
                resource.getTarget().getId());
    }
}

This code uses the org.alfresco.event.sdk.handling.filter.AssocTypeFilter event filter to specify what type of Peer-2-Peer association we are interested in.

For more information about how to extract all the properties from the message payload see the PeerAssociationResource information in Software Development Kits (SDK).

To create an SDK event handler project that uses plain Java event handlers follow the instructions on pure Java event handlers in Software Development Kits (SDK).

Apache Camel

The following code snippet shows how this could be done with an Apache Camel route configuration:

public class SimpleRoute extends RouteBuilder {

    @Override
    public void configure() {
        from("amqpConnection:topic:alfresco.repo.event2")
            .id("PeerAssocDeletedRoute")
            .log("${body}") // Log all incoming events on this topic, even those that we are not interested in
            .choice()
            .when() // When the following is true:
            // The event type is peer assoc deleted
            .jsonpath("$[?(@.type=='org.alfresco.event.assoc.peer.Deleted' && " +
                    // and the association type is fdk:reviews
                    "@.data.resource.assocType=='fdk:reviews')]" )
            // Unpack the data into JSON format
            .unmarshal("publicDataFormat")
            // Call a Spring Bean with the event data
            .bean("peerAssocDeletedEventHandlerImpl", "onReceive(*, COPY)")
            .end();
    }
}

The jsonpath expression uses several of the event data properties to filter out exactly the events we are interested in.

In this case a Spring Bean with ID peerAssocDeletedEventHandlerImpl is called at the end of the route from where you could make the necessary ReST API calls.