001/* 002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 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 * Thomas Roger <troger@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.quota; 019 020import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.ABOUT_TO_REMOVE; 021import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CHECKEDIN; 022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CHECKEDOUT; 023import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED; 024import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED_BY_COPY; 025import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_MOVED; 026import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_UPDATED; 027import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE; 028import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.ABOUT_TO_REMOVE_VERSION; 029import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSITION_EVENT; 030import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSTION_EVENT_OPTION_TRANSITION; 031import static org.nuxeo.ecm.core.api.LifeCycleConstants.DELETE_TRANSITION; 032import static org.nuxeo.ecm.core.api.LifeCycleConstants.UNDELETE_TRANSITION; 033import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_RESTORED; 034import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_RESTORE; 035 036import java.util.Arrays; 037import java.util.Collections; 038import java.util.HashSet; 039import java.util.Set; 040 041import org.nuxeo.ecm.core.event.Event; 042import org.nuxeo.ecm.core.event.EventContext; 043import org.nuxeo.ecm.core.event.EventListener; 044import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 045import org.nuxeo.runtime.api.Framework; 046 047/** 048 * Listener handling default events to update statistics through the {@link org.nuxeo.ecm.quota.QuotaStatsService}. 049 * 050 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 051 * @since 5.5 052 */ 053public class QuotaStatsListener implements EventListener { 054 055 public static final Set<String> EVENTS_TO_HANDLE = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList( 056 DOCUMENT_CREATED, DOCUMENT_CREATED_BY_COPY, DOCUMENT_UPDATED, DOCUMENT_MOVED, ABOUT_TO_REMOVE, 057 BEFORE_DOC_UPDATE, ABOUT_TO_REMOVE_VERSION, DOCUMENT_CHECKEDIN, DOCUMENT_CHECKEDOUT, TRANSITION_EVENT, 058 BEFORE_DOC_RESTORE, DOCUMENT_RESTORED))); 059 060 @Override 061 public void handleEvent(Event event) { 062 EventContext ctx = event.getContext(); 063 if (!(ctx instanceof DocumentEventContext)) { 064 return; 065 } 066 if (!EVENTS_TO_HANDLE.contains(event.getName())) { 067 return; 068 } 069 if (TRANSITION_EVENT.equals(event.getName()) && !isTrashOpEvent((DocumentEventContext) ctx)) { 070 return; 071 } 072 DocumentEventContext docCtx = (DocumentEventContext) ctx; 073 QuotaStatsService quotaStatsService = Framework.getLocalService(QuotaStatsService.class); 074 quotaStatsService.updateStatistics(docCtx, event); 075 } 076 077 protected boolean isTrashOpEvent(DocumentEventContext eventContext) { 078 String transition = (String) eventContext.getProperties().get(TRANSTION_EVENT_OPTION_TRANSITION); 079 if (transition != null && (DELETE_TRANSITION.equals(transition) || UNDELETE_TRANSITION.equals(transition))) { 080 return true; 081 } 082 return false; 083 } 084}