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