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