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 * dmetzler 018 */ 019package org.nuxeo.ecm.restapi.server.jaxrs.adapters; 020 021import javax.servlet.http.HttpServletRequest; 022import javax.ws.rs.Produces; 023import javax.ws.rs.core.MediaType; 024 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.platform.query.api.PageProviderDefinition; 027import org.nuxeo.ecm.platform.query.api.PageProviderService; 028import org.nuxeo.ecm.webengine.model.WebAdapter; 029import org.nuxeo.ecm.webengine.model.exceptions.IllegalParameterException; 030import org.nuxeo.runtime.api.Framework; 031 032/** 033 * @since 5.7.3 034 */ 035@WebAdapter(name = SearchAdapter.NAME, type = "SearchService") 036@Produces({ "application/json+nxentity", "application/json+esentity", MediaType.APPLICATION_JSON }) 037public class SearchAdapter extends DocumentModelListPaginableAdapter { 038 039 public static final String NAME = "search"; 040 041 public static final String pageProviderName = "REST_API_SEARCH_ADAPTER"; 042 043 private String extractQueryFromRequest(final HttpServletRequest request) { 044 String query = request.getParameter("query"); 045 if (query == null) { 046 String fullText = request.getParameter("fullText"); 047 if (fullText == null) { 048 throw new IllegalParameterException("Expecting a query or a fullText parameter"); 049 } 050 String orderBy = request.getParameter("orderBy"); 051 String orderClause = ""; 052 if (orderBy != null) { 053 orderClause = " ORDER BY " + orderBy; 054 } 055 String path; 056 057 DocumentModel doc = getTarget().getAdapter(DocumentModel.class); 058 if (doc.isFolder()) { 059 path = doc.getPathAsString(); 060 } else { 061 path = doc.getPath().removeLastSegments(1).toString(); 062 } 063 query = "SELECT * FROM Document WHERE (ecm:fulltext = \"" + fullText 064 + "\") AND (ecm:isCheckedInVersion = 0) AND (ecm:path STARTSWITH \"" + path + "\")" + orderClause; 065 } 066 return query; 067 } 068 069 @Override 070 protected PageProviderDefinition getPageProviderDefinition() { 071 072 String query = extractQueryFromRequest(ctx.getRequest()); 073 PageProviderService ppService = Framework.getLocalService(PageProviderService.class); 074 PageProviderDefinition ppdefinition = ppService.getPageProviderDefinition(pageProviderName); 075 ppdefinition.setPattern(query); 076 077 if (maxResults != null && !maxResults.isEmpty() && !maxResults.equals("-1")) { 078 // set the maxResults to avoid slowing down queries 079 ppdefinition.getProperties().put("maxResults", maxResults); 080 } 081 return ppdefinition; 082 } 083}