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.contenttype.MetadataType;
024import org.ametys.cms.search.query.Query;
025import org.ametys.cms.search.query.Query.Operator;
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.runtime.parameter.Validator;
028
029/**
030 * A potential search criterion proposed to the creator of an instance of search service.
031 */
032public interface SearchCriterionDefinition
033{
034    /**
035     * Gets the id of the criterion. It must be unique across all criterion definitions
036     * @return the unique id of the criterion
037     */
038    String getId();
039    
040    /**
041     * Gets the label of the criterion.
042     * @return the label of the criterion.
043     */
044    I18nizableText getLabel();
045    
046    /**
047     * Gets the prefix labels, sometimes useful (such as for displaying all criterion definitions) for grouping information.
048     * @return The prefix labels (can be empty)
049     */
050    default List<I18nizableText> getContextPrefixLabels()
051    {
052        return Collections.emptyList();
053    }
054    
055    /**
056     * Gets the ({@link Searchable}) the criterion belongs to.
057     * 
058     * <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}.
059     * If the criterion definition is brought by {@link SearchServiceCommonImpls}, then an {@link Optional#empty()} must be returned.
060     * 
061     * @return the ({@link Searchable}) the criterion belongs to.
062     */
063    Optional<Searchable> getSearchable();
064    
065    /**
066     * Gets the type of criterion
067     * @return the type of criterion
068     */
069    MetadataType getType();
070    
071    /**
072     * Returns <code>true</code> if this criterion definition is enumerated
073     * @return <code>true</code> if this criterion definition is enumerated
074     */
075    boolean isEnumerated();
076    
077    /**
078     * Gets the enumerated entries.
079     * <br>Must be non-{@link Optional#empty empty} if {@link #isEnumerated} returns <code>true</code>.
080     * @param contextualParameters The contextual parameters
081     * @return the enumerated entries
082     */
083    Optional<EnumeratedValues> getEnumeratedValues(Map<String, Object> contextualParameters);
084    
085    /**
086     * Returns <code>true</code> if this criterion definition is enumerated but can contains too much data
087     * @return <code>true</code> if this criterion definition is enumerated but can contains too much data
088     */
089    default boolean isTooBigForStaticEnumerator() 
090    {
091        return false;
092    }
093    
094    /**
095     * Gets the widget
096     * @return the widget
097     */
098    String getWidget();
099    
100    /**
101     * Gets the widget parameters
102     * @return the widget parameters
103     */
104    Map<String, I18nizableText> getWidgetParameters();
105    
106    /**
107     * Gets the validator
108     * @return the validator
109     */
110    Validator getValidator();
111    
112    /**
113     * Gets the JSON representation of this criterion definition
114     * @return the JSON representation of this criterion definition
115     * @throws Exception if an exception occurs
116     */
117    Map<String, Object> toJSON() throws Exception;
118    
119    /**
120     * Gets the query associated to the given value
121     * @param value the value
122     * @param operator the operator
123     * @param language The current search language.
124     * @param contextualParameters the search contextual parameters.
125     * @return the query associated to the given value
126     */
127    Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters);
128}