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;
018
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import javax.ws.rs.GET;
024import javax.ws.rs.Path;
025import javax.ws.rs.Produces;
026import javax.ws.rs.QueryParam;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.IterableQueryResult;
033import org.nuxeo.ecm.core.query.QueryParseException;
034import org.nuxeo.ecm.webengine.WebException;
035import org.nuxeo.ecm.webengine.model.Template;
036import org.nuxeo.ecm.webengine.model.WebObject;
037import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
038import org.nuxeo.runtime.api.Framework;
039import org.nuxeo.search.ui.SearchUIService;
040
041/**
042 * Manage authentication form and logout action
043 *
044 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
045 * @since 5.5
046 */
047@WebObject(type = "Search")
048@Produces("text/html;charset=UTF-8")
049public class Search extends DefaultObject {
050
051    private static final Log log = LogFactory.getLog(Search.class);
052
053    private static final String QUERY_PATTERN = "SELECT ecm:uuid, dc:title, dc:description, common:icon "
054            + "FROM Document WHERE ecm:fulltext = '%s' " + "AND (ecm:isCheckedInVersion = 0) "
055            + "AND (ecm:mixinType != 'HiddenInNavigation') " + "AND (ecm:currentLifeCycleState != 'deleted') %s";
056
057    private static final String ORDER_PATTERN = "ORDER BY %s";
058
059    @GET
060    public Template doGet(@QueryParam("q") String fulltext, @QueryParam("order") String orderParam,
061            @QueryParam("max") Integer max) {
062        String order = (orderParam != null && !"".equals(orderParam.trim())) ? String.format(ORDER_PATTERN, orderParam)
063                : "";
064        String query = String.format(QUERY_PATTERN, fulltext, order);
065
066        return doGetNXQL(query, max).arg("fulltext", fulltext);
067    }
068
069    @GET
070    @Path("nxql")
071    public Template doGetNXQL(@QueryParam("q") String queryString, @QueryParam("max") Integer max) {
072        CoreSession session = ctx.getCoreSession();
073        IterableQueryResult docs;
074
075        if (max == null) {
076            max = 20;
077        }
078
079        try {
080            docs = session.queryAndFetch(queryString, "NXQL");
081        } catch (QueryParseException e) {
082            log.error(e, e);
083            throw new WebException(e.getMessage());
084        }
085        return getView("index").arg("docs", docs.iterator()).arg("size", docs.size()).arg("max", max).arg("q",
086                queryString);
087    }
088
089    @GET
090    @Path("list")
091    public Object doGetFacetedSearches() {
092        Map<String, Object> args = new HashMap<String, Object>();
093        args.put("mySearches", mySearch());
094        args.put("sharedSearches", sharedSearches());
095        return getView("saved-searches").args(args);
096    }
097
098    private List<DocumentModel> mySearch() {
099        CoreSession session = ctx.getCoreSession();
100        return getSearchService().getCurrentUserSavedSearches(session);
101    }
102
103    private Object sharedSearches() {
104        CoreSession session = ctx.getCoreSession();
105        return getSearchService().getSharedSavedSearches(session);
106    }
107
108    private SearchUIService getSearchService() {
109        return Framework.getLocalService(SearchUIService.class);
110    }
111}