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.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.lang3.StringUtils;
47  
48  import fr.paris.lutece.api.user.User;
49  import fr.paris.lutece.plugins.announce.business.Announce;
50  import fr.paris.lutece.plugins.announce.business.AnnounceHome;
51  import fr.paris.lutece.plugins.announce.business.AnnounceNotify;
52  import fr.paris.lutece.plugins.announce.business.AnnounceNotifyHome;
53  import fr.paris.lutece.plugins.announce.business.AnnounceSort;
54  import fr.paris.lutece.plugins.announce.business.Category;
55  import fr.paris.lutece.plugins.announce.business.CategoryHome;
56  import fr.paris.lutece.plugins.announce.service.AnnounceResourceIdService;
57  import fr.paris.lutece.plugins.announce.utils.AnnounceUtils;
58  import fr.paris.lutece.plugins.genericattributes.business.Entry;
59  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
60  import fr.paris.lutece.plugins.genericattributes.business.Field;
61  import fr.paris.lutece.plugins.genericattributes.business.Response;
62  import fr.paris.lutece.portal.business.rbac.RBAC;
63  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
64  import fr.paris.lutece.portal.service.message.AdminMessage;
65  import fr.paris.lutece.portal.service.message.AdminMessageService;
66  import fr.paris.lutece.portal.service.plugin.Plugin;
67  import fr.paris.lutece.portal.service.rbac.RBACService;
68  import fr.paris.lutece.portal.service.template.AppTemplateService;
69  import fr.paris.lutece.portal.service.util.AppPathService;
70  import fr.paris.lutece.portal.service.util.AppPropertiesService;
71  import fr.paris.lutece.portal.service.workflow.WorkflowService;
72  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
73  import fr.paris.lutece.portal.web.constants.Parameters;
74  import fr.paris.lutece.portal.web.util.LocalizedDelegatePaginator;
75  import fr.paris.lutece.util.html.AbstractPaginator;
76  import fr.paris.lutece.util.html.HtmlTemplate;
77  import fr.paris.lutece.util.html.Paginator;
78  import fr.paris.lutece.util.url.UrlItem;
79  
80  /**
81   * This class provides the user interface to manage announce features ( manage, create, modify, remove )
82   */
83  public class AnnounceJspBean extends PluginAdminPageJspBean
84  {
85      /**
86       * Right to manage this feature
87       */
88      public static final String RIGHT_MANAGE_ANNOUNCE = "ANNOUNCE_MANAGEMENT";
89      private static final long serialVersionUID = 6293138267315605660L;
90  
91      /* parameter */
92      private static final String PARAMETER_ANNOUNCE_ID = "announce_id";
93      private static final String SESSION_SORT = "announce.sessionAnnounceSort";
94  
95      /* properties */
96      private static final String PROPERTY_PAGE_TITLE_MANAGE_ANNOUNCES = "announce.manage_announces.pageTitle";
97      private static final String PROPERTY_DEFAULT_LIST_ANNOUNCE_PER_PAGE = "announce.announce.itemsPerPage";
98      private static final String PROPERTY_PAGE_TITLE_PREVIEW_ANNOUNCE = "announce.preview_announce.pageTitle";
99  
100     /* templates */
101     private static final String TEMPLATE_MANAGE_ANNOUNCES = "admin/plugins/announce/manage_announces.html";
102     private static final String TEMPLATE_PREVIEW_ANNOUNCE = "admin/plugins/announce/preview_announce.html";
103 
104     /* Jsp Definition */
105     private static final String JSP_DO_REMOVE_ANNOUNCE = "jsp/admin/plugins/announce/DoRemoveAnnounce.jsp";
106     private static final String JSP_MANAGE_ANNOUNCES = "jsp/admin/plugins/announce/ManageAnnounces.jsp";
107     private static final String JSP_REDIRECT_TO_MANAGE_ANNOUNCES = "ManageAnnounces.jsp";
108 
109     /* Messages */
110     private static final String MESSAGE_CONFIRM_REMOVE_ANNOUNCE = "announce.message.confirmRemoveAnnounce";
111 
112     /* Markers */
113     private static final String MARK_ANNOUNCE = "announce";
114     private static final String MARK_ANNOUNCE_LIST = "list_announces";
115     private static final String MARK_PAGINATOR = "paginator";
116     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
117     private static final String MARK_LIST_RESPONSES = "list_responses";
118     private static final String MARK_RESOURCE_HISTORY = "resource_history";
119     private static final String MARK_RIGHT_DELETE = "right_delete_announce";
120     private static final String MARK_RIGHT_PUBLISH = "right_publish_announce";
121     private static final String MARK_RIGHT_SUSPEND = "right_suspend_announce";
122     private static final String MARK_RIGHT_WORKFLOW_ACTION = "right_execute_workflow_action";
123     private static final String MARK_ENTRY_LIST_GEOLOCATION = "admList_entryTypeGeolocation";
124 
125     /* Variables */
126     private int _nDefaultItemsPerPage;
127     private String _strCurrentPageIndex;
128     private int _nItemsPerPage;
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public Plugin getPlugin( )
135     {
136         Plugin plugin = super.getPlugin( );
137 
138         if ( plugin == null )
139         {
140             plugin = AnnounceUtils.getPlugin( );
141         }
142 
143         return plugin;
144     }
145 
146     /**
147      * Returns the list of announce
148      *
149      * @param request
150      *            The HTTP request
151      * @return the announces list
152      */
153     public String getManageAnnounces( HttpServletRequest request )
154     {
155         setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_ANNOUNCES );
156 
157         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
158 
159         if ( _nDefaultItemsPerPage == 0 )
160         {
161             _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_ANNOUNCE_PER_PAGE, 50 );
162         }
163 
164         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
165 
166         String strSort = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
167         boolean bSortAsc = Boolean.parseBoolean( request.getParameter( Parameters.SORTED_ASC ) );
168         AnnounceSort announceSort;
169 
170         if ( strSort == null )
171         {
172             announceSort = (AnnounceSort) request.getSession( ).getAttribute( SESSION_SORT );
173 
174             if ( announceSort == null )
175             {
176                 announceSort = AnnounceSort.DEFAULT_SORT;
177             }
178         }
179         else
180         {
181             announceSort = AnnounceSort.getAnnounceSort( strSort, bSortAsc );
182             request.getSession( ).setAttribute( SESSION_SORT, announceSort );
183         }
184 
185         List<Integer> listIdAnnounces = AnnounceHome.findAll( announceSort );
186 
187         Paginator<Integer> paginatorId = new Paginator<>( listIdAnnounces, _nItemsPerPage, StringUtils.EMPTY, AbstractPaginator.PARAMETER_PAGE_INDEX,
188                 _strCurrentPageIndex );
189 
190         User user = getUser( );
191         List<Announce> listAnnounces = AnnounceHome.findByListId( paginatorId.getPageItems( ), announceSort );
192         boolean bCanExecuteWorkflowAction = false;
193 
194         if ( WorkflowService.getInstance( ).isAvailable( ) )
195         {
196             bCanExecuteWorkflowAction = RBACService.isAuthorized( Announce.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
197                     AnnounceResourceIdService.PERMISSION_EXECUTE_WORKFLOW_ACTION, user );
198 
199             for ( Announce announce : listAnnounces )
200             {
201                 announce.setCategory( CategoryHome.findByPrimaryKey( announce.getCategory( ).getId( ) ) );
202 
203                 if ( bCanExecuteWorkflowAction )
204                 {
205                     announce.setListWorkflowActions( WorkflowService.getInstance( ).getActions( announce.getId( ), Announce.RESOURCE_TYPE,
206                             announce.getCategory( ).getIdWorkflow( ), user ) );
207                 }
208             }
209         }
210 
211         Map<String, Object> model = new HashMap<>( );
212 
213         LocalizedDelegatePaginator<Announce> paginator = new LocalizedDelegatePaginator<>( listAnnounces, _nItemsPerPage, getURLManageAnnounces( request ),
214                 AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, listIdAnnounces.size( ), getLocale( ) );
215 
216         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
217         model.put( MARK_PAGINATOR, paginator );
218         model.put( MARK_ANNOUNCE_LIST, paginator.getPageItems( ) );
219 
220         model.put( MARK_RIGHT_DELETE,
221                 RBACService.isAuthorized( Announce.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, AnnounceResourceIdService.PERMISSION_DELETE, user ) );
222         model.put( MARK_RIGHT_PUBLISH,
223                 RBACService.isAuthorized( Announce.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, AnnounceResourceIdService.PERMISSION_PUBLISH, user ) );
224         model.put( MARK_RIGHT_SUSPEND,
225                 RBACService.isAuthorized( Announce.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, AnnounceResourceIdService.PERMISSION_SUSPEND, user ) );
226         model.put( MARK_RIGHT_WORKFLOW_ACTION, bCanExecuteWorkflowAction );
227 
228         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_ANNOUNCES, getLocale( ), model );
229 
230         return getAdminPage( templateList.getHtml( ) );
231     }
232 
233     /**
234      * Get the page to display the preview of an announce
235      * 
236      * @param request
237      *            The request
238      * @return The HTML to display
239      */
240     public String getPreviewAnnounce( HttpServletRequest request )
241     {
242         setPageTitleProperty( PROPERTY_PAGE_TITLE_PREVIEW_ANNOUNCE );
243 
244         int nIdAnnounce = Integer.parseInt( request.getParameter( PARAMETER_ANNOUNCE_ID ) );
245         Announce announce = AnnounceHome.findByPrimaryKey( nIdAnnounce );
246         Collection<Entry> listGeolocalisation = new ArrayList<>( );
247         User user = getUser( );
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( ), user ) );
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         User user = getUser( );
315 
316         if ( !RBACService.isAuthorized( Announce.RESOURCE_TYPE, strAnnounceId, AnnounceResourceIdService.PERMISSION_DELETE, user ) )
317         {
318             throw new AccessDeniedException(
319                     "User '" + user.getFirstName( ) + " " + user.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         User user = getUser( );
343 
344         if ( !RBACService.isAuthorized( Announce.RESOURCE_TYPE, strAnnounceId, AnnounceResourceIdService.PERMISSION_DELETE, user ) )
345         {
346             throw new AccessDeniedException(
347                     "User '" + user.getFirstName( ) + " " + user.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         User user = getUser( );
373 
374         if ( !RBACService.isAuthorized( Announce.RESOURCE_TYPE, strAnnounceId, AnnounceResourceIdService.PERMISSION_PUBLISH, user ) )
375         {
376             throw new AccessDeniedException( "User '" + user.getFirstName( ) + " " + user.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         User user = getUser( );
411 
412         if ( !RBACService.isAuthorized( Announce.RESOURCE_TYPE, strAnnounceId, AnnounceResourceIdService.PERMISSION_SUSPEND, user ) )
413         {
414             throw new AccessDeniedException(
415                     "User '" + user.getFirstName( ) + " " + user.getLastName( ) + "' 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         User user = getUser( );
440 
441         if ( !RBACService.isAuthorized( Announce.RESOURCE_TYPE, strAnnounceId, AnnounceResourceIdService.PERMISSION_SUSPEND, user ) )
442         {
443             throw new AccessDeniedException(
444                     "User '" + user.getFirstName( ) + " " + user.getLastName( ) + "' 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 }