001/*
002 * (C) Copyright 2014 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 *     Nelson Silva <nelson.silva@inevo.pt>
016 */
017package org.nuxeo.ecm.platform.spreadsheet;
018
019import static org.jboss.seam.ScopeType.EVENT;
020
021import java.io.Serializable;
022import java.io.UnsupportedEncodingException;
023import java.net.URLEncoder;
024
025import org.jboss.seam.annotations.In;
026import org.jboss.seam.annotations.Name;
027import org.jboss.seam.annotations.Scope;
028
029import org.nuxeo.ecm.platform.contentview.jsf.ContentView;
030import org.nuxeo.ecm.platform.contentview.jsf.ContentViewService;
031import org.nuxeo.ecm.platform.contentview.jsf.ContentViewState;
032import org.nuxeo.ecm.platform.contentview.json.JSONContentViewState;
033import org.nuxeo.ecm.platform.forms.layout.io.Base64;
034import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
035
036/**
037 * Restful actions for Nuxeo Spreadsheet
038 *
039 * @since 6.0
040 */
041@Name("spreadsheetActions")
042@Scope(EVENT)
043public class SpreadsheetActions implements Serializable {
044
045    @In(create = true)
046    protected ContentViewService contentViewService;
047
048    public String urlFor(ContentView contentView) throws UnsupportedEncodingException {
049        String cv = "";
050
051        // Set the content view state
052        ContentViewState state = contentViewService.saveContentView(contentView);
053        if (state != null) {
054            String json = JSONContentViewState.toJSON(state, false);
055            String encoded = Base64.encodeBytes(json.getBytes(), Base64.DONT_BREAK_LINES);
056            cv = URLEncoder.encode(encoded, "UTF-8");
057        }
058
059        return VirtualHostHelper.getContextPathProperty() + "/spreadsheet/index.html?cv=" + cv;
060    }
061}