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.LifeCycleConstants.TRANSITION_EVENT;
023import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_PROXY_PUBLISHED;
024import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_REMOVED;
025import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_RESTORED;
026import static org.nuxeo.ecm.core.api.trash.TrashService.DOCUMENT_TRASHED;
027import static org.nuxeo.ecm.platform.tag.TagService.Feature.TAGS_BELONG_TO_DOCUMENT;
028
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.LifeCycleConstants;
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    @Override
050    public void handleEvent(EventBundle events) {
051        for (Event event : events) {
052            if (acceptEvent(event)) {
053                handleEvent(event);
054            }
055        }
056    }
057
058    protected void handleEvent(Event event) {
059        EventContext ctx = event.getContext();
060        if (ctx instanceof DocumentEventContext) {
061            String name = event.getName();
062            DocumentEventContext docCtx = (DocumentEventContext) ctx;
063            CoreSession session = docCtx.getCoreSession();
064            DocumentModel doc = docCtx.getSourceDocument();
065            if (doc == null) {
066                return;
067            }
068            String docId = doc.getId();
069            TagService tagService = Framework.getService(TagService.class);
070            if (doc instanceof DeletedDocumentModel) {
071                if (!tagService.hasFeature(TAGS_BELONG_TO_DOCUMENT)) {
072                    tagService.removeTags(session, docId);
073                    return;
074                }
075            }
076            switch (name) {
077            case DOCUMENT_PROXY_PUBLISHED:
078                if (doc.isProxy()) {
079                    DocumentModel version = session.getSourceDocument(doc.getRef());
080                    tagService.copyTags(session, version.getId(), docId);
081                }
082                break;
083            case DOCUMENT_RESTORED:
084                String versionUUID = (String) ctx.getProperty(VersioningDocument.RESTORED_VERSION_UUID_KEY);
085                tagService.replaceTags(session, versionUUID, docId);
086                break;
087            case DOCUMENT_REMOVED:
088                if (!tagService.hasFeature(TAGS_BELONG_TO_DOCUMENT)) {
089                    tagService.removeTags(session, docId);
090                }
091                break;
092            case DOCUMENT_TRASHED:
093            case TRANSITION_EVENT:
094                tagService.removeTags(session, docId);
095                break;
096            default:
097                break;
098            }
099        }
100    }
101
102    @Override
103    public boolean acceptEvent(Event event) {
104        String name = event.getName();
105        return DOCUMENT_PROXY_PUBLISHED.equals(name)
106                || DOCUMENT_RESTORED.equals(name)
107                || DOCUMENT_REMOVED.equals(name)
108                || DOCUMENT_TRASHED.equals(name)
109                || (LifeCycleConstants.TRANSITION_EVENT.equals(name) && LifeCycleConstants.DELETED_STATE.equals(event.getContext().getProperty(
110                LifeCycleConstants.TRANSTION_EVENT_OPTION_TO)));
111    }
112}