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