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.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
63 private static final String TEMPLATE_TASK_NOTIFY_MAIL = "admin/plugins/appointment/notification/task_notify_comment_mail.html";
64
65
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
84
85
86
87
88
89
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
99
100
101
102
103
104
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
114
115
116
117
118
119
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
131
132
133
134
135
136
137
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
149
150
151
152
153
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
166
167
168
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
186 for ( Recipient recipient : listRecipients )
187 {
188
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
235
236
237
238
239
240
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 }