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