001/*
002 * (C) Copyright 2012 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.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 *     bjalon
016 */
017package org.nuxeo.ecm.mobile.webengine.adapter;
018
019import java.io.Serializable;
020import java.util.HashMap;
021import java.util.Map;
022
023import javax.ws.rs.GET;
024import javax.ws.rs.QueryParam;
025
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.platform.query.api.PageProvider;
028import org.nuxeo.ecm.platform.query.api.PageProviderService;
029import org.nuxeo.ecm.webengine.model.WebAdapter;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
034 * @since 5.5
035 */
036@WebAdapter(name = "mobileSearch", type = "DefaultSearch", targetType = "MobileDocument")
037public class SearchAdapter extends DefaultMobileAdapter {
038
039    private PageProviderService service;
040
041    @GET
042    public Object doGet(@QueryParam("pageIndex") long pageIndex) {
043
044        PageProvider<?> pageProvider = getPageProvider(pageIndex);
045
046        pageIndex = pageProvider.getCurrentPageIndex();
047
048        Map<String, Object> args = new HashMap<String, Object>();
049        args.put("page", pageProvider.getCurrentPage());
050        args.put("pageNumber", pageProvider.getNumberOfPages());
051        args.put("pageIndex", pageIndex);
052
053        return getView("index").args(args);
054    }
055
056    private PageProvider<?> getPageProvider(long pageIndex) {
057        Map<String, Serializable> prop = new HashMap<String, Serializable>();
058        prop.put("coreSession", (Serializable) ctx.getCoreSession());
059
060        PageProvider<?> pageProvider = getPageProviderService().getPageProvider("search_core_default", null, 9L, null,
061                prop);
062
063        DocumentModel searchCriteria = getDocumentModel();
064        pageProvider.setSearchDocumentModel(searchCriteria);
065
066        if (pageIndex < 0) {
067            pageIndex = 0;
068        }
069        // TODO : Waiting an improvement of CoreQuerySession if pageIndex > 0
070        pageProvider.setCurrentPage(0);
071
072        if (pageIndex > pageProvider.getNumberOfPages()) {
073            pageIndex = pageProvider.getNumberOfPages();
074        }
075        pageProvider.setCurrentPage(pageIndex);
076        return pageProvider;
077    }
078
079    public PageProviderService getPageProviderService() {
080        if (service == null) {
081            service = Framework.getService(PageProviderService.class);
082        }
083        return service;
084    }
085
086}