Max_Salah
Max_Salah

Reputation: 2497

How to cancel page activation programmatically?

I would like to cancel the activation of some page, when x!=y is met. I am trying to do this with EventHandler. The author clicks on activate Page in the sidekick, and my EventHandler gets a replication event and tests if x!=y. If this is met, the page activation has to be canceled. My question is how do I cancel the page activation?

@Component(immediate = true, label = "TEST")
@Service
@Property(name = "event.topics", value = { ReplicationAction.EVENT_TOPIC })
public class EventHandler implements EventHandler {
    String feedback = "";

    public void handleEvent(final Event event) {
        String x = "foo";
        String y = "baar";
        if (x != y) {
            canclePageActivation();
            feedbackForAuthor = "Page can not be activated because x is not equal y";
        }
    }

}

Upvotes: 0

Views: 897

Answers (1)

Tomek Rękawek
Tomek Rękawek

Reputation: 9304

EventHandler is invoked after page is replicated. What you need here is com.day.cq.replication.Preprocessor. If you throw a ReplicationException in the preprocess() method, replication will be cancelled and user will get the exception message:

@Component(metatype = false, immediate = true)
@Service
public class SamplePreprocessor implements Preprocessor {

    @Override
    public void preprocess(ReplicationAction action, ReplicationOptions options) throws ReplicationException {
        if (somethingIsWrong()) {
            throw new ReplicationException("this message will be displayed to the user");
        }
    }
}

Upvotes: 4

Related Questions