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_CHECKEDIN;
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.LifeCycleConstants.TRANSITION_EVENT;
027
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentRef;
031import org.nuxeo.ecm.core.api.IdRef;
032import org.nuxeo.ecm.core.api.LifeCycleConstants;
033import org.nuxeo.ecm.core.api.facet.VersioningDocument;
034import org.nuxeo.ecm.core.event.DeletedDocumentModel;
035import org.nuxeo.ecm.core.event.Event;
036import org.nuxeo.ecm.core.event.EventBundle;
037import org.nuxeo.ecm.core.event.EventContext;
038import org.nuxeo.ecm.core.event.PostCommitFilteringEventListener;
039import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
040import org.nuxeo.runtime.api.Framework;
041
042/**
043 * Listener that copy tags applied on the live document to a version or proxy of this document or replace the existing
044 * tags on a live document by the ones on the version being restored.
045 *
046 * @since 5.7.3
047 */
048public class TaggedVersionListener implements PostCommitFilteringEventListener {
049
050    @Override
051    public void handleEvent(EventBundle events) {
052        for (Event event : events) {
053            if (acceptEvent(event)) {
054                handleEvent(event);
055            }
056        }
057    }
058
059    protected void handleEvent(Event event) {
060        EventContext ctx = event.getContext();
061        if (ctx instanceof DocumentEventContext) {
062            String name = event.getName();
063            DocumentEventContext docCtx = (DocumentEventContext) ctx;
064            CoreSession session = docCtx.getCoreSession();
065            DocumentModel doc = docCtx.getSourceDocument();
066            if (doc == null) {
067                return;
068            }
069            String docId = doc.getId();
070            TagService tagService = Framework.getLocalService(TagService.class);
071            if (doc instanceof DeletedDocumentModel) {
072                tagService.removeTags(session, docId);
073                return;
074            }
075            switch (name) {
076            case DOCUMENT_CHECKEDIN:
077                DocumentRef versionRef = (DocumentRef) ctx.getProperty("checkedInVersionRef");
078                if (versionRef instanceof IdRef) {
079                    tagService.copyTags(session, docId, versionRef.toString());
080                }
081                break;
082            case DOCUMENT_PROXY_PUBLISHED:
083                if (doc.isProxy()) {
084                    DocumentModel version = session.getSourceDocument(doc.getRef());
085                    tagService.copyTags(session, version.getId(), docId);
086                }
087                break;
088            case DOCUMENT_RESTORED:
089                String versionUUID = (String) ctx.getProperty(VersioningDocument.RESTORED_VERSION_UUID_KEY);
090                tagService.replaceTags(session, versionUUID, docId);
091                break;
092            case DOCUMENT_REMOVED:
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_CHECKEDIN.equals(name)
106                || DOCUMENT_PROXY_PUBLISHED.equals(name)
107                || DOCUMENT_RESTORED.equals(name)
108                || DOCUMENT_REMOVED.equals(name)
109                || (LifeCycleConstants.TRANSITION_EVENT.equals(name) && LifeCycleConstants.DELETED_STATE.equals(event.getContext().getProperty(
110                        LifeCycleConstants.TRANSTION_EVENT_OPTION_TO)));
111    }
112}