001/*
002 * (C) Copyright 2013 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.platform.tag;
021
022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_PROXY_PUBLISHED;
023import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_REMOVED;
024import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_RESTORED;
025import static org.nuxeo.ecm.platform.tag.TagService.Feature.TAGS_BELONG_TO_DOCUMENT;
026
027import java.util.Set;
028
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.IdRef;
032import org.nuxeo.ecm.core.api.facet.VersioningDocument;
033import org.nuxeo.ecm.core.event.DeletedDocumentModel;
034import org.nuxeo.ecm.core.event.Event;
035import org.nuxeo.ecm.core.event.EventBundle;
036import org.nuxeo.ecm.core.event.EventContext;
037import org.nuxeo.ecm.core.event.PostCommitFilteringEventListener;
038import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * Listener that copy tags applied on the live document to a version or proxy of this document or replace the existing
043 * tags on a live document by the ones on the version being restored.
044 *
045 * @since 5.7.3
046 */
047public class TaggedVersionListener implements PostCommitFilteringEventListener {
048
049    protected static final Set<String> ACCEPTED_EVENTS = Set.of(DOCUMENT_PROXY_PUBLISHED, DOCUMENT_RESTORED,
050            DOCUMENT_REMOVED);
051
052    @Override
053    public void handleEvent(EventBundle events) {
054        for (Event event : events) {
055            if (acceptEvent(event)) {
056                handleEvent(event);
057            }
058        }
059    }
060
061    protected void handleEvent(Event event) {
062        EventContext ctx = event.getContext();
063        if (!(ctx instanceof DocumentEventContext)) {
064            return;
065        }
066
067        String name = event.getName();
068        DocumentEventContext docCtx = (DocumentEventContext) ctx;
069        CoreSession session = docCtx.getCoreSession();
070        DocumentModel doc = docCtx.getSourceDocument();
071        if (doc == null) {
072            return;
073        }
074
075        String docId = doc.getId();
076        TagService tagService = Framework.getService(TagService.class);
077        if (doc instanceof DeletedDocumentModel && !tagService.hasFeature(TAGS_BELONG_TO_DOCUMENT)) {
078            tagService.removeTags(session, docId);
079            return;
080        }
081
082        switch (name) {
083        case DOCUMENT_PROXY_PUBLISHED:
084            if (doc.isProxy()) {
085                DocumentModel version = session.getSourceDocument(doc.getRef());
086                tagService.copyTags(session, version.getId(), docId);
087            }
088            break;
089        case DOCUMENT_RESTORED:
090            String versionUUID = (String) ctx.getProperty(VersioningDocument.RESTORED_VERSION_UUID_KEY);
091            if (session.exists(new IdRef(docId))) {
092                tagService.replaceTags(session, versionUUID, docId);
093            }
094            break;
095        case DOCUMENT_REMOVED:
096            if (!tagService.hasFeature(TAGS_BELONG_TO_DOCUMENT)) {
097                tagService.removeTags(session, docId);
098            }
099            break;
100        default:
101            break;
102        }
103    }
104
105    @Override
106    public boolean acceptEvent(Event event) {
107        return ACCEPTED_EVENTS.contains(event.getName());
108    }
109}