001/*
002 *  Copyright 2015 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.cms.search.model;
017
018import java.util.Map;
019
020import org.ametys.cms.contenttype.MetadataType;
021import org.ametys.cms.search.SearchField;
022import org.ametys.cms.search.query.Query;
023import org.ametys.cms.search.query.Query.Operator;
024import org.ametys.runtime.i18n.I18nizableText;
025
026/**
027 * Represents a Field that can be searched on.
028 */
029public interface SearchCriterion extends Field
030{
031    /**
032     * Get the SearchCriterion ID.
033     * @return the SearchCriterion ID.
034     */
035    String getId();
036    
037    /**
038     * Retrieves the label.
039     * @return the label.
040     */
041    public I18nizableText getLabel();
042    
043    /**
044     * Retrieves the description.
045     * @return the description.
046     */
047    public I18nizableText getDescription();
048    
049    /**
050     * Test if the SearchCriterion is sortable.
051     * @return <code>true</code> if the criterion is sortable, false otherwise.
052     */
053    default boolean isSortable()
054    {
055        return isSortable(this);
056    }
057    
058    /**
059     * Test if a field is sortable.
060     * @param field the field to test.
061     * @return true if the field is sortable, false otherwise.
062     */
063    static boolean isSortable(Field field)
064    {
065        switch (field.getType())
066        { 
067            case STRING:
068            case MULTILINGUAL_STRING:
069            case LONG:
070            case DATE:
071            case DATETIME:
072            case BOOLEAN:
073            case CONTENT:
074            case SUB_CONTENT:
075            case DOUBLE:
076            case USER:
077            case REFERENCE:
078                return true;
079            case COMPOSITE:
080            case BINARY:
081            case FILE:
082            case RICH_TEXT:
083                return false;
084            default:
085                return false;
086        }
087    }
088    
089    /**
090     * Test if the criterion can be set as a facet (i.e. it has a defined list of discrete values).
091     * @return true if the criterion can be set as a facet, false otherwise.
092     */
093    default boolean isFacetable()
094    {
095        return isFacetable(getType(), getEnumerator() != null);
096    }
097    
098    /**
099     * Test if a field is facetable.
100     * @param type the type of the metadata
101     * @param isEnumerated is the metadata enumerated ?
102     * @return true if the field can be used as a facet, false otherwise.
103     */
104    static boolean isFacetable(MetadataType type, boolean isEnumerated)
105    {
106        return type == MetadataType.CONTENT || type == MetadataType.SUB_CONTENT || type == MetadataType.USER || type == MetadataType.BOOLEAN
107                || isEnumerated;
108    }
109    
110    /**
111     * Get the criterion Operator.
112     * @return the criterion Operator.
113     */
114    Operator getOperator();
115    
116    /**
117     * Get the {@link Query} associated to the given value.
118     * @param value The user-submitted value (or the default value if not set) for this criterion.
119     * @param allValues All the user-submitted values.
120     * @param language The current search language.
121     * @param contextualParameters the search contextual parameters.
122     * @return {@link Query} associated to the given value.
123    */
124    default Query getQuery(Object value, Map<String, Object> allValues, String language, Map<String, Object> contextualParameters)
125    {
126        return getQuery(value, null, allValues, language, contextualParameters);
127    }
128    
129    /**
130     * Get the {@link Query} associated to the given value.
131     * @param value The user-submitted value (or the default value if not set) for this criterion.
132     * @param customOperator In advanced search mode, the operator chosen by the user. <code>null</code> to use the criterion-defined operator (simple search mode).
133     * @param allValues All the user-submitted values.
134     * @param language The current search language.
135     * @param contextualParameters the search contextual parameters.
136     * @return {@link Query} associated to the given value.
137     */
138    Query getQuery(Object value, Operator customOperator, Map<String, Object> allValues, String language, Map<String, Object> contextualParameters);
139    
140    /**
141     * Get the {@link SearchField} representing this search criterion.
142     * @return the {@link SearchField} representing this search criterion.
143     */
144    SearchField getSearchField();
145}