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