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.business;
35  
36  import java.security.GeneralSecurityException;
37  import java.sql.Timestamp;
38  import java.util.Collection;
39  import java.util.List;
40  
41  import javax.validation.constraints.NotNull;
42  
43  import org.apache.commons.lang3.StringUtils;
44  
45  import fr.paris.lutece.plugins.extend.modules.comment.service.CommentAvatarService;
46  import fr.paris.lutece.plugins.extend.modules.comment.util.annotation.Email;
47  import fr.paris.lutece.plugins.extend.service.extender.facade.IExtendableResourceResult;
48  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
49  import fr.paris.lutece.portal.service.editor.EditorBbcodeService;
50  import fr.paris.lutece.portal.service.security.RsaService;
51  import fr.paris.lutece.portal.service.util.AppLogService;
52  
53  /**
54   * 
55   * Comment
56   * 
57   */
58  public class Comment implements IExtendableResourceResult
59  {
60      public static final int COMMENT_STATE_PUBLISHED = 1;
61      public static final int COMMENT_STATE_UN_PUBLISHED = 0;
62      private String _strLuteceUserName;
63      private int _nIdComment;
64      @NotNull
65      private String _strIdExtendableResource;
66      @NotNull
67      private String _strExtendableResourceType;
68      private Timestamp _dateComment;
69      private Timestamp _dateLastModif;
70      private String _strName;
71      @Email
72      private String _strEmail;
73      private String _strIpAddress;
74      private String _strComment;
75      private boolean _bIsPublished;
76      private int _nIdParentComment;
77      private List<Comment> _listSubComments;
78      private int _nNumberSubComments = -1;
79      private boolean _bIsAdminComment;
80      private boolean _bPinned;
81      private int _nCommentOrder;
82      private boolean _bIsImportant;
83      /**
84       * List of the available action of the workflow for this appointment
85       */
86      private Collection<Action> _listWorkflowActions;
87  
88      /**
89       * @return the strIdExtendableResource
90       */
91      @Override
92      public String getIdExtendableResource( )
93      {
94          return _strIdExtendableResource;
95      }
96  
97      /**
98       * @param strIdExtendableResource
99       *            the strIdExtendableResource to set
100      */
101     public void setIdExtendableResource( String strIdExtendableResource )
102     {
103         _strIdExtendableResource = strIdExtendableResource;
104     }
105 
106     /**
107      * @return the extendableResourceType
108      */
109     @Override
110     public String getExtendableResourceType( )
111     {
112         return _strExtendableResourceType;
113     }
114 
115     /**
116      * @param strExtendableResourceType
117      *            the extendableResourceType to set
118      */
119     public void setExtendableResourceType( String strExtendableResourceType )
120     {
121         _strExtendableResourceType = strExtendableResourceType;
122     }
123 
124     /**
125      * Returns the date of the comment
126      * 
127      * @return the date of the comment
128      */
129     public Timestamp getDateComment( )
130     {
131         return _dateComment;
132     }
133 
134     /**
135      * Sets the date of the comment
136      * 
137      * @param dateComment
138      *            the new date
139      */
140     public void setDateComment( Timestamp dateComment )
141     {
142         _dateComment = dateComment;
143     }
144 
145     /**
146      * Get the date of last modification of the comment
147      * 
148      * @return The date of last modification of the comment
149      */
150     public Timestamp getDateLastModif( )
151     {
152         return _dateLastModif;
153     }
154 
155     /**
156      * Set the date of last modification of the comment
157      * 
158      * @param dateLastModif
159      *            The date of last modification of the comment
160      */
161     public void setDateLastModif( Timestamp dateLastModif )
162     {
163         this._dateLastModif = dateLastModif;
164     }
165 
166     /**
167      * Returns the identifier of the object
168      * 
169      * @return The identifier of the object
170      */
171     public int getIdComment( )
172     {
173         return _nIdComment;
174     }
175 
176     /**
177      * Sets the identifier of the object
178      * 
179      * @param nIdComment
180      *            the new identifier
181      */
182     public void setIdComment( int nIdComment )
183     {
184         _nIdComment = nIdComment;
185     }
186 
187     /**
188      * Gets the checks if is published.
189      * 
190      * @return the checks if is published
191      */
192     public boolean isPublished( )
193     {
194         return _bIsPublished;
195     }
196 
197     /**
198      * Sets the published.
199      * 
200      * @param bIsPublished
201      *            the new published
202      */
203     public void setPublished( boolean bIsPublished )
204     {
205         _bIsPublished = bIsPublished;
206     }
207 
208     /**
209      * Returns the comment
210      * 
211      * @return the comment
212      */
213     public String getComment( )
214     {
215         return _strComment;
216     }
217 
218     /**
219      * Sets the comment
220      * 
221      * @param strComment
222      *            the new comment
223      */
224     public void setComment( String strComment )
225     {
226         _strComment = strComment;
227     }
228 
229     /**
230      * Get the content of the comment interpreted as BBCode
231      * 
232      * @return The content of the comment interpreted as BBCode
233      */
234     public String getBBCodeComment( )
235     {
236         try
237         {
238             return EditorBbcodeService.getInstance( ).parse( _strComment );
239         }
240         catch( Exception e )
241         {
242             AppLogService.error( e.getMessage( ), e );
243         }
244         return _strComment;
245     }
246 
247     /**
248      * Returns the email of the user
249      * 
250      * @return the email of the user
251      */
252     public String getEmail( )
253     {
254         return _strEmail;
255     }
256 
257     /**
258      * Sets the email of the user
259      * 
260      * @param strEmail
261      *            the new email
262      */
263     public void setEmail( String strEmail )
264     {
265         _strEmail = strEmail;
266     }
267 
268     /**
269      * Returns the IP address of the user
270      * 
271      * @return the IP address of the user
272      */
273     public String getIpAddress( )
274     {
275         return _strIpAddress;
276     }
277 
278     /**
279      * Sets the IP address of the user
280      * 
281      * @param strIpAddress
282      *            the new IP address
283      */
284     public void setIpAddress( String strIpAddress )
285     {
286         _strIpAddress = strIpAddress;
287     }
288 
289     /**
290      * Returns the name of the user
291      * 
292      * @return the name of the user
293      */
294     public String getName( )
295     {
296         return _strName;
297     }
298 
299     /**
300      * Sets the name of the user
301      * 
302      * @param strName
303      *            the new name
304      */
305     public void setName( String strName )
306     {
307         _strName = strName;
308     }
309 
310     /**
311      * Get the id of the parent comment of this comment
312      * 
313      * @return The id of the parent comment of this comment
314      */
315     public int getIdParentComment( )
316     {
317         return _nIdParentComment;
318     }
319 
320     /**
321      * Set the id of the parent comment of this comment
322      * 
323      * @param nIdParentComment
324      *            The id of the parent comment of this comment
325      */
326     public void setIdParentComment( int nIdParentComment )
327     {
328         this._nIdParentComment = nIdParentComment;
329     }
330 
331     /**
332      * Get the list of sub comments of this comment
333      * 
334      * @return The list of sub comments of this comment. An empty list means that this comment has no sub comment. A null value means that the sub comment list
335      *         has not been initialized.
336      */
337     public List<Comment> getListSubComments( )
338     {
339         return _listSubComments;
340     }
341 
342     /**
343      * Set the list of sub comments of this comment, and update the number of sub comments.
344      * 
345      * @param listSubComments
346      *            The list of sub comments of this comment.
347      */
348     public void setListSubComments( List<Comment> listSubComments )
349     {
350         this._listSubComments = listSubComments;
351         if ( listSubComments != null )
352         {
353             this._nNumberSubComments = listSubComments.size( );
354         }
355     }
356 
357     /**
358      * Get the number of sub comments of this comment
359      * 
360      * @return the number of sub comments of this comment, or -1 if this information is not known.
361      */
362     public int getNumberSubComments( )
363     {
364         return _nNumberSubComments;
365     }
366 
367     /**
368      * Set the number of sub comments of this comment
369      * 
370      * @param nNumberSubComments
371      *            the number of sub comments of this comment, or -1 if this information is not known.
372      */
373     public void setNumberSubComments( int nNumberSubComments )
374     {
375         this._nNumberSubComments = nNumberSubComments;
376     }
377 
378     /**
379      * Check if the comment has been written by an admin or by a front office user
380      * 
381      * @return True if the comment has been written by an admin, false otherwise
382      */
383     public boolean getIsAdminComment( )
384     {
385         return _bIsAdminComment;
386     }
387 
388     /**
389      * Set the admin written status of this comment
390      * 
391      * @param bIsAdminComment
392      *            True if the comment has been written by an admin, false otherwise
393      */
394     public void setIsAdminComment( boolean bIsAdminComment )
395     {
396         this._bIsAdminComment = bIsAdminComment;
397     }
398 
399     /**
400      * Get the id of the first unpublished sub comment of this comment
401      * 
402      * @return The id of the first unpublished sub comment, or 0 if it has no sub comments or no unpublished sub comments. 0 is also returned if sub comments of
403      *         this comment have not been fetched.
404      */
405     public int getFirstUnpublishedSubComment( )
406     {
407         if ( _listSubComments == null || _listSubComments.size( ) == 0 )
408         {
409             return 0;
410         }
411         for ( Comment comment : _listSubComments )
412         {
413             if ( !comment.isPublished( ) )
414             {
415                 return comment.getIdComment( );
416             }
417         }
418         return 0;
419     }
420 
421     /**
422      * Display the avatar of the comment's author
423      * 
424      * @return The HTML code of the avatar
425      */
426     public String getAvatar( )
427     {
428         return CommentAvatarService.getInstance( ).getAvatar( this );
429     }
430 
431     /**
432      * Display the avatar of the comment's author
433      * 
434      * @return The HTML code of the avatar
435      */
436     public String getAvatarUrl( )
437     {
438         return CommentAvatarService.getInstance( ).getAvatarUrl( this );
439     }
440 
441     /**
442      * 
443      * @return the lutece user name
444      */
445     public String getLuteceUserName( )
446     {
447         return _strLuteceUserName;
448     }
449 
450     /**
451      * set the lutece user name
452      * 
453      * @param _strLuteceUserName
454      *            the lutece user name
455      */
456     public void setLuteceUserName( String _strLuteceUserName )
457     {
458         this._strLuteceUserName = _strLuteceUserName;
459     }
460 
461     /**
462      * 
463      * @return true if the comment is pinned
464      */
465     public boolean isPinned( )
466     {
467         return _bPinned;
468     }
469 
470     /**
471      * 
472      * @param bPinned
473      *            true if the comment is pinned
474      */
475     public void setPinned( boolean bPinned )
476     {
477         this._bPinned = bPinned;
478     }
479 
480     /**
481      * 
482      * @return comment order if a comment is pinned
483      */
484     public int getCommentOrder( )
485     {
486         return _nCommentOrder;
487     }
488 
489     /**
490      * specify comment order if a comment is pinned
491      * 
492      * @param _nCommentOrder
493      *            comment order if a comment is pinned
494      */
495     public void setCommentOrder( int _nCommentOrder )
496     {
497         this._nCommentOrder = _nCommentOrder;
498     }
499 
500     /**
501      * 
502      * @return true if a comment is important
503      */
504     public boolean getIsImportant( )
505     {
506         return _bIsImportant;
507     }
508 
509     /**
510      * 
511      * @param _bIsImportant
512      *            btrue if a comment is important
513      */
514     public void setIsImportant( boolean _bIsImportant )
515     {
516         this._bIsImportant = _bIsImportant;
517     }
518 
519     /**
520      * Get the available actions of the workflow for this appointment
521      * 
522      * @return the actions
523      */
524     public Collection<Action> getListWorkflowActions( )
525     {
526         return _listWorkflowActions;
527     }
528 
529     /**
530      * Set the available actions of the workflow for this appointment
531      * 
532      * @param listWorkflowActions
533      */
534     public void setListWorkflowActions( Collection<Action> listWorkflowActions )
535     {
536         _listWorkflowActions = listWorkflowActions;
537     }
538     
539     /**
540      * Get encrypted lutece user name
541      * @return encrypt lutece user name
542      */
543     public String getEncryptedLuteceUserName( )
544     {
545         if( !StringUtils.isEmpty( this._strLuteceUserName ) )
546         {
547             try
548             {
549                 return RsaService.encryptRsa( this._strLuteceUserName );
550             }
551             catch ( GeneralSecurityException e )
552             {
553                 AppLogService.error( "Error when encrypt lutece user name for comment {}", this._nIdComment, e );
554             }
555         }
556         return StringUtils.EMPTY;
557     }
558 
559 }