View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.htmlpage.web;
35  
36  import fr.paris.lutece.plugins.htmlpage.business.HtmlPage;
37  import fr.paris.lutece.plugins.htmlpage.service.HtmlPageService;
38  import fr.paris.lutece.plugins.htmlpage.utils.HtmlPageUtil;
39  import fr.paris.lutece.portal.service.message.SiteMessage;
40  import fr.paris.lutece.portal.service.message.SiteMessageException;
41  import fr.paris.lutece.portal.service.message.SiteMessageService;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.portal.service.plugin.PluginService;
44  import fr.paris.lutece.portal.service.search.SearchEngine;
45  import fr.paris.lutece.portal.service.search.SearchResult;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.portal.service.template.AppTemplateService;
48  import fr.paris.lutece.portal.service.util.AppPropertiesService;
49  import fr.paris.lutece.portal.web.xpages.XPage;
50  import fr.paris.lutece.portal.web.xpages.XPageApplication;
51  import fr.paris.lutece.util.html.HtmlTemplate;
52  
53  import java.util.ArrayList;
54  import java.util.Collection;
55  import java.util.HashMap;
56  import java.util.List;
57  
58  import javax.servlet.http.HttpServletRequest;
59  
60  import org.apache.commons.lang3.StringUtils;
61  
62  /**
63   * This class manages HtmlPage page.
64   */
65  public class HtmlPageApp implements XPageApplication
66  {
67      //////////////////////////////////////////////////////////////////////////////////////////////
68      // Constants
69  
70      /** Serial id */
71      private static final long serialVersionUID = 4891750061103627788L;
72  
73      // Parameters
74      private static final String PARAMETER_PAGE = "page";
75      private static final String PARAMETER_HTMLPAGE_ID = "htmlpage_id";
76      private static final String PARAMETER_QUERY = "query";
77  
78      // Properties
79      private static final String PROPERTY_PAGE_TITLE = "htmlpage.pageTitle";
80      private static final String PROPERTY_PAGE_PATH = "htmlpage.pagePathLabel";
81  
82      // Messages
83      private static final String PROPERTY_MESSAGE_ERROR_HTMLPAGE = "htmlpage.message.pageInvalid";
84      private static final String PROPERTY_MESSAGE_NOT_AUTHORIZED = "htmlpage.message.notAuthorized";
85  
86      // Markers
87      private static final String MARK_HTMLPAGE_LIST = "htmlpages_list";
88      private static final String MARK_HTMLPAGE = "htmlpage";
89      private static final String MARK_PAGE = "page";
90      private static final String MARK_RESULT = "result";
91      private static final String MARK_QUERY = "query";
92  
93      // Templates
94      private static final String TEMPLATE_XPAGE_HTMLPAGE = "skin/plugins/htmlpage/page_htmlpage.html";
95      private static final String TEMPLATE_XPAGE_HTMLPAGE_LISTS = "skin/plugins/htmlpage/htmlpages_list.html";
96      // Bean
97      private static final String BEAN_SEARCH_ENGINE = "htmlpage.htmlpageSearchEngine";
98  
99      // private fields
100     private Plugin _plugin;
101 
102     /** Creates a new instance of HtmlPageApp */
103     public HtmlPageApp( )
104     {
105     }
106 
107     /**
108      * Returns the content of the page HtmlPage. It is composed by a form which to capture the data to send a message to a contact of the portal.
109      * 
110      * @return the Content of the page Contact
111      * @param request
112      *            The http request
113      * @param nMode
114      *            The current mode
115      * @param plugin
116      *            The plugin object
117      * @throws fr.paris.lutece.portal.service.message.SiteMessageException
118      *             Message displayed if an exception occures
119      */
120     public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin ) throws SiteMessageException
121     {
122         XPage page = new XPage( );
123 
124         String strPluginName = request.getParameter( PARAMETER_PAGE );
125         String strQuery = request.getParameter( PARAMETER_QUERY );
126         _plugin = PluginService.getPlugin( strPluginName );
127 
128         page.setTitle( AppPropertiesService.getProperty( PROPERTY_PAGE_TITLE ) );
129         page.setPathLabel( AppPropertiesService.getProperty( PROPERTY_PAGE_PATH ) );
130 
131         String strHtmlPageId = request.getParameter( PARAMETER_HTMLPAGE_ID );
132 
133         if ( StringUtils.isNotBlank( strQuery ) )
134         {
135             page.setContent( getSearch( strQuery, strPluginName, request ) );
136         }
137         else
138         {
139             if ( strHtmlPageId == null )
140             {
141                 page.setContent( getHtmlPagesLists( request ) );
142             }
143 
144             if ( strHtmlPageId != null )
145             {
146                 page.setContent( getHtmlPage( request, strHtmlPageId ) );
147             }
148         }
149 
150         return page;
151     }
152 
153     private String getSearch( String strQuery, String strPluginName, HttpServletRequest request )
154     {
155         SearchEngine engine = SpringContextService.getBean( BEAN_SEARCH_ENGINE );
156         List<SearchResult> listResults = engine.getSearchResults( strQuery, request );
157 
158         HashMap<String, Object> model = new HashMap<String, Object>( );
159         model.put( MARK_RESULT, listResults );
160         model.put( MARK_QUERY, strQuery );
161 
162         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_HTMLPAGE_LISTS, request.getLocale( ), model );
163         return template.getHtml( );
164     }
165 
166     private String getHtmlPagesLists( HttpServletRequest request ) throws SiteMessageException
167     {
168         HashMap<String, Object> model = new HashMap<String, Object>( );
169 
170         Collection<HtmlPage> htmlPageList = HtmlPageService.getInstance( ).getHtmlPageListCache( );
171         Collection<HtmlPage> visibleHtmlPageList = new ArrayList<HtmlPage>( ); // filter the list of lists by role
172 
173         for ( HtmlPage htmlpage : htmlPageList )
174         {
175             if( HtmlPageUtil.isVisible( request, htmlpage.getRole( ) ) )
176             {
177                 visibleHtmlPageList.add( htmlpage );
178             }
179         }
180 
181         model.put( MARK_HTMLPAGE_LIST, visibleHtmlPageList );
182 
183         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_HTMLPAGE_LISTS, request.getLocale( ), model );
184 
185         return template.getHtml( );
186     }
187 
188     /**
189      * Returns the htmlpage page
190      * 
191      * @param request
192      *            The Html request
193      * @param plugin
194      *            The plugin
195      * @return The Html template
196      */
197     private String getHtmlPage( HttpServletRequest request, String strHtmlPageId ) throws SiteMessageException
198     {
199         HashMap<String, Object> model = new HashMap<String, Object>( );
200 
201         int nHtmlPageId = Integer.parseInt( strHtmlPageId );
202         HtmlPage htmlpage = HtmlPageService.getInstance( ).getHtmlPageCache( nHtmlPageId );
203 
204         if ( htmlpage != null )
205         {
206             if ( HtmlPageUtil.isVisible( request, htmlpage.getRole( ) ) )
207             {
208                 model.put( MARK_HTMLPAGE, htmlpage );
209                 model.put( MARK_PAGE, _plugin.getName( ) );
210             }
211             else
212             {
213                 SiteMessageService.setMessage( request, PROPERTY_MESSAGE_NOT_AUTHORIZED, SiteMessage.TYPE_ERROR );
214             }
215         }
216         else
217         {
218             SiteMessageService.setMessage( request, PROPERTY_MESSAGE_ERROR_HTMLPAGE, SiteMessage.TYPE_ERROR );
219         }
220 
221         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_HTMLPAGE, request.getLocale( ), model );
222 
223         return template.getHtml( );
224     }
225 }