001/*
002 *  Copyright 2016 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;
017
018import java.util.List;
019
020import org.apache.commons.lang3.StringUtils;
021
022/**
023 * Represents a search field.
024 */
025public interface SearchField
026{
027    
028    /**
029     * Get the search field name.
030     * @return The search field name.
031     */
032    String getName();
033    
034    /**
035     * Get the sort field corresponding to this field.
036     * @return The sort field.
037     */
038    String getSortField();
039    
040    /**
041     * Get the facet field corresponding to this field.
042     * @return The facet field.
043     */
044    String getFacetField();
045    
046    /**
047     * Indicates if the search field is joined
048     * @return <code>true</code> if the search field is joined
049     */
050    boolean isJoined();
051    
052    /**
053     * Gets the joined paths
054     * @return the joined paths
055     */
056    List<String> getJoinedPaths();
057    
058    /**
059     * Get the facet function corresponding to this field.
060     * @return The facet function.
061     */
062    default String getFacetFunction()
063    {
064        String facetField = getFacetField();
065        if (facetField == null)
066        {
067            return null; // not facetable
068        }
069        
070        List<String> joinedPaths = getJoinedPaths();
071        if (isJoined())
072        {
073            return "join(" + StringUtils.join(joinedPaths, "->") + "," + facetField + ")";
074        }
075        else
076        {
077            return null;
078        }
079    }
080    
081}