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.appointment.web;
35  
36  import java.time.LocalDate;
37  import java.time.LocalTime;
38  import java.time.ZoneId;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import fr.paris.lutece.api.user.User;
47  import fr.paris.lutece.plugins.appointment.business.comment.Comment;
48  import fr.paris.lutece.plugins.appointment.business.comment.CommentHome;
49  import fr.paris.lutece.plugins.appointment.service.AppointmentResourceIdService;
50  import fr.paris.lutece.plugins.appointment.service.CommentService;
51  import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFormDTO;
52  import fr.paris.lutece.plugins.appointment.web.dto.CommentDTO;
53  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
54  import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
55  import fr.paris.lutece.portal.service.message.AdminMessage;
56  import fr.paris.lutece.portal.service.message.AdminMessageService;
57  import fr.paris.lutece.portal.service.rbac.RBACService;
58  import fr.paris.lutece.portal.service.template.AppTemplateService;
59  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
60  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
61  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
62  import fr.paris.lutece.util.date.DateUtil;
63  import fr.paris.lutece.util.html.HtmlTemplate;
64  import fr.paris.lutece.util.url.UrlItem;
65  
66  /**
67   * This class provides the user interface to manage CommentForm features ( manage, create, modify, copy, remove )
68   * 
69   * @author rdeniel
70   * 
71   */
72  @Controller( controllerJsp = CommentJspBean.JSP_MANAGE_COMMENTS, controllerPath = "jsp/admin/plugins/appointment/", right = CommentJspBean.RIGHT_MANAGECOMMENTTFORM )
73  public class CommentJspBean extends AbstractAppointmentFormAndSlotJspBean
74  {
75      /**
76       * 
77       */
78      private static final long serialVersionUID = 9120042889405463752L;
79      public static final String RIGHT_MANAGECOMMENTTFORM = "APPOINTMENT_COMMENT_MANAGEMENT";
80      /**
81       * JSP of this JSP Bean
82       */
83      public static final String JSP_MANAGE_COMMENTS = "Comments.jsp";
84  
85      // Templates
86      public static final String TEMPLATE_CREATE_COMMENT = "/admin/plugins/appointment/comment/create_comment.html";
87      public static final String TEMPLATE_MANAGE_COMMENT = "/admin/plugins/appointment/comment/manage_comment.html";
88      public static final String TEMPLATE_MODIFY_COMMENT = "/admin/plugins/appointment/comment/modify_comment.html";
89      public static final String TEMPLATE_COMMENT_INFO = "/admin/plugins/appointment/comment/comment_infos.html";
90  
91      // Messages
92      private static final String MESSAGE_COMMENT_PAGE_TITLE = "appointment.comment.pageTitle";
93      private static final String VALIDATION_ATTRIBUTES_PREFIX = "appointment.model.entity.appointmentform.attribute";
94  
95      // Parameters
96      private static final String PARAMETER_ID_COMMENT = "id_comment";
97      private static final String PARAMETER_COMMENT = "comment";
98      private static final String PARAMETER_STARTING_VALIDITY_DATE = "startingValidityDate";
99      private static final String PARAMETER_ENDING_VALIDITY_DATE = "endingValidityDate";
100     private static final String PARAMETER_STARTING_VALIDITY_TIME = "startingValidityTime";
101     private static final String PARAMETER_ENDING_VALIDITY_TIME = "endingValidityTime";
102     private static final String PARAMETER_ID_FORM = "id_form";
103     private static final String REFERER = "referer";
104     private static final String PARAMETER_ID_MAILING_LIST = "idMailingList";
105 
106     // Marks
107     private static final String MARK_COMMENT = "comment";
108     private static final String MARK_COMMENT_LIST = "comment_list";
109     private static final String MARK_LOCALE = "locale";
110     private static final String MARK_MAILING_LIST = "mailing_list";
111 
112     // Views
113     private static final String VIEW_ADD_COMMENT = "viewAddComment";
114     private static final String VIEW_MODIFY_COMMENT = "viewModifyComment";
115     private static final String VIEW_MANAGE_COMMENT = "manageComment";
116 
117     // Actions
118     private static final String ACTION_DO_ADD_COMMENT = "doAddComment";
119     private static final String ACTION_DO_REMOVE_COMMENT = "doRemoveComment";
120     private static final String ACTION_DO_MODIFY_COMMENT = "doModifyComment";
121     private static final String ACTION_CONFIRM_REMOVE_COMMENT = "confirmRemoveComment";
122 
123     // Properties
124     private static final String PROPERTY_PAGE_TITLE_MANAGE_COMMENTS = "appointment.manage_comments.pageTitle";
125     private static final String MESSAGE_CONFIRM_REMOVE_COMMENT = "appointment.message.confirmRemoveComment";
126 
127     // Infos
128     private static final String INFO_COMMENT_CREATED = "appointment.info.comment.created";
129     private static final String INFO_COMMENT_UPDATED = "appointment.info.comment.updated";
130     private static final String INFO_COMMENT_REMOVED = "appointment.info.comment.removed";
131     private static final String INFO_COMMENT_ERROR = "appointment.info.comment.error";
132 
133     // Session variable to store working values
134     private Comment _comment;
135 
136     /**
137      * Build the Manage View
138      * 
139      * @param request
140      *            The HTTP request
141      * @return The page
142      * @throws AccessDeniedException
143      */
144     @View( value = VIEW_MANAGE_COMMENT, defaultView = true )
145     public String getManageComment( HttpServletRequest request )
146     {
147         _comment = null;
148         List<CommentDTO> listComments = CommentService.buildCommentDTO( CommentHome.getCommentsList( ) );
149         Map<String, Object> model = getPaginatedListModel( request, MARK_COMMENT_LIST, listComments, JSP_MANAGE_COMMENTS );
150 
151         return getPage( PROPERTY_PAGE_TITLE_MANAGE_COMMENTS, TEMPLATE_MANAGE_COMMENT, model );
152     }
153 
154     /**
155      * Returns the form to create a comment
156      *
157      * @param request
158      *            The Http request
159      * @return the html code of the comment form
160      * @throws AccessDeniedException
161      */
162     @View( VIEW_ADD_COMMENT )
163     public String getViewAddComment( HttpServletRequest request ) throws AccessDeniedException
164     {
165         String strIdForm = request.getParameter( PARAMETER_ID_FORM );
166         int nIdForm = Integer.parseInt( strIdForm );
167 
168         if ( !RBACService.isAuthorized( AppointmentFormDTO.RESOURCE_TYPE, strIdForm, AppointmentResourceIdService.PERMISSION_ADD_COMMENT_FORM,
169                 (User) getUser( ) ) )
170         {
171             throw new AccessDeniedException( AppointmentResourceIdService.PERMISSION_ADD_COMMENT_FORM );
172         }
173         _comment = new Comment( );
174 
175         Map<String, Object> model = getModel( );
176         model.put( MARK_COMMENT, _comment );
177         model.put( MARK_LOCALE, getLocale( ) );
178         model.put( PARAMETER_ID_FORM, nIdForm );
179         model.put( MARK_MAILING_LIST, AdminMailingListService.getMailingLists( getUser( ) ) );
180 
181         return getPage( MESSAGE_COMMENT_PAGE_TITLE, TEMPLATE_CREATE_COMMENT, model );
182 
183     }
184 
185     /**
186      * Process the data capture form of a new comment
187      *
188      * @param request
189      *            The Http Request
190      * @return The Jsp URL of the process result
191      * @throws AccessDeniedException
192      */
193     @Action( ACTION_DO_ADD_COMMENT )
194     public String doAddComment( HttpServletRequest request ) throws AccessDeniedException
195     {
196         User user = getUser( );
197         String strIdForm = request.getParameter( PARAMETER_ID_FORM );
198         int nIdForm = Integer.parseInt( strIdForm );
199         String strReferer = request.getHeader( REFERER );
200         int nIdMailingList = Integer.parseInt( request.getParameter( PARAMETER_ID_MAILING_LIST ) );
201 
202         if ( !RBACService.isAuthorized( AppointmentFormDTO.RESOURCE_TYPE, strIdForm, AppointmentResourceIdService.PERMISSION_ADD_COMMENT_FORM,
203                 (User) getUser( ) ) )
204         {
205             throw new AccessDeniedException( AppointmentResourceIdService.PERMISSION_ADD_COMMENT_FORM );
206         }
207         _comment = ( _comment == null ) ? new Comment( ) : _comment;
208 
209         _comment.setIdForm( nIdForm );
210         _comment.setCreationDate( LocalDate.now( ) );
211         _comment.setCreatorUserName( user.getAccessCode( ) );
212         _comment.setComment( request.getParameter( PARAMETER_COMMENT ) );
213         _comment.setStartingValidityDate( DateUtil.formatDate( request.getParameter( PARAMETER_STARTING_VALIDITY_DATE ), getLocale( ) ).toInstant( )
214                 .atZone( ZoneId.systemDefault( ) ).toLocalDate( ) );
215         _comment.setEndingValidityDate( DateUtil.formatDate( request.getParameter( PARAMETER_ENDING_VALIDITY_DATE ), getLocale( ) ).toInstant( )
216                 .atZone( ZoneId.systemDefault( ) ).toLocalDate( ) );
217         if ( !request.getParameter( PARAMETER_STARTING_VALIDITY_TIME ).isEmpty( ) )
218         {
219             _comment.setStartingValidityTime( LocalTime.parse( request.getParameter( PARAMETER_STARTING_VALIDITY_TIME ) ) );
220         }
221 
222         if ( !request.getParameter( PARAMETER_ENDING_VALIDITY_TIME ).isEmpty( ) )
223         {
224             _comment.setEndingValidityTime( LocalTime.parse( request.getParameter( PARAMETER_ENDING_VALIDITY_TIME ) ) );
225         }
226 
227         // Check constraints
228         if ( !validateBean( _comment, VALIDATION_ATTRIBUTES_PREFIX ) || !validateDateStartEndValidity( _comment ) )
229         {
230             addError( INFO_COMMENT_ERROR, getLocale( ) );
231 
232         }
233         else
234         {
235             CommentService.createAndNotifyMailingList( _comment, nIdMailingList, getLocale( ) );
236             addInfo( INFO_COMMENT_CREATED, getLocale( ) );
237         }
238 
239         if ( StringUtils.isNotBlank( strReferer ) )
240         {
241             return redirect( request, strReferer );
242         }
243 
244         return redirect( request, VIEW_MANAGE_COMMENT );
245     }
246 
247     /**
248      * Returns the form to modify a comment
249      *
250      * @param request
251      *            The Http request
252      * @return the html code of the comment form
253      * @throws AccessDeniedException
254      */
255     @View( VIEW_MODIFY_COMMENT )
256     public String getViewModifyComment( HttpServletRequest request ) throws AccessDeniedException
257     {
258         User user = getUser( );
259         int nIdComment = Integer.parseInt( request.getParameter( PARAMETER_ID_COMMENT ) );
260         _comment = CommentHome.findByPrimaryKey( nIdComment );
261         if ( !RBACService.isAuthorized( AppointmentFormDTO.RESOURCE_TYPE, Integer.toString( _comment.getIdForm( ) ),
262                 AppointmentResourceIdService.PERMISSION_MODERATE_COMMENT_FORM, (User) getUser( ) )
263                 && !_comment.getCreatorUserName( ).equals( user.getAccessCode( ) ) )
264         {
265             throw new AccessDeniedException( AppointmentResourceIdService.PERMISSION_MODERATE_COMMENT_FORM );
266 
267         }
268         Map<String, Object> model = getModel( );
269         model.put( MARK_COMMENT, _comment );
270         model.put( MARK_LOCALE, getLocale( ) );
271         model.put( PARAMETER_ID_FORM, _comment.getIdForm( ) );
272         model.put( MARK_MAILING_LIST, AdminMailingListService.getMailingLists( getUser( ) ) );
273 
274         return getPage( MESSAGE_COMMENT_PAGE_TITLE, TEMPLATE_MODIFY_COMMENT, model );
275 
276     }
277 
278     /**
279      * Process the data capture form of comment modification
280      *
281      * @param request
282      *            The Http Request
283      * @return The Jsp URL of the process result
284      * @throws AccessDeniedException
285      */
286     @Action( ACTION_DO_MODIFY_COMMENT )
287     public String doModifyComment( HttpServletRequest request ) throws AccessDeniedException
288     {
289         User user = getUser( );
290         int nIdComment = Integer.parseInt( request.getParameter( PARAMETER_ID_COMMENT ) );
291         String strReferer = request.getHeader( REFERER );
292         int nIdMailingList = Integer.parseInt( request.getParameter( PARAMETER_ID_MAILING_LIST ) );
293 
294         if ( _comment == null || _comment.getId( ) != nIdComment )
295         {
296             _comment = CommentHome.findByPrimaryKey( nIdComment );
297         }
298         if ( !RBACService.isAuthorized( AppointmentFormDTO.RESOURCE_TYPE, Integer.toString( _comment.getIdForm( ) ),
299                 AppointmentResourceIdService.PERMISSION_MODERATE_COMMENT_FORM, (User) getUser( ) )
300                 && !_comment.getCreatorUserName( ).equals( user.getAccessCode( ) ) )
301         {
302             throw new AccessDeniedException( AppointmentResourceIdService.PERMISSION_MODERATE_COMMENT_FORM );
303         }
304         _comment.setComment( request.getParameter( PARAMETER_COMMENT ) );
305         _comment.setStartingValidityDate( DateUtil.formatDate( request.getParameter( PARAMETER_STARTING_VALIDITY_DATE ), getLocale( ) ).toInstant( )
306                 .atZone( ZoneId.systemDefault( ) ).toLocalDate( ) );
307         _comment.setEndingValidityDate( DateUtil.formatDate( request.getParameter( PARAMETER_ENDING_VALIDITY_DATE ), getLocale( ) ).toInstant( )
308                 .atZone( ZoneId.systemDefault( ) ).toLocalDate( ) );
309         if ( !request.getParameter( PARAMETER_STARTING_VALIDITY_TIME ).isEmpty( ) )
310         {
311             _comment.setStartingValidityTime( LocalTime.parse( request.getParameter( PARAMETER_STARTING_VALIDITY_TIME ) ) );
312         }
313 
314         if ( !request.getParameter( PARAMETER_ENDING_VALIDITY_TIME ).isEmpty( ) )
315         {
316             _comment.setEndingValidityTime( LocalTime.parse( request.getParameter( PARAMETER_ENDING_VALIDITY_TIME ) ) );
317         }
318 
319         // Check constraints
320         if ( !validateBean( _comment, VALIDATION_ATTRIBUTES_PREFIX ) || !validateDateStartEndValidity( _comment ) )
321         {
322             addError( INFO_COMMENT_ERROR, getLocale( ) );
323         }
324         else
325         {
326             CommentService.updateAndNotifyMailingList( _comment, nIdMailingList, getLocale( ) );
327             addInfo( INFO_COMMENT_UPDATED, getLocale( ) );
328         }
329 
330         if ( StringUtils.isNotBlank( strReferer ) )
331         {
332             return redirect( request, strReferer );
333         }
334 
335         return redirect( request, VIEW_MANAGE_COMMENT );
336 
337     }
338 
339     /**
340      * Manages the removal form of a comment whose identifier is in the http request
341      *
342      * @param request
343      *            The Http request
344      * @return the html code to confirm
345      * @throws AccessDeniedException
346      */
347     @Action( ACTION_CONFIRM_REMOVE_COMMENT )
348     public String getConfirmRemoveComment( HttpServletRequest request ) throws AccessDeniedException
349     {
350         User user = getUser( );
351         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_COMMENT ) );
352         _comment = CommentHome.findByPrimaryKey( nId );
353         if ( !RBACService.isAuthorized( AppointmentFormDTO.RESOURCE_TYPE, Integer.toString( _comment.getIdForm( ) ),
354                 AppointmentResourceIdService.PERMISSION_MODERATE_COMMENT_FORM, (User) getUser( ) )
355                 && !_comment.getCreatorUserName( ).equals( user.getAccessCode( ) ) )
356         {
357             throw new AccessDeniedException( AppointmentResourceIdService.PERMISSION_MODERATE_COMMENT_FORM );
358         }
359         UrlItem url = new UrlItem( getActionUrl( ACTION_DO_REMOVE_COMMENT ) );
360         url.addParameter( PARAMETER_ID_COMMENT, nId );
361         url.addParameter( PARAMETER_ID_MAILING_LIST, request.getParameter( PARAMETER_ID_MAILING_LIST ) );
362         url.addParameter( REFERER, request.getHeader( REFERER ) );
363 
364         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_COMMENT, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
365 
366         return redirect( request, strMessageUrl );
367     }
368 
369     /**
370      * Do remove a comment
371      * 
372      * @param request
373      *            the request
374      * @return to the page of the comment
375      * @throws AccessDeniedException
376      */
377     @Action( ACTION_DO_REMOVE_COMMENT )
378     public String doRemoveComment( HttpServletRequest request ) throws AccessDeniedException
379     {
380         User user = getUser( );
381         int nIdComment = Integer.parseInt( request.getParameter( PARAMETER_ID_COMMENT ) );
382         String strReferer = request.getParameter( REFERER );
383         int nIdMailingList = Integer.parseInt( request.getParameter( PARAMETER_ID_MAILING_LIST ) );
384 
385         UrlItem url = new UrlItem( strReferer );
386         url.addParameter( PARAMETER_ID_FORM, _comment.getIdForm( ) );
387 
388         _comment = CommentHome.findByPrimaryKey( nIdComment );
389         if ( !RBACService.isAuthorized( AppointmentFormDTO.RESOURCE_TYPE, Integer.toString( _comment.getIdForm( ) ),
390                 AppointmentResourceIdService.PERMISSION_MODERATE_COMMENT_FORM, (User) getUser( ) )
391                 && !_comment.getCreatorUserName( ).equals( user.getAccessCode( ) ) )
392         {
393             throw new AccessDeniedException( AppointmentResourceIdService.PERMISSION_MODERATE_COMMENT_FORM );
394         }
395 
396         CommentService.removeAndNotifyMailingList( nIdComment, nIdMailingList, getLocale( ) );
397         addInfo( INFO_COMMENT_REMOVED, getLocale( ) );
398         if ( StringUtils.isNotBlank( strReferer ) )
399         {
400             return redirect( request, url.getUrl( ) );
401         }
402 
403         return redirect( request, VIEW_MANAGE_COMMENT );
404     }
405 
406     /**
407      * build The infos/warnings/Errors
408      * 
409      * @return The infos/warnings/Errors
410      */
411     public String getCommentInfos( )
412     {
413 
414         Map<String, Object> model = getModel( );
415         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_COMMENT_INFO, getLocale( ), model );
416 
417         return template.getHtml( );
418     }
419 
420     /**
421      * Validate Comment date
422      * 
423      * @param comment
424      *            the comment
425      * @return boolean
426      */
427     public boolean validateDateStartEndValidity( Comment comment )
428     {
429         return !( comment.getStartingValidityDate( ).isAfter( comment.getEndingValidityDate( ) )
430                 || ( comment.getStartingValidityDate( ).isEqual( comment.getEndingValidityDate( ) ) && comment.getStartingValidityTime( ) != null
431                         && comment.getStartingValidityTime( ).isAfter( comment.getEndingValidityTime( ) ) ) );
432 
433     }
434 }