001/* 002 * (C) Copyright 2013 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 * Thomas Roger 018 */ 019 020package org.nuxeo.ecm.restapi.server.jaxrs; 021 022import java.io.Serializable; 023import java.util.HashMap; 024import java.util.Map; 025 026import javax.servlet.http.HttpServletRequest; 027 028import org.nuxeo.ecm.automation.core.util.Paginable; 029import org.nuxeo.ecm.automation.core.util.PaginablePageProvider; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.NuxeoException; 032import org.nuxeo.ecm.platform.query.api.PageProvider; 033import org.nuxeo.ecm.platform.query.api.PageProviderDefinition; 034import org.nuxeo.ecm.platform.query.api.PageProviderService; 035import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider; 036import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 037import org.nuxeo.runtime.api.Framework; 038 039/** 040 * Paginable WebObject. 041 * <p> 042 * To be extended by WebObject returning paginable entries based on a {@link PageProvider}. 043 * 044 * @since 5.8 045 */ 046public abstract class PaginableObject<T> extends DefaultObject { 047 048 protected Long currentPageIndex; 049 050 protected Long pageSize; 051 052 protected String maxResults; 053 054 @Override 055 protected void initialize(Object... args) { 056 super.initialize(args); 057 058 final HttpServletRequest request = ctx.getRequest(); 059 currentPageIndex = extractLongParam(request, "currentPageIndex", 0L); 060 pageSize = extractLongParam(request, "pageSize", 50L); 061 maxResults = request.getParameter("maxResults"); 062 } 063 064 protected abstract PageProviderDefinition getPageProviderDefinition(); 065 066 protected Object[] getParams() { 067 return new Object[] {}; 068 } 069 070 protected DocumentModel getSearchDocument() { 071 return null; 072 } 073 074 @SuppressWarnings("unchecked") 075 public Paginable<T> getPaginableEntries() { 076 PageProviderDefinition ppDefinition = getPageProviderDefinition(); 077 if (ppDefinition == null) { 078 throw new NuxeoException("Page provider given not found"); 079 } 080 081 PageProviderService pps = Framework.getLocalService(PageProviderService.class); 082 Map<String, Serializable> props = new HashMap<String, Serializable>(); 083 props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) ctx.getCoreSession()); 084 085 return getPaginableEntries((PageProvider<T>) pps.getPageProvider("", ppDefinition, getSearchDocument(), null, 086 pageSize, currentPageIndex, props, getParams())); 087 } 088 089 protected Paginable<T> getPaginableEntries(PageProvider<T> pageProvider) { 090 return new PaginablePageProvider<>(pageProvider); 091 } 092 093 protected Long extractLongParam(HttpServletRequest request, String paramName, Long defaultValue) { 094 String strParam = request.getParameter(paramName); 095 return strParam == null ? defaultValue : Long.parseLong(strParam); 096 } 097 098}