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