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