001/*
002 * (C) Copyright 2013 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-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 *     dmetzler
016 */
017package org.nuxeo.ecm.restapi.server.jaxrs.adapters;
018
019import javax.ws.rs.GET;
020import javax.ws.rs.Path;
021import javax.ws.rs.PathParam;
022import javax.ws.rs.Produces;
023import javax.ws.rs.core.MediaType;
024
025import org.nuxeo.ecm.automation.core.util.Paginable;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.platform.query.api.PageProviderDefinition;
028import org.nuxeo.ecm.platform.query.api.PageProviderService;
029import org.nuxeo.ecm.webengine.model.WebAdapter;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Adapter that expose a page provider that needs only one parameter that is the document Id
034 *
035 * @since 5.7.2
036 */
037@WebAdapter(name = PageProviderAdapter.NAME, type = "PageProviderService")
038@Produces({ "application/json+nxentity", "application/json+esentity", MediaType.APPLICATION_JSON })
039public class PageProviderAdapter extends DocumentModelListPaginableAdapter {
040
041    public static final String NAME = "pp";
042
043    private String pageProviderName = "CURRENT_DOC_CHILDREN";
044
045    @Override
046    protected PageProviderDefinition getPageProviderDefinition() {
047        PageProviderService ppService = Framework.getLocalService(PageProviderService.class);
048        return ppService.getPageProviderDefinition(pageProviderName);
049    }
050
051    @Override
052    protected Object[] getParams() {
053        return new Object[] { getTarget().getAdapter(DocumentModel.class).getId() };
054    }
055
056    @GET
057    @Path("{pageProviderName}")
058    public Paginable<DocumentModel> getProviderDocs(@PathParam("pageProviderName") String providerName)
059            {
060        pageProviderName = providerName;
061        return super.getPaginableEntries();
062    }
063
064}