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.ui.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
025import org.slf4j.Logger;
026
027import org.ametys.cms.search.model.SearchCriterion;
028import org.ametys.runtime.model.ViewItemContainer;
029import org.ametys.runtime.plugin.component.LogEnabled;
030
031/**
032 * Abstract class for SearchUIModel.
033 */
034public abstract class AbstractSearchUIModel implements SearchUIModel, LogEnabled
035{
036    /** The default plugin name for URLs */
037    protected static final String DEFAULT_URL_PLUGIN = "cms";
038    /** The default URL for search */
039    protected static final String DEFAULT_SEARCH_URL = "search/list.json";
040    /** The default URL for CSV export */
041    protected static final String DEFAULT_EXPORT_CSV_URL = "search/export.csv";
042    /** The default URL for DOC export */
043    protected static final String DEFAULT_EXPORT_DOC_URL = "search/export.doc";
044    /** The default URL for XML export */
045    protected static final String DEFAULT_EXPORT_XML_URL = "search/export.xml";
046    /** The default URL for PDF export */
047    protected static final String DEFAULT_EXPORT_PDF_URL = null;
048    /** The URL for print results */
049    protected static final String DEFAULT_PRINT_URL = "search/print.html";
050    
051    /** The logger. */
052    protected Logger _logger;
053    
054    /** The content types of this search model. */
055    protected Set<String> _cTypes;
056    /** The content types excluded from this search model. */
057    protected Set<String> _excludedCTypes;
058    
059    /** The search criteria in simple mode, indexed by ID. */
060    protected Map<String, SearchUICriterion> _searchCriteria;
061    /** The search criteria in advanced mode, indexed by ID. */
062    protected Map<String, SearchUICriterion> _advancedSearchCriteria;
063    /** The search criteria used as facets, indexed by ID. */
064    protected Map<String, SearchUICriterion> _facetedCriteria;
065    /** The result columns */
066    protected ViewItemContainer _resultItems;
067    
068    public void setLogger(final Logger logger)
069    {
070        _logger = logger;
071    }
072    
073    /**
074     * Get the logger.
075     * @return the logger.
076     */
077    protected final Logger getLogger()
078    {
079        return _logger;
080    }
081    
082    @Override
083    public Set<String> getContentTypes(Map<String, Object> contextualParameters)
084    {
085        return Collections.unmodifiableSet(_cTypes);
086    }
087    
088    /**
089     * Set the content types.
090     * @param cTypes The content types.
091     */
092    public void setContentTypes(Set<String> cTypes)
093    {
094        _cTypes = new HashSet<>(cTypes);
095    }
096    
097    @Override
098    public Set<String> getExcludedContentTypes(Map<String, Object> contextualParameters)
099    {
100        return Collections.unmodifiableSet(_excludedCTypes);
101    }
102    
103    /**
104     * Set the excluded content types.
105     * @param cTypes The excluded content types.
106     */
107    public void setExcludedContentTypes(Set<String> cTypes)
108    {
109        _excludedCTypes = new HashSet<>(cTypes);
110    }
111    
112    @Override
113    public Map<String, SearchUICriterion> getCriteria(Map<String, Object> contextualParameters)
114    {
115        return Collections.unmodifiableMap(_searchCriteria);
116    }
117    
118    /**
119     * Set the criteria in simple mode.
120     * @param criteria A collection of search criteria.
121     */
122    public void setCriteria(Collection<SearchUICriterion> criteria)
123    {
124        _searchCriteria = new LinkedHashMap<>();
125        for (SearchCriterion criterion : criteria)
126        {
127            _searchCriteria.put(criterion.getId(), (SearchUICriterion) criterion);
128        }
129    }
130    
131    @Override
132    public Map<String, SearchUICriterion> getAdvancedCriteria(Map<String, Object> contextualParameters)
133    {
134        return Collections.unmodifiableMap(_advancedSearchCriteria);
135    }
136    
137    /**
138     * Set the criteria in advanced mode.
139     * @param criteria A collection of search criteria.
140     */
141    public void setAdvancedCriteria(Collection<SearchUICriterion> criteria)
142    {
143        _advancedSearchCriteria = new LinkedHashMap<>();
144        for (SearchUICriterion criterion : criteria)
145        {
146            _advancedSearchCriteria.put(criterion.getId(), criterion);
147        }
148    }
149    
150    @Override
151    public Map<String, SearchUICriterion> getFacetedCriteria(Map<String, Object> contextualParameters)
152    {
153        return Collections.unmodifiableMap(_facetedCriteria);
154    }
155    
156    /**
157     * Set the criteria to use as facets.
158     * @param criteria A collection of search criteria.
159     */
160    public void setFacetedCriteria(Collection<SearchCriterion> criteria)
161    {
162        _facetedCriteria = new LinkedHashMap<>();
163        for (SearchCriterion criterion : criteria)
164        {
165            _facetedCriteria.put(criterion.getId(), (SearchUICriterion) criterion);
166        }
167    }
168    
169    public ViewItemContainer getResultItems(Map<String, Object> contextualParameters)
170    {
171        return _resultItems;
172    }
173    
174    /**
175     * Set the result items
176     * @param resultItems The result items to set
177     */
178    public void setResultItems(ViewItemContainer resultItems)
179    {
180        _resultItems = resultItems;
181    }
182    
183    @Override
184    public int getPageSize(Map<String, Object> contextualParameters)
185    {
186        // Use the default value or unlimited.
187        return -1;
188    }
189    
190    @Override
191    public String getWorkspace(Map<String, Object> contextualParameters)
192    {
193        // Use the default workspace.
194        return null;
195    }
196    
197    @Override
198    public String getSearchUrl(Map<String, Object> contextualParameters)
199    {
200        return DEFAULT_SEARCH_URL;
201    }
202    
203    @Override
204    public String getSearchUrlPlugin(Map<String, Object> contextualParameters)
205    {
206        return DEFAULT_URL_PLUGIN;
207    }
208    
209    @Override
210    public String getExportCSVUrl(Map<String, Object> contextualParameters)
211    {
212        return DEFAULT_EXPORT_CSV_URL;
213    }
214    
215    @Override
216    public String getExportCSVUrlPlugin(Map<String, Object> contextualParameters)
217    {
218        return DEFAULT_URL_PLUGIN;
219    }
220
221    @Override
222    public String getExportDOCUrl(Map<String, Object> contextualParameters)
223    {
224        return DEFAULT_EXPORT_DOC_URL;
225    }
226
227    @Override
228    public String getExportDOCUrlPlugin(Map<String, Object> contextualParameters)
229    {
230        return DEFAULT_URL_PLUGIN;
231    }
232
233    @Override
234    public String getExportXMLUrl(Map<String, Object> contextualParameters)
235    {
236        return DEFAULT_EXPORT_XML_URL;
237    }
238
239    @Override
240    public String getExportXMLUrlPlugin(Map<String, Object> contextualParameters)
241    {
242        return DEFAULT_URL_PLUGIN;
243    }
244    
245    @Override
246    public String getExportPDFUrl(Map<String, Object> contextualParameters)
247    {
248        return DEFAULT_EXPORT_PDF_URL;
249    }
250    
251    @Override
252    public String getExportPDFUrlPlugin(Map<String, Object> contextualParameters)
253    {
254        return DEFAULT_URL_PLUGIN;
255    }
256
257    @Override
258    public String getPrintUrl(Map<String, Object> contextualParameters)
259    {
260        return DEFAULT_PRINT_URL;
261    }
262
263    @Override
264    public String getPrintUrlPlugin(Map<String, Object> contextualParameters)
265    {
266        return DEFAULT_URL_PLUGIN;
267    }
268    
269    @Override
270    public String getSummaryView()
271    {
272        return null;
273    }
274}