001/*
002 *  Copyright 2018 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.web.frontoffice.search.metamodel.impl;
017
018import java.util.List;
019import java.util.Map;
020import java.util.Optional;
021
022import org.ametys.cms.search.solr.SearcherFactory.FacetDefinition;
023import org.ametys.runtime.i18n.I18nizableText;
024import org.ametys.web.frontoffice.search.metamodel.Returnable;
025import org.ametys.web.frontoffice.search.metamodel.SearchServiceFacetDefinition;
026import org.ametys.web.indexing.solr.SolrWebFieldNames;
027
028/**
029 * {@link SearchServiceFacetDefinition} for {@link PageReturnable}, which apply on a Content.
030 * @param <T> The type of the facet value
031 */
032public class PageContentFacetDefinition<T> implements SearchServiceFacetDefinition
033{
034    ContentFacetDefinition<T> _contentFacetDefinition;
035    Returnable _returnable;
036    
037    /**
038     * Default constructor
039     * @param contentFacetDefinition the {@link ContentFacetDefinition} it is based on
040     * @param returnable the {@link Returnable}
041     */
042    public PageContentFacetDefinition(ContentFacetDefinition<T> contentFacetDefinition, Returnable returnable)
043    {
044        _contentFacetDefinition = contentFacetDefinition;
045        _returnable = returnable;
046    }
047    
048    public String getName()
049    {
050        return PageReturnable.PREFIX_NAME + _contentFacetDefinition._referencingCriterionDefinition.getName();
051    }
052
053    public I18nizableText getLabel()
054    {
055        return _contentFacetDefinition.getLabel();
056    }
057    
058    public I18nizableText getDescription()
059    {
060        return _contentFacetDefinition.getDescription();
061    }
062    
063    public FacetDefinition getFacetDefinition()
064    {
065        FacetDefinition refFacetDefinition = _contentFacetDefinition.getFacetDefinition();
066        
067        List<String> joinedPaths = refFacetDefinition.joinedPaths();
068        joinedPaths.addFirst(SolrWebFieldNames.CONTENT_IDS);
069
070        return new FacetDefinition(getName(), refFacetDefinition.solrFacetFieldName(), joinedPaths);
071    }
072
073    public Optional<I18nizableText> getFacetLabel(String value, Map<String, Object> contextualParameters)
074    {
075        return _contentFacetDefinition.getFacetLabel(value, contextualParameters);
076    }
077
078    public Optional<Returnable> getReturnable()
079    {
080        return Optional.of(_returnable);
081    }
082}