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