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 offset; 051 052 protected Long pageSize; 053 054 protected String maxResults; 055 056 @Override 057 protected void initialize(Object... args) { 058 super.initialize(args); 059 060 final HttpServletRequest request = ctx.getRequest(); 061 currentPageIndex = extractLongParam(request, "currentPageIndex", 0L); 062 String offsetParam = request.getParameter("offset"); 063 offset = offsetParam == null ? null : Long.parseLong(offsetParam); 064 pageSize = extractLongParam(request, "pageSize", 50L); 065 maxResults = request.getParameter("maxResults"); 066 } 067 068 protected abstract PageProviderDefinition getPageProviderDefinition(); 069 070 protected Object[] getParams() { 071 return new Object[] {}; 072 } 073 074 protected DocumentModel getSearchDocument() { 075 return null; 076 } 077 078 @SuppressWarnings("unchecked") 079 public Paginable<T> getPaginableEntries() { 080 PageProviderDefinition ppDefinition = getPageProviderDefinition(); 081 if (ppDefinition == null) { 082 throw new NuxeoException("Page provider given not found"); 083 } 084 085 PageProviderService pps = Framework.getService(PageProviderService.class); 086 Map<String, Serializable> props = new HashMap<>(); 087 props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) ctx.getCoreSession()); 088 089 return getPaginableEntries((PageProvider<T>) pps.getPageProvider("", ppDefinition, getSearchDocument(), null, 090 pageSize, currentPageIndex, offset, props, null, null, getParams())); 091 } 092 093 protected Paginable<T> getPaginableEntries(PageProvider<T> pageProvider) { 094 return new PaginablePageProvider<>(pageProvider); 095 } 096 097 protected Long extractLongParam(HttpServletRequest request, String paramName, Long defaultValue) { 098 String strParam = request.getParameter(paramName); 099 return strParam == null ? defaultValue : Long.parseLong(strParam); 100 } 101 102}