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