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