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