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; 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.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.getService(TagService.class); 069 if (doc instanceof DeletedDocumentModel) { 070 if (!tagService.hasFeature(TAGS_BELONG_TO_DOCUMENT)) { 071 tagService.removeTags(session, docId); 072 return; 073 } 074 } 075 switch (name) { 076 case DOCUMENT_PROXY_PUBLISHED: 077 if (doc.isProxy()) { 078 DocumentModel version = session.getSourceDocument(doc.getRef()); 079 tagService.copyTags(session, version.getId(), docId); 080 } 081 break; 082 case DOCUMENT_RESTORED: 083 String versionUUID = (String) ctx.getProperty(VersioningDocument.RESTORED_VERSION_UUID_KEY); 084 tagService.replaceTags(session, versionUUID, docId); 085 break; 086 case DOCUMENT_REMOVED: 087 if (!tagService.hasFeature(TAGS_BELONG_TO_DOCUMENT)) { 088 tagService.removeTags(session, docId); 089 } 090 break; 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_PROXY_PUBLISHED.equals(name) 104 || DOCUMENT_RESTORED.equals(name) 105 || DOCUMENT_REMOVED.equals(name) 106 || (LifeCycleConstants.TRANSITION_EVENT.equals(name) && LifeCycleConstants.DELETED_STATE.equals(event.getContext().getProperty( 107 LifeCycleConstants.TRANSTION_EVENT_OPTION_TO))); 108 } 109}