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