001/*
002 *  Copyright 2015 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.cms.search.model;
017
018import java.util.Collection;
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.LinkedHashMap;
022import java.util.Map;
023import java.util.Set;
024
025/**
026 * Default implementation of a {@link SearchModel}.
027 */
028public class DefaultSearchModel implements SearchModel
029{
030    
031    /** The content types of this search model. */
032    protected Set<String> _cTypes;
033    /** The content types excluded from this search model. */
034    protected Set<String> _excludedCTypes;
035    
036    /** The simple search criteria, indexed by ID. */
037    protected Map<String, SearchCriterion> _searchCriteria;
038    /** The faceted search criteria, indexed by ID. */
039    protected Map<String, SearchCriterion> _facetedCriteria;
040    /** The simple search criteria, indexed by ID. */
041    protected Map<String, ResultField> _resultFields;
042    
043    public Set<String> getContentTypes(Map<String, Object> contextualParameters)
044    {
045        return Collections.unmodifiableSet(_cTypes);
046    }
047    
048    /**
049     * Set the content types of the models
050     * @param cTypes The content types.
051     */
052    public void setContentTypes(Set<String> cTypes)
053    {
054        _cTypes = new HashSet<>(cTypes);
055    }
056    
057    public Set<String> getExcludedContentTypes(Map<String, Object> contextualParameters)
058    {
059        return Collections.unmodifiableSet(_excludedCTypes);
060    }
061    
062    /**
063     * Set the content types to exclude
064     * @param cTypes The content types to exclude
065     */
066    public void setExcludedContentTypes(Set<String> cTypes)
067    {
068        _excludedCTypes = new HashSet<>(cTypes);
069    }
070    
071    public Map<String, ? extends SearchCriterion> getCriteria(Map<String, Object> contextualParameters)
072    {
073        return Collections.unmodifiableMap(_searchCriteria);
074    }
075    
076    /**
077     * Set the criteria
078     * @param criteria The criterion list
079     */
080    public void setCriteria(Collection<? extends SearchCriterion> criteria)
081    {
082        _searchCriteria = new LinkedHashMap<>();
083        for (SearchCriterion criterion : criteria)
084        {
085            _searchCriteria.put(criterion.getId(), criterion);
086        }
087    }
088    
089    public Map<String, ? extends SearchCriterion> getFacetedCriteria(Map<String, Object> contextualParameters)
090    {
091        return Collections.unmodifiableMap(_facetedCriteria);
092    }
093    
094    /**
095     * Set the faceted criteria
096     * @param criteria The list of faceted criterion
097     */
098    public void setFacetedCriteria(Collection<? extends SearchCriterion> criteria)
099    {
100        _facetedCriteria = new LinkedHashMap<>();
101        for (SearchCriterion criterion : criteria)
102        {
103            _facetedCriteria.put(criterion.getId(), criterion);
104        }
105    }
106    
107    public Map<String, ? extends ResultField> getResultFields(Map<String, Object> contextualParameters)
108    {
109        return Collections.unmodifiableMap(_resultFields);
110    }
111    
112    /**
113     * Set the result fields
114     * @param fields The list of result field
115     */
116    public void setResultFields(Collection<? extends ResultField> fields)
117    {
118        _resultFields = new LinkedHashMap<>();
119        for (ResultField field : fields)
120        {
121            _resultFields.put(field.getId(), field);
122        }
123    }
124    
125}