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.adapter;
021
022import static org.nuxeo.ecm.platform.video.VideoConstants.INFO_PROPERTY;
023import static org.nuxeo.ecm.platform.video.VideoConstants.TRANSCODED_VIDEOS_PROPERTY;
024
025import java.io.Serializable;
026import java.util.Collection;
027import java.util.List;
028import java.util.Map;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.NuxeoException;
035import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
036import org.nuxeo.ecm.platform.video.TranscodedVideo;
037import org.nuxeo.ecm.platform.video.Video;
038import org.nuxeo.ecm.platform.video.VideoConstants;
039import org.nuxeo.ecm.platform.video.VideoDocument;
040import org.nuxeo.ecm.platform.video.VideoInfo;
041
042import com.google.common.collect.Maps;
043
044/**
045 * Default implementation of {@link VideoDocument}.
046 *
047 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
048 * @since 5.5
049 */
050public class VideoDocumentAdapter implements VideoDocument {
051
052    private static final Log log = LogFactory.getLog(VideoDocumentAdapter.class);
053
054    private final DocumentModel doc;
055
056    private final Video video;
057
058    private Map<String, TranscodedVideo> transcodedVideos;
059
060    @SuppressWarnings("unchecked")
061    public VideoDocumentAdapter(DocumentModel doc) {
062        if (!doc.hasFacet(VideoConstants.VIDEO_FACET)) {
063            throw new NuxeoException(doc + " is not a Video document.");
064        }
065        this.doc = doc;
066        BlobHolder bh = doc.getAdapter(BlobHolder.class);
067        Blob blob = bh.getBlob();
068
069        Map<String, Serializable> videoInfoMap = (Map<String, Serializable>) doc.getPropertyValue(INFO_PROPERTY);
070        VideoInfo videoInfo = VideoInfo.fromMap(videoInfoMap);
071        video = Video.fromBlobAndInfo(blob, videoInfo);
072    }
073
074    @Override
075    public Video getVideo() {
076        return video;
077    }
078
079    @Override
080    public Collection<TranscodedVideo> getTranscodedVideos() {
081        if (transcodedVideos == null) {
082            initTranscodedVideos();
083        }
084        return transcodedVideos.values();
085    }
086
087    @Override
088    public TranscodedVideo getTranscodedVideo(String name) {
089        if (transcodedVideos == null) {
090            initTranscodedVideos();
091        }
092        return transcodedVideos.get(name);
093    }
094
095    private void initTranscodedVideos() {
096        if (transcodedVideos == null) {
097            @SuppressWarnings("unchecked")
098            List<Map<String, Serializable>> videos = (List<Map<String, Serializable>>) doc.getPropertyValue(
099                    TRANSCODED_VIDEOS_PROPERTY);
100            transcodedVideos = Maps.newHashMap();
101            for (int i = 0; i < videos.size(); i++) {
102                TranscodedVideo transcodedVideo = TranscodedVideo.fromMapAndPosition(videos.get(i), i);
103                transcodedVideos.put(transcodedVideo.getName(), transcodedVideo);
104            }
105        }
106    }
107
108}