View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.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   * ProcessorNotifySender s
61   *
62   */
63  public class ProcessorNotifySender extends OutputProcessor
64  {
65      //templates
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       * (non-Javadoc)
91       * @see fr.paris.lutece.plugins.form.business.outputprocessor.IOutputProcessor#getOutputConfigForm(fr.paris.lutece.plugins.form.business.Form, java.util.Locale, fr.paris.lutece.portal.service.plugin.Plugin)
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      * (non-Javadoc)
115      * @see fr.paris.lutece.plugins.form.business.outputprocessor.IOutputProcessor#doOutputConfigForm(javax.servlet.http.HttpServletRequest, java.util.Locale, fr.paris.lutece.portal.service.plugin.Plugin)
116      */
117     public String doOutputConfigForm( HttpServletRequest request, Locale locale, Plugin plugin )
118     {
119         // TODO Auto-generated method stub
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      * (non-Javadoc)
156      * @see fr.paris.lutece.plugins.form.business.outputprocessor.IOutputProcessor#process(fr.paris.lutece.plugins.form.business.FormSubmit, javax.servlet.http.HttpServletRequest, fr.paris.lutece.portal.service.plugin.Plugin)
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             //convert the value of the object response to string 
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             // Send Mail
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      * Get the configuration data
241      * @param request the request
242      * @param configuration the configuration object
243      * @param locale the locale
244      * @return Message error if error appear else null
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; // No error
274     }
275 }