001/*
002 * (C) Copyright 2010-2011 Nuxeo SA (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.api.DocumentModel;
020import org.nuxeo.ecm.core.event.Event;
021import org.nuxeo.ecm.core.event.EventContext;
022import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
023import org.nuxeo.ecm.platform.comment.api.CommentManager;
024import org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants;
025import org.nuxeo.ecm.platform.ec.notification.NotificationListenerHook;
026import org.nuxeo.runtime.api.Framework;
027
028/**
029 * @description the related thread of comments is retrieved for sending to its subscribers
030 * @since 5.5
031 * @author vpasquier
032 */
033public class CommentNotificationListener implements NotificationListenerHook {
034
035    @Override
036    public void handleNotifications(Event event) {
037        EventContext ctx = event.getContext();
038        DocumentEventContext docCtx = (DocumentEventContext) ctx;
039        if (docCtx.getSourceDocument().getType().equals("Post")
040                || docCtx.getSourceDocument().getType().equals(CommentsConstants.COMMENT_DOC_TYPE)) {
041            CommentManager commentManager = Framework.getService(CommentManager.class);
042            DocumentModel thread = commentManager.getThreadForComment(docCtx.getSourceDocument());
043            if (thread != null) {
044                Object[] args = { thread, null };
045                docCtx.setArgs(args);
046            }
047        }
048    }
049}