001/*
002 * (C) Copyright 2010-2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 */
015package org.nuxeo.ecm.platform.comment.listener;
016
017import org.nuxeo.ecm.core.api.DocumentModel;
018import org.nuxeo.ecm.core.event.Event;
019import org.nuxeo.ecm.core.event.EventContext;
020import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
021import org.nuxeo.ecm.platform.comment.api.CommentManager;
022import org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants;
023import org.nuxeo.ecm.platform.ec.notification.NotificationListenerHook;
024import org.nuxeo.runtime.api.Framework;
025
026/**
027 * @description the related thread of comments is retrieved for sending to its subscribers
028 * @since 5.5
029 * @author vpasquier
030 */
031public class CommentNotificationListener implements NotificationListenerHook {
032
033    @Override
034    public void handleNotifications(Event event) {
035        EventContext ctx = event.getContext();
036        DocumentEventContext docCtx = (DocumentEventContext) ctx;
037        if (docCtx.getSourceDocument().getType().equals("Post")
038                || docCtx.getSourceDocument().getType().equals(CommentsConstants.COMMENT_DOC_TYPE)) {
039            CommentManager commentManager = Framework.getService(CommentManager.class);
040            DocumentModel thread = commentManager.getThreadForComment(docCtx.getSourceDocument());
041            if (thread != null) {
042                Object[] args = { thread, null };
043                docCtx.setArgs(args);
044            }
045        }
046    }
047}