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