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