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.service;
35
36 import java.sql.Timestamp;
37 import java.util.ArrayList;
38 import java.util.Date;
39 import java.util.List;
40 import java.util.Locale;
41
42 import javax.inject.Inject;
43 import javax.inject.Named;
44 import javax.servlet.http.HttpServletRequest;
45
46 import org.springframework.transaction.annotation.Transactional;
47
48 import fr.paris.lutece.plugins.extend.business.extender.ResourceExtenderDTO;
49 import fr.paris.lutece.plugins.extend.business.extender.ResourceExtenderDTOFilter;
50 import fr.paris.lutece.plugins.extend.modules.comment.business.Comment;
51 import fr.paris.lutece.plugins.extend.modules.comment.business.CommentFilter;
52 import fr.paris.lutece.plugins.extend.modules.comment.business.ICommentDAO;
53 import fr.paris.lutece.plugins.extend.modules.comment.business.config.CommentExtenderConfig;
54 import fr.paris.lutece.plugins.extend.modules.comment.service.extender.CommentResourceExtender;
55 import fr.paris.lutece.plugins.extend.modules.comment.util.constants.CommentConstants;
56 import fr.paris.lutece.plugins.extend.service.extender.IResourceExtenderService;
57 import fr.paris.lutece.plugins.extend.service.extender.ResourceExtenderService;
58 import fr.paris.lutece.plugins.extend.service.extender.config.IResourceExtenderConfigService;
59 import fr.paris.lutece.portal.service.i18n.I18nService;
60 import fr.paris.lutece.portal.service.plugin.Plugin;
61 import fr.paris.lutece.portal.service.workflow.WorkflowService;
62 import fr.paris.lutece.util.ReferenceList;
63
64
65
66
67
68
69 public class CommentService implements ICommentService
70 {
71
72 public static final String BEAN_SERVICE = "extend-comment.commentService";
73
74 @Inject
75 private ICommentDAO _commentDAO;
76 @Inject
77 @Named( ResourceExtenderService.BEAN_SERVICE )
78 private IResourceExtenderService _resourceExtenderService;
79 @Inject
80 @Named( CommentConstants.BEAN_CONFIG_SERVICE )
81 private IResourceExtenderConfigService _configService;
82
83
84
85
86 @Override
87 @Transactional( CommentPlugin.TRANSACTION_MANAGER )
88 public synchronized void create( Comment comment, HttpServletRequest request )
89 {
90 Timestamp currentTimestamp = new Timestamp( new Date( ).getTime( ) );
91 comment.setDateComment( currentTimestamp );
92 comment.setDateLastModif( currentTimestamp );
93 _commentDAO.insert( comment, CommentPlugin.getPlugin( ) );
94 if ( comment.getIdParentComment( ) > 0 )
95 {
96 Comment parentComment = findByPrimaryKey( comment.getIdParentComment( ) );
97 parentComment.setDateLastModif( currentTimestamp );
98 update( parentComment );
99 }
100 processWorkflow( comment );
101 CommentListenerService.createComment( comment.getExtendableResourceType( ), comment.getIdExtendableResource( ), comment.isPublished( ), request );
102 }
103
104
105
106
107 @Override
108 @Transactional( CommentPlugin.TRANSACTION_MANAGER )
109 public synchronized void create( Comment comment )
110 {
111 Timestamp currentTimestamp = new Timestamp( new Date( ).getTime( ) );
112 comment.setDateComment( currentTimestamp );
113 comment.setDateLastModif( currentTimestamp );
114 _commentDAO.insert( comment, CommentPlugin.getPlugin( ) );
115 if ( comment.getIdParentComment( ) > 0 )
116 {
117 Comment parentComment = findByPrimaryKey( comment.getIdParentComment( ) );
118 parentComment.setDateLastModif( currentTimestamp );
119 update( parentComment );
120 }
121 processWorkflow( comment );
122 CommentListenerService.createComment( comment.getExtendableResourceType( ), comment.getIdExtendableResource( ), comment.isPublished( ) );
123 }
124
125
126
127
128 @Override
129 @Transactional( CommentPlugin.TRANSACTION_MANAGER )
130 public void update( Comment comment )
131 {
132 Comment oldComment = findByPrimaryKey( comment.getIdComment( ) );
133 comment.setDateLastModif( new Timestamp( new Date( ).getTime( ) ) );
134 _commentDAO.store( comment, CommentPlugin.getPlugin( ) );
135 if ( oldComment.isPublished( ) ^ comment.isPublished( ) )
136 {
137 CommentListenerService.publishComment( comment.getExtendableResourceType( ), comment.getIdExtendableResource( ), comment.isPublished( ) );
138 }
139
140 }
141
142
143
144
145 @Override
146 @Transactional( CommentPlugin.TRANSACTION_MANAGER )
147 public void updateCommentStatus( int nIdComment, boolean bPublished )
148 {
149 _commentDAO.updateCommentStatus( nIdComment, bPublished, CommentPlugin.getPlugin( ) );
150 CommentListenerService.publishComment( nIdComment, bPublished );
151 }
152
153
154
155
156 @Override
157 @Transactional( CommentPlugin.TRANSACTION_MANAGER )
158 public void remove( int nIdComment )
159 {
160 Comment comment = findByPrimaryKey( nIdComment );
161 if ( CommentListenerService.hasListener( ) )
162 {
163
164 List<Integer> listIdRemovedComments = new ArrayList<>( );
165 listIdRemovedComments.add( nIdComment );
166 CommentListenerService.deleteComment( comment.getExtendableResourceType( ), comment.getIdExtendableResource( ), listIdRemovedComments );
167 }
168 if ( WorkflowService.getInstance( ).isAvailable( ) )
169 {
170 String resourceType = getResourceType( comment.getExtendableResourceType( ) );
171 WorkflowService.getInstance( ).doRemoveWorkFlowResource( nIdComment, resourceType );
172
173 }
174 _commentDAO.delete( nIdComment, CommentPlugin.getPlugin( ) );
175 }
176
177
178
179
180 @Override
181 @Transactional( CommentPlugin.TRANSACTION_MANAGER )
182 public void removeByResource( String strIdExtendableResource, String strExtendableResourceType )
183 {
184 List<Integer> listRemovedComments = findIdsByResource( strIdExtendableResource, strExtendableResourceType, false );
185 if ( CommentListenerService.hasListener( ) )
186 {
187
188 CommentListenerService.deleteComment( strExtendableResourceType, strIdExtendableResource, listRemovedComments );
189 }
190 if ( WorkflowService.getInstance( ).isAvailable( ) )
191 {
192 String resourceType = getResourceType( strExtendableResourceType );
193 for ( int nIdComment : listRemovedComments )
194 {
195
196 WorkflowService.getInstance( ).doRemoveWorkFlowResource( nIdComment, resourceType );
197
198 }
199
200
201 }
202 _commentDAO.deleteByResource( strIdExtendableResource, strExtendableResourceType, CommentPlugin.getPlugin( ) );
203 }
204
205
206
207
208
209
210 @Override
211 public Comment findByPrimaryKey( int nIdComment )
212 {
213 return _commentDAO.load( nIdComment, CommentPlugin.getPlugin( ) );
214 }
215
216
217
218
219 @Override
220 public List<Integer> findIdsByResource( String strIdExtendableResource, String strExtendableResourceType, boolean bPublishedOnly )
221 {
222 return _commentDAO.findIdsByResource( strIdExtendableResource, strExtendableResourceType, bPublishedOnly, CommentPlugin.getPlugin( ) );
223 }
224
225
226
227
228 @Override
229 public List<Comment> findByResource( String strIdExtendableResource, String strExtendableResourceType, boolean bPublishedOnly, boolean bAscSort )
230 {
231
232 CommentFilter/modules/comment/business/CommentFilter.html#CommentFilter">CommentFilter commentFilter = new CommentFilter( );
233 if ( bPublishedOnly )
234 {
235 commentFilter.setCommentState( Comment.COMMENT_STATE_PUBLISHED );
236 }
237
238 commentFilter.setAscSort( bAscSort );
239
240 return _commentDAO.findParentCommentsByResource( strIdExtendableResource, strExtendableResourceType, commentFilter, 0, 0, CommentPlugin.getPlugin( ) );
241 }
242
243
244
245
246 @Override
247 public int getCommentNb( String strIdExtendableResource, String strExtendableResourceType, boolean bParentsOnly, boolean bPublishedOnly )
248 {
249 return _commentDAO.getCommentNb( strIdExtendableResource, strExtendableResourceType, bParentsOnly, bPublishedOnly, CommentPlugin.getPlugin( ) );
250 }
251
252
253
254
255 @Override
256 public List<Comment> findLastComments( String strIdExtendableResource, String strExtendableResourceType, int nNbComments, boolean bPublishedOnly,
257 boolean bParentsOnly, boolean bGetNumberSubComments, boolean bDisplaySubComments, boolean bSortedByDateCreation )
258 {
259 Plugin plugin = CommentPlugin.getPlugin( );
260 List<Comment> listComments = new ArrayList<>( );
261 List<Comment> listSubComments = new ArrayList<>( );
262
263 List<Comment> listCommentsPinned = findCommentsPinned( strIdExtendableResource, strExtendableResourceType, nNbComments,
264 bPublishedOnly ? Comment.COMMENT_STATE_PUBLISHED : null, true, bGetNumberSubComments, null );
265
266 listComments.addAll( listCommentsPinned );
267
268 if ( nNbComments == 0 || listCommentsPinned.size( ) != nNbComments )
269 {
270 if ( listCommentsPinned.size( ) != nNbComments )
271 {
272 nNbComments = nNbComments - listCommentsPinned.size( );
273 }
274 List<Comment> listLastComments = _commentDAO.selectLastComments( strIdExtendableResource, strExtendableResourceType, nNbComments, bPublishedOnly,
275 bParentsOnly, plugin, bSortedByDateCreation );
276 if ( bGetNumberSubComments )
277 {
278 for ( Comment comment : listLastComments )
279 {
280 listSubComments = findByIdParent( comment.getIdComment( ), bPublishedOnly );
281
282 comment.setNumberSubComments( listSubComments.size( ) );
283 if ( bDisplaySubComments )
284 {
285 comment.setListSubComments( listSubComments );
286 }
287 }
288 }
289 listComments.addAll( listLastComments );
290 }
291
292 return listComments;
293 }
294
295
296
297
298 @Override
299 public List<Comment> findByResource( String strIdExtendableResource, String strExtendableResourceType, boolean bPublishedOnly,
300 String strSortedAttributeName, boolean bAscSort, int nItemsOffset, int nMaxItemsNumber, boolean bLoadSubComments )
301 {
302
303 CommentFilter/modules/comment/business/CommentFilter.html#CommentFilter">CommentFilter commentFilter = new CommentFilter( );
304 if ( bPublishedOnly )
305 {
306 commentFilter.setCommentState( Comment.COMMENT_STATE_PUBLISHED );
307 }
308 commentFilter.setSortedAttributeName( strSortedAttributeName );
309 commentFilter.setAscSort( bAscSort );
310 return findByResource( strIdExtendableResource, strExtendableResourceType, commentFilter, nItemsOffset, nMaxItemsNumber, bLoadSubComments );
311 }
312
313
314
315
316 @Override
317 public List<Comment> findByResource( String strIdExtendableResource, String strExtendableResourceType, CommentFilter commentFilter, int nItemsOffset,
318 int nMaxItemsNumber, boolean bLoadSubComments )
319 {
320
321 List<Comment> listComments = new ArrayList<>( );
322
323 listComments.addAll( _commentDAO.findParentCommentsByResource( strIdExtendableResource, strExtendableResourceType, commentFilter, nItemsOffset,
324 nMaxItemsNumber, CommentPlugin.getPlugin( ) ) );
325
326 if ( bLoadSubComments )
327 {
328 for ( Comment comment : listComments )
329 {
330 comment.setListSubComments( this.findByIdParent( comment.getIdComment( ),
331 commentFilter.getCommentState( ) != null && commentFilter.getCommentState( ).equals( Comment.COMMENT_STATE_PUBLISHED ),
332 commentFilter.getSortedAttributeName( ), commentFilter.getAscSort( ) ) );
333 }
334 }
335
336 return listComments;
337 }
338
339
340
341
342 @Override
343 public List<Comment> findByIdParent( int nIdParent, boolean bPublishedOnly )
344 {
345 return this.findByIdParent( nIdParent, bPublishedOnly, null, true );
346 }
347
348
349
350
351 @Override
352 public List<Comment> findByIdParent( int nIdParent, boolean bPublishedOnly, String strSortedAttributeName, boolean bAscSort )
353 {
354 CommentFilter/modules/comment/business/CommentFilter.html#CommentFilter">CommentFilter commentFilter = new CommentFilter( );
355
356 if ( bPublishedOnly )
357 {
358 commentFilter.setCommentState( Comment.COMMENT_STATE_PUBLISHED );
359 }
360 commentFilter.setSortedAttributeName( strSortedAttributeName );
361 commentFilter.setAscSort( bAscSort );
362
363 return _commentDAO.findByIdParent( nIdParent, commentFilter, CommentPlugin.getPlugin( ) );
364 }
365
366
367
368
369 @Override
370 public int countByIdParent( int nIdParent, boolean bPublishedOnly )
371 {
372 return _commentDAO.countByIdParent( nIdParent, bPublishedOnly, CommentPlugin.getPlugin( ) );
373 }
374
375
376
377
378 @Override
379 public List<Integer> findIdMostCommentedResources( String strExtendableResourceType, boolean bPublishedOnly, int nItemsOffset, int nMaxItemsNumber )
380 {
381 return _commentDAO.findIdMostCommentedResources( strExtendableResourceType, bPublishedOnly, nItemsOffset, nMaxItemsNumber, CommentPlugin.getPlugin( ) );
382 }
383
384
385
386
387 @Override
388 public ReferenceList getRefListCommentStates( Locale locale )
389 {
390 ReferenceList refListDiggSubmitState = new ReferenceList( );
391 refListDiggSubmitState.addItem( "", I18nService.getLocalizedString( CommentConstants.PROPERTY_COMMENT_ALL_STATE, locale ) );
392 refListDiggSubmitState.addItem( Comment.COMMENT_STATE_PUBLISHED,
393 I18nService.getLocalizedString( CommentConstants.PROPERTY_COMMENT_STATE_PUBLISHED, locale ) );
394 refListDiggSubmitState.addItem( Comment.COMMENT_STATE_UN_PUBLISHED,
395 I18nService.getLocalizedString( CommentConstants.PROPERTY_COMMENT_STATE_UN_PUBLISHED, locale ) );
396 return refListDiggSubmitState;
397 }
398
399
400
401
402 @Override
403 public ReferenceList getRefListFilterAsImportant( Locale locale )
404 {
405 ReferenceList refListFilterAsImportant = new ReferenceList( );
406 refListFilterAsImportant.addItem( "", I18nService.getLocalizedString( CommentConstants.PROPERTY_COMMENT_FILTER_BY_IMPORTANT, locale ) );
407 refListFilterAsImportant.addItem( Boolean.TRUE.toString( ),
408 I18nService.getLocalizedString( CommentConstants.PROPERTY_COMMENT_FILTER_BY_IMPORTANT_ALL_FLAG_IMPORTANT, locale ) );
409 refListFilterAsImportant.addItem( Boolean.FALSE.toString( ),
410 I18nService.getLocalizedString( CommentConstants.PROPERTY_COMMENT_FILTER_BY_IMPORTANT_ALL_NOT_FLAG_AS_IMPORTANT, locale ) );
411 return refListFilterAsImportant;
412 }
413
414
415
416
417 @Override
418 public ReferenceList getRefListFilterAsPinned( Locale locale )
419 {
420 ReferenceList refListDiggSubmitState = new ReferenceList( );
421 refListDiggSubmitState.addItem( "", I18nService.getLocalizedString( CommentConstants.PROPERTY_COMMENT_FILTER_BY_PINNED, locale ) );
422 refListDiggSubmitState.addItem( Boolean.TRUE.toString( ),
423 I18nService.getLocalizedString( CommentConstants.PROPERTY_COMMENT_FILTER_BY_PINNED_ALL_PINNED, locale ) );
424 refListDiggSubmitState.addItem( Boolean.FALSE.toString( ),
425 I18nService.getLocalizedString( CommentConstants.PROPERTY_COMMENT_FILTER_BY_PINNED_ALL_NOT_PINNED, locale ) );
426 return refListDiggSubmitState;
427 }
428
429 @Override
430 public List<Comment> findCommentsPinned( String strIdExtendableResource, String strExtendableResourceType, int nNbComments, Integer nCommentState,
431 boolean bParentsOnly, boolean bGetNumberSubComments, String strFilterUserName )
432 {
433 Plugin plugin = CommentPlugin.getPlugin( );
434 CommentFilter/extend/modules/comment/business/CommentFilter.html#CommentFilter">CommentFilter filter = new CommentFilter( );
435 filter.setPinned( true );
436 filter.setAscSort( false );
437 filter.setSortedAttributeName( CommentConstants.SORT_BY_COMMENT_ORDER );
438 filter.setCommentState( nCommentState );
439 filter.setLuteceUserName( strFilterUserName );
440
441 List<Comment> listComments = _commentDAO.findParentCommentsByResource( strIdExtendableResource, strExtendableResourceType, filter, 0, nNbComments,
442 plugin );
443
444 if ( bGetNumberSubComments )
445 {
446 for ( Comment comment : listComments )
447 {
448 comment.setNumberSubComments( _commentDAO.countByIdParent( comment.getIdComment( ),
449 ( nCommentState != null && nCommentState.equals( Comment.COMMENT_STATE_PUBLISHED ) ), plugin ) );
450 }
451 }
452 return listComments;
453
454 }
455
456 @Override
457 @Transactional( CommentPlugin.TRANSACTION_MANAGER )
458 public void updateFlagImportant( int nIdComment, boolean bImportant )
459 {
460 Plugin plugin = CommentPlugin.getPlugin( );
461 Comment comment = findByPrimaryKey( nIdComment );
462 if ( comment != null )
463 {
464 comment.setIsImportant( bImportant );
465 }
466 _commentDAO.store( comment, plugin );
467 }
468
469 @Override
470 @Transactional( CommentPlugin.TRANSACTION_MANAGER )
471 public void updateCommentPinned( int nIdComment, boolean bPinned )
472 {
473 Plugin plugin = CommentPlugin.getPlugin( );
474 Comment comment = findByPrimaryKey( nIdComment );
475 if ( comment != null )
476 {
477 comment.setPinned( bPinned );
478 if ( bPinned )
479 {
480
481 CommentFilter/extend/modules/comment/business/CommentFilter.html#CommentFilter">CommentFilter filter = new CommentFilter( );
482 filter.setPinned( true );
483 List<Comment> listComment = findByResource( comment.getIdExtendableResource( ), comment.getExtendableResourceType( ), filter, 0, 10000, false );
484 int nOrder = 1;
485 for ( Comment commentPinned : listComment )
486 {
487 if ( commentPinned.getCommentOrder( ) >= nOrder )
488 {
489 nOrder = commentPinned.getCommentOrder( ) + 1;
490 }
491 }
492 comment.setCommentOrder( nOrder );
493 }
494
495 }
496 _commentDAO.store( comment, plugin );
497 }
498
499 private void processWorkflow( Comment comment )
500 {
501
502 String resourceType = getResourceType( comment.getExtendableResourceType( ) );
503 ResourceExtenderDTOFilter filter = new ResourceExtenderDTOFilter( );
504 filter.setFilterExtendableResourceType( comment.getExtendableResourceType( ) );
505 filter.setFilterIdExtendableResource( comment.getIdExtendableResource( ) );
506 filter.setFilterExtenderType( CommentResourceExtender.EXTENDER_TYPE_COMMENT );
507 filter.setIncludeWildcardResource( true );
508
509 List<ResourceExtenderDTO> listResourceExtender = _resourceExtenderService.findByFilter( filter );
510 int nIdExtendable = listResourceExtender.get( 0 ).getIdExtender( );
511
512 CommentExtenderConfig../../../fr/paris/lutece/plugins/extend/modules/comment/business/config/CommentExtenderConfig.html#CommentExtenderConfig">CommentExtenderConfig config = (CommentExtenderConfig) _configService.find( nIdExtendable );
513 int idWorkflow = config.getIdWorkflow( );
514 if ( idWorkflow > 0 )
515 {
516
517 WorkflowService.getInstance( ).getState( comment.getIdComment( ), resourceType, idWorkflow, nIdExtendable );
518 WorkflowService.getInstance( ).executeActionAutomatic( comment.getIdComment( ), resourceType, idWorkflow, nIdExtendable );
519 }
520 }
521
522 public String getResourceType( String extendableResourceType )
523 {
524
525 StringBuilder resourceType = new StringBuilder( );
526 resourceType.append( CommentResourceExtender.EXTENDER_TYPE_COMMENT );
527 resourceType.append( "-" );
528 resourceType.append( extendableResourceType );
529
530 return resourceType.toString( );
531 }
532
533 @Override
534 public List<Comment> findByListResource(List<String> listIdExtendableResource, String strExtendableResourceType) {
535
536 return _commentDAO.selectByListResource(listIdExtendableResource, strExtendableResourceType, CommentPlugin.getPlugin( ) );
537 }
538
539 @Override
540 public List<Comment> findCommentsByLuteceUser(String strLuteceUserName) {
541 return _commentDAO.findCommentsByLuteceUserName(strLuteceUserName, CommentPlugin.getPlugin( ));
542 }
543
544 }