001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Olivier Grisel <ogrisel@nuxeo.com>
016 */
017package org.nuxeo.drive.listener;
018
019import org.nuxeo.drive.service.NuxeoDriveManager;
020import org.nuxeo.ecm.collections.api.CollectionConstants;
021import org.nuxeo.ecm.core.api.IdRef;
022import org.nuxeo.ecm.core.api.LifeCycleConstants;
023import org.nuxeo.ecm.core.event.Event;
024import org.nuxeo.ecm.core.event.EventListener;
025import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
026import org.nuxeo.runtime.api.Framework;
027
028/**
029 * Notify the NuxeoDriveManager service in case of document deletions so as to make it possible to invalidate any cache.
030 */
031public class NuxeoDriveCacheInvalidationListener implements EventListener {
032
033    @Override
034    public void handleEvent(Event event) {
035        DocumentEventContext docCtx;
036        if (event.getContext() instanceof DocumentEventContext) {
037            docCtx = (DocumentEventContext) event.getContext();
038        } else {
039            // not interested in event that are not related to documents
040            return;
041        }
042        String transition = (String) docCtx.getProperty(LifeCycleConstants.TRANSTION_EVENT_OPTION_TRANSITION);
043        if (transition != null
044                && !(LifeCycleConstants.DELETE_TRANSITION.equals(transition) || LifeCycleConstants.UNDELETE_TRANSITION.equals(transition))) {
045            // not interested in lifecycle transitions that are not related to
046            // document deletion
047            return;
048        }
049        NuxeoDriveManager driveManager = Framework.getLocalService(NuxeoDriveManager.class);
050        if (CollectionConstants.ADDED_TO_COLLECTION.equals(event.getName())
051                || CollectionConstants.REMOVED_FROM_COLLECTION.equals(event.getName())) {
052            driveManager.invalidateCollectionSyncRootMemberCache();
053        } else {
054            driveManager.handleFolderDeletion((IdRef) docCtx.getSourceDocument().getRef());
055        }
056    }
057
058}