001package org.nuxeo.ecm.rating.listener; 002 003import static org.nuxeo.ecm.activity.ActivityHelper.createDocumentActivityObject; 004import static org.nuxeo.ecm.core.api.LifeCycleConstants.DELETED_STATE; 005import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSITION_EVENT; 006import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSTION_EVENT_OPTION_TO; 007import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_REMOVED; 008 009import org.nuxeo.ecm.core.api.DocumentModel; 010import org.nuxeo.ecm.core.event.Event; 011import org.nuxeo.ecm.core.event.EventListener; 012import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 013import org.nuxeo.ecm.rating.api.RatingService; 014import org.nuxeo.runtime.api.Framework; 015 016/** 017 * Remove rates made when a document is removed or his lifecycle state is changing to DELETED_STATE 018 * 019 * @author <a href="mailto:akervern@nuxeo.com">Arnaud Kervern</a> 020 */ 021public class RatingListener implements EventListener { 022 @Override 023 public void handleEvent(Event event) { 024 if (event.getContext() instanceof DocumentEventContext) { 025 DocumentEventContext ctx = (DocumentEventContext) event.getContext(); 026 DocumentModel document = ctx.getSourceDocument(); 027 028 if (DOCUMENT_REMOVED.equals(event.getName())) { 029 cancelRates(document); 030 } else if (TRANSITION_EVENT.equals(event.getName())) { 031 String destinationState = (String) ctx.getProperty(TRANSTION_EVENT_OPTION_TO); 032 if (DELETED_STATE.equals(destinationState)) { 033 cancelRates(document); 034 } 035 } 036 } 037 } 038 039 protected void cancelRates(DocumentModel document) { 040 RatingService ratingService = Framework.getLocalService(RatingService.class); 041 ratingService.cancelRates(createDocumentActivityObject(document), null); 042 } 043}