001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
016 *
017 */
018
019package org.nuxeo.ecm.liveconnect.update.listener;
020
021import java.util.Map;
022
023import org.nuxeo.ecm.core.blob.BlobManager;
024import org.nuxeo.ecm.core.blob.BlobProvider;
025import org.nuxeo.ecm.core.event.Event;
026import org.nuxeo.ecm.core.event.EventBundle;
027import org.nuxeo.ecm.core.event.PostCommitEventListener;
028import org.nuxeo.ecm.liveconnect.update.BatchUpdateBlobProvider;
029import org.nuxeo.runtime.api.Framework;
030
031public class BlobProviderDocumentsUpdateListener implements PostCommitEventListener {
032
033    public static final String BLOB_PROVIDER_DOCUMENT_UPDATE_EVENT = "blobProviderDocumentUpdateEvent";
034
035    @Override
036    public void handleEvent(EventBundle events) {
037
038        for (Event each : events) {
039            onEvent(each);
040        }
041
042    }
043
044    protected void onEvent(Event event) {
045        Map<String, BlobProvider> blobProviders = Framework.getService(BlobManager.class).getBlobProviders();
046        if (BLOB_PROVIDER_DOCUMENT_UPDATE_EVENT.equals(event.getName())) {
047            // Trigger update for all providers
048            for (BlobProvider blobProvider : blobProviders.values()) {
049                if (blobProvider instanceof BatchUpdateBlobProvider) {
050                    ((BatchUpdateBlobProvider) blobProvider).processDocumentsUpdate();
051                }
052            }
053        } else {
054            // Trigger update for a given provider (we assume the event name is the name of the provider)
055            ((BatchUpdateBlobProvider) blobProviders.get(event.getName())).processDocumentsUpdate();
056        }
057    }
058
059}