001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
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 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.platform.video.listener;
019
020import static org.nuxeo.ecm.platform.video.VideoConstants.VIDEO_CHANGED_EVENT;
021
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.event.Event;
024import org.nuxeo.ecm.core.event.EventBundle;
025import org.nuxeo.ecm.core.event.EventContext;
026import org.nuxeo.ecm.core.event.PostCommitFilteringEventListener;
027import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
028import org.nuxeo.ecm.platform.video.service.VideoService;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Listener to launch {@link org.nuxeo.ecm.platform.video.service.AutomaticVideoConversion}s when creating or updating a
033 * video file.
034 *
035 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
036 * @since 5.5
037 */
038public class VideoAutomaticConversionListener implements PostCommitFilteringEventListener {
039
040    @Override
041    public void handleEvent(EventBundle events) {
042        for (Event event : events) {
043            if (VIDEO_CHANGED_EVENT.equals(event.getName())) {
044                handleEvent(event);
045            }
046        }
047    }
048
049    private void handleEvent(Event event) {
050        EventContext ctx = event.getContext();
051        if (!(ctx instanceof DocumentEventContext)) {
052            return;
053        }
054
055        DocumentEventContext docCtx = (DocumentEventContext) ctx;
056        DocumentModel doc = docCtx.getSourceDocument();
057
058        VideoService videoService = Framework.getLocalService(VideoService.class);
059        videoService.launchAutomaticConversions(doc);
060    }
061
062    @Override
063    public boolean acceptEvent(Event event) {
064        return VIDEO_CHANGED_EVENT.equals(event.getName());
065    }
066
067}