View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.appointment.service;
35  
36  import java.sql.Date;
37  import java.util.ArrayList;
38  import java.util.Collection;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Locale;
42  import java.util.Map;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import fr.paris.lutece.plugins.appointment.business.comment.Comment;
47  import fr.paris.lutece.plugins.appointment.business.comment.CommentHome;
48  import fr.paris.lutece.plugins.appointment.business.comment.CommentNotificationConfig;
49  import fr.paris.lutece.plugins.appointment.business.comment.CommentNotificationConfig.NotificationType;
50  import fr.paris.lutece.plugins.appointment.business.comment.CommentNotificationHome;
51  import fr.paris.lutece.plugins.appointment.business.form.Form;
52  import fr.paris.lutece.plugins.appointment.web.dto.CommentDTO;
53  import fr.paris.lutece.portal.business.mailinglist.Recipient;
54  import fr.paris.lutece.portal.business.user.AdminUser;
55  import fr.paris.lutece.portal.business.user.AdminUserHome;
56  import fr.paris.lutece.portal.service.mail.MailService;
57  import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
58  import fr.paris.lutece.portal.service.template.AppTemplateService;
59  
60  public class CommentService
61  {
62      // TEMPLATES
63      private static final String TEMPLATE_TASK_NOTIFY_MAIL = "admin/plugins/appointment/notification/task_notify_comment_mail.html";
64  
65      // MARKS
66      private static final String MARK_MESSAGE = "message";
67      private static final String MARK_COMMENT = "comment";
68      private static final String MARK_CREATION_DATE = "creation_date";
69      private static final String MARK_FORM_ID = "form_id";
70      private static final String MARK_FORM_TITLE = "form_title";
71      private static final String MARK_CREATOR_USER_NAME = "creator_user_name";
72      private static final String MARK_DATE_END_VALIDITY = "date_end_validity";
73      private static final String MARK_TIME_END_VALIDITY = "time_end_validity";
74      private static final String MARK_DATE_START_VALIDITY = "date_start_validity";
75      private static final String MARK_TIME_START_VALIDITY = "time_start_validity";
76  
77      private CommentService( )
78      {
79  
80      }
81  
82      /**
83       * fin List Comments between two dates
84       * 
85       * @param startingDate
86       *            the date start
87       * @param endingDate
88       *            the date end
89       * @returnThe list of the comments
90       */
91      public static List<Comment> finListComments( Date startingDate, Date endingDate, int nIdForm )
92      {
93  
94          return CommentHome.selectCommentsList( startingDate, endingDate, nIdForm );
95      }
96  
97      /**
98       * fin List Comments between two dates
99       * 
100      * @param startingDate
101      *            the date start
102      * @param endingDate
103      *            the date end
104      * @returnThe list of the comments
105      */
106     public static List<Comment> findListCommentsInclusive( Date startingDate, Date endingDate, int nIdForm )
107     {
108 
109         return CommentHome.selectCommentsListInclusive( startingDate, endingDate, nIdForm );
110     }
111 
112     /**
113      * Create an instance of the comment class and notify the mailing list
114      * 
115      * @param comment
116      *            The instance of the Comment which contains the informations to store
117      * @param idMailingList
118      *            the mailing list id to notify
119      * @return The instance of comment which has been created with its primary key.
120      */
121     public static Commentr/paris/lutece/plugins/appointment/business/comment/Comment.html#Comment">Comment createAndNotifyMailingList( Comment comment, int idMailingList, Locale locale )
122     {
123         CommentHome.create( comment );
124         sendNotification( comment, idMailingList, NotificationType.CREATE, locale );
125 
126         return comment;
127     }
128 
129     /**
130      * Update of the comment which is specified in parameter and notify the mailing list
131      * 
132      * @param comment
133      *            The instance of the Comment which contains the data to store
134      * @param idMailingList
135      *            the mailing list id to notify
136      * 
137      * @return The instance of the comment which has been updated
138      */
139     public static Commentr/paris/lutece/plugins/appointment/business/comment/Comment.html#Comment">Comment updateAndNotifyMailingList( Comment comment, int idMailingList, Locale locale )
140     {
141         CommentHome.update( comment );
142         sendNotification( comment, idMailingList, NotificationType.UPDATE, locale );
143 
144         return comment;
145     }
146 
147     /**
148      * Remove the comment whose identifier is specified in parameter and notify the mailing list
149      * 
150      * @param nKey
151      *            The comment Id
152      * @param idMailingList
153      *            the mailing list id to notify
154      */
155     public static void removeAndNotifyMailingList( int nKey, int idMailingList, Locale locale )
156     {
157         Comment comment = CommentHome.findByPrimaryKey( nKey );
158         sendNotification( comment, idMailingList, NotificationType.DELETE, locale );
159 
160         CommentHome.remove( nKey );
161     }
162 
163     /**
164      * 
165      * @param comment
166      * @param idMailingList
167      *            the mailing list id to notify
168      * @param locale
169      */
170     public static void sendNotification( Comment comment, int idMailingList, NotificationType type, Locale locale )
171     {
172 
173         CommentNotificationConfig config = CommentNotificationHome.loadCommentNotificationConfigByType( type.name( ) );
174 
175         if ( config != null && idMailingList != -1 )
176         {
177 
178             String strSenderEmail = MailService.getNoReplyEmail( );
179             Collection<Recipient> listRecipients = AdminMailingListService.getRecipients( idMailingList );
180             Map<String, Object> model = fillModel( comment, config );
181 
182             String strContent = AppTemplateService
183                     .getTemplateFromStringFtl( AppTemplateService.getTemplate( TEMPLATE_TASK_NOTIFY_MAIL, locale, model ).getHtml( ), locale, model )
184                     .getHtml( );
185             // Send Mail
186             for ( Recipient recipient : listRecipients )
187             {
188                 // Build the mail message
189                 MailService.sendMailHtml( recipient.getEmail( ), config.getSenderName( ), strSenderEmail, config.getSubject( ), strContent );
190             }
191         }
192 
193     }
194 
195     public static List<CommentDTO> buildCommentDTO( List<Comment> listComment )
196     {
197 
198         List<CommentDTO> listComments = new ArrayList<>( );
199         List<Form> listFom = FormService.findAllForms( );
200         Collection<AdminUser> listAdminUser = AdminUserHome.findUserList( );
201         AdminUser user = null;
202         Form frm = null;
203         for ( Comment comment : listComment )
204         {
205             CommentDTOtment/web/dto/CommentDTO.html#CommentDTO">CommentDTO commentDTO = new CommentDTO( );
206             commentDTO.setId( comment.getId( ) );
207             commentDTO.setComment( comment.getComment( ) );
208             commentDTO.setCreationDate( comment.getCreationDate( ) );
209             commentDTO.setCreatorUserName( comment.getCreatorUserName( ) );
210             commentDTO.setEndingValidityDate( comment.getEndingValidityDate( ) );
211             commentDTO.setEndingValidityTime( comment.getEndingValidityTime( ) );
212             commentDTO.setIdForm( comment.getIdForm( ) );
213             commentDTO.setStartingValidityDate( comment.getStartingValidityDate( ) );
214             commentDTO.setStartingValidityTime( comment.getStartingValidityTime( ) );
215 
216             frm = listFom.stream( ).filter( form -> form.getIdForm( ) == comment.getIdForm( ) ).findAny( ).orElse( null );
217             user = listAdminUser.stream( ).filter( usr -> usr.getAccessCode( ).equals( comment.getCreatorUserName( ) ) ).findAny( ).orElse( null );
218             if ( frm != null )
219             {
220                 commentDTO.setFormTitle( frm.getTitle( ) );
221             }
222             if ( user != null )
223             {
224                 commentDTO.setUserFirstName( user.getFirstName( ) );
225                 commentDTO.setUserLastName( user.getLastName( ) );
226             }
227             listComments.add( commentDTO );
228         }
229 
230         return listComments;
231     }
232 
233     /**
234      * Get a model to generate email content for a given comment and a given task.
235      * 
236      * @param comment
237      *            The comment
238      * @param locale
239      *            The locale
240      * @return The model with data
241      */
242     private static Map<String, Object> fillModel( Comment comment, CommentNotificationConfig config )
243     {
244         Map<String, Object> model = new HashMap<>( );
245         Form form = FormService.findFormLightByPrimaryKey( comment.getIdForm( ) );
246 
247         AdminUser user = AdminUserHome.findUserByLogin( comment.getCreatorUserName( ) );
248         StringBuilder builder = new StringBuilder( );
249         builder.append( user.getFirstName( ) ).append( StringUtils.SPACE ).append( user.getLastName( ) );
250         model.put( MARK_COMMENT, comment.getComment( ) );
251         model.put( MARK_CREATION_DATE, comment.getCreationDate( ).format( Utilities.getFormatter( ) ) );
252         model.put( MARK_FORM_ID, form.getIdForm( ) );
253         model.put( MARK_FORM_TITLE, form.getTitle( ) );
254         model.put( MARK_CREATOR_USER_NAME, builder.toString( ) );
255         model.put( MARK_DATE_END_VALIDITY, comment.getEndingValidityDate( ).format( Utilities.getFormatter( ) ) );
256         model.put( MARK_TIME_END_VALIDITY, comment.getEndingValidityTime( ) );
257         model.put( MARK_DATE_START_VALIDITY, comment.getStartingValidityDate( ).format( Utilities.getFormatter( ) ) );
258         model.put( MARK_TIME_START_VALIDITY, comment.getStartingValidityTime( ) );
259         model.put( MARK_MESSAGE, config.getMessage( ) );
260 
261         return model;
262     }
263 
264 }