001/* 002 * (C) Copyright 2010-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 * Olivier Grisel 018 */ 019package org.nuxeo.ecm.platform.suggestbox.service.suggesters; 020 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.Arrays; 024import java.util.Collections; 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028 029import org.apache.commons.lang.StringUtils; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.query.QueryParseException; 032import org.nuxeo.ecm.platform.query.api.PageProvider; 033import org.nuxeo.ecm.platform.query.api.PageProviderService; 034import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider; 035import org.nuxeo.ecm.platform.query.nxql.NXQLQueryBuilder; 036import org.nuxeo.ecm.platform.suggestbox.service.DocumentSuggestion; 037import org.nuxeo.ecm.platform.suggestbox.service.Suggester; 038import org.nuxeo.ecm.platform.suggestbox.service.Suggestion; 039import org.nuxeo.ecm.platform.suggestbox.service.SuggestionContext; 040import org.nuxeo.ecm.platform.suggestbox.service.SuggestionException; 041import org.nuxeo.ecm.platform.suggestbox.service.descriptors.SuggesterDescriptor; 042import org.nuxeo.runtime.api.Framework; 043 044/** 045 * Perform a NXQL full-text query (on the title by default) on the repository and suggest to navigate to the top 046 * documents matching that query. 047 * 048 * @author ogrisel 049 */ 050public class DocumentLookupSuggester implements Suggester { 051 052 protected List<String> highlights = null; 053 054 protected String providerName = "DEFAULT_DOCUMENT_SUGGESTION"; 055 056 protected SuggesterDescriptor descriptor; 057 058 @Override 059 public void initWithParameters(SuggesterDescriptor descriptor) { 060 this.descriptor = descriptor; 061 String providerName = descriptor.getParameters().get("providerName"); 062 if (providerName != null) { 063 this.providerName = providerName; 064 } 065 String highlightFields = descriptor.getParameters().get("highlightFields"); 066 if (highlightFields != null) { 067 if (!StringUtils.isBlank(highlightFields)) { 068 String[] fields = highlightFields.split(","); 069 highlights = Arrays.asList(fields); 070 } 071 } 072 } 073 074 @Override 075 @SuppressWarnings("unchecked") 076 public List<Suggestion> suggest(String userInput, SuggestionContext context) throws SuggestionException { 077 PageProviderService ppService = Framework.getLocalService(PageProviderService.class); 078 Map<String, Serializable> props = new HashMap<String, Serializable>(); 079 props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) context.session); 080 userInput = NXQLQueryBuilder.sanitizeFulltextInput(userInput); 081 if (userInput.trim().isEmpty()) { 082 return Collections.emptyList(); 083 } 084 if (!userInput.endsWith(" ")) { 085 // perform a prefix search on the last typed word 086 userInput += "*"; 087 } 088 try { 089 List<Suggestion> suggestions = new ArrayList<Suggestion>(); 090 PageProvider<DocumentModel> pp = (PageProvider<DocumentModel>) ppService.getPageProvider(providerName, null, 091 null, null, null, props, highlights, null, new Object[] { userInput }); 092 for (DocumentModel doc : pp.getCurrentPage()) { 093 suggestions.add(DocumentSuggestion.fromDocumentModel(doc)); 094 } 095 return suggestions; 096 } catch (QueryParseException e) { 097 throw new SuggestionException(String.format("Suggester '%s' failed to perform query with input '%s'", 098 descriptor.getName(), userInput), e); 099 } 100 } 101}