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;
021
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.nuxeo.ecm.core.api.Blob;
027
028/**
029 * Object wrapping a transcoded video and related {@link VideoInfo}.
030 * <p>
031 * The {@code TranscodedVideo} is identified by its name and a position, if any, in the list of {@code TranscodedVideo}s
032 * for a given @{link VideoDocument}.
033 * <p>
034 * If this {@code TranscodedVideo} is not part
035 *
036 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
037 * @since 5.5
038 */
039public final class TranscodedVideo extends Video {
040
041    private static final String NAME = "name";
042
043    private static final String CONTENT = "content";
044
045    private static final String INFO = "info";
046
047    private final String name;
048
049    private final int position;
050
051    /**
052     * Build a {@code TranscodedVideo} from a {@code Map} of attributes and a {@code position}
053     */
054    public static TranscodedVideo fromMapAndPosition(Map<String, Serializable> map, int position) {
055        Blob blob = (Blob) map.get(CONTENT);
056        @SuppressWarnings("unchecked")
057        Map<String, Serializable> info = (Map<String, Serializable>) map.get(INFO);
058        VideoInfo videoInfo = VideoInfo.fromMap(info);
059        String name = (String) map.get(NAME);
060        return new TranscodedVideo(blob, videoInfo, name, position);
061    }
062
063    /**
064     * Build a {@code TranscodedVideo} from a {@code name}, video {@code blob} and related {@code videoInfo}.
065     */
066    public static TranscodedVideo fromBlobAndInfo(String name, Blob blob, VideoInfo videoInfo) {
067        return new TranscodedVideo(blob, videoInfo, name, -1);
068    }
069
070    private TranscodedVideo(Blob blob, VideoInfo videoInfo, String name, int position) {
071        super(blob, videoInfo);
072        this.name = name;
073        this.position = position;
074    }
075
076    /**
077     * Returns the name of this {@code TranscodedVideo}.
078     */
079    public String getName() {
080        return name;
081    }
082
083    /**
084     * Returns the video {@code Blob} property name of this {@code TranscodedVideo}.
085     */
086    public String getBlobPropertyName() {
087        if (position == -1) {
088            throw new IllegalStateException(
089                    "This transcoded video is not yet persisted, cannot generate property name.");
090        }
091        return "vid:transcodedVideos/" + position + "/content";
092    }
093
094    /**
095     * Returns a {@code Map} of attributes for this {@code TranscodedVideo}.
096     * <p>
097     * Used when saving this {@code TranscodedVideo} to a {@code DocumentModel} property.
098     */
099    public Map<String, Serializable> toMap() {
100        Map<String, Serializable> map = new HashMap<String, Serializable>();
101        map.put(NAME, name);
102        map.put(CONTENT, (Serializable) blob);
103        map.put(INFO, (Serializable) videoInfo.toMap());
104        return map;
105    }
106
107}