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