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.SearchField;
023import org.ametys.runtime.i18n.I18nizableText;
024import org.ametys.web.frontoffice.search.metamodel.FacetDefinition;
025import org.ametys.web.frontoffice.search.metamodel.Returnable;
026
027/**
028 * {@link FacetDefinition} for {@link ContentReturnable}.
029 * <br>Based on a {@link ContentSearchCriterionDefinition}.
030 */
031public class ContentFacetDefinition implements FacetDefinition
032{
033    ContentSearchCriterionDefinition _contentSearchCriterionDefinition;
034    Returnable _returnable;
035    
036    /**
037     * Default constructor
038     * @param contentSearchCriterionDefinition the {@link ContentSearchCriterionDefinition} it is based on
039     * @param returnable the {@link Returnable}
040     */
041    public ContentFacetDefinition(ContentSearchCriterionDefinition contentSearchCriterionDefinition, Returnable returnable)
042    {
043        _contentSearchCriterionDefinition = contentSearchCriterionDefinition;
044        _returnable = returnable;
045    }
046    
047    @Override
048    public String getId()
049    {
050        return ContentReturnable.__PREFIX_ID + _contentSearchCriterionDefinition.getId();
051    }
052
053    @Override
054    public I18nizableText getLabel()
055    {
056        return _contentSearchCriterionDefinition.getLabel();
057    }
058
059    @Override
060    public SearchField getSearchField()
061    {
062        SearchField searchField = _contentSearchCriterionDefinition.getSearchUICriterion().getSearchField();
063        return new ProxySearchField(getId(), searchField);
064    }
065
066    @Override
067    public Optional<I18nizableText> getFacetLabel(String value, Map<String, Object> contextualParameters)
068    {
069        return _contentSearchCriterionDefinition.getFacetLabel(value, contextualParameters);
070    }
071
072    @Override
073    public Optional<Returnable> getReturnable()
074    {
075        return Optional.of(_returnable);
076    }
077    
078    private static class ProxySearchField implements SearchField
079    {
080        SearchField _proxied;
081        String _realName;
082
083        ProxySearchField(String realName, SearchField proxied)
084        {
085            _proxied = proxied;
086            _realName = realName;
087        }
088
089        @Override
090        public String getName()
091        {
092            return _realName;
093        }
094
095        @Override
096        public String getSortField()
097        {
098            return _proxied.getSortField();
099        }
100
101        @Override
102        public String getFacetField()
103        {
104            return _proxied.getFacetField();
105        }
106
107        @Override
108        public boolean isJoined()
109        {
110            return _proxied.isJoined();
111        }
112
113        @Override
114        public List<String> getJoinedPaths()
115        {
116            return _proxied.getJoinedPaths();
117        }
118    }
119}