001/* 002 * (C) Copyright 2014 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 * <a href="mailto:grenard@nuxeo.com">Guillaume</a> 018 */ 019package org.nuxeo.ecm.collections.core.listener; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.collections.api.CollectionManager; 024import org.nuxeo.ecm.core.api.DocumentModel; 025import org.nuxeo.ecm.core.api.event.DocumentEventTypes; 026import org.nuxeo.ecm.core.event.Event; 027import org.nuxeo.ecm.core.event.EventBundle; 028import org.nuxeo.ecm.core.event.EventContext; 029import org.nuxeo.ecm.core.event.PostCommitEventListener; 030import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 031import org.nuxeo.runtime.api.Framework; 032 033/** 034 * Asynchronous event handler to update Collection of a removed CollectiomMember and CollectionMember of a Collection. 035 * 036 * @since 5.9.3 037 */ 038public class RemovedCollectionListener implements PostCommitEventListener { 039 040 private static final Log log = LogFactory.getLog(RemovedCollectionListener.class); 041 042 @Override 043 public void handleEvent(EventBundle bundle) { 044 for (Event each : bundle) { 045 final EventContext ctx = each.getContext(); 046 if (!(ctx instanceof DocumentEventContext)) { 047 continue; 048 } 049 050 final String eventId = each.getName(); 051 if (!eventId.equals(DocumentEventTypes.DOCUMENT_REMOVED)) { 052 continue; 053 } 054 055 onEvent(each); 056 } 057 } 058 059 protected void onEvent(final Event event) { 060 061 final DocumentEventContext docCxt = (DocumentEventContext) event.getContext(); 062 063 final DocumentModel doc = docCxt.getSourceDocument(); 064 065 final CollectionManager collectionManager = Framework.getService(CollectionManager.class); 066 067 final boolean isCollectionRemoved = collectionManager.isCollection(doc); 068 final boolean isCollectionMemberRemoved = collectionManager.isCollected(doc); 069 070 if (isCollectionRemoved || isCollectionMemberRemoved) { 071 if (isCollectionRemoved) { 072 log.trace(String.format("Collection %s removed", doc.getId())); 073 collectionManager.processRemovedCollection(doc); 074 } else if (isCollectionMemberRemoved) { 075 log.trace(String.format("CollectionMember %s removed", doc.getId())); 076 collectionManager.processRemovedCollectionMember(doc); 077 } 078 } 079 } 080 081}