View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.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   * Service to manage listeners over comments
51   */
52  public class CommentListenerService
53  {
54      /**
55       * Constant that represents every extendable resource type
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       * Private constructor
66       */
67      private CommentListenerService( )
68      {
69  
70      }
71  
72      /**
73       * Register a comment listener.
74       * 
75       * @param strExtendableResourceType
76       *            The extendable resource type associated with the listener. Use {@link #CONSTANT_EVERY_EXTENDABLE_RESOURCE_TYPE} to associated the listener
77       *            with every resource type.
78       * @param listener
79       *            The listener to register
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       * Check if there is listeners to notify
95       * 
96       * @return True if there is at last one listener, false otherwise
97       */
98      public static boolean hasListener( )
99      {
100         return _bHasListeners;
101     }
102 
103     /**
104      * Notify to listeners the creation of a comment. Only listeners associated with the extendable resource type of the comment are notified.
105      * 
106      * @param strExtendableResourceType
107      *            The extendable resource type of the created comment
108      * @param strIdExtendableResource
109      *            The extendable resource id of the comment
110      * @param bPublished
111      *            True if the comment is published, false otherwise
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      * Notify to listeners the creation of a comment. Only listeners associated with the extendable resource type of the comment are notified.
135      * 
136      * @param strExtendableResourceType
137      *            The extendable resource type of the created comment
138      * @param strIdExtendableResource
139      *            The extendable resource id of the comment
140      * @param bPublished
141      *            True if the comment is published, false otherwise
142      * @param request
143      *            the HTTP request
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      * Notify to listeners the modification of a comment. Only listeners associated with the extendable resource type of the comment are notified.
167      * 
168      * @param nIdComment
169      *            The id of the updated comment
170      * @param bPublished
171      *            True if the comment was published, false if it was unpublished
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      * Notify to listeners the modification of a comment. Only listeners associated with the extendable resource type of the comment are notified.
184      * 
185      * @param strExtendableResourceType
186      *            The extendable resource type of the updated comment
187      * @param strIdExtendableResource
188      *            The extendable resource id of the comment
189      * @param bPublished
190      *            True if the comment was published, false if it was unpublished
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      * Notify to listeners the modification of a comment. Only listeners associated with the extendable resource type of the comment are notified.
214      * 
215      * @param strExtendableResourceType
216      *            The extendable resource type of the removed comment
217      * @param strIdExtendableResource
218      *            The extendable resource id of the comment
219      * @param listIdRemovedComment
220      *            The list of ids of removed comments
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      * Notify the check comment
251      * 
252      * @param listErrors
253      * @return
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      * Notify the check comment
295      * 
296      * @param listErrors
297      * @return
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      * Check if a user can comment. Call listeners.
338      * 
339      * @param user
340      *            The lutece user
341      * @param strIdExtendableResource
342      *            The id of the extendable resource
343      * @param strExtendableResourceType
344      *            The type of the resource
345      * @return true when the user has the rights, otherwise false
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      * Get the comment DAO
385      * 
386      * @return the comment DAO
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 }