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 *      Nelson Silva
016 */
017
018package org.nuxeo.ecm.media.publishing;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.NuxeoException;
025import org.nuxeo.ecm.core.api.event.DocumentEventCategories;
026import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
027import org.nuxeo.ecm.core.event.Event;
028import org.nuxeo.ecm.core.event.EventProducer;
029import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
030import org.nuxeo.ecm.core.work.api.Work;
031import org.nuxeo.ecm.core.work.api.WorkManager;
032import org.nuxeo.ecm.media.publishing.adapter.PublishableMedia;
033import org.nuxeo.ecm.media.publishing.upload.MediaPublishingUploadWork;
034import org.nuxeo.runtime.api.Framework;
035import org.nuxeo.runtime.model.ComponentInstance;
036import org.nuxeo.runtime.model.DefaultComponent;
037
038import java.io.IOException;
039import java.util.Map;
040
041/**
042 * @since 7.3
043 */
044public class MediaPublishingServiceImpl extends DefaultComponent implements MediaPublishingService {
045
046    protected static final Log log = LogFactory.getLog(MediaPublishingServiceImpl.class);
047
048    public static final String PROVIDER_EP = "providers";
049
050    protected MediaPublishingProviderRegistry providers = new MediaPublishingProviderRegistry();
051
052    @Override
053    public String[] getAvailableProviders(DocumentModel doc) {
054        return providers.getServices().toArray(new String[]{});
055    }
056
057    public MediaPublishingProvider getProvider(String provider) {
058        MediaPublishingProviderDescriptor descriptor = providers.lookup(provider);
059        return (MediaPublishingProvider) Framework.getService(descriptor.getService());
060    }
061
062    @Override
063    public String publish(DocumentModel doc, String serviceId, String account, Map<String, String> options) {
064        MediaPublishingProvider service = getProvider(serviceId);
065        WorkManager workManager = Framework.getLocalService(WorkManager.class);
066        if (workManager == null) {
067            throw new RuntimeException("No WorkManager available");
068        }
069
070        Work work = new MediaPublishingUploadWork(serviceId, service, doc.getRepositoryName(), doc.getId(), doc.getCoreSession(), account, options);
071        workManager.schedule(work, WorkManager.Scheduling.IF_NOT_RUNNING_OR_SCHEDULED);
072        return work.getId();
073    }
074
075    @Override
076    public void unpublish(DocumentModel doc, String provider) {
077        MediaPublishingProvider service = getProvider(provider);
078        PublishableMedia media = doc.getAdapter(PublishableMedia.class);
079        try {
080            if (service.unpublish(media)) {
081                // Remove provider from the list of published providers
082                media.removeProvider(provider);
083
084                // Track unpublish in document history
085                CoreSession session = doc.getCoreSession();
086                DocumentEventContext ctx = new DocumentEventContext(session, session.getPrincipal(), doc);
087                ctx.setComment("Video unpublished from " + provider);
088                ctx.setCategory(DocumentEventCategories.EVENT_DOCUMENT_CATEGORY);
089                Event event = ctx.newEvent(DocumentEventTypes.DOCUMENT_PUBLISHED);
090                EventProducer evtProducer = Framework.getService(EventProducer.class);
091                evtProducer.fireEvent(event);
092                session.saveDocument(doc);
093                session.save();
094            }
095        } catch (IOException e) {
096            throw new NuxeoException("Failed to unpublish media", e);
097        }
098    }
099
100    @Override
101    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
102        if (PROVIDER_EP.equals(extensionPoint)) {
103            MediaPublishingProviderDescriptor provider = (MediaPublishingProviderDescriptor) contribution;
104            providers.addContribution(provider);
105        }
106    }
107}