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    boolean isTooBigForStaticEnumerator();
090    
091    /**
092     * Gets the widget
093     * @return the widget
094     */
095    String getWidget();
096    
097    /**
098     * Gets the widget parameters
099     * @return the widget parameters
100     */
101    Map<String, I18nizableText> getWidgetParameters();
102    
103    /**
104     * Gets the validator
105     * @return the validator
106     */
107    Validator getValidator();
108    
109    /**
110     * Gets the JSON representation of this criterion definition
111     * @return the JSON representation of this criterion definition
112     * @throws Exception if an exception occurs
113     */
114    Map<String, Object> toJSON() throws Exception;
115    
116    /**
117     * Gets the query associated to the given value
118     * @param value the value
119     * @param operator the operator
120     * @param language The current search language.
121     * @param contextualParameters the search contextual parameters.
122     * @return the query associated to the given value
123     */
124    Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters);
125    
126    /**
127     * Gets the query for testing the value is empty
128     * @param language The current search language.
129     * @param contextualParameters the search contextual parameters.
130     * @return the query for testing the value is empty
131     */
132    Query getEmptyValueQuery(String language, Map<String, Object> contextualParameters);
133}