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    @SuppressWarnings("unchecked")
048    public void handleEvent(Event event) {
049
050        EventContext ctx = event.getContext();
051
052        if (!(ctx instanceof DocumentEventContext)) {
053            // we are only interested in propagating notification for document
054            // proxies
055            return;
056        }
057        DocumentEventContext docCtx = (DocumentEventContext) ctx;
058        DocumentModel publishedDoc = docCtx.getSourceDocument();
059        if (!publishedDoc.isProxy()) {
060            // we are only interested in the publication of proxy documents
061            return;
062        }
063
064        NotificationService service = NotificationServiceHelper.getNotificationService();
065        if (service == null) {
066            log.error("Unable to get NotificationService, exiting");
067            return;
068        }
069
070        List<String> replacedProxyIds = (List<String>) ctx.getProperties().get(CoreEventConstants.REPLACED_PROXY_IDS);
071        if (replacedProxyIds == null) {
072            return;
073        }
074
075        for (String replacedProxyId : replacedProxyIds) {
076            // there should be only one replaced proxy, but just in case,
077            // iterate over them
078            DocumentModel fromDoc = ctx.getCoreSession().getDocument(new IdRef(replacedProxyId));
079            fromDoc.getAdapter(SubscriptionAdapter.class).copySubscriptionsTo(publishedDoc);
080        }
081        ctx.getCoreSession().saveDocument(publishedDoc);
082    }
083
084
085}