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.announce.web;
35  
36  import java.sql.Timestamp;
37  import java.util.ArrayList;
38  import java.util.Calendar;
39  import java.util.Collection;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Map;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  import org.apache.commons.lang.StringUtils;
47  
48  import fr.paris.lutece.plugins.announce.business.Announce;
49  import fr.paris.lutece.plugins.announce.business.AnnounceHome;
50  import fr.paris.lutece.plugins.announce.business.AnnounceNotify;
51  import fr.paris.lutece.plugins.announce.business.AnnounceNotifyHome;
52  import fr.paris.lutece.plugins.announce.business.AnnounceSort;
53  import fr.paris.lutece.plugins.announce.business.Category;
54  import fr.paris.lutece.plugins.announce.business.CategoryHome;
55  import fr.paris.lutece.plugins.announce.service.AnnounceResourceIdService;
56  import fr.paris.lutece.plugins.announce.utils.AnnounceUtils;
57  import fr.paris.lutece.plugins.genericattributes.business.Entry;
58  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
59  import fr.paris.lutece.plugins.genericattributes.business.Field;
60  import fr.paris.lutece.plugins.genericattributes.business.Response;
61  import fr.paris.lutece.portal.business.rbac.RBAC;
62  import fr.paris.lutece.portal.business.user.AdminUser;
63  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
64  import fr.paris.lutece.portal.service.admin.AdminUserService;
65  import fr.paris.lutece.portal.service.message.AdminMessage;
66  import fr.paris.lutece.portal.service.message.AdminMessageService;
67  import fr.paris.lutece.portal.service.plugin.Plugin;
68  import fr.paris.lutece.portal.service.rbac.RBACService;
69  import fr.paris.lutece.portal.service.template.AppTemplateService;
70  import fr.paris.lutece.portal.service.util.AppPathService;
71  import fr.paris.lutece.portal.service.util.AppPropertiesService;
72  import fr.paris.lutece.portal.service.workflow.WorkflowService;
73  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
74  import fr.paris.lutece.portal.web.constants.Parameters;
75  import fr.paris.lutece.portal.web.util.LocalizedDelegatePaginator;
76  import fr.paris.lutece.util.html.AbstractPaginator;
77  import fr.paris.lutece.util.html.HtmlTemplate;
78  import fr.paris.lutece.util.html.Paginator;
79  import fr.paris.lutece.util.url.UrlItem;
80  
81  /**
82   * This class provides the user interface to manage announce features ( manage, create, modify, remove )
83   */
84  public class AnnounceJspBean extends PluginAdminPageJspBean
85  {
86      /**
87       * Right to manage this feature
88       */
89      public static final String RIGHT_MANAGE_ANNOUNCE = "ANNOUNCE_MANAGEMENT";
90      private static final long serialVersionUID = 6293138267315605660L;
91  
92      /* parameter */
93      private static final String PARAMETER_ANNOUNCE_ID = "announce_id";
94      private static final String SESSION_SORT = "announce.sessionAnnounceSort";
95  
96      /* properties */
97      private static final String PROPERTY_PAGE_TITLE_MANAGE_ANNOUNCES = "announce.manage_announces.pageTitle";
98      private static final String PROPERTY_DEFAULT_LIST_ANNOUNCE_PER_PAGE = "announce.announce.itemsPerPage";
99      private static final String PROPERTY_PAGE_TITLE_PREVIEW_ANNOUNCE = "announce.preview_announce.pageTitle";
100 
101     /* templates */
102     private static final String TEMPLATE_MANAGE_ANNOUNCES = "admin/plugins/announce/manage_announces.html";
103     private static final String TEMPLATE_PREVIEW_ANNOUNCE = "admin/plugins/announce/preview_announce.html";
104 
105     /* Jsp Definition */
106     private static final String JSP_DO_REMOVE_ANNOUNCE = "jsp/admin/plugins/announce/DoRemoveAnnounce.jsp";
107     private static final String JSP_MANAGE_ANNOUNCES = "jsp/admin/plugins/announce/ManageAnnounces.jsp";
108     private static final String JSP_REDIRECT_TO_MANAGE_ANNOUNCES = "ManageAnnounces.jsp";
109 
110     /* Messages */
111     private static final String MESSAGE_CONFIRM_REMOVE_ANNOUNCE = "announce.message.confirmRemoveAnnounce";
112 
113     /* Markers */
114     private static final String MARK_ANNOUNCE = "announce";
115     private static final String MARK_ANNOUNCE_LIST = "list_announces";
116     private static final String MARK_PAGINATOR = "paginator";
117     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
118     private static final String MARK_LIST_RESPONSES = "list_responses";
119     private static final String MARK_RESOURCE_HISTORY = "resource_history";
120     private static final String MARK_RIGHT_DELETE = "right_delete_announce";
121     private static final String MARK_RIGHT_PUBLISH = "right_publish_announce";
122     private static final String MARK_RIGHT_SUSPEND = "right_suspend_announce";
123     private static final String MARK_RIGHT_WORKFLOW_ACTION = "right_execute_workflow_action";
124     private static final String MARK_ENTRY_LIST_GEOLOCATION = "admList_entryTypeGeolocation";
125 
126     /* Variables */
127     private int _nDefaultItemsPerPage;
128     private String _strCurrentPageIndex;
129     private int _nItemsPerPage;
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public Plugin getPlugin( )
136     {
137         Plugin plugin = super.getPlugin( );
138 
139         if ( plugin == null )
140         {
141             plugin = AnnounceUtils.getPlugin( );
142         }
143 
144         return plugin;
145     }
146 
147     /**
148      * Returns the list of announce
149      *
150      * @param request
151      *            The HTTP request
152      * @return the announces list
153      */
154     public String getManageAnnounces( HttpServletRequest request )
155     {
156         setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_ANNOUNCES );
157 
158         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
159 
160         if ( _nDefaultItemsPerPage == 0 )
161         {
162             _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_ANNOUNCE_PER_PAGE, 50 );
163         }
164 
165         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
166 
167         String strSort = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
168         boolean bSortAsc = Boolean.parseBoolean( request.getParameter( Parameters.SORTED_ASC ) );
169         AnnounceSort announceSort;
170 
171         if ( strSort == null )
172         {
173             announceSort = (AnnounceSort) request.getSession( ).getAttribute( SESSION_SORT );
174 
175             if ( announceSort == null )
176             {
177                 announceSort = AnnounceSort.DEFAULT_SORT;
178             }
179         }
180         else
181         {
182             announceSort = AnnounceSort.getAnnounceSort( strSort, bSortAsc );
183             request.getSession( ).setAttribute( SESSION_SORT, announceSort );
184         }
185 
186         List<Integer> listIdAnnounces = AnnounceHome.findAll( announceSort );
187 
188         Paginator<Integer> paginatorId = new Paginator<>( listIdAnnounces, _nItemsPerPage, StringUtils.EMPTY, AbstractPaginator.PARAMETER_PAGE_INDEX,
189                 _strCurrentPageIndex );
190 
191         AdminUser user = AdminUserService.getAdminUser( request );
192         List<Announce> listAnnounces = AnnounceHome.findByListId( paginatorId.getPageItems( ), announceSort );
193         boolean bCanExecuteWorkflowAction = false;
194 
195         if ( WorkflowService.getInstance( ).isAvailable( ) )
196         {
197             bCanExecuteWorkflowAction = RBACService.isAuthorized( Announce.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
198                     AnnounceResourceIdService.PERMISSION_EXECUTE_WORKFLOW_ACTION, user );
199 
200             for ( Announce announce : listAnnounces )
201             {
202                 announce.setCategory( CategoryHome.findByPrimaryKey( announce.getCategory( ).getId( ) ) );
203 
204                 if ( bCanExecuteWorkflowAction )
205                 {
206                     announce.setListWorkflowActions( WorkflowService.getInstance( ).getActions( announce.getId( ), Announce.RESOURCE_TYPE,
207                             announce.getCategory( ).getIdWorkflow( ), getUser( ) ) );
208                 }
209             }
210         }
211 
212         Map<String, Object> model = new HashMap<>( );
213 
214         LocalizedDelegatePaginator<Announce> paginator = new LocalizedDelegatePaginator<>( listAnnounces, _nItemsPerPage, getURLManageAnnounces( request ),
215                 AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, listIdAnnounces.size( ), getLocale( ) );
216 
217         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
218         model.put( MARK_PAGINATOR, paginator );
219         model.put( MARK_ANNOUNCE_LIST, paginator.getPageItems( ) );
220 
221         model.put( MARK_RIGHT_DELETE,
222                 RBACService.isAuthorized( Announce.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, AnnounceResourceIdService.PERMISSION_DELETE, user ) );
223         model.put( MARK_RIGHT_PUBLISH,
224                 RBACService.isAuthorized( Announce.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, AnnounceResourceIdService.PERMISSION_PUBLISH, user ) );
225         model.put( MARK_RIGHT_SUSPEND,
226                 RBACService.isAuthorized( Announce.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, AnnounceResourceIdService.PERMISSION_SUSPEND, user ) );
227         model.put( MARK_RIGHT_WORKFLOW_ACTION, bCanExecuteWorkflowAction );
228 
229         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_ANNOUNCES, getLocale( ), model );
230 
231         return getAdminPage( templateList.getHtml( ) );
232     }
233 
234     /**
235      * Get the page to display the preview of an announce
236      * 
237      * @param request
238      *            The request
239      * @return The HTML to display
240      */
241     public String getPreviewAnnounce( HttpServletRequest request )
242     {
243         setPageTitleProperty( PROPERTY_PAGE_TITLE_PREVIEW_ANNOUNCE );
244 
245         int nIdAnnounce = Integer.parseInt( request.getParameter( PARAMETER_ANNOUNCE_ID ) );
246         Announce announce = AnnounceHome.findByPrimaryKey( nIdAnnounce );
247         Collection<Entry> listGeolocalisation = new ArrayList<>( );
248 
249         Collection<Response> listResponses = AnnounceHome.findListResponse( announce.getId( ), false );
250         for ( Response response : listResponses )
251         {
252 
253             if ( response.getEntry( ) != null && response.getEntry( ).getEntryType( ) != null
254                     && "announce.entryTypeGeolocation".equals( response.getEntry( ).getEntryType( ).getBeanName( ) ) )
255             {
256                 Entry entry = EntryHome.findByPrimaryKey( response.getEntry( ).getIdEntry( ) );
257                 for ( Field filed : entry.getFields( ) )
258                 {
259 
260                     if ( response.getField( ) != null && filed.getIdField( ) == response.getField( ).getIdField( ) )
261                     {
262                         response.setField( filed );
263                     }
264                 }
265 
266                 boolean bool = true;
267 
268                 for ( Entry ent : listGeolocalisation )
269                 {
270                     if ( ent.getIdEntry( ) == ( entry.getIdEntry( ) ) )
271                     {
272                         bool = false;
273                     }
274                 }
275                 if ( bool )
276                 {
277                     listGeolocalisation.add( entry );
278                 }
279             }
280 
281         }
282 
283         HashMap<String, Object> model = new HashMap<>( );
284         model.put( MARK_ENTRY_LIST_GEOLOCATION, listGeolocalisation );
285         model.put( MARK_LIST_RESPONSES, listResponses );
286         model.put( MARK_ANNOUNCE, announce );
287 
288         Category category = CategoryHome.findByPrimaryKey( announce.getCategory( ).getId( ) );
289         announce.setCategory( category );
290 
291         if ( ( category.getIdWorkflow( ) > 0 ) && WorkflowService.getInstance( ).isAvailable( ) )
292         {
293             model.put( MARK_RESOURCE_HISTORY, WorkflowService.getInstance( ).getDisplayDocumentHistory( nIdAnnounce, Announce.RESOURCE_TYPE,
294                     category.getIdWorkflow( ), request, getLocale( ) ) );
295         }
296 
297         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PREVIEW_ANNOUNCE, getLocale( ), model );
298 
299         return getAdminPage( template.getHtml( ) );
300     }
301 
302     /**
303      * Manages the removal form of a announce whose identifier is in the http request
304      *
305      * @param request
306      *            The HTTP request
307      * @return the HTML code to confirm
308      * @throws AccessDeniedException
309      *             Id the user is not authorized to access this feature
310      */
311     public String getConfirmRemoveAnnounce( HttpServletRequest request ) throws AccessDeniedException
312     {
313         String strAnnounceId = request.getParameter( PARAMETER_ANNOUNCE_ID );
314         AdminUser adminUser = AdminUserService.getAdminUser( request );
315 
316         if ( !RBACService.isAuthorized( Announce.RESOURCE_TYPE, strAnnounceId, AnnounceResourceIdService.PERMISSION_DELETE, adminUser ) )
317         {
318             throw new AccessDeniedException(
319                     "User '" + adminUser.getFirstName( ) + " " + adminUser.getLastName( ) + "' is not authorized to remove announce with id " + strAnnounceId );
320         }
321 
322         int nIdAnnounce = Integer.parseInt( strAnnounceId );
323 
324         UrlItem url = new UrlItem( JSP_DO_REMOVE_ANNOUNCE );
325         url.addParameter( PARAMETER_ANNOUNCE_ID, nIdAnnounce );
326 
327         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_ANNOUNCE, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
328     }
329 
330     /**
331      * Treats the removal form of a announce
332      *
333      * @param request
334      *            The HTTP request
335      * @return the JSP URL to display the form to manage announces
336      * @throws AccessDeniedException
337      *             Id the user is not authorized to access this feature
338      */
339     public String doRemoveAnnounce( HttpServletRequest request ) throws AccessDeniedException
340     {
341         String strAnnounceId = request.getParameter( PARAMETER_ANNOUNCE_ID );
342         AdminUser adminUser = AdminUserService.getAdminUser( request );
343 
344         if ( !RBACService.isAuthorized( Announce.RESOURCE_TYPE, strAnnounceId, AnnounceResourceIdService.PERMISSION_DELETE, adminUser ) )
345         {
346             throw new AccessDeniedException(
347                     "User '" + adminUser.getFirstName( ) + " " + adminUser.getLastName( ) + "' is not authorized to remove announce with id " + strAnnounceId );
348         }
349 
350         int nIdAnnounce = Integer.parseInt( strAnnounceId );
351 
352         AnnounceHome.remove( nIdAnnounce );
353 
354         // if the operation occurred well, redirects towards the list
355         return JSP_REDIRECT_TO_MANAGE_ANNOUNCES;
356     }
357 
358     /**
359      * Publish a category
360      *
361      * @param request
362      *            The Http request
363      * @param bPublished
364      *            True to publish the announce, false to unpublish it
365      * @return the jsp URL to display the form to manage categories
366      * @throws AccessDeniedException
367      *             Id the user is not authorized to access this feature
368      */
369     public String doPublishAnnounce( HttpServletRequest request, boolean bPublished ) throws AccessDeniedException
370     {
371         String strAnnounceId = request.getParameter( PARAMETER_ANNOUNCE_ID );
372         AdminUser adminUser = AdminUserService.getAdminUser( request );
373 
374         if ( !RBACService.isAuthorized( Announce.RESOURCE_TYPE, strAnnounceId, AnnounceResourceIdService.PERMISSION_PUBLISH, adminUser ) )
375         {
376             throw new AccessDeniedException( "User '" + adminUser.getFirstName( ) + " " + adminUser.getLastName( )
377                     + "' is not authorized to publish/unpublish announce with id " + strAnnounceId );
378         }
379 
380         if ( StringUtils.isNumeric( strAnnounceId ) )
381         {
382             int nIdAnnounce = Integer.parseInt( strAnnounceId );
383             Announce announce = AnnounceHome.findByPrimaryKey( nIdAnnounce );
384             if ( announce.getDatePublication( ).getTime( ) == new Timestamp( 0 ).getTime( ) )
385             {
386                 AnnounceNotifyess/AnnounceNotify.html#AnnounceNotify">AnnounceNotify announceNotify = new AnnounceNotify( );
387                 announceNotify.setIdAnnounce( announce.getId( ) );
388                 AnnounceNotifyHome.create( announceNotify );
389             }
390             announce.setDateCreation( new Timestamp( Calendar.getInstance( ).getTimeInMillis( ) ) );
391             announce.setPublished( bPublished );
392             AnnounceHome.setPublished( announce );
393         }
394         // if the operation occurred well, redirects towards the list
395         return JSP_REDIRECT_TO_MANAGE_ANNOUNCES;
396     }
397 
398     /**
399      * enables an announce
400      * 
401      * @param request
402      *            the httpRequest
403      * @return template redirection
404      * @throws AccessDeniedException
405      *             Id the user is not authorized to access this feature
406      */
407     public String doEnableAnnounce( HttpServletRequest request ) throws AccessDeniedException
408     {
409         String strAnnounceId = request.getParameter( PARAMETER_ANNOUNCE_ID );
410         AdminUser adminUser = AdminUserService.getAdminUser( request );
411 
412         if ( !RBACService.isAuthorized( Announce.RESOURCE_TYPE, strAnnounceId, AnnounceResourceIdService.PERMISSION_SUSPEND, adminUser ) )
413         {
414             throw new AccessDeniedException( "User '" + adminUser.getFirstName( ) + " " + adminUser.getLastName( )
415                     + "' is not authorized to enable/disable announce with id " + strAnnounceId );
416         }
417 
418         int nIdAnnounce = Integer.parseInt( strAnnounceId );
419         Announce announce = AnnounceHome.findByPrimaryKey( nIdAnnounce );
420         announce.setSuspended( false );
421         AnnounceHome.setSuspended( announce );
422 
423         // if the operation occurred well, redirects towards the list
424         return JSP_REDIRECT_TO_MANAGE_ANNOUNCES;
425     }
426 
427     /**
428      * Publish a category
429      *
430      * @param request
431      *            The HTTP request
432      * @return the JSP URL to display the form to manage categories
433      * @throws AccessDeniedException
434      *             Id the user is not authorized to access this feature
435      */
436     public String doSuspendAnnounce( HttpServletRequest request ) throws AccessDeniedException
437     {
438         String strAnnounceId = request.getParameter( PARAMETER_ANNOUNCE_ID );
439         AdminUser adminUser = AdminUserService.getAdminUser( request );
440 
441         if ( !RBACService.isAuthorized( Announce.RESOURCE_TYPE, strAnnounceId, AnnounceResourceIdService.PERMISSION_SUSPEND, adminUser ) )
442         {
443             throw new AccessDeniedException( "User '" + adminUser.getFirstName( ) + " " + adminUser.getLastName( )
444                     + "' is not authorized to enable/disable announce with id " + strAnnounceId );
445         }
446 
447         int nIdAnnounce = Integer.parseInt( strAnnounceId );
448         Announce announce = AnnounceHome.findByPrimaryKey( nIdAnnounce );
449         announce.setSuspended( true );
450         AnnounceHome.setSuspended( announce );
451 
452         // if the operation occurred well, redirects towards the list
453         return JSP_REDIRECT_TO_MANAGE_ANNOUNCES;
454     }
455 
456     /**
457      * Get the URL to manage announces
458      * 
459      * @param request
460      *            The request
461      * @return The URL to manage announces.
462      */
463     public static String getURLManageAnnounces( HttpServletRequest request )
464     {
465         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MANAGE_ANNOUNCES );
466 
467         return url.getUrl( );
468     }
469 }