001package org.nuxeo.ecm.platform.importer.filter;
002
003import org.apache.commons.logging.Log;
004import org.apache.commons.logging.LogFactory;
005import org.nuxeo.ecm.core.event.EventServiceAdmin;
006import org.nuxeo.runtime.api.Framework;
007
008public class EventServiceConfiguratorFilter implements ImporterFilter {
009
010    private static final Log log = LogFactory.getLog(EventServiceConfiguratorFilter.class);
011
012    protected boolean blockSyncPostCommitProcessing = false;
013
014    protected boolean blockAsyncProcessing = false;
015
016    protected boolean blockMimeTypeDetection = false;
017
018    protected boolean blockNotifications = true;
019
020    protected boolean bulkMode = false;
021
022    protected EventServiceAdmin eventAdmin = null;
023
024    protected static final String NOTIF_LISTENER = "notificationListener";
025
026    protected static final String MIME_LISTENER = "mimetypeIconUpdater";
027
028    public EventServiceConfiguratorFilter(Boolean blockSyncPostCommitProcessing, Boolean blockAsyncProcessing,
029            Boolean blockMimeTypeDetection, Boolean bulkMode) {
030        if (blockAsyncProcessing != null) {
031            this.blockAsyncProcessing = blockAsyncProcessing;
032        }
033        if (blockSyncPostCommitProcessing != null) {
034            this.blockSyncPostCommitProcessing = blockSyncPostCommitProcessing;
035        }
036        if (blockMimeTypeDetection != null) {
037            this.blockMimeTypeDetection = blockMimeTypeDetection;
038        }
039        if (bulkMode != null) {
040            this.bulkMode = bulkMode;
041        }
042    }
043
044    public void handleBeforeImport() {
045        eventAdmin = Framework.getLocalService(EventServiceAdmin.class);
046
047        if (eventAdmin != null) {
048            if (true == bulkMode) {
049                eventAdmin.setBulkModeEnabled(true);
050            }
051            if (true == blockMimeTypeDetection) {
052                eventAdmin.setListenerEnabledFlag(MIME_LISTENER, false);
053            }
054            if (true == blockNotifications) {
055                eventAdmin.setListenerEnabledFlag(NOTIF_LISTENER, false);
056            }
057            if (true == blockAsyncProcessing) {
058                eventAdmin.setBlockAsyncHandlers(true);
059            } else {
060                eventAdmin.setBlockAsyncHandlers(false);
061            }
062            if (true == blockSyncPostCommitProcessing) {
063                eventAdmin.setBlockSyncPostCommitHandlers(true);
064            } else {
065                eventAdmin.setBlockSyncPostCommitHandlers(false);
066            }
067        } else {
068            log.warn("EventServiceAdmin service was not found ... Possible that the import process will not proceed ok");
069        }
070    }
071
072    public void handleAfterImport(Exception e) {
073        if (eventAdmin != null) {
074            eventAdmin.setBulkModeEnabled(false);
075            eventAdmin.setBlockAsyncHandlers(false);
076            eventAdmin.setBlockSyncPostCommitHandlers(false);
077            eventAdmin.setListenerEnabledFlag(NOTIF_LISTENER, true);
078            eventAdmin.setListenerEnabledFlag(MIME_LISTENER, true);
079        }
080        eventAdmin = null;
081    }
082
083    public boolean getBlockNotifications() {
084        return blockNotifications;
085    }
086
087    public void setBlockNotifications(boolean blockNotifications) {
088        this.blockNotifications = blockNotifications;
089    }
090
091    public String toString() {
092        return String.format(
093                "blockSyncPostCommitProcessing set %b, blockAsyncProcessing set %b, blockMimeTypeDetection set %b and blockNotifications set %b",
094                blockSyncPostCommitProcessing, blockAsyncProcessing, blockMimeTypeDetection, blockNotifications);
095    }
096}