Expand my Community achievements bar.

Attention: Experience League Community will undergo scheduled maintenance on Tuesday, August 20th between 10-11 PM PDT. During this time, the Community and its content will not be accessible. We apologize for any inconvenience this may cause.

How to keep track of old and new path after asset is moved

Avatar

Level 4

Hello, community!!

My current project has used 3rd party search provider to index the pages and documents. A scheduler runs and indexes the published documents to the 3rd party source. If a document is unpublished and is present in the 3rd party source it is deleted from the source. For all these indexing there is a scheduler that runs every midnight. 

The problem arises when someone moves a document in aem, the scheduler runs and indexes the document in the 3rd party source once again as a new entry. As the document in the old path was never unpublished, we cannot even delete it from the source. 

 

I need a solution so that whenever I move a document (asset under a certain path), I can keep track of the old and new paths. On completion of the move operation, I want to remove the old path from the source (3rd party) and index the new path. Is there a way to keep track of the paths? Can we do this with the help of the event listener or handler? 

1 Reply

Avatar

Community Advisor

Hi @Ankan_Ghosh,

You can use EventHandler together with DAM EVENT_TOPIC, which provides DAM/Asset specific events. Next you can use below properties form event to get old and new path:

  • srcAbsPath - contains information about old/original path
  • assetPath - contains information about new/destination path

Here is a code example.

package com.sample.events;

import com.day.cq.dam.api.DamEvent;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(
        immediate = true,
        service = EventHandler.class,
        property = EventConstants.EVENT_TOPIC + "=" + DamEvent.EVENT_TOPIC)
public class DAMEventHandler implements EventHandler {

    private static final Logger LOG = LoggerFactory.getLogger(DAMEventHandler.class);

    @Override
    public void handleEvent(Event event) {
        DamEvent damEvent = DamEvent.fromEvent(event);
        if (DamEvent.Type.ASSET_MOVED.equals(damEvent.getType())) {
            if (event.containsProperty("assetPath") && event.containsProperty("srcAbsPath")) {
                String oldPath = event.getProperty("srcAbsPath").toString();
                String newPath = event.getProperty("assetPath").toString();
                LOG.info("Old path: " + oldPath);
                LOG.info("New path: " + newPath);
                // place for some other code
            }
        }
    }
}

Please make sure you do not put any heavy logic inside EventHandler, rather use Sling Jobs to delegate this. Heavy logic can cause an issue that EventHandler will be blacklisted an will not be run anymore.