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.text.DateFormat;
020import java.util.ArrayList;
021import java.util.Date;
022import java.util.List;
023import java.util.Map;
024
025import org.nuxeo.ecm.platform.suggestbox.service.CommonSuggestionTypes;
026import org.nuxeo.ecm.platform.suggestbox.service.ComponentInitializationException;
027import org.nuxeo.ecm.platform.suggestbox.service.SearchDocumentsSuggestion;
028import org.nuxeo.ecm.platform.suggestbox.service.Suggester;
029import org.nuxeo.ecm.platform.suggestbox.service.Suggestion;
030import org.nuxeo.ecm.platform.suggestbox.service.SuggestionContext;
031import org.nuxeo.ecm.platform.suggestbox.service.SuggestionException;
032import org.nuxeo.ecm.platform.suggestbox.service.descriptors.SuggesterDescriptor;
033import org.nuxeo.ecm.platform.suggestbox.utils.DateMatcher;
034
035/**
036 * Simple stateless suggester that parses the input and suggest to search document by date if the input can be
037 * interpreted as a date in the user locale.
038 * 
039 * @deprecated since 6.0. This suggester is not used anymore with the new search tab.
040 */
041@Deprecated
042public class DocumentSearchByDateSuggester implements Suggester {
043
044    static final String type = CommonSuggestionTypes.SEARCH_DOCUMENTS;
045
046    static final String LABEL_BEFORE_PREFIX = "label.search.beforeDate_";
047
048    static final String LABEL_AFTER_PREFIX = "label.search.afterDate_";
049
050    protected String[] searchFields;
051
052    protected String label;
053
054    protected String iconURL;
055
056    protected String suggesterId = "DocumentSearchByDateSuggester";
057
058    @Override
059    public List<Suggestion> suggest(String userInput, SuggestionContext context) throws SuggestionException {
060        List<Suggestion> suggestions = new ArrayList<Suggestion>();
061        I18nHelper i18n = I18nHelper.instanceFor(context.messages);
062
063        // TODO: use SimpleDateFormat and use the locale information from the
064        // context
065        DateMatcher matcher = DateMatcher.fromInput(userInput);
066        DateFormat labelDateFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM, context.locale);
067
068        if (matcher.hasMatch()) {
069            Date date = matcher.getDateSuggestion().getTime();
070            String formattedDateLabel = labelDateFormatter.format(date);
071
072            for (String field : searchFields) {
073                String searchFieldAfter = field + "_min";
074                String labelAfterPrefix = LABEL_AFTER_PREFIX + field.replace(':', '_');
075                String labelAfter = i18n.translate(labelAfterPrefix, formattedDateLabel);
076                suggestions.add(new SearchDocumentsSuggestion(suggesterId, labelAfter, iconURL).withSearchCriterion(
077                        searchFieldAfter, date));
078
079                String searchFieldBefore = field + "_max";
080                String labelBeforePrefix = LABEL_BEFORE_PREFIX + field.replace(':', '_');
081                String labelBefore = i18n.translate(labelBeforePrefix, formattedDateLabel);
082                suggestions.add(new SearchDocumentsSuggestion(suggesterId, labelBefore, iconURL).withSearchCriterion(
083                        searchFieldBefore, date));
084            }
085        }
086        return suggestions;
087    }
088
089    @Override
090    public void initWithParameters(SuggesterDescriptor descriptor) throws ComponentInitializationException {
091        Map<String, String> params = descriptor.getParameters();
092        iconURL = params.get("iconURL");
093        String searchFields = params.get("searchFields");
094        if (searchFields == null || iconURL == null) {
095            throw new ComponentInitializationException(String.format("Could not initialize suggester '%s': "
096                    + "searchFields and iconURL" + " are mandatory parameters", descriptor.getName()));
097        }
098        this.searchFields = searchFields.split(", *");
099    }
100
101}