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