View Javadoc
1   /*
2    * Copyright (c) 2002-2023, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.document.web.docsearch;
35  
36  import fr.paris.lutece.plugins.document.business.DocumentType;
37  import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
38  import fr.paris.lutece.plugins.document.service.docsearch.DocSearchItem;
39  import fr.paris.lutece.plugins.document.service.docsearch.DocSearchService;
40  import fr.paris.lutece.plugins.document.utils.IntegerUtils;
41  import fr.paris.lutece.portal.service.i18n.I18nService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
45  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  import fr.paris.lutece.util.string.StringUtil;
48  import fr.paris.lutece.util.url.UrlItem;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  import java.util.HashMap;
53  import java.util.List;
54  import java.util.Locale;
55  import java.util.Map;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  
60  /**
61   * DocSearchJspBean
62   */
63  public class DocSearchJspBean extends PluginAdminPageJspBean
64  {
65      /**
66       * Generated serial version UID
67       */
68      private static final long serialVersionUID = 1299568272034875394L;
69  
70      ////////////////////////////////////////////////////////////////////////////
71      // Constants
72      private static final String TEMPLATE_RESULTS = "admin/plugins/document/docsearch/search_results.html";
73      private static final String PROPERTY_SEARCH_PAGE_URL = "document.docsearch.pageSearch.baseUrl";
74      private static final String PROPERTY_RESULTS_PER_PAGE = "document.docsearch.nb.docs.per.page";
75      private static final String MESSAGE_INVALID_SEARCH_TERMS = "document.message.invalidSearchTerms";
76      private static final String PROPERTY_PAGE_TITLE_SEARCH = "document.search_results.pageTitle";
77      private static final String DEFAULT_RESULTS_PER_PAGE = "10";
78      private static final String DEFAULT_PAGE_INDEX = "1";
79      private static final String PARAMETER_PAGE_INDEX = "page_index";
80      private static final String PARAMETER_NB_ITEMS_PER_PAGE = "items_per_page";
81      private static final String PARAMETER_QUERY = "query";
82      private static final String PARAMETER_ADVANCED_SEARCH = "advanced_search";
83      private static final String MARK_RESULTS_LIST = "results_list";
84      private static final String MARK_QUERY = "query";
85      private static final String MARK_PAGINATOR = "paginator";
86      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
87      private static final String MARK_ERROR = "error";
88      private static final String MARK_SELECTED_TYPE = "selected_type";
89      private static final String MARK_TITLE = "title";
90      private static final String MARK_SUMMARY = "summary";
91      private static final String MARK_TYPE_LIST = "document_type_list";
92      private static final String MARK_DATE_QUERY = "date_query";
93      private static final String MARK_LOCALE = "locale";
94      private static final String PARAMETER_TITLE = "title";
95      private static final String PARAMETER_SUMMARY = "summary";
96      private static final String PARAMETER_DATE = "date";
97      private static final String PARAMETER_TYPE = "document_type";
98  
99      /**
100      * Returns search results
101      *
102      * @param request The HTTP request.
103      * @return The HTML code of the page.
104      */
105     public String getSearch( HttpServletRequest request )
106     {
107         setPageTitleProperty( PROPERTY_PAGE_TITLE_SEARCH );
108 
109         String strQuery = request.getParameter( PARAMETER_QUERY );
110         String strSearchPageUrl = AppPropertiesService.getProperty( PROPERTY_SEARCH_PAGE_URL );
111         String strError = StringUtils.EMPTY;
112         Map<String, Object> model = new HashMap<String, Object>(  );
113         Locale locale = getLocale(  );
114 
115         // Check XSS characters
116         if ( ( strQuery != null ) && ( StringUtil.containsXssCharacters( strQuery ) ) )
117         {
118             strError = I18nService.getLocalizedString( MESSAGE_INVALID_SEARCH_TERMS, locale );
119             strQuery = StringUtils.EMPTY;
120         }
121 
122         String strNbItemPerPage = request.getParameter( PARAMETER_NB_ITEMS_PER_PAGE );
123         String strDefaultNbItemPerPage = AppPropertiesService.getProperty( PROPERTY_RESULTS_PER_PAGE,
124                 DEFAULT_RESULTS_PER_PAGE );
125         strNbItemPerPage = ( strNbItemPerPage != null ) ? strNbItemPerPage : strDefaultNbItemPerPage;
126 
127         int nNbItemsPerPage = IntegerUtils.convert( strNbItemPerPage );
128         String strCurrentPageIndex = request.getParameter( PARAMETER_PAGE_INDEX );
129         strCurrentPageIndex = ( strCurrentPageIndex != null ) ? strCurrentPageIndex : DEFAULT_PAGE_INDEX;
130 
131         int nPageIndex = IntegerUtils.convert( strCurrentPageIndex );
132         int nStartIndex = ( nPageIndex - 1 ) * nNbItemsPerPage;
133         List<DocSearchItem> listResults;
134 
135         UrlItem url = new UrlItem( strSearchPageUrl );
136 
137         if ( request.getParameter( PARAMETER_ADVANCED_SEARCH ) != null )
138         {
139             boolean bTitle = false;
140             boolean bSummary = false;
141 
142             model.put( "advanced_search", true );
143 
144             if ( request.getParameter( PARAMETER_TITLE ) != null )
145             {
146                 model.put( MARK_TITLE, true );
147                 bTitle = true;
148                 url.addParameter( PARAMETER_TITLE, "true" );
149             }
150 
151             if ( request.getParameter( PARAMETER_SUMMARY ) != null )
152             {
153                 model.put( MARK_SUMMARY, true );
154                 bSummary = true;
155                 url.addParameter( PARAMETER_SUMMARY, "true" );
156             }
157 
158             String strDate = request.getParameter( PARAMETER_DATE );
159             String strDocumentType = request.getParameter( PARAMETER_TYPE );
160             DocumentType docType = null;
161 
162             if ( strDocumentType != null )
163             {
164                 docType = DocumentTypeHome.findByPrimaryKey( strDocumentType );
165                 model.put( MARK_SELECTED_TYPE, strDocumentType );
166                 url.addParameter( PARAMETER_TYPE, strDocumentType );
167             }
168             else
169             {
170                 model.put( MARK_SELECTED_TYPE, "-1" );
171             }
172 
173             model.put( MARK_DATE_QUERY, strDate );
174 
175             if ( strDate != null )
176             {
177                 url.addParameter( PARAMETER_DATE, strDate );
178             }
179 
180             model.put( MARK_TYPE_LIST, AdvancedSearch.getRefListDocumentType(  ) );
181 
182             listResults = DocSearchService.getInstance(  ).getSearchResults( strQuery, bTitle, bSummary, strDate,
183                     docType );
184         }
185         else
186         {
187             listResults = DocSearchService.getInstance(  ).getSearchResults( strQuery, nStartIndex, getUser(  ) );
188         }
189 
190         url.addParameter( PARAMETER_QUERY, strQuery );
191         url.addParameter( PARAMETER_NB_ITEMS_PER_PAGE, nNbItemsPerPage );
192 
193         if ( request.getParameter( PARAMETER_ADVANCED_SEARCH ) != null )
194         {
195             url.addParameter( PARAMETER_ADVANCED_SEARCH, "true" );
196         }
197 
198         LocalizedPaginator<DocSearchItem> paginator = new LocalizedPaginator<DocSearchItem>( listResults,
199                 nNbItemsPerPage, url.getUrl(  ), PARAMETER_PAGE_INDEX, strCurrentPageIndex, getLocale(  ) );
200 
201         model.put( MARK_RESULTS_LIST, paginator.getPageItems(  ) );
202         model.put( MARK_QUERY, strQuery );
203         model.put( MARK_PAGINATOR, paginator );
204         model.put( MARK_NB_ITEMS_PER_PAGE, strNbItemPerPage );
205         model.put( MARK_ERROR, strError );
206         model.put( MARK_LOCALE, request.getLocale(  ) );
207 
208         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_RESULTS, locale, model );
209 
210         return getAdminPage( template.getHtml(  ) );
211     }
212 }