001/*
002 * (C) Copyright 2012 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 *     Olivier Grisel <ogrisel@nuxeo.com>
018 */
019package org.nuxeo.drive.listener;
020
021import static org.nuxeo.ecm.core.api.trash.TrashService.DOCUMENT_TRASHED;
022import static org.nuxeo.ecm.core.api.trash.TrashService.DOCUMENT_UNTRASHED;
023
024import org.nuxeo.drive.service.NuxeoDriveManager;
025import org.nuxeo.ecm.collections.api.CollectionConstants;
026import org.nuxeo.ecm.core.api.IdRef;
027import org.nuxeo.ecm.core.api.LifeCycleConstants;
028import org.nuxeo.ecm.core.event.Event;
029import org.nuxeo.ecm.core.event.EventListener;
030import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * Notify the NuxeoDriveManager service in case of document deletions so as to make it possible to invalidate any cache.
035 */
036public class NuxeoDriveCacheInvalidationListener implements EventListener {
037
038    @Override
039    public void handleEvent(Event event) {
040        DocumentEventContext docCtx;
041        if (event.getContext() instanceof DocumentEventContext) {
042            docCtx = (DocumentEventContext) event.getContext();
043        } else {
044            // not interested in event that are not related to documents
045            return;
046        }
047        String transition = (String) docCtx.getProperty(LifeCycleConstants.TRANSTION_EVENT_OPTION_TRANSITION);
048        String eventName = event.getName();
049        if (!DOCUMENT_TRASHED.equals(eventName) && !DOCUMENT_UNTRASHED.equals(eventName) && transition != null
050                && !(LifeCycleConstants.DELETE_TRANSITION.equals(transition)
051                        || LifeCycleConstants.UNDELETE_TRANSITION.equals(transition))) {
052            // not interested in lifecycle transitions that are not related to
053            // document deletion
054            return;
055        }
056        NuxeoDriveManager driveManager = Framework.getService(NuxeoDriveManager.class);
057        if (CollectionConstants.ADDED_TO_COLLECTION.equals(event.getName())
058                || CollectionConstants.REMOVED_FROM_COLLECTION.equals(event.getName())) {
059            driveManager.invalidateCollectionSyncRootMemberCache();
060        } else {
061            driveManager.handleFolderDeletion((IdRef) docCtx.getSourceDocument().getRef());
062        }
063    }
064
065}