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.Collections;
019import java.util.List;
020import java.util.Optional;
021
022import org.apache.avalon.framework.context.Context;
023import org.apache.commons.collections4.ListUtils;
024import org.apache.commons.lang3.StringUtils;
025
026import org.ametys.cms.content.indexing.solr.SolrFieldNames;
027
028/**
029 * Content generic search field.
030 */
031public class ContentSearchField extends StringSearchField
032{
033    private boolean _isMultilingual;
034    private Optional<Context> _context;
035    
036    /**
037     * Build a content search field.
038     * @param path The field path.
039     */
040    public ContentSearchField(String path)
041    {
042        this(path, false, Optional.empty());
043    }
044    /**
045     * Build a content search field.
046     * @param joinPaths The join field paths
047     * @param finalPath The final field path.
048     */
049    public ContentSearchField(List<String> joinPaths, String finalPath)
050    {
051        this(joinPaths, finalPath, false, Optional.empty());
052    }
053    
054    /**
055     * Build a content search field.
056     * @param path The field path.
057     * @param isMultilingual <code>true</code> if the Content has a multlilingual title
058     * @param context The context
059     */
060    public ContentSearchField(String path, boolean isMultilingual, Optional<Context> context)
061    {
062        super(path);
063        _isMultilingual = isMultilingual;
064        _context = context;
065    }
066    /**
067     * Build a content search field.
068     * @param joinPaths The join field paths
069     * @param finalPath The final field path.
070     * @param isMultilingual <code>true</code> if the Content has a multilingual title
071     * @param context The context
072     */
073    public ContentSearchField(List<String> joinPaths, String finalPath, boolean isMultilingual, Optional<Context> context)
074    {
075        super(joinPaths, finalPath);
076        _isMultilingual = isMultilingual;
077        _context = context;
078    }
079    
080    @Override
081    public List<String> getJoinedPaths()
082    {
083        // As the join must be done until the id field, '_path' is part of the join
084        return _joinPaths != null ? ListUtils.union(_joinPaths, Collections.singletonList(_path)) : Collections.emptyList();
085    }
086    
087    @Override
088    protected String _getSortFieldSuffix()
089    {
090        return _isMultilingual
091                ? _getMultilingualSortFieldSuffix()
092                : super._getSortFieldSuffix();
093    }
094    
095    private String _getMultilingualSortFieldSuffix()
096    {
097        String languageSuffix = MultilingualStringSearchField.getCurrentLanguage(_context)
098            .map("_"::concat)
099            .orElse(StringUtils.EMPTY);
100        return super._getSortFieldSuffix() + languageSuffix;
101    }
102    
103    @Override
104    public String getFacetField()
105    {
106        if (isJoined())
107        {
108            // The join must be done until the id field !
109            return SolrFieldNames.ID + "_dv";
110        }
111        else
112        {
113            return super.getFacetField();
114        }
115    }
116}