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