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