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