001/*
002 * (C) Copyright 2006-2011 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021package org.nuxeo.ecm.core.management.events;
022
023import java.util.Arrays;
024import java.util.List;
025
026import org.nuxeo.ecm.core.api.LifeCycleConstants;
027import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
028import org.nuxeo.ecm.core.event.EventBundle;
029import org.nuxeo.ecm.core.event.PostCommitEventListener;
030import org.nuxeo.ecm.core.event.impl.ReconnectedEventBundleImpl;
031import org.nuxeo.ecm.core.api.trash.TrashService;
032import org.nuxeo.runtime.management.counters.CounterHelper;
033
034/**
035 * AsyncEventListener that collects events to update the Simon counters
036 *
037 * @author Tiry (tdelprat@nuxeo.com)
038 */
039public class EventCounterListener implements PostCommitEventListener {
040
041    // Counters used
042    public static final String EVENT_CREATE_COUNTER = "org.nuxeo.event.create";
043
044    public static final String EVENT_UPDATE_COUNTER = "org.nuxeo.event.update";
045
046    public static final String EVENT_REMOVE_COUNTER = "org.nuxeo.event.remove";
047
048    // Event tracked
049    protected static final List<String> createEvents = Arrays.asList(DocumentEventTypes.DOCUMENT_CREATED,
050            DocumentEventTypes.DOCUMENT_CREATED_BY_COPY, DocumentEventTypes.DOCUMENT_IMPORTED, DocumentEventTypes.DOCUMENT_PROXY_PUBLISHED,
051            DocumentEventTypes.DOCUMENT_PROXY_UPDATED);
052
053    protected static final List<String> updateEvents = Arrays.asList(DocumentEventTypes.DOCUMENT_CHECKEDIN,
054            DocumentEventTypes.DOCUMENT_CHECKEDOUT, DocumentEventTypes.DOCUMENT_CHILDREN_ORDER_CHANGED,
055            DocumentEventTypes.DOCUMENT_LOCKED, DocumentEventTypes.DOCUMENT_MOVED, DocumentEventTypes.DOCUMENT_PUBLISHED,
056            DocumentEventTypes.DOCUMENT_SECURITY_UPDATED, DocumentEventTypes.DOCUMENT_UNLOCKED, DocumentEventTypes.DOCUMENT_UPDATED,
057            LifeCycleConstants.TRANSITION_EVENT, TrashService.DOCUMENT_TRASHED, TrashService.DOCUMENT_UNTRASHED);
058
059    protected static final List<String> removeEvents = Arrays.asList(DocumentEventTypes.DOCUMENT_REMOVED,
060            DocumentEventTypes.VERSION_REMOVED);
061
062    @Override
063    public void handleEvent(EventBundle events) {
064        if (events instanceof ReconnectedEventBundleImpl) {
065            ReconnectedEventBundleImpl bundle = (ReconnectedEventBundleImpl) events;
066            updateCounters(bundle.getEventNames());
067        }
068    }
069
070    protected void updateCounters(List<String> eventNames) {
071
072        int created = 0;
073        int updated = 0;
074        int removed = 0;
075
076        for (String eventName : eventNames) {
077            if (createEvents.contains(eventName)) {
078                created += 1;
079            } else if (updateEvents.contains(eventName)) {
080                updated += 1;
081            } else if (removeEvents.contains(eventName)) {
082                removed += 1;
083            }
084        }
085
086        if (created > 0) {
087            CounterHelper.increaseCounter(EVENT_CREATE_COUNTER, created);
088        }
089        if (updated > 0) {
090            CounterHelper.increaseCounter(EVENT_UPDATE_COUNTER, updated);
091        }
092        if (removed > 0) {
093            CounterHelper.increaseCounter(EVENT_REMOVE_COUNTER, removed);
094        }
095    }
096
097}