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;
023import static org.nuxeo.ecm.platform.video.VideoConstants.HAS_STORYBOARD_FACET;
024import static org.nuxeo.ecm.platform.video.VideoConstants.HAS_VIDEO_PREVIEW_FACET;
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.core.work.api.WorkManager;
034import org.nuxeo.ecm.platform.video.VideoHelper;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * Work to process the video info of a Video document and schedule two works to process the storyboard and conversions,
039 * see {@link VideoStoryboardWork} and {@link VideoConversionWork}.
040 *
041 * @since 10.1
042 */
043public class VideoInfoWork extends AbstractWork {
044
045    private static final long serialVersionUID = 1L;
046
047    private static final Log log = LogFactory.getLog(VideoInfoWork.class);
048
049    public static final String CATEGORY_VIDEO_INFO = "videoInfo";
050
051    protected static String computeIdPrefix(String repositoryName, String docId) {
052        return repositoryName + ':' + docId + ":videoinfo:";
053    }
054
055    public VideoInfoWork(String repositoryName, String docId) {
056        super(computeIdPrefix(repositoryName, docId));
057        setDocument(repositoryName, docId);
058    }
059
060    @Override
061    public boolean isIdempotent() {
062        return false;
063    }
064
065    @Override
066    public String getCategory() {
067        return CATEGORY_VIDEO_INFO;
068    }
069
070    @Override
071    public String getTitle() {
072        return "Video Info: " + getId();
073    }
074
075    @Override
076    public void work() {
077        setStatus("Updating video info");
078        setProgress(Progress.PROGRESS_INDETERMINATE);
079        openSystemSession();
080
081        // get video blob and update video info
082        DocumentModel doc = session.getDocument(new IdRef(docId));
083        BlobHolder blobHolder = doc.getAdapter(BlobHolder.class);
084        Blob video = blobHolder.getBlob();
085        log.debug(String.format("Updating video info of document %s.", doc));
086        VideoHelper.updateVideoInfo(doc, video);
087        log.debug(String.format("End updating video info of document %s.", doc));
088
089        // save document
090        if (doc.isVersion()) {
091            doc.putContextData(ALLOW_VERSION_WRITE, Boolean.TRUE);
092        }
093        session.saveDocument(doc);
094
095        if (doc.hasFacet(HAS_VIDEO_PREVIEW_FACET) && doc.hasFacet(HAS_STORYBOARD_FACET)) {
096            // schedule storyboard work
097            WorkManager workManager = Framework.getService(WorkManager.class);
098            VideoStoryboardWork work = new VideoStoryboardWork(doc.getRepositoryName(), doc.getId());
099            log.debug(String.format("Scheduling work: storyboard of Video document %s.", doc));
100            workManager.schedule(work, true);
101        }
102
103        // schedule conversion work
104        VideoService videoService = Framework.getService(VideoService.class);
105        log.debug(String.format("Launching automatic conversions of Video document %s.", doc));
106        videoService.launchAutomaticConversions(doc);
107
108        setStatus("Done");
109    }
110
111}