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.Collections;
019import java.util.List;
020import java.util.Optional;
021
022import org.ametys.cms.content.indexing.solr.SolrFieldNames;
023import org.ametys.cms.contenttype.ContentType;
024import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
025import org.ametys.cms.search.SearchField;
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.web.frontoffice.search.metamodel.FacetDefinition;
028import org.ametys.web.frontoffice.search.metamodel.Returnable;
029
030/**
031 * Common {@link FacetDefinition} to filter on the pseudo-content type of the document
032 */
033public class CommonPseudoContentTypeFacetDefinition implements FacetDefinition
034{
035    /** The id of the facet definition */
036    protected static final String __ID = "common$pseudoContentType";
037    
038    I18nizableText _label;
039    ContentTypeExtensionPoint _cTypeEP;
040    
041    /**
042     * Default constructor
043     * @param cTypeEP The extension point for content types
044     */
045    public CommonPseudoContentTypeFacetDefinition(ContentTypeExtensionPoint cTypeEP)
046    {
047        _label = new I18nizableText("plugin.web", "PLUGINS_WEB_SERVICE_SEARCH_COMMON_FACET_PSEUDO_CONTENT_TYPE");
048        _cTypeEP = cTypeEP;
049    }
050    
051    @Override
052    public String getId()
053    {
054        return __ID;
055    }
056
057    @Override
058    public I18nizableText getLabel()
059    {
060        return _label;
061    }
062    
063    @Override
064    public SearchField getSearchField()
065    {
066        return new PseudoContentTypeSearchField(getId());
067    }
068    
069    @Override
070    public I18nizableText getFacetLabel(String value, String currentLang)
071    {
072        if (SolrFieldNames.PSEUDO_CONTENT_TYPE_VALUE_RESOURCE.equals(value))
073        {
074            return new I18nizableText("plugin.web", "PLUGINS_WEB_SERVICE_SEARCH_COMMON_FACET_PSEUDO_CONTENT_TYPE_VALUE_RESOURCE");
075        }
076        else
077        {
078            // Try to get the label of the content type
079            return Optional.ofNullable(value)
080                    .map(_cTypeEP::getExtension)
081                    .map(ContentType::getLabel)
082                    .orElse(new I18nizableText(value));
083        }
084    }
085    
086    @Override
087    public Optional<Returnable> getReturnable()
088    {
089        // common
090        return Optional.empty();
091    }
092    
093    private static final class PseudoContentTypeSearchField implements SearchField
094    {
095        private String _name;
096        
097        PseudoContentTypeSearchField(String name)
098        {
099            _name = name;
100        }
101        
102        @Override
103        public String getSortField()
104        {
105            return null;
106        }
107        
108        @Override
109        public String getName()
110        {
111            return _name;
112        }
113        
114        @Override
115        public String getFacetField()
116        {
117            return SolrFieldNames.PSEUDO_CONTENT_TYPES + "_dv";
118        }
119        
120        @Override
121        public boolean isJoined()
122        {
123            return false;
124        }
125        
126        @Override
127        public List<String> getJoinedPaths()
128        {
129            return Collections.emptyList();
130        }
131    }
132}