001/*
002 * (C) Copyright 2009 Nuxeo SAS (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 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.ec.notification;
021
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.IdRef;
028import org.nuxeo.ecm.core.api.event.CoreEventConstants;
029import org.nuxeo.ecm.core.event.Event;
030import org.nuxeo.ecm.core.event.EventContext;
031import org.nuxeo.ecm.core.event.EventListener;
032import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
033import org.nuxeo.ecm.platform.ec.notification.service.NotificationService;
034import org.nuxeo.ecm.platform.ec.notification.service.NotificationServiceHelper;
035
036/**
037 * Propagate previously set notifications when a proxy is replaced by a new version.
038 *
039 * @author ogrisel
040 */
041public class ProxySubscriptionPropagationListener implements EventListener {
042
043    private static final Log log = LogFactory.getLog(ProxySubscriptionPropagationListener.class);
044
045    @SuppressWarnings("unchecked")
046    public void handleEvent(Event event) {
047
048        EventContext ctx = event.getContext();
049
050        if (!(ctx instanceof DocumentEventContext)) {
051            // we are only interested in propagating notification for document
052            // proxies
053            return;
054        }
055        DocumentEventContext docCtx = (DocumentEventContext) ctx;
056        DocumentModel publishedDoc = docCtx.getSourceDocument();
057        if (!publishedDoc.isProxy()) {
058            // we are only interested in the publication of proxy documents
059            return;
060        }
061
062        NotificationService service = NotificationServiceHelper.getNotificationService();
063        if (service == null) {
064            log.error("Unable to get NotificationService, exiting");
065            return;
066        }
067
068        List<String> replacedProxyIds = (List<String>) ctx.getProperties().get(CoreEventConstants.REPLACED_PROXY_IDS);
069        if (replacedProxyIds == null) {
070            return;
071        }
072
073        for (String replacedProxyId : replacedProxyIds) {
074            // there should be only one replaced proxy, but just in case,
075            // iterate over them
076            DocumentModel fromDoc = ctx.getCoreSession().getDocument(new IdRef(replacedProxyId));
077            fromDoc.getAdapter(SubscriptionAdapter.class).copySubscriptionsTo(publishedDoc);
078        }
079        ctx.getCoreSession().saveDocument(publishedDoc);
080    }
081
082
083}