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.form.modules.processornotifysender.business;
35
36 import fr.paris.lutece.plugins.form.business.Form;
37 import fr.paris.lutece.plugins.form.business.FormSubmit;
38 import fr.paris.lutece.plugins.form.business.Recap;
39 import fr.paris.lutece.plugins.form.business.RecapHome;
40 import fr.paris.lutece.plugins.form.business.Response;
41 import fr.paris.lutece.plugins.form.business.outputprocessor.OutputProcessor;
42 import fr.paris.lutece.plugins.form.utils.FormUtils;
43 import fr.paris.lutece.portal.service.i18n.I18nService;
44 import fr.paris.lutece.portal.service.mail.MailService;
45 import fr.paris.lutece.portal.service.plugin.Plugin;
46 import fr.paris.lutece.portal.service.template.AppTemplateService;
47 import fr.paris.lutece.portal.service.util.AppLogService;
48 import fr.paris.lutece.portal.service.util.AppPropertiesService;
49 import fr.paris.lutece.util.html.HtmlTemplate;
50
51 import java.util.HashMap;
52 import java.util.Locale;
53 import java.util.Map;
54
55 import javax.servlet.http.HttpServletRequest;
56
57
58
59
60
61
62
63 public class ProcessorNotifySender extends OutputProcessor
64 {
65
66 private static final String TEMPLATE_CONFIGURATION_NOTIFY_SENDER = "admin/plugins/form/modules/processornotifysender/configuration_notify_sender.html";
67 private static final String TEMPLATE_NOTIFICATION_NOTIFY_SENDER = "admin/plugins/form/modules/processornotifysender/notification_notify_sender.html";
68 private static final String TEMPLATE_NOTIFICATION_NOTIFY_SENDER_RECAP = "admin/plugins/form/modules/processornotifysender/notification_notify_sender_recap.html";
69 private static final String PROPERTY_NOTIFICATION_NOTIFY_SENDER_SUBJECT = "module.form.processornotifysender.notification_notify_sender.sender_subject";
70 private static final String PROPERTY_NOTIFICATION_NOTIFY_SENDER_SENDER_NAME = "module.form.processornotifysender.notification_notify_sender.sender_name";
71 private static final String PARAMETER_ID_FORM = "id_form";
72 private static final String PARAMETER_ID_ENTRY_EMAIL_SENDER = "id_entry_email_sender";
73 private static final String PARAMETER_MAIL_MESSAGE = "mail_message";
74 private static final String PARAMETER_SEND_RECAP = "send_recap";
75 private static final String MARK_FORM = "form";
76 private static final String MARK_REF_LIST_ENTRY = "entry_list";
77 private static final String MARK_CONFIGURATION = "configuration";
78 private static final String MARK_RECAP_HTML = "recap";
79 private static final String MARK_MESSAGE_RECAP = "messageRecap";
80 private static final String MARK_LOCALE = "locale";
81 private static final String MARK_FORM_SUBMIT = "formSubmit";
82 private static final String MARK_MESSAGE = "mail_message";
83 private static final String MARK_TITLE = "mail_title";
84 private static final String MESSAGE_CONFIGURATION_ERROR_ENTRY_NOT_SELECTED = "module.form.processornotifysender.message.error.configuration.entry_not_selected";
85 private static final String MESSAGE_ERROR_NO_CONFIGURATION_ASSOCIATED = "module.form.processornotifysender.message.error.no_configuration_associated";
86 private static final String MESSAGE_RECAP_INFORMATION = "module.form.processornotifysender.configuration_notify_sender.send_recap";
87 private static final String PROPERTY_TAG_RECAP = "processornotifysender.recap_tag";
88
89
90
91
92
93 public String getOutputConfigForm( HttpServletRequest request, Form form, Locale locale, Plugin plugin )
94 {
95 NotifySenderConfiguration configuration = NotifySenderConfigurationHome.findByPrimaryKey( form.getIdForm( ),
96 plugin );
97
98 String strMessageRecap = I18nService.getLocalizedString( MESSAGE_RECAP_INFORMATION,
99 new String[] { AppPropertiesService.getProperty( PROPERTY_TAG_RECAP ) }, locale );
100 Map<String, Object> model = new HashMap<String, Object>( );
101 model.put( MARK_FORM, form );
102 model.put( MARK_CONFIGURATION, configuration );
103 model.put( MARK_CONFIGURATION, configuration );
104 model.put( MARK_LOCALE, locale );
105 model.put( MARK_REF_LIST_ENTRY, FormUtils.getRefListAllQuestions( form.getIdForm( ), plugin ) );
106 model.put( MARK_MESSAGE_RECAP, strMessageRecap );
107
108 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CONFIGURATION_NOTIFY_SENDER, locale, model );
109
110 return template.getHtml( );
111 }
112
113
114
115
116
117 public String doOutputConfigForm( HttpServletRequest request, Locale locale, Plugin plugin )
118 {
119
120 String strIdForm = request.getParameter( PARAMETER_ID_FORM );
121 int nIdForm = -1;
122
123 try
124 {
125 nIdForm = Integer.parseInt( strIdForm );
126 }
127 catch ( NumberFormatException ne )
128 {
129 AppLogService.error( ne );
130 }
131
132 NotifySenderConfigurationssornotifysender/business/NotifySenderConfiguration.html#NotifySenderConfiguration">NotifySenderConfiguration configuration = new NotifySenderConfiguration( );
133 configuration.setIdForm( nIdForm );
134
135 String strError = getConfigurationData( request, configuration, locale );
136
137 if ( strError != null )
138 {
139 return strError;
140 }
141
142 if ( NotifySenderConfigurationHome.findByPrimaryKey( nIdForm, plugin ) != null )
143 {
144 NotifySenderConfigurationHome.update( configuration, plugin );
145 }
146 else
147 {
148 NotifySenderConfigurationHome.create( configuration, plugin );
149 }
150
151 return null;
152 }
153
154
155
156
157
158 public String process( FormSubmit formSubmit, HttpServletRequest request, Plugin plugin )
159 {
160 NotifySenderConfiguration configuration = NotifySenderConfigurationHome.findByPrimaryKey( formSubmit.getForm( )
161 .getIdForm( ),
162 plugin );
163
164 if ( configuration == null )
165 {
166 return MESSAGE_ERROR_NO_CONFIGURATION_ASSOCIATED;
167 }
168
169 String strSubject = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_NOTIFY_SENDER_SUBJECT,
170 request.getLocale( ) );
171 String strSenderName = I18nService.getLocalizedString( PROPERTY_NOTIFICATION_NOTIFY_SENDER_SENDER_NAME,
172 request.getLocale( ) );
173 String strSenderEmail = MailService.getNoReplyEmail( );
174
175 String strEmailSender = FormUtils.EMPTY_STRING;
176
177
178 for ( Response response : formSubmit.getListResponse( ) )
179 {
180 if ( response.getEntry( ).getIdEntry( ) == configuration.getIdEntryEmailSender( ) )
181 {
182 strEmailSender = response.getEntry( )
183 .getResponseValueForExport( request, response, request.getLocale( ) );
184 }
185 }
186
187 HashMap<String, Object> model = new HashMap<String, Object>( );
188 Recap recap = RecapHome.findByPrimaryKey( formSubmit.getForm( ).getRecap( ).getIdRecap( ), plugin );
189
190 if ( ( recap != null ) && recap.isRecapData( ) )
191 {
192
193 for ( Response response : formSubmit.getListResponse( ) )
194 {
195 byte[] byResponseValue = response.getValueResponse( );
196
197 if ( byResponseValue != null )
198 {
199 response.setToStringValueResponse( response.getEntry( )
200 .getResponseValueForRecap( request, response,
201 request.getLocale( ) ) );
202 }
203 else
204 {
205 response.setToStringValueResponse( FormUtils.EMPTY_STRING );
206 }
207 }
208 }
209
210 model.put( MARK_RECAP_HTML, recap );
211 model.put( MARK_FORM_SUBMIT, formSubmit );
212
213 HtmlTemplate templateRecap = AppTemplateService.getTemplate( TEMPLATE_NOTIFICATION_NOTIFY_SENDER_RECAP,
214 request.getLocale( ), model );
215
216
217 String strTagRecap = AppPropertiesService.getProperty( PROPERTY_TAG_RECAP );
218 String strMessage = configuration.getMessage( ).replace( strTagRecap, templateRecap.getHtml( ) );
219
220 model.put( MARK_MESSAGE, strMessage );
221 model.put( MARK_TITLE, strSubject );
222
223 HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_NOTIFICATION_NOTIFY_SENDER, request.getLocale( ),
224 model );
225
226 try
227 {
228
229 MailService.sendMailHtml( strEmailSender, strSenderName, strSenderEmail, strSubject, t.getHtml( ) );
230 }
231 catch ( Exception e )
232 {
233 AppLogService.error( " Error during Process > Notify sender : " + e.getMessage( ) );
234 }
235
236 return null;
237 }
238
239
240
241
242
243
244
245
246 private String getConfigurationData( HttpServletRequest request, NotifySenderConfiguration configuration,
247 Locale locale )
248 {
249 String strIdEntryEmailSender = request.getParameter( PARAMETER_ID_ENTRY_EMAIL_SENDER );
250
251 int nIdEntryEmailSender = -1;
252 String strMailMessage = request.getParameter( PARAMETER_MAIL_MESSAGE );
253 String strSendRecap = request.getParameter( PARAMETER_SEND_RECAP );
254
255 try
256 {
257 nIdEntryEmailSender = Integer.parseInt( strIdEntryEmailSender );
258 }
259 catch ( NumberFormatException ne )
260 {
261 AppLogService.error( ne );
262 }
263
264 if ( nIdEntryEmailSender == -1 )
265 {
266 return MESSAGE_CONFIGURATION_ERROR_ENTRY_NOT_SELECTED;
267 }
268
269 configuration.setIdEntryEmailSender( nIdEntryEmailSender );
270
271 configuration.setMessage( strMailMessage );
272
273 return null;
274 }
275 }