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