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.workflow.modules.comment.web;
35  
36  import fr.paris.lutece.api.user.User;
37  import fr.paris.lutece.plugins.workflow.modules.comment.business.CommentValue;
38  import fr.paris.lutece.plugins.workflow.modules.comment.service.CommentResourceIdService;
39  import fr.paris.lutece.plugins.workflow.modules.comment.service.CommentValueService;
40  import fr.paris.lutece.plugins.workflow.modules.comment.service.ICommentValueService;
41  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
42  import fr.paris.lutece.plugins.workflow.web.task.TaskComponentManager;
43  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
44  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
45  import fr.paris.lutece.plugins.workflowcore.service.resource.ResourceHistoryService;
46  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
47  import fr.paris.lutece.plugins.workflowcore.service.task.ITaskService;
48  import fr.paris.lutece.plugins.workflowcore.service.task.TaskService;
49  import fr.paris.lutece.plugins.workflowcore.web.task.ITaskComponentManager;
50  import fr.paris.lutece.portal.business.user.AdminUser;
51  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
52  import fr.paris.lutece.portal.service.admin.AdminUserService;
53  import fr.paris.lutece.portal.service.message.AdminMessage;
54  import fr.paris.lutece.portal.service.message.AdminMessageService;
55  import fr.paris.lutece.portal.service.rbac.RBACService;
56  import fr.paris.lutece.portal.service.spring.SpringContextService;
57  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
58  import fr.paris.lutece.util.url.UrlItem;
59  
60  import java.io.UnsupportedEncodingException;
61  import java.net.URLDecoder;
62  import java.net.URLEncoder;
63  import java.util.Iterator;
64  import java.util.List;
65  
66  import javax.servlet.http.HttpServletRequest;
67  
68  import org.apache.commons.lang3.StringUtils;
69  
70  /**
71   * Controller for Comments
72   *
73   */
74  public class CommentJspBean extends MVCAdminJspBean
75  {
76      /**
77       * Generated serial id
78       */
79      private static final long serialVersionUID = 5300419950066235152L;
80  
81      // Parameters
82      private static final String PARAMETER_ID_HISTORY = "id_history";
83      private static final String PARAMETER_ID_TASK = "id_task";
84      private static final String PARAMETER_RETURN_URL = "return_url";
85  
86      // JSPs
87      private static final String JSP_DO_REMOVE_COMMENT = "jsp/admin/plugins/workflow/modules/comment/DoRemoveComment.jsp";
88  
89      // Messages
90      private static final String MESSAGE_CONFIRM_REMOVE_COMMENT = "module.workflow.comment.message.confirm_remove_comment";
91  
92      // Other constants
93      private static final String PARAMETER_ENCODING = "UTF-8";
94  
95      // Services
96      private ICommentValueService _commentValueService = SpringContextService.getBean( CommentValueService.BEAN_SERVICE );
97      private IResourceHistoryService _resourceHistoryService = SpringContextService.getBean( ResourceHistoryService.BEAN_SERVICE );
98      private ITaskService _taskService = SpringContextService.getBean( TaskService.BEAN_SERVICE );
99      private ITaskComponentManager _taskComponentManager = SpringContextService.getBean( TaskComponentManager.BEAN_MANAGER );
100 
101     /**
102      * Gets the confirmation page to remove a comment
103      *
104      * @param request
105      *            The HTTP request
106      * @throws AccessDeniedException
107      *             the {@link AccessDeniedException}
108      * @return the confirmation page to remove the comment
109      * @throws UnsupportedEncodingException
110      *             if there is an exception during the encoding of the return_url parameter
111      */
112     public String getConfirmRemoveComment( HttpServletRequest request ) throws AccessDeniedException, UnsupportedEncodingException
113     {
114         if ( !canDeleteComment( request ) )
115         {
116             throw new AccessDeniedException( "The connected user is not allowed to delete this comment" );
117         }
118 
119         String strIdHistory = request.getParameter( PARAMETER_ID_HISTORY );
120         String strIdTask = request.getParameter( PARAMETER_ID_TASK );
121         String strReturnUrl = request.getParameter( PARAMETER_RETURN_URL );
122 
123         UrlItem url = new UrlItem( JSP_DO_REMOVE_COMMENT );
124         url.addParameter( PARAMETER_ID_HISTORY, strIdHistory );
125         url.addParameter( PARAMETER_ID_TASK, strIdTask );
126         url.addParameter( PARAMETER_RETURN_URL, URLEncoder.encode( strReturnUrl, PARAMETER_ENCODING ) );
127 
128         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_COMMENT, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
129     }
130 
131     /**
132      * Removes a comment
133      *
134      * @param request
135      *            The HTTP request
136      * @throws AccessDeniedException
137      *             the {@link AccessDeniedException}
138      * @return The URL to go after performing the action
139      * @throws UnsupportedEncodingException
140      *             if there is an exception during the decoding of the return_url parameter
141      */
142     public String doRemoveComment( HttpServletRequest request ) throws AccessDeniedException, UnsupportedEncodingException
143     {
144         if ( !canDeleteComment( request ) )
145         {
146             throw new AccessDeniedException( "The connected user is not allowed to delete this comment" );
147         }
148 
149         String strIdHistory = request.getParameter( PARAMETER_ID_HISTORY );
150         int nIdHistory = WorkflowUtils.convertStringToInt( strIdHistory );
151         String strIdTask = request.getParameter( PARAMETER_ID_TASK );
152         int nIdTask = WorkflowUtils.convertStringToInt( strIdTask );
153 
154         _commentValueService.removeByHistory( nIdHistory, nIdTask, WorkflowUtils.getPlugin( ) );
155 
156         // Remove history if no other task information to display
157         ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdHistory );
158         List<ITask> listActionTasks = _taskService.getListTaskByIdAction( resourceHistory.getAction( ).getId( ), request.getLocale( ) );
159 
160         Iterator<ITask> iterator = listActionTasks.iterator( );
161         boolean informationToDisplay = false;
162         while ( iterator.hasNext( ) )
163         {
164             ITask task = iterator.next( );
165 
166             String strTaskinformation = _taskComponentManager.getDisplayTaskInformation( resourceHistory.getId( ), request, request.getLocale( ), task );
167             if ( !StringUtils.isEmpty( strTaskinformation ) )
168             {
169                 informationToDisplay = true;
170                 break;
171             }
172         }
173 
174         if ( !informationToDisplay )
175         {
176             // Does the action resource should really be deleted if no information is displayed in history ?
177             for ( ITask actionTask : listActionTasks )
178             {
179                 actionTask.doRemoveTaskInformation( nIdHistory );
180             }
181             _resourceHistoryService.remove( nIdHistory );
182         }
183 
184         return URLDecoder.decode( request.getParameter( PARAMETER_RETURN_URL ), PARAMETER_ENCODING );
185     }
186 
187     /**
188      * Tests whether the comment can be delete or not
189      * 
190      * @param request
191      *            the request
192      * @return {@code true} if the comment can be deleted, {@code false} otherwise
193      */
194     private boolean canDeleteComment( HttpServletRequest request )
195     {
196         String strIdHistory = request.getParameter( PARAMETER_ID_HISTORY );
197         int nIdHistory = WorkflowUtils.convertStringToInt( strIdHistory );
198         String strIdTask = request.getParameter( PARAMETER_ID_TASK );
199         int nIdTask = WorkflowUtils.convertStringToInt( strIdTask );
200         AdminUser userConnected = AdminUserService.getAdminUser( request );
201 
202         CommentValue commentValue = _commentValueService.findByPrimaryKey( nIdHistory, nIdTask, WorkflowUtils.getPlugin( ) );
203 
204         boolean bHasPermissionDeletion = RBACService.isAuthorized( commentValue, CommentResourceIdService.PERMISSION_DELETE, (User) userConnected );
205         boolean bIsOwner = _commentValueService.isOwner( nIdHistory, userConnected );
206 
207         return bHasPermissionDeletion || bIsOwner;
208     }
209 }