001/*
002 * (C) Copyright 2018 Nuxeo (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 *       Kevin Leturc <kleturc@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.ec.notification;
020
021import static org.nuxeo.ecm.platform.ec.notification.SubscriptionAdapter.NOTIFIABLE_FACET;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentRef;
028import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
029import org.nuxeo.ecm.core.event.Event;
030import org.nuxeo.ecm.core.event.EventListener;
031import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
032
033/**
034 * Listener used to intercept {@link DocumentEventTypes#DOCUMENT_CHECKEDIN} events in order to clean notification from
035 * version.
036 *
037 * @since 10.2
038 */
039public class NotificationCheckedInListener implements EventListener {
040
041    private static final Log log = LogFactory.getLog(NotificationCheckedInListener.class);
042
043    @Override
044    public void handleEvent(Event event) {
045        if (!DocumentEventTypes.DOCUMENT_CHECKEDIN.equals(event.getName())) {
046            return;
047        }
048        if (!(event.getContext() instanceof DocumentEventContext)) {
049            log.warn("Can not handle event that is not bound to a DocumentEventContext");
050            return;
051        }
052        DocumentEventContext context = (DocumentEventContext) event.getContext();
053        CoreSession session = context.getCoreSession();
054        DocumentModel docModel = context.getSourceDocument();
055        if (docModel.hasFacet(NOTIFIABLE_FACET)) {
056            // remove notifications from version
057            DocumentRef versionRef = (DocumentRef) context.getProperty("checkedInVersionRef");
058            DocumentModel version = session.getDocument(versionRef);
059            version.getAdapter(SubscriptionAdapter.class).clearNotification();
060            session.saveDocument(version);
061        }
062    }
063}