001/*
002 * (C) Copyright 2011 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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.contentview.seam;
018
019import static org.jboss.seam.ScopeType.EVENT;
020
021import java.io.Serializable;
022import java.io.UnsupportedEncodingException;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.jboss.seam.annotations.In;
027import org.jboss.seam.annotations.Name;
028import org.jboss.seam.annotations.Scope;
029import org.nuxeo.ecm.core.api.SortInfo;
030import org.nuxeo.ecm.platform.contentview.jsf.ContentView;
031import org.nuxeo.ecm.platform.contentview.jsf.ContentViewService;
032import org.nuxeo.ecm.platform.contentview.jsf.ContentViewState;
033import org.nuxeo.ecm.platform.contentview.jsf.ContentViewStateImpl;
034import org.nuxeo.ecm.platform.contentview.json.JSONContentViewState;
035
036/**
037 * Restful actions for save and restore of a content view
038 *
039 * @since 5.4.2
040 */
041@Name("contentViewRestActions")
042@Scope(EVENT)
043public class ContentViewRestActions implements Serializable {
044
045    private static final long serialVersionUID = 1L;
046
047    @In(create = true)
048    protected ContentViewService contentViewService;
049
050    public String getContentViewState(ContentView contentView) throws UnsupportedEncodingException {
051        ContentViewState state = contentViewService.saveContentView(contentView);
052        if (state != null) {
053            return JSONContentViewState.toJSON(state, true);
054        }
055        return null;
056    }
057
058    public ContentView restoreContentView(String contentViewName, Long currentPage, Long pageSize,
059            List<SortInfo> sortInfos, String jsonContentViewState) throws UnsupportedEncodingException {
060        ContentViewState state = null;
061        if (jsonContentViewState != null && jsonContentViewState.trim().length() != 0) {
062            state = JSONContentViewState.fromJSON(jsonContentViewState, true);
063        } else if (contentViewName != null) {
064            // restore only from name
065            state = new ContentViewStateImpl();
066            state.setContentViewName(contentViewName);
067        }
068        if (state != null) {
069            // apply current page and page size when set
070            if (currentPage != null && currentPage.longValue() != -1) {
071                state.setCurrentPage(currentPage);
072            }
073            if (pageSize != null && pageSize.longValue() != -1) {
074                state.setPageSize(pageSize);
075            }
076            if (sortInfos != null) {
077                state.setSortInfos(sortInfos);
078            }
079        }
080        return contentViewService.restoreContentView(state);
081    }
082
083    public List<SortInfo> getSortInfos(String sortColumn, boolean ascending) {
084        List<SortInfo> sortInfos = new ArrayList<SortInfo>();
085        sortInfos.add(new SortInfo(sortColumn, ascending));
086        return sortInfos;
087    }
088
089}