001/*
002 * (C) Copyright 2018 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 *     BenoƮt Delbosc <bdelbosc@nuxeo.com>
018 *     Antoine Taillefer <ataillefer@nuxeo.com>
019 */
020package org.nuxeo.ecm.platform.video.service;
021
022import static org.nuxeo.ecm.core.api.CoreSession.ALLOW_VERSION_WRITE;
023
024import java.io.IOException;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.IdRef;
031import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
032import org.nuxeo.ecm.core.work.AbstractWork;
033import org.nuxeo.ecm.platform.video.VideoHelper;
034
035/**
036 * Work to process the storyboard of a Video document.
037 *
038 * @since 10.1
039 */
040public class VideoStoryboardWork extends AbstractWork {
041
042    private static final long serialVersionUID = 1L;
043
044    private static final Log log = LogFactory.getLog(VideoStoryboardWork.class);
045
046    public static final String CATEGORY_VIDEO_STORYBOARD = "videoStoryboard";
047
048    protected static String computeIdPrefix(String repositoryName, String docId) {
049        return repositoryName + ':' + docId + ":videostoryboard:";
050    }
051
052    public VideoStoryboardWork(String repositoryName, String docId) {
053        super(computeIdPrefix(repositoryName, docId));
054        setDocument(repositoryName, docId);
055    }
056
057    @Override
058    public boolean isIdempotent() {
059        return false;
060    }
061
062    @Override
063    public String getCategory() {
064        return CATEGORY_VIDEO_STORYBOARD;
065    }
066
067    @Override
068    public String getTitle() {
069        return "Video Storyboard: " + getId();
070    }
071
072    @Override
073    public void work() {
074        setProgress(Progress.PROGRESS_INDETERMINATE);
075        openSystemSession();
076
077        // get video blob
078        DocumentModel doc = session.getDocument(new IdRef(docId));
079        BlobHolder blobHolder = doc.getAdapter(BlobHolder.class);
080        Blob video = blobHolder.getBlob();
081
082        // update storyboard
083        setStatus("Updating storyboard");
084        log.debug(String.format("Updating storyboard of Video document %s.", doc));
085        VideoHelper.updateStoryboard(doc, video);
086        log.debug(String.format("End updating storyboard of Video document %s.", doc));
087
088        // update previews
089        setStatus("Updating previews");
090        log.debug(String.format("Updating previews of Video document %s.", doc));
091        try {
092            VideoHelper.updatePreviews(doc, video);
093            log.debug(String.format("End updating previews of Video document %s.", doc));
094        } catch (IOException e) {
095            // this should only happen if the hard drive is full
096            log.debug(String.format("Failed to extract previews of Video document %s.", doc), e);
097        }
098
099        // save document
100        if (doc.isVersion()) {
101            doc.putContextData(ALLOW_VERSION_WRITE, Boolean.TRUE);
102        }
103        session.saveDocument(doc);
104
105        setStatus("Done");
106    }
107
108}