001/* 002 * (C) Copyright 2012 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 * Arnaud Kervern 018 */ 019package org.nuxeo.ecm.rating.listener; 020 021import static org.nuxeo.ecm.activity.ActivityHelper.createDocumentActivityObject; 022import static org.nuxeo.ecm.core.api.LifeCycleConstants.DELETED_STATE; 023import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSITION_EVENT; 024import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSTION_EVENT_OPTION_TO; 025import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_REMOVED; 026import static org.nuxeo.ecm.core.api.trash.TrashService.DOCUMENT_TRASHED; 027 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.event.Event; 030import org.nuxeo.ecm.core.event.EventListener; 031import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 032import org.nuxeo.ecm.rating.api.RatingService; 033import org.nuxeo.runtime.api.Framework; 034 035/** 036 * Remove rates made when a document is removed or his lifecycle state is changing to DELETED_STATE 037 * 038 * @author <a href="mailto:akervern@nuxeo.com">Arnaud Kervern</a> 039 */ 040public class RatingListener implements EventListener { 041 @Override 042 public void handleEvent(Event event) { 043 if (event.getContext() instanceof DocumentEventContext) { 044 DocumentEventContext ctx = (DocumentEventContext) event.getContext(); 045 DocumentModel document = ctx.getSourceDocument(); 046 047 if (DOCUMENT_REMOVED.equals(event.getName())) { 048 cancelRates(document); 049 } else if (DOCUMENT_TRASHED.equals(event.getName())) { 050 cancelRates(document); 051 } else if (TRANSITION_EVENT.equals(event.getName())) { 052 String destinationState = (String) ctx.getProperty(TRANSTION_EVENT_OPTION_TO); 053 if (DELETED_STATE.equals(destinationState)) { 054 cancelRates(document); 055 } 056 } 057 } 058 } 059 060 protected void cancelRates(DocumentModel document) { 061 RatingService ratingService = Framework.getService(RatingService.class); 062 ratingService.cancelRates(createDocumentActivityObject(document), null); 063 } 064}