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