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