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