001/*
002 *  Copyright 2017 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.solr.field;
017
018import java.util.List;
019
020import org.apache.commons.collections4.CollectionUtils;
021import org.apache.commons.lang3.StringUtils;
022
023import org.ametys.cms.search.JoinableSearchField;
024import org.ametys.cms.search.SearchField;
025
026/**
027 * SearchField 
028 */
029public class JoinedSystemSearchField implements JoinableSearchField
030{   
031    /** The join paths */
032    protected List<String> _joinPaths;
033    /** The reference search field */
034    protected SearchField _refSearchField;
035
036    /**
037     * Build a JoinedSystemSearchField
038     * @param joinPaths The join field paths. Must not be null or empty.
039     * @param refSearchField The reference search field. Must not be joined.
040     */
041    public JoinedSystemSearchField(List<String> joinPaths, SearchField refSearchField)
042    {
043        if (CollectionUtils.isEmpty(joinPaths))
044        {
045            throw new IllegalArgumentException("The join path must not be empty.");
046        }
047        if (refSearchField instanceof JoinableSearchField && ((JoinableSearchField) refSearchField).isJoined())
048        {
049            throw new IllegalArgumentException("The passed reference search field must not already be a joined search field.");
050        }
051        _joinPaths = joinPaths;
052        _refSearchField = refSearchField;
053    }
054    
055    @Override
056    public String getName()
057    {
058        return StringUtils.join(_joinPaths, '/') + '/' + _refSearchField.getName();
059    }
060    
061    @Override
062    public boolean isJoined()
063    {
064        return true;
065    }
066    
067    @Override
068    public String getJoinedPath()
069    {
070        return StringUtils.join(_joinPaths, "->");
071    }
072    
073    @Override
074    public String getSortField()
075    {
076        String refSortField = _refSearchField.getSortField();
077        return refSortField != null ? "join(" + getJoinedPath() + "," + refSortField + ")" : null;
078    }
079
080    @Override
081    public String getFacetField()
082    {
083        return _refSearchField.getFacetField();
084    }
085    
086    @Override
087    public String getFacetFunction()
088    {
089        String refFacetField = _refSearchField.getFacetField();
090        return refFacetField != null ? "join(" + getJoinedPath() + "," + refFacetField + ")" : null;
091    }
092}