如何以编程方式取消页面激活?

时间:2013-10-29 15:08:08

标签: java event-handling cq5

我希望在满足x!=y时取消激活某个页面。我试图用EventHandler来做这件事。作者点击了activate Page中的sidekick,我的EventHandler获得了一个复制事件并测试了x!=y。如果满足,则必须取消激活页面。我的问题是如何取消页面激活?

@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";
        }
    }

}

1 个答案:

答案 0 :(得分:3)

复制页面后,

EventHandler被调用。你需要的是com.day.cq.replication.Preprocessor。如果在ReplicationException方法中抛出preprocess(),则复制将被取消,用户将收到异常消息:

@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");
        }
    }
}