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