1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.extend.modules.comment.web;
35
36 import java.sql.Timestamp;
37 import java.util.Date;
38 import java.util.HashMap;
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.plugins.extend.business.extender.ResourceExtenderDTO;
47 import fr.paris.lutece.plugins.extend.business.extender.ResourceExtenderDTOFilter;
48 import fr.paris.lutece.plugins.extend.modules.comment.business.Comment;
49 import fr.paris.lutece.plugins.extend.modules.comment.service.CommentPlugin;
50 import fr.paris.lutece.plugins.extend.modules.comment.service.CommentService;
51 import fr.paris.lutece.plugins.extend.modules.comment.service.ICommentService;
52 import fr.paris.lutece.plugins.extend.modules.comment.service.extender.CommentResourceExtender;
53 import fr.paris.lutece.plugins.extend.modules.comment.util.constants.CommentConstants;
54 import fr.paris.lutece.plugins.extend.service.extender.IResourceExtenderService;
55 import fr.paris.lutece.plugins.extend.service.extender.ResourceExtenderService;
56 import fr.paris.lutece.plugins.extend.service.extender.config.IResourceExtenderConfigService;
57 import fr.paris.lutece.plugins.extend.service.extender.history.IResourceExtenderHistoryService;
58 import fr.paris.lutece.plugins.extend.service.extender.history.ResourceExtenderHistoryService;
59 import fr.paris.lutece.portal.business.user.AdminUser;
60 import fr.paris.lutece.portal.service.admin.AdminUserService;
61 import fr.paris.lutece.portal.service.message.AdminMessage;
62 import fr.paris.lutece.portal.service.message.AdminMessageService;
63 import fr.paris.lutece.portal.service.spring.SpringContextService;
64 import fr.paris.lutece.portal.service.template.AppTemplateService;
65 import fr.paris.lutece.portal.service.util.AppException;
66 import fr.paris.lutece.portal.service.util.AppLogService;
67 import fr.paris.lutece.portal.service.util.AppPathService;
68 import fr.paris.lutece.portal.service.workflow.WorkflowService;
69 import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
70 import fr.paris.lutece.portal.web.constants.Messages;
71 import fr.paris.lutece.util.html.HtmlTemplate;
72 import fr.paris.lutece.util.http.SecurityUtil;
73 import fr.paris.lutece.util.url.UrlItem;
74
75
76
77
78
79
80 public class CommentJspBean extends PluginAdminPageJspBean
81 {
82
83
84
85 private static final long serialVersionUID = -1787559203779077088L;
86
87
88 private static final String JSP_VIEW_EXTENDER_INFO = "../../ViewExtenderInfo.jsp";
89 private static final String JSP_URL_DO_REMOVE_COMMENT = "jsp/admin/plugins/extend/modules/comment/DoRemoveComment.jsp";
90 private static final String JSP_VIEW_TASKS_FORM = "../comment/GetTasksFormWorkflow.jsp";
91
92
93 private static final String MESSAGE_MANDATORY_FIELD = "portal.util.message.mandatoryField";
94 private static final String MESSAGE_TITLE_CREATE_COMMENT = "module.extend.comment.create_comment.pageTitle";
95
96
97 private static final String TEMPLATE_CREATE_COMMENT = "admin/plugins/extend/modules/comment/create_comment.html";
98 private static final String TEMPLATE_TASKS_FORM_WORKFLOW = "admin/plugins/extend/modules/comment/tasks_form_workflow.html";
99
100
101 private static final String MARK_TASK_FORM = "tasks_form";
102
103
104 private static final String CONSTANT_SPACE = " ";
105
106 private ICommentService _commentService = SpringContextService.getBean( CommentService.BEAN_SERVICE );
107 private IResourceExtenderHistoryService _resourceHistoryService = SpringContextService.getBean( ResourceExtenderHistoryService.BEAN_SERVICE );
108
109 private IResourceExtenderService _resourceExtenderService = SpringContextService.getBean( ResourceExtenderService.BEAN_SERVICE );
110
111
112
113
114
115
116
117
118 public String doPublishUnpublishComment( HttpServletRequest request )
119 {
120 String strIdComment = request.getParameter( CommentConstants.PARAMETER_ID_COMMENT );
121
122 if ( StringUtils.isNotBlank( strIdComment ) && StringUtils.isNumeric( strIdComment ) )
123 {
124 int nIdComment = Integer.parseInt( strIdComment );
125 Comment comment = _commentService.findByPrimaryKey( nIdComment );
126
127 if ( comment != null )
128 {
129 try
130 {
131 _commentService.updateCommentStatus( comment.getIdComment( ), !comment.isPublished( ) );
132 }
133 catch( Exception ex )
134 {
135
136 AppLogService.error( ex.getMessage( ) + " when updating a comment", ex );
137
138 return AdminMessageService.getMessageUrl( request, CommentConstants.MESSAGE_ERROR_GENERIC_MESSAGE, AdminMessage.TYPE_ERROR );
139 }
140
141 return getPostBackUrl( request, comment );
142 }
143 }
144
145 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
146 }
147
148
149
150
151
152
153
154
155
156
157 public String doFlagImportantComment( HttpServletRequest request, boolean cancelFlag )
158 {
159 String strIdComment = request.getParameter( CommentConstants.PARAMETER_ID_COMMENT );
160
161 if ( StringUtils.isNotBlank( strIdComment ) && StringUtils.isNumeric( strIdComment ) )
162 {
163 int nIdComment = Integer.parseInt( strIdComment );
164 Comment comment = _commentService.findByPrimaryKey( nIdComment );
165
166 if ( comment != null )
167 {
168 try
169 {
170
171 _commentService.updateFlagImportant( comment.getIdComment( ), !cancelFlag );
172 }
173 catch( Exception ex )
174 {
175
176 AppLogService.error( ex.getMessage( ) + " when updating a comment", ex );
177
178 return AdminMessageService.getMessageUrl( request, CommentConstants.MESSAGE_ERROR_GENERIC_MESSAGE, AdminMessage.TYPE_ERROR );
179 }
180
181 return getPostBackUrl( request, comment );
182 }
183 }
184
185 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
186 }
187
188
189
190
191
192
193
194
195
196
197 public String doPinned( HttpServletRequest request, boolean cancelPinned )
198 {
199 String strIdComment = request.getParameter( CommentConstants.PARAMETER_ID_COMMENT );
200
201 if ( StringUtils.isNotBlank( strIdComment ) && StringUtils.isNumeric( strIdComment ) )
202 {
203 int nIdComment = Integer.parseInt( strIdComment );
204 Comment comment = _commentService.findByPrimaryKey( nIdComment );
205
206 if ( comment != null )
207 {
208 try
209 {
210 _commentService.updateCommentPinned( nIdComment, !cancelPinned );
211 }
212 catch( Exception ex )
213 {
214
215 AppLogService.error( ex.getMessage( ) + " when updating a comment", ex );
216
217 return AdminMessageService.getMessageUrl( request, CommentConstants.MESSAGE_ERROR_GENERIC_MESSAGE, AdminMessage.TYPE_ERROR );
218 }
219
220 return getPostBackUrl( request, comment );
221 }
222 }
223
224 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
225 }
226
227
228
229
230
231
232
233
234 public String getConfirmRemoveComment( HttpServletRequest request )
235 {
236 UrlItem url = new UrlItem( JSP_URL_DO_REMOVE_COMMENT );
237 url.addParameter( CommentConstants.PARAMETER_ID_COMMENT, request.getParameter( CommentConstants.PARAMETER_ID_COMMENT ) );
238 url.addParameter( CommentConstants.PARAMETER_FROM_URL, StringUtils.replace( request.getParameter( CommentConstants.PARAMETER_FROM_URL ),
239 CommentConstants.CONSTANT_AND, CommentConstants.CONSTANT_AND_HTML ) );
240 addViewResourceInUrl( request, url );
241
242 return AdminMessageService.getMessageUrl( request, CommentConstants.MESSAGE_CONFIRM_REMOVE_COMMENT, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
243 }
244
245
246
247
248
249
250
251
252 public String doRemoveComment( HttpServletRequest request )
253 {
254 String strIdComment = request.getParameter( CommentConstants.PARAMETER_ID_COMMENT );
255
256 if ( StringUtils.isNotBlank( strIdComment ) && StringUtils.isNumeric( strIdComment ) )
257 {
258 int nIdComment = Integer.parseInt( strIdComment );
259 Comment comment = _commentService.findByPrimaryKey( nIdComment );
260
261 if ( comment != null )
262 {
263 try
264 {
265 _commentService.remove( nIdComment );
266 }
267 catch( Exception ex )
268 {
269
270 AppLogService.error( ex.getMessage( ) + " when updating a comment", ex );
271
272 return AdminMessageService.getMessageUrl( request, CommentConstants.MESSAGE_ERROR_GENERIC_MESSAGE, AdminMessage.TYPE_ERROR );
273 }
274
275 return getPostBackUrl( request, comment );
276 }
277 }
278
279 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
280 }
281
282
283
284
285
286
287
288
289 public String getCreateComment( HttpServletRequest request )
290 {
291 setPageTitleProperty( MESSAGE_TITLE_CREATE_COMMENT );
292
293 String strExtendableResourceType = request.getParameter( CommentConstants.PARAMETER_EXTENDABLE_RESOURCE_TYPE );
294 String strIdExtendableResource = request.getParameter( CommentConstants.PARAMETER_ID_EXTENDABLE_RESOURCE );
295 String strIdParentComment = request.getParameter( CommentConstants.PARAMETER_ID_COMMENT );
296 String strViewAllResources = request.getParameter( CommentConstants.PARAMETER_VIEW_ALL_RESOURCES );
297
298 AdminUser user = AdminUserService.getAdminUser( request );
299
300 Map<String, Object> model = new HashMap<String, Object>( );
301 model.put( CommentConstants.MARK_ALL_RESOURCES, !StringUtils.isEmpty( strViewAllResources ) && new Boolean( strViewAllResources ) );
302 model.put( CommentConstants.MARK_EXTENDABLE_RESOURCE_TYPE, strExtendableResourceType );
303 model.put( CommentConstants.MARK_ID_EXTENDABLE_RESOURCE, strIdExtendableResource );
304 model.put( CommentConstants.PARAMETER_ID_COMMENT, strIdParentComment );
305 model.put( CommentConstants.PARAMETER_NAME, user.getFirstName( ) + CONSTANT_SPACE + user.getLastName( ).toUpperCase( ) );
306 model.put( CommentConstants.MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
307 model.put( CommentConstants.MARK_LOCALE, AdminUserService.getLocale( request ) );
308 model.put( CommentConstants.PARAMETER_FROM_URL, StringUtils.replace( request.getParameter( CommentConstants.PARAMETER_FROM_URL ),
309 CommentConstants.CONSTANT_AND, CommentConstants.CONSTANT_AND_HTML ) );
310 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_COMMENT, AdminUserService.getLocale( request ), model );
311
312 return getAdminPage( template.getHtml( ) );
313 }
314
315
316
317
318
319
320
321
322 public String doCreateComment( HttpServletRequest request )
323 {
324 String strExtendableResourceType = request.getParameter( CommentConstants.PARAMETER_EXTENDABLE_RESOURCE_TYPE );
325 String strIdExtendableResource = request.getParameter( CommentConstants.PARAMETER_ID_EXTENDABLE_RESOURCE );
326 String strIdParentComment = request.getParameter( CommentConstants.PARAMETER_ID_COMMENT );
327 String strName = request.getParameter( CommentConstants.PARAMETER_NAME );
328
329 String strComment = request.getParameter( CommentConstants.MARK_COMMENT );
330 if ( StringUtils.isEmpty( strComment ) || StringUtils.isEmpty( strName ) )
331 {
332 return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, AdminMessage.TYPE_STOP );
333 }
334
335 int nIdParentComment = 0;
336 if ( StringUtils.isNotEmpty( strIdParentComment ) && StringUtils.isNumeric( strIdParentComment ) )
337 {
338 nIdParentComment = Integer.parseInt( strIdParentComment );
339
340 if ( nIdParentComment > 0 )
341 {
342 Comment parrentComment = _commentService.findByPrimaryKey( nIdParentComment );
343
344 if ( parrentComment.getIdParentComment( ) > 0 )
345 {
346 nIdParentComment = parrentComment.getIdParentComment( );
347 }
348 }
349 }
350 AdminUser user = AdminUserService.getAdminUser( request );
351
352 Commentugins/extend/modules/comment/business/Comment.html#Comment">Comment comment = new Comment( );
353 comment.setIdExtendableResource( strIdExtendableResource );
354 comment.setExtendableResourceType( strExtendableResourceType );
355 comment.setIdParentComment( nIdParentComment );
356 comment.setComment( strComment );
357 Timestamp currentDate = new Timestamp( new Date( ).getTime( ) );
358 comment.setDateComment( currentDate );
359 comment.setDateLastModif( currentDate );
360 comment.setName( strName );
361 comment.setEmail( user.getEmail( ) );
362 comment.setPublished( true );
363 comment.setIpAddress( SecurityUtil.getRealIp( request ) );
364 comment.setIsAdminComment( true );
365
366 _commentService.create( comment, request );
367 _resourceHistoryService.create( CommentResourceExtender.EXTENDER_TYPE_COMMENT, strIdExtendableResource, strExtendableResourceType, request );
368
369
370 String strPostBackUrl = (String) request.getSession( ).getAttribute( CommentPlugin.PLUGIN_NAME + CommentConstants.SESSION_COMMENT_ADMIN_POST_BACK_URL );
371 request.getSession( ).setAttribute( CommentPlugin.PLUGIN_NAME + CommentConstants.SESSION_COMMENT_ADMIN_POST_BACK_URL, null );
372 if ( StringUtils.isEmpty( strPostBackUrl ) )
373 {
374 strPostBackUrl = JSP_VIEW_EXTENDER_INFO;
375 }
376 UrlItem url = new UrlItem( strPostBackUrl );
377 url.addParameter( CommentConstants.PARAMETER_EXTENDER_TYPE, CommentResourceExtender.EXTENDER_TYPE_COMMENT );
378 addIdExtendableResourceInUrl( strIdExtendableResource, request, url );
379 url.addParameter( CommentConstants.PARAMETER_EXTENDABLE_RESOURCE_TYPE, strExtendableResourceType );
380
381 if ( nIdParentComment > 0 )
382 {
383 url.addParameter( CommentConstants.PARAMETER_ID_COMMENT, nIdParentComment );
384 }
385 url.addParameter( CommentConstants.PARAMETER_FROM_URL, StringUtils.replace( request.getParameter( CommentConstants.PARAMETER_FROM_URL ),
386 CommentConstants.CONSTANT_AND, CommentConstants.CONSTANT_AND_HTML ) );
387
388 return url.getUrl( );
389 }
390
391 private void addIdExtendableResourceInUrl( String strIdExtendableResource, HttpServletRequest request, UrlItem url )
392 {
393 String strViewAllResources = request.getParameter( CommentConstants.PARAMETER_VIEW_ALL_RESOURCES );
394 if ( !StringUtils.isEmpty( strViewAllResources ) && new Boolean( strViewAllResources ) )
395 {
396 url.addParameter( CommentConstants.PARAMETER_ID_EXTENDABLE_RESOURCE, CommentConstants.CONSTANT_ALL_RESSOURCE_ID );
397 }
398 else
399 {
400 url.addParameter( CommentConstants.PARAMETER_ID_EXTENDABLE_RESOURCE, strIdExtendableResource );
401 }
402
403 }
404
405 private void addViewResourceInUrl( HttpServletRequest request, UrlItem url )
406 {
407
408 String strViewAllResources = request.getParameter( CommentConstants.PARAMETER_VIEW_ALL_RESOURCES );
409 if ( !StringUtils.isEmpty( strViewAllResources ) )
410 {
411 url.addParameter( CommentConstants.PARAMETER_VIEW_ALL_RESOURCES, strViewAllResources );
412 }
413
414 }
415
416
417
418
419
420
421
422
423 public String doProcessWorkflowAction( HttpServletRequest request )
424 {
425 String strIdAction = request.getParameter( CommentConstants.PARAMETER_ID_ACTION );
426 String strIdComment = request.getParameter( CommentConstants.PARAMETER_ID_COMMENT );
427
428 if ( StringUtils.isNumeric( strIdAction ) && StringUtils.isNumeric( strIdComment ) )
429 {
430 int nIdAction = Integer.parseInt( strIdAction );
431 int nIdComment = Integer.parseInt( strIdComment );
432
433 Comment comment = _commentService.findByPrimaryKey( nIdComment );
434 String resourceType = _commentService.getResourceType( comment.getExtendableResourceType( ) );
435
436 int nIdExtendable = getIdExtender( comment );
437
438 try
439 {
440 if ( WorkflowService.getInstance( ).isDisplayTasksForm( nIdAction, getLocale( ) ) )
441 {
442 UrlItem url = new UrlItem( JSP_VIEW_TASKS_FORM );
443
444 url.addParameter( CommentConstants.PARAMETER_ID_ACTION, strIdAction );
445 url.addParameter( CommentConstants.PARAMETER_ID_COMMENT, strIdComment );
446
447 return url.getUrl( );
448 }
449 else
450 {
451 WorkflowService.getInstance( ).doProcessAction( nIdComment, resourceType, nIdAction, nIdExtendable, request, getLocale( ), false, getUser( ) );
452 }
453 }
454 catch( Exception e )
455 {
456 AppLogService.error( "Error processing action for record {} ", nIdComment, e.getMessage( ), e );
457 }
458
459 return getPostBackUrl( request, comment );
460 }
461 return AdminMessageService.getMessageUrl( request, CommentConstants.MESSAGE_ERROR_GENERIC_MESSAGE, AdminMessage.TYPE_ERROR );
462 }
463
464
465
466
467
468
469 public String getTasksFormWorkflow( HttpServletRequest request )
470 {
471 String strIdAction = request.getParameter( CommentConstants.PARAMETER_ID_ACTION );
472 String strIdComment = request.getParameter( CommentConstants.PARAMETER_ID_COMMENT );
473
474 Map<String, Object> model = new HashMap<>( );
475
476 if ( StringUtils.isNumeric( strIdAction ) && StringUtils.isNumeric( strIdComment ) )
477 {
478 int nIdAction = Integer.parseInt( strIdAction );
479 int nIdComment = Integer.parseInt( strIdComment );
480
481 Comment comment = _commentService.findByPrimaryKey( nIdComment );
482 String resourceType = _commentService.getResourceType( comment.getExtendableResourceType( ) );
483 String strHtmlTasksForm = WorkflowService.getInstance( ).getDisplayTasksForm( nIdComment, resourceType, nIdAction, request, getLocale( ),
484 getUser( ) );
485
486 model.put( CommentConstants.PARAMETER_ID_ACTION, strIdAction );
487 model.put( CommentConstants.PARAMETER_ID_COMMENT, strIdComment );
488 model.put( MARK_TASK_FORM, strHtmlTasksForm );
489 }
490
491 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASKS_FORM_WORKFLOW, AdminUserService.getLocale( request ), model );
492
493 return getAdminPage( template.getHtml( ) );
494 }
495
496
497
498
499
500
501 public String doSaveTaskForm( HttpServletRequest request )
502 {
503 String strIdAction = request.getParameter( CommentConstants.PARAMETER_ID_ACTION );
504 String strIdComment = request.getParameter( CommentConstants.PARAMETER_ID_COMMENT );
505
506 if ( StringUtils.isNumeric( strIdAction ) && StringUtils.isNumeric( strIdComment ) )
507 {
508 int nIdAction = Integer.parseInt( strIdAction );
509 int nIdComment = Integer.parseInt( strIdComment );
510
511 Comment comment = _commentService.findByPrimaryKey( nIdComment );
512 String resourceType = _commentService.getResourceType( comment.getExtendableResourceType( ) );
513
514 int nIdExtendable = getIdExtender( comment );
515
516 try
517 {
518 String strError = WorkflowService.getInstance( ).doSaveTasksForm( comment.getIdComment( ) , resourceType, nIdAction, nIdExtendable,
519 request, getLocale( ), getUser( ) );
520
521 if( strError != null )
522 {
523 return AdminMessageService.getMessageUrl( request, CommentConstants.MESSAGE_ERROR_GENERIC_MESSAGE, AdminMessage.TYPE_ERROR );
524 }
525 else
526 {
527 WorkflowService.getInstance( ).doProcessAction( comment.getIdComment( ), resourceType, nIdAction, nIdExtendable, request, getLocale( ),
528 false, getUser( ) );
529 }
530 }
531 catch ( AppException e )
532 {
533 AppLogService.error( "Error processing action for record {}", nIdComment, e );
534 }
535
536 return getPostBackUrl( request, comment );
537 }
538 return getAdminPage( JSP_VIEW_EXTENDER_INFO );
539 }
540
541
542
543
544
545
546 private int getIdExtender( Comment comment )
547 {
548 ResourceExtenderDTOFilter filter = new ResourceExtenderDTOFilter( );
549 filter.setFilterExtendableResourceType( comment.getExtendableResourceType( ) );
550 filter.setFilterIdExtendableResource( comment.getIdExtendableResource( ) );
551 filter.setFilterExtenderType( CommentResourceExtender.EXTENDER_TYPE_COMMENT );
552 filter.setIncludeWildcardResource( true );
553
554 List<ResourceExtenderDTO> listResourceExtender = _resourceExtenderService.findByFilter( filter );
555 return listResourceExtender.get( 0 ).getIdExtender( );
556 }
557
558
559
560
561
562
563
564 private String getPostBackUrl( HttpServletRequest request, Comment comment )
565 {
566 String strPostBackUrl = (String) request.getSession( )
567 .getAttribute( CommentPlugin.PLUGIN_NAME + CommentConstants.SESSION_COMMENT_ADMIN_POST_BACK_URL );
568
569 request.getSession( ).setAttribute( CommentPlugin.PLUGIN_NAME + CommentConstants.SESSION_COMMENT_ADMIN_POST_BACK_URL, null );
570
571 if ( StringUtils.isEmpty( strPostBackUrl ) )
572 {
573 strPostBackUrl = JSP_VIEW_EXTENDER_INFO;
574 }
575
576 UrlItem url = new UrlItem( strPostBackUrl );
577 url.addParameter( CommentConstants.PARAMETER_EXTENDER_TYPE, CommentResourceExtender.EXTENDER_TYPE_COMMENT );
578 addIdExtendableResourceInUrl( comment.getIdExtendableResource( ), request, url );
579
580 url.addParameter( CommentConstants.PARAMETER_EXTENDABLE_RESOURCE_TYPE, comment.getExtendableResourceType( ) );
581 if ( comment.getIdParentComment( ) > 0 )
582 {
583 url.addParameter( CommentConstants.PARAMETER_ID_COMMENT, comment.getIdParentComment( ) );
584 }
585
586 url.addParameter( CommentConstants.PARAMETER_FROM_URL, StringUtils.replace( request.getParameter( CommentConstants.PARAMETER_FROM_URL ),
587 CommentConstants.CONSTANT_AND, CommentConstants.CONSTANT_AND_HTML ) );
588
589 return url.getUrl( );
590 }
591 }