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 fr.paris.lutece.plugins.extend.modules.comment.business.Comment;
37 import fr.paris.lutece.plugins.extend.modules.comment.business.ICommentDAO;
38 import fr.paris.lutece.portal.service.security.LuteceUser;
39 import fr.paris.lutece.portal.service.spring.SpringContextService;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 import javax.servlet.http.HttpServletRequest;
48
49
50
51
52 public class CommentListenerService
53 {
54
55
56
57 public static final String CONSTANT_EVERY_EXTENDABLE_RESOURCE_TYPE = "*";
58
59 private static Map<String, List<ICommentListener>> _mapListeners = new HashMap<String, List<ICommentListener>>( );
60 private static boolean _bHasListeners;
61
62 private static volatile ICommentDAO _commentDAO;
63
64
65
66
67 private CommentListenerService( )
68 {
69
70 }
71
72
73
74
75
76
77
78
79
80
81 public static synchronized void registerListener( String strExtendableResourceType, ICommentListener listener )
82 {
83 List<ICommentListener> listListeners = _mapListeners.get( strExtendableResourceType );
84 if ( listListeners == null )
85 {
86 listListeners = new ArrayList<ICommentListener>( );
87 _mapListeners.put( strExtendableResourceType, listListeners );
88 }
89 listListeners.add( listener );
90 _bHasListeners = true;
91 }
92
93
94
95
96
97
98 public static boolean hasListener( )
99 {
100 return _bHasListeners;
101 }
102
103
104
105
106
107
108
109
110
111
112
113 public static void createComment( String strExtendableResourceType, String strIdExtendableResource, boolean bPublished )
114 {
115 List<ICommentListener> listListeners = _mapListeners.get( strExtendableResourceType );
116 if ( listListeners != null )
117 {
118 for ( ICommentListener listener : listListeners )
119 {
120 listener.createComment( strIdExtendableResource, bPublished );
121 }
122 }
123 listListeners = _mapListeners.get( CONSTANT_EVERY_EXTENDABLE_RESOURCE_TYPE );
124 if ( listListeners != null )
125 {
126 for ( ICommentListener listener : listListeners )
127 {
128 listener.createComment( strIdExtendableResource, bPublished );
129 }
130 }
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public static void createComment( String strExtendableResourceType, String strIdExtendableResource, boolean bPublished, HttpServletRequest request )
146 {
147 List<ICommentListener> listListeners = _mapListeners.get( strExtendableResourceType );
148 if ( listListeners != null )
149 {
150 for ( ICommentListener listener : listListeners )
151 {
152 listener.createComment( strIdExtendableResource, bPublished, request );
153 }
154 }
155 listListeners = _mapListeners.get( CONSTANT_EVERY_EXTENDABLE_RESOURCE_TYPE );
156 if ( listListeners != null )
157 {
158 for ( ICommentListener listener : listListeners )
159 {
160 listener.createComment( strIdExtendableResource, bPublished, request );
161 }
162 }
163 }
164
165
166
167
168
169
170
171
172
173 public static void publishComment( int nIdComment, boolean bPublished )
174 {
175 if ( _mapListeners.size( ) > 0 )
176 {
177 Comment comment = getCommentDAO( ).load( nIdComment, CommentPlugin.getPlugin( ) );
178 publishComment( comment.getExtendableResourceType( ), comment.getIdExtendableResource( ), bPublished );
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191
192 public static void publishComment( String strExtendableResourceType, String strIdExtendableResource, boolean bPublished )
193 {
194 List<ICommentListener> listListeners = _mapListeners.get( strExtendableResourceType );
195 if ( listListeners != null )
196 {
197 for ( ICommentListener listener : listListeners )
198 {
199 listener.publishComment( strIdExtendableResource, bPublished );
200 }
201 }
202 listListeners = _mapListeners.get( CONSTANT_EVERY_EXTENDABLE_RESOURCE_TYPE );
203 if ( listListeners != null )
204 {
205 for ( ICommentListener listener : listListeners )
206 {
207 listener.publishComment( strIdExtendableResource, bPublished );
208 }
209 }
210 }
211
212
213
214
215
216
217
218
219
220
221
222 public static void deleteComment( String strExtendableResourceType, String strIdExtendableResource, List<Integer> listIdRemovedComment )
223 {
224 try
225 {
226 List<ICommentListener> listListeners = _mapListeners.get( strExtendableResourceType );
227 if ( listListeners != null )
228 {
229 for ( ICommentListener listener : listListeners )
230 {
231 listener.deleteComment( strIdExtendableResource, listIdRemovedComment );
232 }
233 }
234 listListeners = _mapListeners.get( CONSTANT_EVERY_EXTENDABLE_RESOURCE_TYPE );
235 if ( listListeners != null )
236 {
237 for ( ICommentListener listener : listListeners )
238 {
239 listener.deleteComment( strIdExtendableResource, listIdRemovedComment );
240 }
241 }
242 }
243 catch( Exception e )
244 {
245 AppLogService.error( e.getMessage( ), e );
246 }
247 }
248
249
250
251
252
253
254
255 public static String checkComment( String comment, String strExtendableResourceType, String uidUser )
256 {
257
258 StringBuilder sbError = new StringBuilder( );
259 try
260 {
261 List<ICommentListener> listListeners = _mapListeners.get( strExtendableResourceType );
262 if ( listListeners != null )
263 {
264 for ( ICommentListener listener : listListeners )
265 {
266 String strError = listener.checkComment( comment, uidUser );
267 if ( strError != null && !strError.isEmpty( ) )
268 {
269 sbError.append( strError );
270 }
271 }
272 }
273 listListeners = _mapListeners.get( CONSTANT_EVERY_EXTENDABLE_RESOURCE_TYPE );
274 if ( listListeners != null )
275 {
276 for ( ICommentListener listener : listListeners )
277 {
278 String strError = listener.checkComment( comment, uidUser );
279 if ( strError != null && !strError.isEmpty( ) )
280 {
281 sbError.append( strError );
282 }
283 }
284 }
285 }
286 catch( Exception e )
287 {
288 AppLogService.error( e.getMessage( ), e );
289 }
290 return sbError.toString( );
291 }
292
293
294
295
296
297
298
299 public static String checkComment( String comment, String strExtendableResourceType, String strResourceId, String uidUser )
300 {
301 StringBuilder sbError = new StringBuilder( );
302 try
303 {
304 List<ICommentListener> listListeners = _mapListeners.get( strExtendableResourceType );
305 if ( listListeners != null )
306 {
307 for ( ICommentListener listener : listListeners )
308 {
309 String strError = listener.checkComment( comment, uidUser, strExtendableResourceType, strResourceId );
310 if ( strError != null && !strError.isEmpty( ) )
311 {
312 sbError.append( strError );
313 }
314 }
315 }
316 listListeners = _mapListeners.get( CONSTANT_EVERY_EXTENDABLE_RESOURCE_TYPE );
317 if ( listListeners != null )
318 {
319 for ( ICommentListener listener : listListeners )
320 {
321 String strError = listener.checkComment( comment, uidUser, strExtendableResourceType, strResourceId );
322 if ( strError != null && !strError.isEmpty( ) )
323 {
324 sbError.append( strError );
325 }
326 }
327 }
328 }
329 catch( Exception e )
330 {
331 AppLogService.error( e.getMessage( ), e );
332 }
333 return sbError.toString( );
334 }
335
336
337
338
339
340
341
342
343
344
345
346
347 public static boolean canComment( LuteceUser user, String strIdExtendableResource, String strExtendableResourceType )
348 {
349 try
350 {
351 List<ICommentListener> listListeners = _mapListeners.get( strExtendableResourceType );
352 if ( listListeners != null )
353 {
354 for ( ICommentListener listener : listListeners )
355 {
356 if ( !listener.canComment( user, strIdExtendableResource, strExtendableResourceType ) )
357 {
358 return false;
359 }
360
361 }
362 }
363 listListeners = _mapListeners.get( CONSTANT_EVERY_EXTENDABLE_RESOURCE_TYPE );
364 if ( listListeners != null )
365 {
366 for ( ICommentListener listener : listListeners )
367 {
368 if ( !listener.canComment( user, strIdExtendableResource, strExtendableResourceType ) )
369 {
370 return false;
371 }
372
373 }
374 }
375 }
376 catch( Exception e )
377 {
378 AppLogService.error( e.getMessage( ), e );
379 }
380 return true;
381 }
382
383
384
385
386
387
388 private static ICommentDAO getCommentDAO( )
389 {
390 if ( _commentDAO == null )
391 {
392 synchronized( CommentListenerService.class )
393 {
394 if ( _commentDAO == null )
395 {
396 List<ICommentDAO> listDao = SpringContextService.getBeansOfType( ICommentDAO.class );
397 if ( listDao != null && listDao.size( ) > 0 )
398 {
399 _commentDAO = listDao.get( 0 );
400 }
401 }
402 }
403 }
404 return _commentDAO;
405 }
406 }