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    // @since 8.3
039    protected boolean blockIndexing = false;
040
041    protected boolean bulkMode = false;
042
043    protected static final String NOTIF_LISTENER = "notificationListener";
044
045    protected static final String MIME_LISTENER = "mimetypeIconUpdater";
046
047    protected static final String INDEXING_LISTENER = "elasticSearchInlineListener";
048
049    public EventServiceConfiguratorFilter(Boolean blockSyncPostCommitProcessing, Boolean blockAsyncProcessing,
050                                          Boolean blockMimeTypeDetection, Boolean blockIndexing, Boolean bulkMode) {
051        if (blockAsyncProcessing != null) {
052            this.blockAsyncProcessing = blockAsyncProcessing;
053        }
054        if (blockSyncPostCommitProcessing != null) {
055            this.blockSyncPostCommitProcessing = blockSyncPostCommitProcessing;
056        }
057        if (blockMimeTypeDetection != null) {
058            this.blockMimeTypeDetection = blockMimeTypeDetection;
059        }
060        if (blockIndexing != null) {
061            this.blockIndexing = blockIndexing;
062        }
063        if (bulkMode != null) {
064            this.bulkMode = bulkMode;
065        }
066    }
067
068    public void handleBeforeImport() {
069        EventServiceAdmin eventAdmin = Framework.getLocalService(EventServiceAdmin.class);
070        if (eventAdmin == null) {
071            log.error("EventServiceAdmin service was not found ... Possible that the import process will not proceed ok");
072            return;
073        }
074        eventAdmin.setBulkModeEnabled(bulkMode);
075        eventAdmin.setBlockAsyncHandlers(blockAsyncProcessing);
076        eventAdmin.setBlockSyncPostCommitHandlers(blockSyncPostCommitProcessing);
077        if (blockMimeTypeDetection) {
078            eventAdmin.setListenerEnabledFlag(MIME_LISTENER, false);
079        }
080        if (blockNotifications) {
081            eventAdmin.setListenerEnabledFlag(NOTIF_LISTENER, false);
082        }
083        if (blockIndexing) {
084            eventAdmin.setListenerEnabledFlag(INDEXING_LISTENER, false);
085        }
086    }
087
088    public void handleAfterImport(Exception e) {
089        EventServiceAdmin eventAdmin = Framework.getLocalService(EventServiceAdmin.class);
090        if (eventAdmin != null) {
091            log.info("Restoring default event listeners and bulk mode");
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            eventAdmin.setListenerEnabledFlag(INDEXING_LISTENER, true);
098        }
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, blockNotifications set %b, blockIndexing set %b, bulkMode set %b",
112                blockSyncPostCommitProcessing, blockAsyncProcessing, blockMimeTypeDetection, blockNotifications, blockIndexing, bulkMode);
113    }
114}