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.instance;
017
018import java.util.Collection;
019import java.util.List;
020import java.util.Optional;
021
022import org.apache.commons.lang3.tuple.Pair;
023
024import org.ametys.cms.search.Sort;
025import org.ametys.cms.search.Sort.Order;
026import org.ametys.cms.search.advanced.AbstractTreeNode;
027import org.ametys.web.frontoffice.search.SearchService;
028import org.ametys.web.frontoffice.search.instance.model.FOSearchCriterion;
029import org.ametys.web.frontoffice.search.instance.model.Link;
030import org.ametys.web.frontoffice.search.instance.model.ResultDisplay;
031import org.ametys.web.frontoffice.search.instance.model.RightCheckingMode;
032import org.ametys.web.frontoffice.search.instance.model.SearchContext;
033import org.ametys.web.frontoffice.search.metamodel.AdditionalParameterValueMap;
034import org.ametys.web.frontoffice.search.metamodel.AdditionalSearchServiceParameter;
035import org.ametys.web.frontoffice.search.metamodel.FacetDefinition;
036import org.ametys.web.frontoffice.search.metamodel.Returnable;
037import org.ametys.web.frontoffice.search.metamodel.Searchable;
038import org.ametys.web.frontoffice.search.metamodel.SortDefinition;
039import org.ametys.web.repository.page.ZoneItem;
040
041/**
042 * A wrapper class representing an instance of {@link SearchService}.
043 * <br>It can be seen as a {@link ZoneItem} as well as a FO SearchModel created on the fly by the webmaster.
044 */
045public class SearchServiceInstance
046{
047    private String _id;
048    private String _title;
049    private List<Returnable> _returnables;
050    private Collection<Searchable> _searchables;
051    private Collection<AdditionalSearchServiceParameter> _additionalParameters;
052    private AdditionalParameterValueMap _additionalParameterValues;
053    private Collection<SearchContext> _contexts;
054    private AbstractTreeNode<FOSearchCriterion> _criterionTree;
055    private boolean _computeCriteriaCounts;
056    private Collection<FacetDefinition> _facets;
057    private List<Pair<SortDefinition, Order>> _initialSorts;
058    private Collection<SortDefinition> _proposedSorts;
059    private Integer _resultsPerPage;
060    private Integer _maxResults;
061    private RightCheckingMode _rightCheckingMode;
062    private String _xslt;
063    private ResultDisplay _resultDisplay;
064    private Link _link;
065    private boolean _handleRss;
066    private boolean _saveUserPrefs;
067
068    /**
069     * Creates an instance of search service
070     * @param id the id
071     * @param title the title
072     * @param returnables the returnables
073     * @param searchables the searchables
074     * @param additionalParameters the additional parameters
075     * @param additionalParameterValues the additional parameter values
076     * @param contexts the search contexts
077     * @param criterionTree the tree of criteria. Can be null if there is no criterion.
078     * @param computeCriteriaCounts <code>true</code> if the counts of enumerated criteria must be computed
079     * @param facets the facets
080     * @param initialSorts the initial sorts
081     * @param proposedSorts the proposed sorts
082     * @param resultsPerPage the number of results per page. Can be null
083     * @param maxResults the maximum number of results. Can be null
084     * @param rightCheckingMode the right checking mode
085     * @param xslt the XSLT
086     * @param resultDisplay the result display object
087     * @param link the link object
088     * @param handleRss <code>true</code> if it handles RSS
089     * @param saveUserPrefs <code>true</code> if it saves the search parameters in the user preferences
090     */
091    SearchServiceInstance(String id, 
092            String title, 
093            List<Returnable> returnables, 
094            Collection<Searchable> searchables,
095            Collection<AdditionalSearchServiceParameter> additionalParameters,
096            AdditionalParameterValueMap additionalParameterValues,
097            Collection<SearchContext> contexts,
098            AbstractTreeNode<FOSearchCriterion> criterionTree,
099            boolean computeCriteriaCounts,
100            Collection<FacetDefinition> facets,
101            List<Pair<SortDefinition, Sort.Order>> initialSorts,
102            Collection<SortDefinition> proposedSorts,
103            Integer resultsPerPage,
104            Integer maxResults,
105            RightCheckingMode rightCheckingMode,
106            String xslt,
107            ResultDisplay resultDisplay,
108            Link link,
109            boolean handleRss,
110            boolean saveUserPrefs)
111    {
112        _id = id;
113        _title = title;
114        _returnables = returnables;
115        _searchables = searchables;
116        _additionalParameterValues = additionalParameterValues;
117        _additionalParameters = additionalParameters;
118        _contexts = contexts;
119        _criterionTree = criterionTree;
120        _computeCriteriaCounts = computeCriteriaCounts;
121        _facets = facets;
122        _initialSorts = initialSorts;
123        _proposedSorts = proposedSorts;
124        _resultsPerPage = resultsPerPage;
125        _maxResults = maxResults;
126        _rightCheckingMode = rightCheckingMode;
127        _xslt = xslt;
128        _resultDisplay = resultDisplay;
129        _link = link;
130        _handleRss = handleRss;
131        _saveUserPrefs = saveUserPrefs;
132    }
133    
134    /**
135     * Gets the id
136     * @return the id
137     */
138    public String getId()
139    {
140        return _id;
141    }
142    
143    /**
144     * Gets the title
145     * @return the title
146     */
147    public String getTitle()
148    {
149        return _title;
150    }
151    
152    /**
153     * Gets the {@link Returnable}s
154     * @return the {@link Returnable}s
155     */
156    public List<Returnable> getReturnables()
157    {
158        return _returnables;
159    }
160    
161    /**
162     * Gets the {@link Searchable}s
163     * @return the {@link Searchable}s
164     */
165    public Collection<Searchable> getSearchables()
166    {
167        return _searchables;
168    }
169    
170    /**
171     * Gets the additional parameters
172     * @return the additional parameters
173     */
174    public Collection<AdditionalSearchServiceParameter> getAdditionalParameters()
175    {
176        return _additionalParameters;
177    }
178    
179    /**
180     * Gets the additional parameter values
181     * @return the additional parameter values
182     */
183    public AdditionalParameterValueMap getAdditionalParameterValues()
184    {
185        return _additionalParameterValues;
186    }
187    
188    /**
189     * Gets the {@link SearchContext}s
190     * @return the {@link SearchContext}s
191     */
192    public Collection<SearchContext> getContexts()
193    {
194        return _contexts;
195    }
196    
197    /**
198     * Gets the {@link AbstractTreeNode tree} of {@link FOSearchCriterion FOSearchCriteria}
199     * @return the {@link AbstractTreeNode tree} of {@link FOSearchCriterion FOSearchCriteria}
200     */
201    public Optional<AbstractTreeNode<FOSearchCriterion>> getCriterionTree()
202    {
203        return Optional.ofNullable(_criterionTree);
204    }
205    
206    /**
207     * Returns <code>true</code> if the counts of enumerated criteria must be computed
208     * @return <code>true</code> if the counts of enumerated criteria must be computed
209     */
210    public boolean computeCriteriaCounts()
211    {
212        return _computeCriteriaCounts;
213    }
214    
215    /**
216     * Gets the {@link FacetDefinition}s
217     * @return the {@link FacetDefinition}s
218     */
219    public Collection<FacetDefinition> getFacets()
220    {
221        return _facets;
222    }
223    
224    /**
225     * Gets the initial {@link SortDefinition}s with their sort order
226     * @return the initial {@link SortDefinition}s with their sort order
227     */
228    public List<Pair<SortDefinition, Sort.Order>> getInitialSorts()
229    {
230        return _initialSorts;
231    }
232    
233    /**
234     * Gets the proposed {@link SortDefinition}s
235     * @return the proposed {@link SortDefinition}s
236     */
237    public Collection<SortDefinition> getProposedSorts()
238    {
239        return _proposedSorts;
240    }
241    
242    /**
243     * Gets the number of results per page
244     * @return the number of results per page
245     */
246    public Optional<Integer> resultsPerPage()
247    {
248        return Optional.ofNullable(_resultsPerPage);
249    }
250    
251    /**
252     * Gets the maximum number of results
253     * @return the maximum number of results
254     */
255    public Optional<Integer> maxResults()
256    {
257        return Optional.ofNullable(_maxResults);
258    }
259    
260    /**
261     * Gets the {@link RightCheckingMode}
262     * @return the {@link RightCheckingMode}
263     */
264    public RightCheckingMode getRightCheckingMode()
265    {
266        return _rightCheckingMode;
267    }
268    
269    /**
270     * Gets the XSLT
271     * @return the XSLT
272     */
273    public String getXslt()
274    {
275        return _xslt;
276    }
277    
278    /**
279     * Gets the result display
280     * @return the result display
281     */
282    public ResultDisplay getResultDisplay()
283    {
284        return _resultDisplay;
285    }
286    
287    /**
288     * Gets the link
289     * @return the link
290     */
291    public Link getLink()
292    {
293        return _link;
294    }
295    
296    /**
297     * Returns <code>true</code> if it handles RSS
298     * @return <code>true</code> if it handles RSS
299     */
300    public boolean handleRss() 
301    {
302        return _handleRss;
303    }
304    
305    /**
306     * Returns <code>true</code> if it saves the search parameters in the user preferences
307     * @return <code>true</code> if it saves the search parameters in the user preferences
308     */
309    public boolean saveUserPrefs()
310    {
311        return _saveUserPrefs;
312    }
313    
314    @Override
315    public String toString()
316    {
317        return "SearchServiceInstance(id: " + _id + ")";
318    }
319}