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