001/*
002 * (C) Copyright 2009 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 *     Thierry Delprat
018 */
019package org.nuxeo.ecm.platform.importer.filter;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.core.event.EventServiceAdmin;
024import org.nuxeo.runtime.api.Framework;
025
026public class EventServiceConfiguratorFilter implements ImporterFilter {
027
028    private static final Log log = LogFactory.getLog(EventServiceConfiguratorFilter.class);
029
030    protected boolean blockSyncPostCommitProcessing = false;
031
032    protected boolean blockAsyncProcessing = false;
033
034    protected boolean blockMimeTypeDetection = false;
035
036    protected boolean blockNotifications = true;
037
038    protected boolean bulkMode = false;
039
040    protected EventServiceAdmin eventAdmin = null;
041
042    protected static final String NOTIF_LISTENER = "notificationListener";
043
044    protected static final String MIME_LISTENER = "mimetypeIconUpdater";
045
046    public EventServiceConfiguratorFilter(Boolean blockSyncPostCommitProcessing, Boolean blockAsyncProcessing,
047            Boolean blockMimeTypeDetection, Boolean bulkMode) {
048        if (blockAsyncProcessing != null) {
049            this.blockAsyncProcessing = blockAsyncProcessing;
050        }
051        if (blockSyncPostCommitProcessing != null) {
052            this.blockSyncPostCommitProcessing = blockSyncPostCommitProcessing;
053        }
054        if (blockMimeTypeDetection != null) {
055            this.blockMimeTypeDetection = blockMimeTypeDetection;
056        }
057        if (bulkMode != null) {
058            this.bulkMode = bulkMode;
059        }
060    }
061
062    public void handleBeforeImport() {
063        eventAdmin = Framework.getLocalService(EventServiceAdmin.class);
064
065        if (eventAdmin != null) {
066            if (true == bulkMode) {
067                eventAdmin.setBulkModeEnabled(true);
068            }
069            if (true == blockMimeTypeDetection) {
070                eventAdmin.setListenerEnabledFlag(MIME_LISTENER, false);
071            }
072            if (true == blockNotifications) {
073                eventAdmin.setListenerEnabledFlag(NOTIF_LISTENER, false);
074            }
075            if (true == blockAsyncProcessing) {
076                eventAdmin.setBlockAsyncHandlers(true);
077            } else {
078                eventAdmin.setBlockAsyncHandlers(false);
079            }
080            if (true == blockSyncPostCommitProcessing) {
081                eventAdmin.setBlockSyncPostCommitHandlers(true);
082            } else {
083                eventAdmin.setBlockSyncPostCommitHandlers(false);
084            }
085        } else {
086            log.warn("EventServiceAdmin service was not found ... Possible that the import process will not proceed ok");
087        }
088    }
089
090    public void handleAfterImport(Exception e) {
091        if (eventAdmin != null) {
092            eventAdmin.setBulkModeEnabled(false);
093            eventAdmin.setBlockAsyncHandlers(false);
094            eventAdmin.setBlockSyncPostCommitHandlers(false);
095            eventAdmin.setListenerEnabledFlag(NOTIF_LISTENER, true);
096            eventAdmin.setListenerEnabledFlag(MIME_LISTENER, true);
097        }
098        eventAdmin = null;
099    }
100
101    public boolean getBlockNotifications() {
102        return blockNotifications;
103    }
104
105    public void setBlockNotifications(boolean blockNotifications) {
106        this.blockNotifications = blockNotifications;
107    }
108
109    public String toString() {
110        return String.format(
111                "blockSyncPostCommitProcessing set %b, blockAsyncProcessing set %b, blockMimeTypeDetection set %b and blockNotifications set %b",
112                blockSyncPostCommitProcessing, blockAsyncProcessing, blockMimeTypeDetection, blockNotifications);
113    }
114}