001/*
002 * (C) Copyright 2017 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 *     Guillaume Renard <grenard@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.userworkspace.core.listener;
020
021import org.nuxeo.common.utils.Path;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
024import org.nuxeo.ecm.core.api.trash.TrashService;
025import org.nuxeo.ecm.core.event.Event;
026import org.nuxeo.ecm.core.event.EventContext;
027import org.nuxeo.ecm.core.event.EventListener;
028import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
029import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Listener to invalidate {@link UserWorkspaceService} when a domain is removed.
034 *
035 * @since 9.3
036 */
037public class InvalidateUserWorkspacesListener implements EventListener {
038
039    @Override
040    public void handleEvent(Event event) {
041        EventContext ec = event.getContext();
042        if (!(ec instanceof DocumentEventContext)) {
043            return;
044        }
045        String evtName = event.getName();
046        if (!DocumentEventTypes.DOCUMENT_REMOVED.equals(evtName)
047                && !TrashService.DOCUMENT_TRASHED.equals(evtName)
048                && !TrashService.DOCUMENT_UNTRASHED.equals(evtName)) {
049            return;
050        }
051        DocumentEventContext context = (DocumentEventContext) ec;
052        DocumentModel doc = context.getSourceDocument();
053        Path path = doc.getPath();
054        if (path == null) {
055            // Placeless document
056            return;
057        }
058        if (path.segmentCount() == 1) {
059            // the document is under root like are the domains
060            // we need to invalidate user workspace location
061            Framework.getService(UserWorkspaceService.class).invalidate();
062        }
063    }
064
065}