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 LONG:
057            case DATE:
058            case DATETIME:
059            case BOOLEAN:
060            case CONTENT:
061            case SUB_CONTENT:
062            case DOUBLE:
063            case USER:
064                return true;
065            case COMPOSITE:
066            case BINARY:
067            case FILE:
068            case RICH_TEXT:
069            case REFERENCE:
070                return false;
071            default:
072                return false;
073        }
074    }
075    
076    /**
077     * Test if the criterion can be set as a facet (i.e. it has a defined list of discrete values).
078     * @return true if the criterion can be set as a facet, false otherwise.
079     */
080    default boolean isFacetable()
081    {
082        return isFacetable(getType(), getEnumerator() != null);
083    }
084    
085    /**
086     * Test if a field is facetable.
087     * @param type the type of the metadata
088     * @param isEnumerated is the metadata enumerated ?
089     * @return true if the field can be used as a facet, false otherwise.
090     */
091    static boolean isFacetable(MetadataType type, boolean isEnumerated)
092    {
093        boolean facetable = false;
094        
095        if (type == MetadataType.CONTENT || type == MetadataType.SUB_CONTENT || type == MetadataType.USER)
096        {
097            facetable = true;
098        }
099        else if (isEnumerated)
100        {
101            facetable = true;
102        }
103        
104        return facetable;
105    }
106    
107    /**
108     * Get the criterion Operator.
109     * @return the criterion Operator.
110     */
111    Operator getOperator();
112    
113    /**
114     * Get the {@link Query} associated to the given value.
115     * @param value The user-submitted value (or the default value if not set) for this criterion.
116     * @param allValues All the user-submitted values.
117     * @param language The current search language.
118     * @param contextualParameters the search contextual parameters.
119     * @return {@link Query} associated to the given value.
120    */
121    default Query getQuery(Object value, Map<String, Object> allValues, String language, Map<String, Object> contextualParameters)
122    {
123        return getQuery(value, null, allValues, language, contextualParameters);
124    }
125    
126    /**
127     * Get the {@link Query} associated to the given value.
128     * @param value The user-submitted value (or the default value if not set) for this criterion.
129     * @param customOperator In advanced search mode, the operator chosen by the user. <code>null</code> to use the criterion-defined operator (simple search mode).
130     * @param allValues All the user-submitted values.
131     * @param language The current search language.
132     * @param contextualParameters the search contextual parameters.
133     * @return {@link Query} associated to the given value.
134     */
135    Query getQuery(Object value, Operator customOperator, Map<String, Object> allValues, String language, Map<String, Object> contextualParameters);
136    
137    /**
138     * Get the {@link SearchField} representing this search criterion.
139     * @return the {@link SearchField} representing this search criterion.
140     */
141    SearchField getSearchField();
142
143}