1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
62
63 public class DocSearchJspBean extends PluginAdminPageJspBean
64 {
65
66
67
68 private static final long serialVersionUID = 1299568272034875394L;
69
70
71
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
101
102
103
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
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 }