View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.business.portlet.DocumentPortletHome;
39  import fr.paris.lutece.plugins.document.service.docsearch.DocSearchItem;
40  import fr.paris.lutece.plugins.document.service.docsearch.DocSearchService;
41  import fr.paris.lutece.plugins.document.service.publishing.PublishingService;
42  import fr.paris.lutece.plugins.document.utils.IntegerUtils;
43  import fr.paris.lutece.portal.business.page.Page;
44  import fr.paris.lutece.portal.business.page.PageHome;
45  import fr.paris.lutece.portal.business.portlet.Portlet;
46  import fr.paris.lutece.portal.service.i18n.I18nService;
47  import fr.paris.lutece.portal.service.plugin.Plugin;
48  import fr.paris.lutece.portal.service.security.SecurityService;
49  import fr.paris.lutece.portal.service.template.AppTemplateService;
50  import fr.paris.lutece.portal.web.xpages.XPage;
51  import fr.paris.lutece.portal.web.xpages.XPageApplication;
52  import fr.paris.lutece.util.ReferenceList;
53  import fr.paris.lutece.util.html.HtmlTemplate;
54  
55  import java.text.ParseException;
56  import java.text.SimpleDateFormat;
57  
58  import java.util.ArrayList;
59  import java.util.Collection;
60  import java.util.HashMap;
61  import java.util.Iterator;
62  import java.util.List;
63  
64  import javax.servlet.http.HttpServletRequest;
65  
66  
67  /**
68   * AdvancedSearch
69   */
70  public class AdvancedSearch implements XPageApplication
71  {
72      private static final String TEMPLATE_ADVANCED_SEARCH = "skin/plugins/document/advanced_search.html";
73      private static final String MARK_LOCALE = "locale";
74      private static final String MARK_URL = "url";
75      private static final String MARK_SELECTED_TYPE = "selected_type";
76      private static final String MARK_TITLE = "title";
77      private static final String MARK_SUMMARY = "summary";
78      private static final String MARK_QUERY = "query";
79      private static final String MARK_DATE_QUERY = "date_query";
80      private static final String MARK_CONTENT = "content";
81      private static final String MARK_TYPE_LIST = "document_type_list";
82      private static final String MARK_DOC_SEARCH_ITEM = "docSearchItem";
83      private static final String MARK_ID_PORTLET = "idPortlet";
84      private static final String MARK_DATE_ERROR = "date_error";
85      private static final String PARAMETER_QUERY = "query";
86      private static final String PARAMETER_SEARCH_QUERY = "search_query";
87      private static final String PARAMETER_TITLE = "title";
88      private static final String PARAMETER_SUMMARY = "summary";
89      private static final String PARAMETER_DATE = "date";
90      private static final String PARAMETER_TYPE = "document_type";
91      private static final String MESSAGE_TITLE = "document.advanced_search.title";
92      private static final String MESSAGE_DATE_ERROR = "document.advanced_search.date_error";
93  
94      /**
95       * Return the form for advanced search with results
96       * @param request the request
97       * @param plugin the plugin
98       * @return the xpage
99       */
100     public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin )
101     {
102         DocSearchService dss = DocSearchService.getInstance(  );
103         XPage page = new XPage(  );
104         HashMap<Object, Object> model = new HashMap<Object, Object>(  );
105 
106         String url = request.getRequestURL(  ).toString(  );
107         model.put( MARK_URL, url );
108 
109         if ( request.getParameter( PARAMETER_SEARCH_QUERY ) != null )
110         {
111             String strSearchQuery = request.getParameter( PARAMETER_QUERY );
112 
113             boolean bTitle = false;
114             boolean bSummary = false;
115 
116             if ( request.getParameter( PARAMETER_TITLE ) != null )
117             {
118                 model.put( MARK_TITLE, true );
119                 bTitle = true;
120             }
121 
122             if ( request.getParameter( PARAMETER_SUMMARY ) != null )
123             {
124                 model.put( MARK_SUMMARY, true );
125                 bSummary = true;
126             }
127 
128             String strDate = request.getParameter( PARAMETER_DATE );
129 
130             if ( ( strDate != null ) && !( strDate.equals( "" ) ) && !( isValidDate( strDate ) ) )
131             {
132                 model.put( MARK_DATE_ERROR, I18nService.getLocalizedString( MESSAGE_DATE_ERROR, request.getLocale(  ) ) );
133             }
134 
135             String strDocumentType = request.getParameter( PARAMETER_TYPE );
136             DocumentType docType = null;
137 
138             if ( strDocumentType != null )
139             {
140                 docType = DocumentTypeHome.findByPrimaryKey( strDocumentType );
141                 model.put( MARK_SELECTED_TYPE, strDocumentType );
142             }
143 
144             List<DocSearchItem> result = dss.getSearchResults( strSearchQuery, bTitle, bSummary, strDate, docType );
145             List<HashMap<String, Object>> finalResult = new ArrayList<HashMap<String, Object>>(  );
146             PublishingService service = PublishingService.getInstance(  );
147 
148             Iterator<DocSearchItem> i = result.iterator(  );
149 
150             while ( i.hasNext(  ) )
151             {
152                 DocSearchItem docSearchItem = i.next(  );
153 
154                 if ( service.isPublished( IntegerUtils.convert( docSearchItem.getId(  ) ) ) )
155                 {
156                     Collection<Portlet> c = service.getPortletsByDocumentId( docSearchItem.getId(  ) );
157                     Iterator<Portlet> it = c.iterator(  );
158                     int idPortlet = -1;
159 
160                     if ( it.hasNext(  ) )
161                     {
162                         idPortlet = it.next(  ).getId(  );
163                     }
164 
165                     Page pagePortlet = null;
166 
167                     if ( idPortlet != -1 )
168                     {
169                         Portlet portlet = DocumentPortletHome.findByPrimaryKey( idPortlet );
170                         pagePortlet = PageHome.getPage( portlet.getPageId(  ) );
171                     }
172 
173                     if ( ( pagePortlet == null ) || ( pagePortlet.getRole(  ).equals( Page.ROLE_NONE ) ) ||
174                             ( ( SecurityService.isAuthenticationEnable(  ) ) &&
175                             ( SecurityService.getInstance(  ).isUserInRole( request, pagePortlet.getRole(  ) ) ) ) )
176                     {
177                         HashMap<String, Object> currentHashMap = new HashMap<String, Object>(  );
178                         currentHashMap.put( MARK_DOC_SEARCH_ITEM, docSearchItem );
179                         currentHashMap.put( MARK_ID_PORTLET, idPortlet );
180                         finalResult.add( currentHashMap );
181                     }
182                 }
183             }
184 
185             //replace all quotes by html code
186             strSearchQuery = strSearchQuery.replaceAll( "\"", "&quot;" );
187 
188             model.put( MARK_QUERY, strSearchQuery );
189             model.put( MARK_DATE_QUERY, strDate );
190 
191             if ( finalResult.size(  ) > 0 )
192             {
193                 model.put( MARK_CONTENT, finalResult );
194             }
195         }
196         else
197         {
198             model.put( MARK_SELECTED_TYPE, "-1" );
199         }
200 
201         model.put( MARK_TYPE_LIST, getRefListDocumentType(  ) );
202         model.put( MARK_LOCALE, request.getLocale(  ) );
203 
204         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADVANCED_SEARCH, request.getLocale(  ), model );
205 
206         page.setContent( template.getHtml(  ) );
207         page.setTitle( I18nService.getLocalizedString( MESSAGE_TITLE, request.getLocale(  ) ) );
208         page.setPathLabel( I18nService.getLocalizedString( MESSAGE_TITLE, request.getLocale(  ) ) );
209 
210         return page;
211     }
212 
213     /**
214      * Check if the date is well formed.
215      * @param strDate
216      * @return true if the date is valid else false
217      */
218     private boolean isValidDate( String strDate )
219     {
220         SimpleDateFormat f = new SimpleDateFormat( "dd/MM/yy" );
221         f.setLenient( false );
222 
223         try
224         {
225             f.parse( strDate );
226 
227             return true;
228         }
229         catch ( ParseException e )
230         {
231             return false;
232         }
233     }
234 
235     /**
236      * The document referencelist
237      * @return The document referencelist
238      */
239     public static ReferenceList getRefListDocumentType(  )
240     {
241         Collection<DocumentType> listDocumentType = DocumentTypeHome.findAll(  );
242         ReferenceList reflistDocumentType = new ReferenceList(  );
243 
244         reflistDocumentType.addItem( -1, " " );
245 
246         for ( DocumentType documentType : listDocumentType )
247         {
248             reflistDocumentType.addItem( documentType.getCode(  ), documentType.getName(  ) );
249         }
250 
251         return reflistDocumentType;
252     }
253 }