001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.platform.tag;
019
020import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CHECKEDIN;
021import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_PROXY_PUBLISHED;
022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_REMOVED;
023import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_RESTORED;
024import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSITION_EVENT;
025
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentRef;
029import org.nuxeo.ecm.core.api.IdRef;
030import org.nuxeo.ecm.core.api.LifeCycleConstants;
031import org.nuxeo.ecm.core.api.facet.VersioningDocument;
032import org.nuxeo.ecm.core.event.DeletedDocumentModel;
033import org.nuxeo.ecm.core.event.Event;
034import org.nuxeo.ecm.core.event.EventBundle;
035import org.nuxeo.ecm.core.event.EventContext;
036import org.nuxeo.ecm.core.event.PostCommitFilteringEventListener;
037import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Listener that copy tags applied on the live document to a version or proxy of this document or replace the existing
042 * tags on a live document by the ones on the version being restored.
043 *
044 * @since 5.7.3
045 */
046public class TaggedVersionListener implements PostCommitFilteringEventListener {
047
048    @Override
049    public void handleEvent(EventBundle events) {
050        for (Event event : events) {
051            if (acceptEvent(event)) {
052                handleEvent(event);
053            }
054        }
055    }
056
057    protected void handleEvent(Event event) {
058        EventContext ctx = event.getContext();
059        if (ctx instanceof DocumentEventContext) {
060            String name = event.getName();
061            DocumentEventContext docCtx = (DocumentEventContext) ctx;
062            CoreSession session = docCtx.getCoreSession();
063            DocumentModel doc = docCtx.getSourceDocument();
064            if (doc == null) {
065                return;
066            }
067            String docId = doc.getId();
068            TagService tagService = Framework.getLocalService(TagService.class);
069            if (doc instanceof DeletedDocumentModel) {
070                tagService.removeTags(session, docId);
071                return;
072            }
073            switch (name) {
074            case DOCUMENT_CHECKEDIN:
075                DocumentRef versionRef = (DocumentRef) ctx.getProperty("checkedInVersionRef");
076                if (versionRef instanceof IdRef) {
077                    tagService.copyTags(session, docId, versionRef.toString());
078                }
079                break;
080            case DOCUMENT_PROXY_PUBLISHED:
081                if (doc.isProxy()) {
082                    DocumentModel version = session.getSourceDocument(doc.getRef());
083                    tagService.copyTags(session, version.getId(), docId);
084                }
085                break;
086            case DOCUMENT_RESTORED:
087                String versionUUID = (String) ctx.getProperty(VersioningDocument.RESTORED_VERSION_UUID_KEY);
088                tagService.replaceTags(session, versionUUID, docId);
089                break;
090            case DOCUMENT_REMOVED:
091            case TRANSITION_EVENT:
092                tagService.removeTags(session, docId);
093                break;
094            default:
095                break;
096            }
097        }
098    }
099
100    @Override
101    public boolean acceptEvent(Event event) {
102        String name = event.getName();
103        return DOCUMENT_CHECKEDIN.equals(name)
104                || DOCUMENT_PROXY_PUBLISHED.equals(name)
105                || DOCUMENT_RESTORED.equals(name)
106                || DOCUMENT_REMOVED.equals(name)
107                || (LifeCycleConstants.TRANSITION_EVENT.equals(name) && LifeCycleConstants.DELETED_STATE.equals(event.getContext().getProperty(
108                        LifeCycleConstants.TRANSTION_EVENT_OPTION_TO)));
109    }
110}