001/*
002 * (C) Copyright 2010-2020 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 */
017package org.nuxeo.ecm.platform.comment.listener;
018
019import org.nuxeo.ecm.core.event.Event;
020import org.nuxeo.ecm.core.event.EventContext;
021import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
022import org.nuxeo.ecm.platform.comment.api.AnnotationConstants;
023import org.nuxeo.ecm.platform.comment.api.CommentConstants;
024import org.nuxeo.ecm.platform.ec.notification.NotificationListenerHook;
025
026/**
027 * Sets the top level document being commented as the source document of the event as some implementations fail on
028 * retrieving it and this avoids useless recursive calls. The top level document is the one holding the notification
029 * subscriptions, not the comments hierarchy below it.
030 *
031 * @since 5.5
032 * @author vpasquier
033 */
034public class CommentNotificationListener implements NotificationListenerHook {
035
036    @Override
037    public void handleNotifications(Event event) {
038        EventContext ctx = event.getContext();
039        DocumentEventContext docCtx = (DocumentEventContext) ctx;
040        if (docCtx.getSourceDocument().getType().equals("Post")
041                || docCtx.getSourceDocument().getType().equals(AnnotationConstants.ANNOTATION_DOC_TYPE)
042                || docCtx.getSourceDocument().getType().equals(CommentConstants.COMMENT_DOC_TYPE)) {
043            Object[] args = { docCtx.getProperty(CommentConstants.TOP_LEVEL_DOCUMENT), null };
044            docCtx.setArgs(args);
045        }
046    }
047}