001/*
002 * (C) Copyright 2010 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 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.platform.video.listener;
021
022import static org.nuxeo.ecm.core.api.CoreSession.ALLOW_VERSION_WRITE;
023import static org.nuxeo.ecm.platform.video.VideoConstants.HAS_STORYBOARD_FACET;
024import static org.nuxeo.ecm.platform.video.VideoConstants.VIDEO_CHANGED_EVENT;
025
026import java.io.IOException;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
033import org.nuxeo.ecm.core.event.Event;
034import org.nuxeo.ecm.core.event.EventBundle;
035import org.nuxeo.ecm.core.event.EventContext;
036import org.nuxeo.ecm.core.event.PostCommitFilteringEventListener;
037import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
038import org.nuxeo.ecm.platform.video.VideoHelper;
039
040/**
041 * Core event listener to compute / update the storyboard of a Video document
042 *
043 * @author ogrisel
044 */
045public class VideoStoryboardListener implements PostCommitFilteringEventListener {
046
047    public static final Log log = LogFactory.getLog(VideoStoryboardListener.class);
048
049    @Override
050    public void handleEvent(EventBundle events) {
051        for (Event event : events) {
052            if (VIDEO_CHANGED_EVENT.equals(event.getName())) {
053                handleEvent(event);
054            }
055        }
056    }
057
058    public void handleEvent(Event event) {
059        EventContext ctx = event.getContext();
060        if (!(ctx instanceof DocumentEventContext)) {
061            return;
062        }
063        DocumentEventContext docCtx = (DocumentEventContext) ctx;
064        DocumentModel doc = docCtx.getSourceDocument();
065        if (doc.hasFacet(HAS_STORYBOARD_FACET)) {
066            BlobHolder blobHolder = doc.getAdapter(BlobHolder.class);
067            VideoHelper.updateStoryboard(doc, blobHolder.getBlob());
068            try {
069                VideoHelper.updatePreviews(doc, blobHolder.getBlob());
070            } catch (IOException e) {
071                // this should only happen if the hard drive is full
072                log.error(
073                        String.format("Failed to extract previews for video '%s': %s", doc.getTitle(), e.getMessage()),
074                        e);
075            }
076            CoreSession session = docCtx.getCoreSession();
077            if (doc.isVersion()) {
078                doc.putContextData(ALLOW_VERSION_WRITE, Boolean.TRUE);
079            }
080            session.saveDocument(doc);
081            session.save();
082        }
083    }
084
085    @Override
086    public boolean acceptEvent(Event event) {
087        return VIDEO_CHANGED_EVENT.equals(event.getName());
088    }
089}