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.util.Collections;
020import java.util.List;
021import java.util.Map;
022
023import org.nuxeo.ecm.platform.suggestbox.service.ComponentInitializationException;
024import org.nuxeo.ecm.platform.suggestbox.service.SearchDocumentsSuggestion;
025import org.nuxeo.ecm.platform.suggestbox.service.Suggester;
026import org.nuxeo.ecm.platform.suggestbox.service.Suggestion;
027import org.nuxeo.ecm.platform.suggestbox.service.SuggestionContext;
028import org.nuxeo.ecm.platform.suggestbox.service.SuggestionException;
029import org.nuxeo.ecm.platform.suggestbox.service.descriptors.SuggesterDescriptor;
030
031/**
032 * Simple stateless document search suggester that propose to use the user input for searching a specific field.
033 * 
034 * @deprecated since 6.0. This suggester is not used anymore with the new search tab.
035 */
036@Deprecated
037public class DocumentSearchByPropertySuggester implements Suggester {
038
039    protected String searchField = "fsd:ecm_fulltext";
040
041    protected String suggesterId = "DocumentSearchByPropertySuggester";
042
043    /**
044     * @since 5.8
045     */
046    protected String[] searchFields;
047
048    protected String label = "label.searchDocumentsByKeywords";
049
050    protected String description = "";
051
052    protected String iconURL = "/img/facetedSearch.png";
053
054    protected boolean disabled;
055
056    @Override
057    public List<Suggestion> suggest(String userInput, SuggestionContext context) throws SuggestionException {
058        I18nHelper i18n = I18nHelper.instanceFor(context.messages);
059        String i18nLabel = i18n.translate(label, userInput);
060        SearchDocumentsSuggestion suggestion = new SearchDocumentsSuggestion(suggesterId, i18nLabel, iconURL).withSearchCriterion(
061                searchField, userInput);
062        if (searchFields != null) {
063            for (String field : searchFields) {
064                suggestion.withSearchCriterion(field, userInput);
065            }
066        }
067        if (disabled) {
068            suggestion.disable();
069        }
070        if (description != null) {
071            suggestion.withDescription(i18n.translate(description, userInput));
072        }
073        return Collections.<Suggestion> singletonList(suggestion);
074    }
075
076    @Override
077    public void initWithParameters(SuggesterDescriptor descriptor) throws ComponentInitializationException {
078        Map<String, String> params = descriptor.getParameters();
079        searchField = params.get("searchField");
080        label = params.get("label");
081        String iconURL = params.get("iconURL");
082        if (iconURL != null) {
083            this.iconURL = iconURL;
084        }
085        description = params.get("description");
086        String disabled = params.get("disabled");
087        if (disabled != null) {
088            this.disabled = Boolean.parseBoolean(disabled);
089        }
090        String psearchFields = params.get("searchFields");
091        if (psearchFields != null) {
092            searchFields = psearchFields.split(", *");
093        }
094        if (label == null || (searchField == null && searchFields == null)) {
095            throw new ComponentInitializationException(String.format("Could not initialize suggester '%s': "
096                    + "label, searchField (or searchFields)" + " are mandatory parameters", descriptor.getName()));
097        }
098    }
099
100}