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.solr.field;
017
018import java.util.List;
019import java.util.Locale;
020import java.util.Objects;
021import java.util.Optional;
022
023import org.apache.avalon.framework.context.Context;
024import org.apache.cocoon.components.ContextHelper;
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.cms.search.cocoon.SearchAction;
028
029/**
030 * String generic search field.
031 */
032public class MultilingualStringSearchField extends AbstractModelItemSearchField
033{
034    private Optional<Context> _context;
035
036    /**
037     * Build a multilingual string search field.
038     * @param path The field path.
039     * @param context The context
040     */
041    public MultilingualStringSearchField(String path, Optional<Context> context)
042    {
043        super(path);
044        _context = context;
045    }
046    
047    /**
048     * Build a multilingual string search field.
049     * @param joinPaths The join field paths
050     * @param finalPath The final field path.
051     * @param context The context
052     */
053    public MultilingualStringSearchField(List<String> joinPaths, String finalPath, Optional<Context> context)
054    {
055        super(joinPaths, finalPath);
056        _context = context;
057    }
058
059    @Override
060    protected String _getSortFieldSuffix()
061    {
062        return getCurrentLanguage(_context)
063                .map("_"::concat)
064                .orElse(StringUtils.EMPTY)
065                + "_s_sort";
066    }
067    
068    /**
069     * Gets the current language
070     * @param context The context
071     * @return the current language
072     */
073    protected static Optional<String> getCurrentLanguage(Optional<Context> context)
074    {
075        return context
076                .map(c -> {
077                    try
078                    {
079                        return ContextHelper.getRequest(c);
080                    }
081                    catch (RuntimeException e)
082                    {
083                        return null;
084                    }
085                })
086                .filter(Objects::nonNull)
087                .map(r -> r.getAttribute(SearchAction.SEARCH_LOCALE))
088                .filter(Locale.class::isInstance)
089                .map(Locale.class::cast)
090                .map(Locale::getLanguage);
091    }
092    
093    @Override
094    protected String _getFacetFieldSuffix()
095    {
096        return "_s_dv";
097    }
098
099}