001/*
002 *  Copyright 2018 Anyware Services
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 */
016package org.ametys.web.frontoffice.search.metamodel;
017
018import java.util.Collections;
019import java.util.List;
020import java.util.Map;
021import java.util.Optional;
022
023import org.ametys.cms.search.model.CriterionDefinition;
024import org.ametys.cms.search.query.Query;
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.runtime.model.Enumerator;
027import org.ametys.runtime.model.type.DataContext;
028
029/**
030 * A potential criterion definition proposed to the creator of an instance of search service.
031 * @param <T> Type of the criterion value
032 */
033public interface SearchServiceCriterionDefinition<T> extends CriterionDefinition<T> 
034{
035    /**
036     * Gets the prefix labels, sometimes useful (such as for displaying all criterion definitions) for grouping information.
037     * @return The prefix labels (can be empty)
038     */
039    default List<I18nizableText> getContextPrefixLabels()
040    {
041        return Collections.emptyList();
042    }
043    
044    /**
045     * Gets the ({@link Searchable}) the criterion belongs to.
046     * 
047     * <br><b>Important</b>: if the criterion definition is brought by a specific Searchable, then this searchable must be returned in a non-empty {@link Optional}.
048     * If the criterion definition is brought by {@link SearchServiceCommonImpls}, then an {@link Optional#empty()} must be returned.
049     * 
050     * @return the ({@link Searchable}) the criterion belongs to.
051     */
052    Optional<Searchable> getSearchable();
053    
054    /**
055     * Sets the ({@link Searchable}) the criterion belongs to.
056     * @param searchable the {@link Searchable} to set
057     */
058    void setSearchable(Searchable searchable);
059    
060    /**
061     * Determines if this criterion definition is enumerated
062     * @return <code>true</code> if this criterion definition is enumerated, <code>false</code> otherwise
063     */
064    boolean isEnumerated();
065    
066    /**
067     * Gets the {@link RestrictedEnumerator} used by the final user to fill the criterion.
068     * Values of this {@link Enumerator} can be restricted according to entered values during service configuration
069     * @param contextualParameters The contextual parameters
070     * @return the enumerated entries
071     */
072    RestrictedEnumerator<T> getRestrictedEnumerator(Map<String, Object> contextualParameters);
073    
074    /**
075     * Converts the given value for {@link RestrictedEnumerator} 
076     * @param value the value to convert
077     * @return the converted value
078     */
079    @SuppressWarnings("unchecked")
080    default T convertRestrictedValue(Object value)
081    {
082        return (T) getType().fromJSONForClient(value, DataContext.newInstance());
083    }
084    
085    /**
086     * Gets the query for testing the value is empty
087     * @param language The current search language.
088     * @param contextualParameters the search contextual parameters.
089     * @return the query for testing the value is empty
090     */
091    Query getEmptyValueQuery(String language, Map<String, Object> contextualParameters);
092    
093    /**
094     * Retrieves the facet value to use for SAX events
095     * @param value the facet value
096     * @param contextualParameters the contextual parameters
097     * @return the facet value to use for SAX events
098     */
099    default String facetValueToSAX(String value, Map<String, Object> contextualParameters)
100    {
101        return value;
102    }
103}