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