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.business.HtmlPageHome;
38  import fr.paris.lutece.plugins.htmlpage.service.EnumStatus;
39  import fr.paris.lutece.plugins.htmlpage.utils.HtmlPageUtil;
40  import fr.paris.lutece.portal.business.role.RoleHome;
41  import fr.paris.lutece.portal.service.message.AdminMessage;
42  import fr.paris.lutece.portal.service.message.AdminMessageService;
43  import fr.paris.lutece.portal.service.template.AppTemplateService;
44  import fr.paris.lutece.portal.service.util.AppPathService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
47  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
48  import fr.paris.lutece.portal.web.constants.Messages;
49  import fr.paris.lutece.util.ReferenceList;
50  import fr.paris.lutece.util.html.HtmlTemplate;
51  import fr.paris.lutece.util.html.Paginator;
52  import fr.paris.lutece.util.url.UrlItem;
53  
54  import java.sql.Timestamp;
55  import java.util.Collection;
56  import java.util.HashMap;
57  import java.util.List;
58  import java.util.Map;
59  
60  import javax.servlet.http.HttpServletRequest;
61  
62  import org.apache.commons.lang3.StringUtils;
63  
64  /**
65   *
66   * @author lenaini
67   */
68  public class HtmlPageJspBean extends PluginAdminPageJspBean
69  {
70      // Right
71      public static final String RIGHT_MANAGE_HTMLPAGE = "HTMLPAGE_MANAGEMENT";
72  
73      // properties for page titles
74      private static final String PROPERTY_PAGE_TITLE_HTMLPAGE_LIST = "htmlpage.manage_htmlpage.pageTitle";
75      private static final String PROPERTY_PAGE_TITLE_MODIFY = "htmlpage.modify_htmlpage.pageTitle";
76      private static final String PROPERTY_PAGE_TITLE_CREATE = "htmlpage.create_htmlpage.pageTitle";
77  
78      // Properties
79      private static final String PROPERTY_DEFAULT_HTMLPAGE_LIST_PER_PAGE = "htmlpage.htmlPageList.itemsPerPage";
80  
81      // Messages
82      private static final String MESSAGE_CONFIRM_REMOVE_HTMLPAGE = "htmlpage.message.confirmRemoveHtmlPage";
83      private static final String MESSAGE_INVALID_DATE_START = "htmlpage.message.invalidDateStart";
84      private static final String MESSAGE_INVALID_DATE_END = "htmlpage.message.invalidDateEnd";
85      
86      // Markers
87      private static final String MARK_LIST_HTMLPAGE_LIST = "htmlpage_list";
88      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
89      private static final String MARK_WORKGROUPS_LIST = "workgroups_list";
90      private static final String MARK_WEBAPP_URL = "webapp_url";
91      private static final String MARK_PROD_URL = "prod_url";
92      
93      private static final String MARK_LOCALE = "locale";
94      private static final String MARK_HTML_CONTENT = "html_content";
95      private static final String MARK_HTMLPAGE = "htmlpage";
96      private static final String MARK_ROLES_LIST = "roles_list";
97      private static final String MARK_PAGINATOR = "paginator";
98      private static final String MARK_STATUS = "status_list";
99  
100     // parameters
101     private static final String PARAMETER_HTMLPAGE_ID = "id_htmlpage";
102     private static final String PARAMETER_HTMLPAGE_DESCRIPTION = "description";
103     private static final String PARAMETER_HTMLPAGE_STATUS = "status";
104     private static final String PARAMETER_HTMLPAGE_WORKGROUP = "workgroup";
105     private static final String PARAMETER_HTMLPAGE_HTML_CONTENT = "html_content";
106     private static final String PARAMETER_ID_HTMLPAGE_LIST = "htmlpage_list_id";
107     private static final String PARAMETER_HTMLPAGE_ROLE = "role";
108     private static final String PARAMETER_PAGE_INDEX = "page_index";
109     private static final String PARAMETER_HTMLPAGE_DATE_START = "date_start";
110     private static final String PARAMETER_HTMLPAGE_DATE_END = "date_end";
111     
112     // templates
113     private static final String TEMPLATE_MANAGE_HTMLPAGE = "/admin/plugins/htmlpage/manage_htmlpage.html";
114     private static final String TEMPLATE_CREATE_HTMLPAGE = "/admin/plugins/htmlpage/create_htmlpage.html";
115     private static final String TEMPLATE_MODIFY_HTMLPAGE = "/admin/plugins/htmlpage/modify_htmlpage.html";
116 
117     // Jsp Definition
118     private static final String JSP_DO_REMOVE_HTMLPAGE = "jsp/admin/plugins/htmlpage/DoRemoveHtmlPage.jsp";
119     private static final String JSP_REDIRECT_TO_MANAGE_HTMLPAGE = "ManageHtmlPage.jsp";
120 
121     // Variables
122     private int _nDefaultItemsPerPage;
123     private String _strCurrentPageIndex;
124     private int _nItemsPerPage;
125 
126     /**
127      * returns the template of the HtmlPageLists management
128      * 
129      * @param request
130      *            The HttpRequest
131      * @return template of lists management
132      */
133     public String getManageHtmlPage( HttpServletRequest request )
134     {
135         setPageTitleProperty( PROPERTY_PAGE_TITLE_HTMLPAGE_LIST );
136 
137         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
138         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_HTMLPAGE_LIST_PER_PAGE, 50 );
139         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
140 
141         Collection<HtmlPage> listHtmlPageList = HtmlPageHome.findAll( getPlugin( ) );
142         listHtmlPageList = AdminWorkgroupService.getAuthorizedCollection( listHtmlPageList, getUser( ) );
143 
144         Paginator paginator = new Paginator( (List<HtmlPage>) listHtmlPageList, _nItemsPerPage, getHomeUrl( request ), PARAMETER_PAGE_INDEX,
145                 _strCurrentPageIndex );
146 
147         Map<String, Object> model = new HashMap<String, Object>( );
148         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
149         model.put( MARK_PAGINATOR, paginator );
150         model.put( MARK_LIST_HTMLPAGE_LIST, paginator.getPageItems( ) );
151 
152         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_HTMLPAGE, getLocale( ), model );
153 
154         return getAdminPage( templateList.getHtml( ) );
155     }
156 
157     /**
158      * Returns the form to create a htmlpage
159      *
160      * @param request
161      *            The Http request
162      * @return the html code of the htmlpage form
163      */
164     public String getCreateHtmlPage( HttpServletRequest request )
165     {
166         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE );
167 
168         Map<String, Object> model = new HashMap<String, Object>( );
169         ReferenceList workgroupsList = AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) );
170         model.put( MARK_WORKGROUPS_LIST, workgroupsList );
171         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
172         model.put( MARK_PROD_URL, AppPathService.getProdUrl(request) );
173         model.put( MARK_LOCALE, getLocale( ) );
174         model.put( MARK_HTML_CONTENT, "" );
175         model.put( MARK_ROLES_LIST, RoleHome.getRolesList( ) );
176         model.put( MARK_STATUS, EnumStatus.getReferenceList( ) );
177 
178         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_HTMLPAGE, getLocale( ), model );
179 
180         return getAdminPage( template.getHtml( ) );
181     }
182 
183     /**
184      * Process the data capture form of a new htmlpage
185      *
186      * @param request
187      *            The Http Request
188      * @return The Jsp URL of the process result
189      */
190     public String doCreateHtmlPage( HttpServletRequest request )
191     {
192         String strDescription = request.getParameter( PARAMETER_HTMLPAGE_DESCRIPTION );
193         String strHtmlContent = request.getParameter( PARAMETER_HTMLPAGE_HTML_CONTENT );
194         String strStatus = request.getParameter( PARAMETER_HTMLPAGE_STATUS );
195         String strWorkgroup = request.getParameter( PARAMETER_HTMLPAGE_WORKGROUP );
196         String strRole = request.getParameter( PARAMETER_HTMLPAGE_ROLE );
197 
198         // Mandatory fields
199         if ( ( strDescription == null ) || strDescription.trim( ).equals( "" ) )
200         {
201             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
202         }
203 
204         HtmlPagemlpage/business/HtmlPage.html#HtmlPage">HtmlPage htmlpage = new HtmlPage( );
205 
206         htmlpage.setDescription( strDescription );
207         htmlpage.setHtmlContent( strHtmlContent );
208         htmlpage.setStatus( Integer.parseInt( strStatus ) );
209         htmlpage.setWorkgroup( strWorkgroup );
210         htmlpage.setRole( strRole );
211         
212         String strDateStart = request.getParameter( PARAMETER_HTMLPAGE_DATE_START );
213         String strDateEnd = request.getParameter( PARAMETER_HTMLPAGE_DATE_END );
214         
215         Timestamp dateStart = HtmlPageUtil.convertToTimestamp( strDateStart );
216         Timestamp dateEnd = HtmlPageUtil.convertToTimestamp( strDateEnd );
217 
218         if ( StringUtils.isNotEmpty( strDateStart ) && dateStart == null )
219         {
220             return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_DATE_START, AdminMessage.TYPE_STOP );
221         }
222         
223         if (  StringUtils.isNotEmpty( strDateEnd ) && dateEnd == null  )
224         {
225             return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_DATE_END, AdminMessage.TYPE_STOP );
226         }
227            
228             
229 		htmlpage.setDateStart( dateStart );
230 		htmlpage.setDateEnd( dateEnd );
231 
232         HtmlPageHome.create( htmlpage, getPlugin( ) );
233 
234         // if the operation occurred well, redirects towards the list of the HtmlPages
235         return JSP_REDIRECT_TO_MANAGE_HTMLPAGE;
236     }
237 
238     /**
239      * Process the data capture form of a new htmlpage from copy of other
240      *
241      * @param request
242      *            The Http Request
243      * @return The Jsp URL of the process result
244      */
245     public String doDuplicateHtmlPage( HttpServletRequest request )
246     {
247         int nId = Integer.parseInt( request.getParameter( PARAMETER_HTMLPAGE_ID ) );
248 
249         HtmlPage htmlpage = HtmlPageHome.findByPrimaryKey( nId, getPlugin( ) );
250 
251         HtmlPagesiness/HtmlPage.html#HtmlPage">HtmlPage duplicateHtmlPage = new HtmlPage( );
252         duplicateHtmlPage.setDescription( htmlpage.getDescription( ) );
253         duplicateHtmlPage.setHtmlContent( htmlpage.getHtmlContent( ) );
254         duplicateHtmlPage.setStatus( htmlpage.getStatus( ) );
255         duplicateHtmlPage.setWorkgroup( htmlpage.getWorkgroup( ) );
256         duplicateHtmlPage.setRole( htmlpage.getRole( ) );
257 
258         HtmlPageHome.create( duplicateHtmlPage, getPlugin( ) );
259 
260         // if the operation occurred well, redirects towards the list of the HtmlPages
261         return JSP_REDIRECT_TO_MANAGE_HTMLPAGE;
262     }
263 
264     /**
265      * Returns the form to update info about a htmlPage
266      *
267      * @param request
268      *            The Http request
269      * @return The HTML form to update info
270      */
271     public String getModifyHtmlPage( HttpServletRequest request )
272     {
273         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY );
274 
275         int nId = Integer.parseInt( request.getParameter( PARAMETER_HTMLPAGE_ID ) );
276         HtmlPage htmlPage = HtmlPageHome.findByPrimaryKey( nId, getPlugin( ) );
277 
278         Map<String, Object> model = new HashMap<String, Object>( );
279         ReferenceList workgroupsList = AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) );
280         model.put( MARK_WORKGROUPS_LIST, workgroupsList );
281         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
282         model.put( MARK_PROD_URL, AppPathService.getProdUrl(request) );
283         model.put( MARK_LOCALE, getLocale( ) );
284         model.put( MARK_HTMLPAGE, htmlPage );
285         model.put( MARK_ROLES_LIST, RoleHome.getRolesList( ) );
286         model.put( MARK_STATUS, EnumStatus.getReferenceList( ) );
287         
288         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_HTMLPAGE, getLocale( ), model );
289 
290         return getAdminPage( template.getHtml( ) );
291     }
292 
293     /**
294      * Process the change form of a htmlPage
295      *
296      * @param request
297      *            The Http request
298      * @return The Jsp URL of the process result
299      */
300     public String doModifyHtmlPage( HttpServletRequest request )
301     {
302         // Mandatory fields
303         if ( request.getParameter( PARAMETER_HTMLPAGE_DESCRIPTION ).equals( "" ) || request.getParameter( PARAMETER_HTMLPAGE_HTML_CONTENT ).equals( "" ) )
304         {
305             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
306         }
307 
308         int nId = Integer.parseInt( request.getParameter( PARAMETER_HTMLPAGE_ID ) );
309 
310         HtmlPage htmlPage = HtmlPageHome.findByPrimaryKey( nId, getPlugin( ) );
311         htmlPage.setDescription( request.getParameter( PARAMETER_HTMLPAGE_DESCRIPTION ) );
312         htmlPage.setHtmlContent( request.getParameter( PARAMETER_HTMLPAGE_HTML_CONTENT ) );
313         htmlPage.setStatus( Integer.parseInt( request.getParameter( PARAMETER_HTMLPAGE_STATUS ) ) );
314         htmlPage.setWorkgroup( request.getParameter( PARAMETER_HTMLPAGE_WORKGROUP ) );
315         htmlPage.setRole( request.getParameter( PARAMETER_HTMLPAGE_ROLE ) );
316 
317         String strDateStart = request.getParameter( PARAMETER_HTMLPAGE_DATE_START );
318         String strDateEnd = request.getParameter( PARAMETER_HTMLPAGE_DATE_END );
319         
320         Timestamp dateStart = HtmlPageUtil.convertToTimestamp( strDateStart );
321         Timestamp dateEnd = HtmlPageUtil.convertToTimestamp( strDateEnd );
322 
323         if ( StringUtils.isNotEmpty( strDateStart ) && dateStart == null )
324         {
325             return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_DATE_START, AdminMessage.TYPE_STOP );
326         }
327         
328         if (  StringUtils.isNotEmpty( strDateEnd ) && dateEnd == null  )
329         {
330             return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_DATE_END, AdminMessage.TYPE_STOP );
331         }
332             
333 		htmlPage.setDateStart( dateStart );
334 		htmlPage.setDateEnd( dateEnd );
335 
336         HtmlPageHome.update( htmlPage, getPlugin( ) );
337 
338         // if the operation occurred well, redirects towards the list of the HtmlPages
339         return JSP_REDIRECT_TO_MANAGE_HTMLPAGE;
340     }
341 
342     /**
343      * Manages the removal form of a htmlPage whose identifier is in the http request
344      *
345      * @param request
346      *            The Http request
347      * @return the html code to confirm
348      */
349     public String getConfirmRemoveHtmlPage( HttpServletRequest request )
350     {
351         int nIdHtmlPage = Integer.parseInt( request.getParameter( PARAMETER_HTMLPAGE_ID ) );
352 
353         UrlItem url = new UrlItem( JSP_DO_REMOVE_HTMLPAGE );
354         url.addParameter( PARAMETER_HTMLPAGE_ID, nIdHtmlPage );
355         url.addParameter( PARAMETER_ID_HTMLPAGE_LIST, request.getParameter( PARAMETER_ID_HTMLPAGE_LIST ) );
356 
357         Object [ ] args = {
358                 request.getParameter( PARAMETER_HTMLPAGE_DESCRIPTION )
359         };
360 
361         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_HTMLPAGE, args, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
362     }
363 
364     /**
365      * Treats the removal form of a htmlPage
366      *
367      * @param request
368      *            The Http request
369      * @return the jsp URL to display the form to manage htmlPages
370      */
371     public String doRemoveHtmlPage( HttpServletRequest request )
372     {
373         int nIdHtmlPage = Integer.parseInt( request.getParameter( PARAMETER_HTMLPAGE_ID ) );
374 
375         HtmlPage htmlPage = HtmlPageHome.findByPrimaryKey( nIdHtmlPage, getPlugin( ) );
376         HtmlPageHome.remove( htmlPage, getPlugin( ) );
377 
378         // if the operation occurred well, redirects towards the list of the HtmlPages
379         return JSP_REDIRECT_TO_MANAGE_HTMLPAGE;
380     }
381 }