View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.extend.modules.opengraph.web;
35  
36  import fr.paris.lutece.plugins.extend.modules.opengraph.business.OpengraphSocialHub;
37  import fr.paris.lutece.plugins.extend.modules.opengraph.service.OpengraphResourceIdService;
38  import fr.paris.lutece.plugins.extend.modules.opengraph.service.OpengraphService;
39  import fr.paris.lutece.portal.business.user.AdminUser;
40  import fr.paris.lutece.portal.service.admin.AdminUserService;
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.rbac.RBACService;
44  import fr.paris.lutece.portal.service.spring.SpringContextService;
45  import fr.paris.lutece.portal.service.template.AppTemplateService;
46  import fr.paris.lutece.portal.service.util.AppPathService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
49  import fr.paris.lutece.portal.web.pluginaction.DefaultPluginActionResult;
50  import fr.paris.lutece.portal.web.pluginaction.IPluginActionResult;
51  import fr.paris.lutece.util.datatable.DataTableManager;
52  import fr.paris.lutece.util.html.HtmlTemplate;
53  import fr.paris.lutece.util.url.UrlItem;
54  
55  import org.apache.commons.lang3.StringUtils;
56  
57  import java.util.HashMap;
58  import java.util.Map;
59  
60  import javax.servlet.http.HttpServletRequest;
61  
62  /**
63   * JspBean of module extend Opengraph.
64   */
65  public class OpengraphJspBean extends AdminFeaturesPageJspBean
66  {
67      /**
68       * Right to manage opengraph social hub
69       */
70      public static final String MANAGE_OPENGRAPH_SOCIALHUB = "MANAGE_OPENGRAPH_SOCIALHUB";
71  
72      /**
73       * Serial Version UID
74       */
75      private static final long serialVersionUID = -7731456994777583575L;
76  
77      // PROPERTIES
78      private static final String PROPERTY_DEFAULT_ITEMS_PER_PAGE = "module.extend.opengraph.manage_opengraph_socialhub.itemsPerPage";
79      private static final String PROPERTY_MANAGE_OPENGRAPH_PAGE_TITLE = "module.extend.opengraph.manage_opengraph_socialhub.pageTitle";
80      private static final String PROPERTY_ADD_OPENGRAPH_PAGE_TITLE = "module.extend.opengraph.add_opengraph_socialhub.pageTitle";
81      private static final String PROPERTY_MODIFY_OPENGRAPH_PAGE_TITLE = "module.extend.opengraph.modify_opengraph_socialhub.pageTitle";
82  
83      // MESSAGES
84      private static final String MESSAGE_LABEL_SOCIALHUB_NAME = "module.extend.opengraph.manage_opengraph_socialhub.socialhub.name";
85      private static final String MESSAGE_LABEL_ACTION = "module.extend.opengraph.manage_opengraph_socialhub.socialhub.action";
86      private static final String MESSAGE_UNAUTHORIZED_ACTION = "extend.message.unauthorizedAction";
87      private static final String MESSAGE_CONFIRM_REMOVE_SOCIALHUB = "module.extend.opengraph.remove_socialhub.confirmRemoveSocialHub";
88      private static final String MESSAGE_MANDATORY_FIELDS = "portal.util.message.mandatoryFields";
89      private static final String MESSAGE_SOCIALHUB_NOT_FOUND = "module.extend.opengraph.manage_opengraph_socialhub.socialhub.socialhubNotFound";
90  
91      // PARAMETERS
92      private static final String PARAMETER_SOCIALHUB_NAME = "name";
93      private static final String PARAMETER_CANCEL = "cancel";
94      private static final String PARAMETER_ID_SOCIALHUB = "id_socialhub";
95      private static final String PARAM_NAME = "name";
96      private static final String PARAM_CONTENT_HEADER = "content_header";
97      private static final String PARAM_CONTENT_BODY = "content_body";
98      private static final String PARAM_CONTENT_FOOTER = "content_footer";
99  
100     // MARKS
101     private static final String MARK_DATA_TABLE_MANAGER = "dataTableManager";
102     private static final String MARK_PERMISSIONS = "permissions";
103     private static final String MARK_LOCALE = "locale";
104     private static final String MARK_WEBAPP_URL = "webapp_url";
105     private static final String MARK_SOCIALHUB = "socialhub";
106 
107     // TEMPLATES
108     private static final String TEMPLATE_MANAGE_OPENGRAPH_SOCIALHUB = "admin/plugins/extend/modules/opengraph/manage_opengraph_socialhub.html";
109     private static final String TEMPLATE_ADD_OPENGRAPH_SOCIALHUB = "admin/plugins/extend/modules/opengraph/add_opengraph_socialhub.html";
110     private static final String TEMPLATE_MODIFY_OPENGRAPH_SOCIALHUB = "admin/plugins/extend/modules/opengraph/modify_opengraph_socialhub.html";
111 
112     // URL
113     private static final String JSP_URL_MANAGE_OPENGRAPH_SOCIALHUB = "jsp/admin/plugins/extend/modules/opengraph/GetManageOpengraphSocialHub.jsp";
114     private static final String JSP_URL_REMOVE_OPENGRAPH_SOCIALHUB = "jsp/admin/plugins/extend/modules/opengraph/DoRemoveOpengraphSocialHub.jsp";
115 
116     // local variables
117     private OpengraphService _opengraphService = SpringContextService.getBean( OpengraphService.BEAN_NAME );
118     private DataTableManager<OpengraphSocialHub> _dataTableManager;
119 
120     /**
121      * Get the ManageOpengraphSocialHub page
122      * 
123      * @param request
124      *            The request
125      * @return The html of the content to display
126      */
127     public String getManageOpengraphSocialHub( HttpServletRequest request )
128     {
129         setPageTitleProperty( PROPERTY_MANAGE_OPENGRAPH_PAGE_TITLE );
130 
131         if ( _dataTableManager == null )
132         {
133             _dataTableManager = new DataTableManager<OpengraphSocialHub>( JSP_URL_MANAGE_OPENGRAPH_SOCIALHUB, JSP_URL_MANAGE_OPENGRAPH_SOCIALHUB,
134                     AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_ITEMS_PER_PAGE, 50 ), true );
135             _dataTableManager.addColumn( MESSAGE_LABEL_SOCIALHUB_NAME, PARAMETER_SOCIALHUB_NAME, true );
136             _dataTableManager.addActionColumn( MESSAGE_LABEL_ACTION );
137         }
138 
139         _dataTableManager.filterSortAndPaginate( request, _opengraphService.findAll( ) );
140 
141         Map<String, Object> model = new HashMap<String, Object>( );
142         model.put( MARK_DATA_TABLE_MANAGER, _dataTableManager );
143 
144         AdminUser user = AdminUserService.getAdminUser( request );
145 
146         Map<String, Boolean> mapPermissions = new HashMap<String, Boolean>( );
147         mapPermissions.put( OpengraphResourceIdService.PERMISSION_MODIFY_SOCIALHUB,
148                 RBACService.isAuthorized( OpengraphSocialHub.RESOURCE_TYPE, null, OpengraphResourceIdService.PERMISSION_MODIFY_SOCIALHUB, user ) );
149         mapPermissions.put( OpengraphResourceIdService.PERMISSION_ADD_SOCIALHUB,
150                 RBACService.isAuthorized( OpengraphSocialHub.RESOURCE_TYPE, null, OpengraphResourceIdService.PERMISSION_ADD_SOCIALHUB, user ) );
151         mapPermissions.put( OpengraphResourceIdService.PERMISSION_REMOVE_SOCIALHUB,
152                 RBACService.isAuthorized( OpengraphSocialHub.RESOURCE_TYPE, null, OpengraphResourceIdService.PERMISSION_REMOVE_SOCIALHUB, user ) );
153 
154         model.put( MARK_PERMISSIONS, mapPermissions );
155 
156         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_OPENGRAPH_SOCIALHUB, AdminUserService.getLocale( request ), model );
157         String strContent = getAdminPage( template.getHtml( ) );
158 
159         _dataTableManager.clearItems( );
160 
161         return strContent;
162     }
163 
164     /**
165      * Get the page to add a social hub
166      * 
167      * @param request
168      *            The request
169      * @return The result
170      */
171     public IPluginActionResult getAddOpengraphSocialHub( HttpServletRequest request )
172     {
173         if ( !RBACService.isAuthorized( OpengraphSocialHub.RESOURCE_TYPE, null, OpengraphResourceIdService.PERMISSION_ADD_SOCIALHUB,
174                 AdminUserService.getAdminUser( request ) ) )
175         {
176             IPluginActionResult result = new DefaultPluginActionResult( );
177             result.setRedirect( AdminMessageService.getMessageUrl( request, MESSAGE_UNAUTHORIZED_ACTION, AdminMessage.TYPE_STOP ) );
178 
179             return result;
180         }
181 
182         setPageTitleProperty( PROPERTY_ADD_OPENGRAPH_PAGE_TITLE );
183 
184         Map<String, Object> model = new HashMap<String, Object>( );
185         model.put( MARK_LOCALE, AdminUserService.getLocale( request ) );
186         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
187 
188         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADD_OPENGRAPH_SOCIALHUB, AdminUserService.getLocale( request ), model );
189 
190         IPluginActionResult result = new DefaultPluginActionResult( );
191         result.setHtmlContent( template.getHtml( ) );
192 
193         return result;
194     }
195 
196     /**
197      * Do add a social hub
198      * 
199      * @param request
200      *            The request
201      * @return The URL of the next page to display
202      */
203     public String doAddOpengraphSocialHub( HttpServletRequest request )
204     {
205         if ( request.getParameter( PARAMETER_CANCEL ) != null )
206         {
207             return AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_OPENGRAPH_SOCIALHUB;
208         }
209 
210         if ( !RBACService.isAuthorized( OpengraphSocialHub.RESOURCE_TYPE, null, OpengraphResourceIdService.PERMISSION_ADD_SOCIALHUB,
211                 AdminUserService.getAdminUser( request ) ) )
212         {
213             return AdminMessageService.getMessageUrl( request, MESSAGE_UNAUTHORIZED_ACTION, AdminMessage.TYPE_STOP );
214         }
215 
216         String strName = request.getParameter( PARAM_NAME );
217 
218         if ( StringUtils.isBlank( strName ) )
219         {
220             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
221         }
222 
223         String strContentHeader = request.getParameter( PARAM_CONTENT_HEADER );
224         String strContentBody = request.getParameter( PARAM_CONTENT_BODY );
225 
226         if ( StringUtils.isBlank( strContentBody ) )
227         {
228             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
229         }
230 
231         String strContentFooter = request.getParameter( PARAM_CONTENT_FOOTER );
232 
233         OpengraphSocialHubpengraph/business/OpengraphSocialHub.html#OpengraphSocialHub">OpengraphSocialHub opengraphSocialHub = new OpengraphSocialHub( );
234         opengraphSocialHub.setName( strName );
235         opengraphSocialHub.setContentHeader( strContentHeader );
236         opengraphSocialHub.setContentBody( strContentBody );
237         opengraphSocialHub.setContentFooter( strContentFooter );
238 
239         _opengraphService.createOpengraphSocialHub( opengraphSocialHub );
240 
241         return AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_OPENGRAPH_SOCIALHUB;
242     }
243 
244     /**
245      * Get the page to modify a social hub
246      * 
247      * @param request
248      *            The request
249      * @return The result
250      */
251     public IPluginActionResult getModifyOpengraphSocialHub( HttpServletRequest request )
252     {
253         if ( !RBACService.isAuthorized( OpengraphSocialHub.RESOURCE_TYPE, null, OpengraphResourceIdService.PERMISSION_MODIFY_SOCIALHUB,
254                 AdminUserService.getAdminUser( request ) ) )
255         {
256             IPluginActionResult result = new DefaultPluginActionResult( );
257             result.setRedirect( AdminMessageService.getMessageUrl( request, MESSAGE_UNAUTHORIZED_ACTION, AdminMessage.TYPE_STOP ) );
258 
259             return result;
260         }
261 
262         setPageTitleProperty( PROPERTY_MODIFY_OPENGRAPH_PAGE_TITLE );
263 
264         int nOpengraphSocialHubId = 0;
265 
266         try
267         {
268             nOpengraphSocialHubId = Integer.parseInt( request.getParameter( PARAMETER_ID_SOCIALHUB ) );
269         }
270         catch( NumberFormatException e )
271         {
272             IPluginActionResult result = new DefaultPluginActionResult( );
273             result.setRedirect( AdminMessageService.getMessageUrl( request, MESSAGE_SOCIALHUB_NOT_FOUND, AdminMessage.TYPE_STOP ) );
274 
275             return result;
276         }
277 
278         OpengraphSocialHub opengraphSocialHub = _opengraphService.getOpengraphSocialHub( nOpengraphSocialHubId );
279 
280         if ( opengraphSocialHub == null )
281         {
282             IPluginActionResult result = new DefaultPluginActionResult( );
283             result.setRedirect( AdminMessageService.getMessageUrl( request, MESSAGE_SOCIALHUB_NOT_FOUND, AdminMessage.TYPE_STOP ) );
284 
285             return result;
286         }
287 
288         Map<String, Object> model = new HashMap<String, Object>( );
289         model.put( MARK_LOCALE, AdminUserService.getLocale( request ) );
290         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
291         model.put( MARK_SOCIALHUB, opengraphSocialHub );
292 
293         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_OPENGRAPH_SOCIALHUB, AdminUserService.getLocale( request ), model );
294 
295         IPluginActionResult result = new DefaultPluginActionResult( );
296         result.setHtmlContent( template.getHtml( ) );
297 
298         return result;
299     }
300 
301     /**
302      * Do modify a social hub
303      * 
304      * @param request
305      *            The request
306      * @return The URL of the next page to display
307      */
308     public String doModifyOpengraphSocialHub( HttpServletRequest request )
309     {
310         if ( request.getParameter( PARAMETER_CANCEL ) != null )
311         {
312             return AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_OPENGRAPH_SOCIALHUB;
313         }
314 
315         if ( !RBACService.isAuthorized( OpengraphSocialHub.RESOURCE_TYPE, null, OpengraphResourceIdService.PERMISSION_MODIFY_SOCIALHUB,
316                 AdminUserService.getAdminUser( request ) ) )
317         {
318             return AdminMessageService.getMessageUrl( request, MESSAGE_UNAUTHORIZED_ACTION, AdminMessage.TYPE_STOP );
319         }
320 
321         String strName = request.getParameter( PARAM_NAME );
322 
323         if ( StringUtils.isBlank( strName ) )
324         {
325             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
326         }
327 
328         String strContentHeader = request.getParameter( PARAM_CONTENT_HEADER );
329         String strContentBody = request.getParameter( PARAM_CONTENT_BODY );
330 
331         if ( StringUtils.isBlank( strContentBody ) )
332         {
333             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
334         }
335 
336         String strContentFooter = request.getParameter( PARAM_CONTENT_FOOTER );
337 
338         int nOpengraphSocialHubId = 0;
339 
340         try
341         {
342             nOpengraphSocialHubId = Integer.parseInt( request.getParameter( PARAMETER_ID_SOCIALHUB ) );
343         }
344         catch( NumberFormatException e )
345         {
346             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
347         }
348 
349         OpengraphSocialHubpengraph/business/OpengraphSocialHub.html#OpengraphSocialHub">OpengraphSocialHub opengraphSocialHub = new OpengraphSocialHub( );
350         opengraphSocialHub.setName( strName );
351         opengraphSocialHub.setContentHeader( strContentHeader );
352         opengraphSocialHub.setContentBody( strContentBody );
353         opengraphSocialHub.setContentFooter( strContentFooter );
354         opengraphSocialHub.setOpengraphSocialHubId( nOpengraphSocialHubId );
355 
356         _opengraphService.updateOpengraphSocialHub( opengraphSocialHub );
357 
358         return AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_OPENGRAPH_SOCIALHUB;
359     }
360 
361     /**
362      * Get a confirmation page to ask a user to confirm that he wants to remove a social hub
363      * 
364      * @param request
365      *            The request
366      * @return The URL of the next page to display
367      */
368     public String confirmRemoveOpengraphSocialHub( HttpServletRequest request )
369     {
370         String strId = request.getParameter( PARAMETER_ID_SOCIALHUB );
371 
372         if ( StringUtils.isBlank( strId ) )
373         {
374             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
375         }
376 
377         UrlItem url = new UrlItem( JSP_URL_REMOVE_OPENGRAPH_SOCIALHUB );
378         url.addParameter( PARAMETER_ID_SOCIALHUB, strId );
379 
380         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_SOCIALHUB, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
381     }
382 
383     /**
384      * Remove a social hub
385      * 
386      * @param request
387      *            The request
388      * @return The URL of the next page to display
389      */
390     public String doRemoveOpengraphSocialHub( HttpServletRequest request )
391     {
392         String strId = request.getParameter( PARAMETER_ID_SOCIALHUB );
393 
394         if ( StringUtils.isBlank( strId ) )
395         {
396             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
397         }
398 
399         int nId = 0;
400 
401         try
402         {
403             nId = Integer.parseInt( strId );
404         }
405         catch( NumberFormatException e )
406         {
407             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
408         }
409 
410         _opengraphService.removeOpengraphSocialHub( nId );
411 
412         return AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_OPENGRAPH_SOCIALHUB;
413     }
414 }