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.requesttime;
017
018import java.util.Optional;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.cocoon.environment.Request;
022import org.apache.cocoon.environment.Response;
023import org.slf4j.Logger;
024import org.xml.sax.ContentHandler;
025
026import org.ametys.cms.search.SearchResults;
027import org.ametys.cms.search.solr.SearcherFactory.Searcher;
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.web.frontoffice.search.SearchService;
030import org.ametys.web.frontoffice.search.instance.SearchServiceInstance;
031import org.ametys.web.frontoffice.search.requesttime.SearchServiceDebugModeHelper.DebugMode;
032import org.ametys.web.frontoffice.search.requesttime.input.SearchUserInputs;
033import org.ametys.web.repository.page.Page;
034import org.ametys.web.repository.site.Site;
035
036/**
037 * Wrapper of arguments passed to {@link SearchComponent#execute(SearchComponentArguments) SearchComponent#execute}
038 */
039public class SearchComponentArguments
040{
041    private ContentHandler _contentHandler;
042    private Parameters _generatorParameters;
043    private SearchServiceInstance _serviceInstance;
044    private SearchService _service;
045    private SearchUserInputs _userInputs;
046    private Request _request;
047    private Response _response;
048    private int _resultPageIndex;
049    private int _start;
050    private int _resultsPerPage;
051    private Site _currentSite;
052    private Page _currentPage;
053    private String _currentLang;
054    private boolean _launchSearch;
055    private SearchResults<AmetysObject> _results;
056    private Searcher _searcher;
057    private Logger _logger;
058    private DebugMode _debugMode;
059    
060    SearchComponentArguments(ContentHandler contentHandler,
061            Parameters generatorParameters,
062            SearchServiceInstance serviceInstance,
063            SearchService service,
064            SearchUserInputs userInputs,
065            Request request,
066            Response response,
067            int resultPageIndex,
068            int start,
069            int resultsPerPage,
070            Site currentSite,
071            Page currentPage,
072            String currentLang,
073            boolean launchSearch,
074            Searcher searcher,
075            Logger logger,
076            DebugMode debugMode)
077    {
078        _contentHandler = contentHandler;
079        _generatorParameters = generatorParameters;
080        _serviceInstance = serviceInstance;
081        _service = service;
082        _userInputs = userInputs;
083        _request = request;
084        _response = response;
085        _resultPageIndex = resultPageIndex;
086        _start = start;
087        _resultsPerPage = resultsPerPage;
088        _currentSite = currentSite;
089        _currentPage = currentPage;
090        _currentLang = currentLang;
091        _launchSearch = launchSearch;
092        _searcher = searcher;
093        _logger = logger;
094        _debugMode = debugMode;
095    }
096    
097    /**
098     * Gets the content handler
099     * @return the content handler
100     */
101    public ContentHandler contentHandler()
102    {
103        return _contentHandler;
104    }
105    
106    /**
107     * Gets the parameters of the generator, allowing to change some behaviors depending on the URL the request comes from
108     * @return the parameters of the generator
109     */
110    public Parameters generatorParameters()
111    {
112        return _generatorParameters;
113    }
114    
115    /**
116     * Gets the search service instance
117     * @return the search service instance
118     */
119    public SearchServiceInstance serviceInstance()
120    {
121        return _serviceInstance;
122    }
123    
124    /**
125     * Gets the search service
126     * @return the search service
127     */
128    public SearchService service()
129    {
130        return _service;
131    }
132    
133    /**
134     * Gets the user inputs
135     * @return the user inputs
136     */
137    public SearchUserInputs userInputs()
138    {
139        return _userInputs;
140    }
141    
142    /**
143     * Gets the current request
144     * @return the current request
145     */
146    public Request request()
147    {
148        return _request;
149    }
150    
151    /**
152     * Gets the response
153     * @return the response
154     */
155    public Response response()
156    {
157        return _response;
158    }
159    
160    /**
161     * Gets the result page index
162     * @return the result page index
163     */
164    public int resultPageIndex()
165    {
166        return _resultPageIndex;
167    }
168    
169    /**
170     * Gets the start index (i.e. offset) of the search
171     * @return the start index
172     */
173    public int start()
174    {
175        return _start;
176    }
177    
178    /**
179     * Gets the number of results per page
180     * @return the number of results per page
181     */
182    public int resultsPerPage()
183    {
184        return _resultsPerPage;
185    }
186    
187    /**
188     * Gets the current site
189     * @return the current site
190     */
191    public Site currentSite()
192    {
193        return _currentSite;
194    }
195    
196    /**
197     * Gets the current page
198     * @return the current page
199     */
200    public Page currentPage()
201    {
202        return _currentPage;
203    }
204    
205    /**
206     * Gets the current lang
207     * @return the current lang
208     */
209    public String currentLang()
210    {
211        return _currentLang;
212    }
213    
214    /**
215     * Returns <code>true</code> if the search has to be launch
216     * @return <code>true</code> if the search has to be launch
217     */
218    public boolean launchSearch()
219    {
220        return _launchSearch;
221    }
222    
223    /**
224     * Sets the results
225     * @param results the results
226     */
227    public void setResults(SearchResults<AmetysObject> results)
228    {
229        _results = results;
230    }
231    
232    /**
233     * Gets the results of the search. Can be empty if search has not been executed yet
234     * @return the results of the search, or an empty optional if search has not been executed yet
235     */
236    public Optional<SearchResults<AmetysObject>> results()
237    {
238        return Optional.ofNullable(_results);
239    }
240    
241    /**
242     * Gets the searcher
243     * @return the searcher
244     */
245    public Searcher searcher()
246    {
247        return _searcher;
248    }
249    
250    /**
251     * Gets the logger
252     * @return the logger
253     */
254    public Logger logger()
255    {
256        return _logger;
257    }
258    
259    /**
260     * Returns <code>true</code> if debug is activated
261     * @return <code>true</code> if debug is activated
262     */
263    public boolean isDebug()
264    {
265        return _debugMode != null;
266    }
267    
268    /**
269     * Gets the debug mode
270     * @return the debug mode
271     */
272    public Optional<DebugMode> debugMode()
273    {
274        return Optional.ofNullable(_debugMode);
275    }
276}