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.ArrayList;
019import java.util.List;
020import java.util.Map;
021import java.util.Optional;
022
023import org.ametys.cms.search.SearchField;
024import org.ametys.runtime.i18n.I18nizableText;
025import org.ametys.web.frontoffice.search.metamodel.FacetDefinition;
026import org.ametys.web.frontoffice.search.metamodel.Returnable;
027import org.ametys.web.indexing.solr.SolrWebFieldNames;
028
029/**
030 * {@link FacetDefinition} for {@link PageReturnable}, which apply on a Content.
031 */
032public class PageContentFacetDefinition implements FacetDefinition
033{
034    ContentFacetDefinition _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 contentFacetDefinition, Returnable returnable)
043    {
044        _contentFacetDefinition = contentFacetDefinition;
045        _returnable = returnable;
046    }
047    
048    @Override
049    public String getId()
050    {
051        return PageReturnable.__PREFIX_ID + _contentFacetDefinition._contentSearchCriterionDefinition.getId();
052    }
053
054    @Override
055    public I18nizableText getLabel()
056    {
057        return _contentFacetDefinition.getLabel();
058    }
059
060    @Override
061    public SearchField getSearchField()
062    {
063        SearchField refSearchField = _contentFacetDefinition.getSearchField();
064        return new PageContentSearchField(getId(), refSearchField);
065    }
066
067    @Override
068    public Optional<I18nizableText> getFacetLabel(String value, Map<String, Object> contextualParameters)
069    {
070        return _contentFacetDefinition.getFacetLabel(value, contextualParameters);
071    }
072
073    @Override
074    public Optional<Returnable> getReturnable()
075    {
076        return Optional.of(_returnable);
077    }
078    
079    private static class PageContentSearchField implements SearchField
080    {
081        String _name;
082        SearchField _refSearchField;
083        
084        PageContentSearchField(String name, SearchField refField)
085        {
086            _name = name;
087            _refSearchField = refField;
088        }
089        
090        @Override
091        public String getName()
092        {
093            return _name;
094        }
095
096        @Override
097        public String getSortField()
098        {
099            return null;
100        }
101
102        @Override
103        public String getFacetField()
104        {
105            return _refSearchField.getFacetField();
106        }
107
108        @Override
109        public boolean isJoined()
110        {
111            return true;
112        }
113        
114        @Override
115        public List<String> getJoinedPaths()
116        {
117            List<String> joinedPaths = new ArrayList<>();
118
119            joinedPaths.add(SolrWebFieldNames.CONTENT_IDS);
120            if (_refSearchField.isJoined())
121            {
122                joinedPaths.addAll(_refSearchField.getJoinedPaths());
123            }
124            
125            return joinedPaths;
126        }
127    }
128}