001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
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.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 *     Olivier Grisel
016 */
017package org.nuxeo.ecm.platform.video.storyboard;
018
019import static org.nuxeo.ecm.platform.video.VideoConstants.STORYBOARD_PROPERTY;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.codehaus.jackson.map.ObjectMapper;
026import org.codehaus.jackson.node.ObjectNode;
027import org.jboss.seam.ScopeType;
028import org.jboss.seam.annotations.Name;
029import org.jboss.seam.annotations.Scope;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.PropertyException;
032import org.nuxeo.ecm.platform.video.VideoConstants;
033
034/**
035 * Backing bean for the Storyboard view of an document with the video storyboard facet.
036 *
037 * @author ogrisel
038 */
039@Name("storyboardActions")
040@Scope(ScopeType.EVENT)
041public class StoryboardActions {
042
043    public List<StoryboardItem> getItems(DocumentModel doc) throws PropertyException {
044        if (!doc.hasFacet(VideoConstants.HAS_STORYBOARD_FACET)) {
045            return Collections.emptyList();
046        }
047        int size = doc.getProperty(STORYBOARD_PROPERTY).getValue(List.class).size();
048        List<StoryboardItem> items = new ArrayList<StoryboardItem>(size);
049        for (int i = 0; i < size; i++) {
050            items.add(new StoryboardItem(doc, STORYBOARD_PROPERTY, i));
051        }
052        return items;
053    }
054
055    public String getStoryboardItemsAsJsonSettings(DocumentModel doc) throws PropertyException {
056        List<StoryboardItem> items = getItems(doc);
057        ObjectMapper o = new ObjectMapper();
058        ObjectNode settings = o.createObjectNode();
059        for (StoryboardItem storyboardItem : items) {
060            ObjectNode thumb = o.createObjectNode();
061            thumb.put("src", storyboardItem.getUrl());
062            settings.put(storyboardItem.getTimecode().split("\\.")[0], thumb);
063        }
064        return settings.toString();
065    }
066
067}