Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Getting cq:lastReplicationAction value after Page deletion.

Avatar

Level 8

Hello,

I have created AEM page. At this time, there will not be any entry for cq:lastReplicationAction. I have created ReplicationEventHandler that will trigger during page activation, deactivation, deletion time.  

Now, I have 2 scenario's.  1: Page is deleted before any publishing the page (Not even published single time) .  2:  Page is deleted After publishing earlier. 

Now, I have deleted this page. When I Read the "cq:lastReplicationAction" from page Properties in my ReplicationEventHandler  file, for both scenario's it gives me: "Delete" . Actually, for the 1st scenario I was expecting null and for the 2nd scenario I was expecting some value, since that page is published earlier.

 

Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Level 2

Hi @Mahesh_Gunaje, Actually, EventHandler gets triggered after the event occurs, so by that time the "cq:lastReplicationAction" property value is already assigned.

In your case, you need to use a preprocessor to process the replication action before it happens.

Here is an example for your convenience:

@Component(
service = Preprocessor.class,
immediate = true,
property = {Constants.SERVICE_RANKING + ":Integer=1"}
)
public class ReplicationPreprocessor implements Preprocessor {

@Reference
private ResourceResolverFactory resourceResolverFactory;

@Override
public void preprocess(ReplicationAction action, ReplicationOptions options) {
ResourceResolver resourceResolver = resourceResolverFactory.getThreadResourceResolver();
PageManager pageManager;

if (resourceResolver != null) {
pageManager = resourceResolver.adaptTo(PageManager.class);

if (pageManager != null) {
Page currentPage = pageManager.getPage(action.getPath());

if(currentPage != null) {
ValueMap properties = currentPage.getProperties();
String lastReplicationAction = properties.get("cq:lastReplicationAction", "");
// Work with this 'lastReplicationAction' value
}
}
}
}
}

 

Let me know if this works for you.
Thanks

View solution in original post

2 Replies

Avatar

Correct answer by
Level 2

Hi @Mahesh_Gunaje, Actually, EventHandler gets triggered after the event occurs, so by that time the "cq:lastReplicationAction" property value is already assigned.

In your case, you need to use a preprocessor to process the replication action before it happens.

Here is an example for your convenience:

@Component(
service = Preprocessor.class,
immediate = true,
property = {Constants.SERVICE_RANKING + ":Integer=1"}
)
public class ReplicationPreprocessor implements Preprocessor {

@Reference
private ResourceResolverFactory resourceResolverFactory;

@Override
public void preprocess(ReplicationAction action, ReplicationOptions options) {
ResourceResolver resourceResolver = resourceResolverFactory.getThreadResourceResolver();
PageManager pageManager;

if (resourceResolver != null) {
pageManager = resourceResolver.adaptTo(PageManager.class);

if (pageManager != null) {
Page currentPage = pageManager.getPage(action.getPath());

if(currentPage != null) {
ValueMap properties = currentPage.getProperties();
String lastReplicationAction = properties.get("cq:lastReplicationAction", "");
// Work with this 'lastReplicationAction' value
}
}
}
}
}

 

Let me know if this works for you.
Thanks

Avatar

Community Advisor

Hi @Mahesh_Gunaje 
When you delete the page(using UI or pageMagaer api), the page is unpublished automatically , so you don't need to explicitly write any event listener to listen delete/unpublish events.



Arun Patidar