001/*
002 *  Copyright 2023 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.impl;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.LinkedHashMap;
022import java.util.Map;
023
024import org.ametys.cms.search.model.DefaultSearchModel;
025import org.ametys.cms.search.model.SearchCriterion;
026import org.ametys.cms.search.ui.model.SearchUICriterion;
027import org.ametys.cms.search.ui.model.SearchUIModel;
028
029/**
030 * Default implementation of a {@link SearchUIModel}.
031 */
032public class DefaultSearchUIModel extends DefaultSearchModel implements SearchUIModel
033{
034    /** The simple search ui criteria, indexed by ID. */
035    protected Map<String, SearchUICriterion> _uiCriteria;
036    /** The faceted search ui criteria, indexed by ID. */
037    protected Map<String, SearchUICriterion> _uiFacetedCriteria;
038    /** The advanced search ui criteria, indexed by ID. */
039    protected Map<String, SearchUICriterion> _uiAdvancedCriteria;
040    
041    /** The page size **/
042    protected int _pageSize;
043    
044    /** The URL for search */
045    protected String _searchUrl;
046    /** The plugin name for search */
047    protected String _searchUrlPlugin;
048    
049    /** The URL for CSV export of results */
050    protected String _exportCSVUrl;
051    /** The plugin name for CSV export of results */
052    protected String _exportCSVUrlPlugin;
053    /** The URL for doc export of results */
054    protected String _exportDOCUrl;
055    /** The plugin name for doc export of results */
056    protected String _exportDOCUrlPlugin;
057    /** The URL for XML export of results */
058    protected String _exportXMLUrl;
059    /** The plugin name for XML export of results */
060    protected String _exportXMLUrlPlugin;
061    /** The URL for PDF export of results */
062    protected String _exportPDFUrl;
063    /** The plugin name for PDF export of results */
064    protected String _exportPDFUrlPlugin;
065    
066    /** The URL for print results */    
067    protected String _printUrl;
068    /** The plugin name for print results */ 
069    protected String _printUrlPlugin;
070    
071    /** The name of the view to use for summary of the content */
072    protected String _summaryView;
073    
074    /**
075     * Default constructor.
076     */
077    public DefaultSearchUIModel()
078    {
079        super();
080        
081        _uiCriteria = new LinkedHashMap<>();
082        _uiFacetedCriteria = new LinkedHashMap<>();
083        _uiAdvancedCriteria = new LinkedHashMap<>();
084    }
085    
086    /**
087     * Constructor by copying an existing {@link SearchUIModel}.
088     * @param searchUIModelToCopy The {@link SearchUIModel} to copy
089     * @param contextualParameters The contextual parameters
090     */
091    public DefaultSearchUIModel(SearchUIModel searchUIModelToCopy, Map<String, Object> contextualParameters)
092    {
093        super(searchUIModelToCopy, contextualParameters);
094        
095        setAdvancedCriteria(new ArrayList<>(searchUIModelToCopy.getAdvancedCriteria(contextualParameters).values()));
096        
097        setPageSize(searchUIModelToCopy.getPageSize(contextualParameters));
098        
099        setSearchUrl(searchUIModelToCopy.getSearchUrl(contextualParameters));
100        setSearchUrlPlugin(searchUIModelToCopy.getSearchUrlPlugin(contextualParameters));
101        
102        setExportCSVUrl(searchUIModelToCopy.getExportCSVUrl(contextualParameters));
103        setExportCSVUrlPlugin(searchUIModelToCopy.getExportCSVUrlPlugin(contextualParameters));
104        setExportDOCUrl(searchUIModelToCopy.getExportDOCUrl(contextualParameters));
105        setExportDOCUrlPlugin(searchUIModelToCopy.getExportDOCUrlPlugin(contextualParameters));
106        setExportXMLUrl(searchUIModelToCopy.getExportXMLUrl(contextualParameters));
107        setExportXMLUrlPlugin(searchUIModelToCopy.getExportXMLUrlPlugin(contextualParameters));
108        setExportPDFUrl(searchUIModelToCopy.getExportPDFUrl(contextualParameters));
109        setExportPDFUrlPlugin(searchUIModelToCopy.getExportPDFUrlPlugin(contextualParameters));
110        
111        setPrintUrl(searchUIModelToCopy.getPrintUrl(contextualParameters));
112        setPrintUrlPlugin(searchUIModelToCopy.getPrintUrlPlugin(contextualParameters));
113    }
114
115    @Override
116    public Map<String, SearchUICriterion> getCriteria(Map<String, Object> contextualParameters)
117    {
118        return Collections.unmodifiableMap(_uiCriteria);
119    }
120    
121    @Override
122    public void addCriterion(SearchCriterion criterion)
123    {
124        super.addCriterion(criterion);
125        
126        if (criterion instanceof SearchUICriterion uiCriterion)
127        {
128            _uiCriteria.put(uiCriterion.getId(), uiCriterion);
129        }
130    }
131    
132    @Override
133    public void setCriteria(Collection< ? extends SearchCriterion> criteria)
134    {
135        super.setCriteria(criteria);
136        
137        _uiCriteria = new LinkedHashMap<>();
138        for (SearchCriterion criterion : criteria)
139        {
140            if (criterion instanceof SearchUICriterion uiCriterion)
141            {
142                _uiCriteria.put(uiCriterion.getId(), uiCriterion);
143            }
144        }
145    }
146    
147    @Override
148    public Map<String, SearchUICriterion> getFacetedCriteria(Map<String, Object> contextualParameters)
149    {
150        return Collections.unmodifiableMap(_uiFacetedCriteria);
151    }
152    
153    @Override
154    public void addFacetedCriterion(SearchCriterion criterion)
155    {
156        super.addFacetedCriterion(criterion);
157        
158        if (criterion instanceof SearchUICriterion uiCriterion)
159        {
160            _uiFacetedCriteria.put(uiCriterion.getId(), uiCriterion);
161        }
162    }
163    
164    @Override
165    public void setFacetedCriteria(Collection< ? extends SearchCriterion> criteria)
166    {
167        super.setFacetedCriteria(criteria);
168        
169        _uiFacetedCriteria = new LinkedHashMap<>();
170        for (SearchCriterion criterion : criteria)
171        {
172            if (criterion instanceof SearchUICriterion uiCriterion)
173            {
174                _uiFacetedCriteria.put(uiCriterion.getId(), uiCriterion);
175            }
176        }
177    }
178    
179    public Map<String, SearchUICriterion> getAdvancedCriteria(Map<String, Object> contextualParameters)
180    {
181        return Collections.unmodifiableMap(_uiAdvancedCriteria);
182    }
183    
184    /**
185     * Add an advanced criterion
186     * @param criterion the advanced criterion to add
187     */
188    public void addAdvancedCriterion(SearchUICriterion criterion)
189    {
190        _uiAdvancedCriteria.put(criterion.getId(), criterion);
191    }
192    
193    /**
194     * Set the advanced criteria
195     * @param criteria The list of advanced criterion
196     */
197    public void setAdvancedCriteria(Collection< ? extends SearchUICriterion> criteria)
198    {
199        _uiAdvancedCriteria = new LinkedHashMap<>();
200        for (SearchUICriterion criterion : criteria)
201        {
202            _uiAdvancedCriteria.put(criterion.getId(), criterion);
203        }
204    }
205
206    public int getPageSize(Map<String, Object> contextualParameters)
207    {
208        return _pageSize;
209    }
210    
211    /**
212     * Set the page size
213     * @param pageSize The page size to set. Set to negative or 0 for unlimited or default
214     */
215    public void setPageSize(int pageSize)
216    {
217        _pageSize = pageSize;
218    }
219
220    public String getSearchUrl(Map<String, Object> contextualParameters)
221    {
222        return _searchUrl;
223    }
224    
225    /**
226     * Set the URL for search
227     * @param searchUrl The URL to set
228     */
229    public void setSearchUrl(String searchUrl)
230    {
231        _searchUrl = searchUrl;
232    }
233
234    public String getSearchUrlPlugin(Map<String, Object> contextualParameters)
235    {
236        return _searchUrlPlugin;
237    }
238    
239    /**
240     * Set the plugin name for search
241     * @param searchUrlPlugin the plugin name to set
242     */
243    public void setSearchUrlPlugin(String searchUrlPlugin)
244    {
245        _searchUrlPlugin = searchUrlPlugin;
246    }
247
248    public String getExportCSVUrl(Map<String, Object> contextualParameters)
249    {
250        return _exportCSVUrl;
251    }
252    
253    /**
254     * Set the URL for CSV export of results
255     * @param exportCSVUrl The URL to set
256     */
257    public void setExportCSVUrl(String exportCSVUrl)
258    {
259        _exportCSVUrl = exportCSVUrl;
260    }
261
262    public String getExportCSVUrlPlugin(Map<String, Object> contextualParameters)
263    {
264        return _exportCSVUrlPlugin;
265    }
266    
267    /**
268     * Set the plugin name for CSV export of results
269     * @param exportCSVUrlPlugin the plugin name to set
270     */
271    public void setExportCSVUrlPlugin(String exportCSVUrlPlugin)
272    {
273        _exportCSVUrlPlugin = exportCSVUrlPlugin;
274    }
275
276    public String getExportDOCUrl(Map<String, Object> contextualParameters)
277    {
278        return _exportDOCUrl;
279    }
280
281    /**
282     * Set the URL for Doc export of results
283     * @param exportDOCUrl The URL to set
284     */
285    public void setExportDOCUrl(String exportDOCUrl)
286    {
287        _exportDOCUrl = exportDOCUrl;
288    }
289
290    public String getExportDOCUrlPlugin(Map<String, Object> contextualParameters)
291    {
292        return _exportDOCUrlPlugin;
293    }
294    
295    /**
296     * Set the plugin name for Doc export of results
297     * @param exportDOCUrlPlugin the plugin name to set
298     */
299    public void setExportDOCUrlPlugin(String exportDOCUrlPlugin)
300    {
301        _exportDOCUrlPlugin = exportDOCUrlPlugin;
302    }
303
304    public String getExportXMLUrl(Map<String, Object> contextualParameters)
305    {
306        return _exportXMLUrl;
307    }
308    
309    /**
310     * Set the URL for XML export of results
311     * @param exportXMLUrl The URL to set
312     */
313    public void setExportXMLUrl(String exportXMLUrl)
314    {
315        _exportXMLUrl = exportXMLUrl;
316    }
317
318    public String getExportXMLUrlPlugin(Map<String, Object> contextualParameters)
319    {
320        return _exportXMLUrlPlugin;
321    }
322    
323    /**
324     * Set the plugin name for XML export of results
325     * @param exportXMLUrlPlugin the plugin name to set
326     */
327    public void setExportXMLUrlPlugin(String exportXMLUrlPlugin)
328    {
329        _exportXMLUrlPlugin = exportXMLUrlPlugin;
330    }
331
332    public String getExportPDFUrl(Map<String, Object> contextualParameters)
333    {
334        return _exportPDFUrl;
335    }
336    
337    /**
338     * Set the URL for PDF export of results
339     * @param exportPDFUrl The URL to set
340     */
341    public void setExportPDFUrl(String exportPDFUrl)
342    {
343        _exportPDFUrl = exportPDFUrl;
344    }
345
346    public String getExportPDFUrlPlugin(Map<String, Object> contextualParameters)
347    {
348        return _exportPDFUrlPlugin;
349    }
350    
351    /**
352     * Set the plugin name for PDF export of results
353     * @param exportPDFUrlPlugin the plugin name to set
354     */
355    public void setExportPDFUrlPlugin(String exportPDFUrlPlugin)
356    {
357        _exportPDFUrlPlugin = exportPDFUrlPlugin;
358    }
359
360    public String getPrintUrl(Map<String, Object> contextualParameters)
361    {
362        return _printUrl;
363    }
364    
365    /**
366     * Set the URL for print results
367     * @param printUrl The URL to set
368     */
369    public void setPrintUrl(String printUrl)
370    {
371        _printUrl = printUrl;
372    }
373
374    public String getPrintUrlPlugin(Map<String, Object> contextualParameters)
375    {
376        return _printUrlPlugin;
377    }
378    
379    /**
380     * Set the plugin name for print results
381     * @param printUrlPlugin the plugin name to set
382     */
383    public void setPrintUrlPlugin(String printUrlPlugin)
384    {
385        _printUrlPlugin = printUrlPlugin;
386    }
387
388    public String getSummaryView()
389    {
390        return _summaryView;
391    }
392
393    /**
394     * Set The name of the view to use for summary of the content
395     * @param summaryView the name to set
396     */
397    public void setSummaryView(String summaryView)
398    {
399        _summaryView = summaryView;
400    }
401}