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