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.formengine.web.mail;
35  
36  import fr.paris.lutece.plugins.formengine.business.mail.MailConfiguration;
37  import fr.paris.lutece.plugins.formengine.business.mail.MailConfigurationHome;
38  import fr.paris.lutece.portal.service.message.AdminMessage;
39  import fr.paris.lutece.portal.service.message.AdminMessageService;
40  import fr.paris.lutece.portal.service.template.AppTemplateService;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
44  import fr.paris.lutece.portal.web.constants.Messages;
45  import fr.paris.lutece.portal.web.constants.Parameters;
46  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
47  import fr.paris.lutece.util.html.HtmlTemplate;
48  import fr.paris.lutece.util.html.Paginator;
49  import fr.paris.lutece.util.sort.AttributeComparator;
50  import fr.paris.lutece.util.string.StringUtil;
51  
52  import org.apache.commons.beanutils.BeanUtils;
53  import org.apache.commons.lang.StringUtils;
54  
55  import java.lang.reflect.InvocationTargetException;
56  
57  import java.util.Collections;
58  import java.util.HashMap;
59  import java.util.List;
60  import java.util.Map;
61  
62  import javax.servlet.http.HttpServletRequest;
63  
64  
65  /**
66   *
67   * MailConfigurationJspBean
68   *
69   */
70  public class MailConfigurationJspBean extends PluginAdminPageJspBean
71  {
72      public static final String RIGHT_MAIL_CONFIGURATION = "FORMENGINE_MAIL_CONFIGURATION_MANAGEMENT";
73      private static final String PROPERTY_MAIL_CONFIGURATION_PER_PAGE = "formengine.mail_configuration.itemsPerPage";
74      private static final String MARK_MAIL_CONFIGURATION = "mail_configuration";
75      private static final String MARK_MAIL_CONFIGURATION_LIST = "mail_configuration_list";
76      private static final String MARK_PAGINATOR = "paginator";
77      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
78      private static final String PARAMETER_FORM = "form";
79      private static final String PARAMETER_SENDER_MAIL = "sender_mail";
80      private static final String PARAMETER_SENDER_NAME = "sender_name";
81      private static final String PARAMETER_OBJECT = "object";
82      private static final String PARAMETER_CANCEL = "cancel";
83      private static final String PROPERTY_MESSAGE_MANAGE_MAIL_CONFIGURATIONS_TITLE = "formengine.mail_configuration.label.manage.title";
84      private static final String PROPERTY_MESSAGE_MODIFY_MAIL_CONFIGURATION_TITLE = "formengine.mail_configuration.label.modify_mail_configuration.title";
85      private static final String PROPERTY_MESSAGE_ERROR_MAIL_FORMAT = "formengine.mail_configuration.error.mailFormat";
86  
87      //private static final String PROPERTY_MESSAGE_CONFIRM_REMOVE_MAIL_CONFIGURATION = "formengine.mail_configuration.message.confirmRemove";
88  
89      // Templates files path
90      private static final String TEMPLATE_MANAGE_MAIL_CONFIGURATIONS = "admin/plugins/formengine/mail/manage_mail_configurations.html";
91      private static final String TEMPLATE_MODIFY_MAIL_CONFIGURATION = "admin/plugins/formengine/mail/modify_mail_configuration.html";
92  
93      // JSP
94      // private static final String JSP_REMOVE_MAIL_CONFIGURATION = "jsp/admin/plugins/formengine/mail/DoRemoveMailConfiguration.jsp";
95  
96      // variables
97      private int _nItemsPerPage;
98      private int _nDefaultItemsPerPage;
99      private String _strCurrentPageIndex;
100 
101     /**
102      * Gets the list of mail configurations
103      * @param request the request
104      * @return html code
105      */
106     public String getManageMailConfiguration( HttpServletRequest request )
107     {
108         List<MailConfiguration> listMailConfiguration = MailConfigurationHome.getMailConfigurationsList( getPlugin(  ) );
109 
110         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
111         String strAscSort = null;
112 
113         if ( strSortedAttributeName != null )
114         {
115             strAscSort = request.getParameter( Parameters.SORTED_ASC );
116 
117             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
118 
119             Collections.sort( listMailConfiguration, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
120         }
121 
122         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_MAIL_CONFIGURATION_PER_PAGE, 50 );
123         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
124         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
125                 _nDefaultItemsPerPage );
126 
127         LocalizedPaginator<MailConfiguration> paginator = new LocalizedPaginator<MailConfiguration>( listMailConfiguration,
128                 _nItemsPerPage, getHomeUrl( request ), Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex,
129                 getLocale(  ) );
130 
131         Map<String, Object> model = new HashMap<String, Object>(  );
132         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
133         model.put( MARK_PAGINATOR, paginator );
134         model.put( MARK_MAIL_CONFIGURATION_LIST, paginator.getPageItems(  ) );
135 
136         setPageTitleProperty( PROPERTY_MESSAGE_MANAGE_MAIL_CONFIGURATIONS_TITLE );
137 
138         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_MAIL_CONFIGURATIONS, getLocale(  ),
139                 model );
140 
141         return getAdminPage( template.getHtml(  ) );
142     }
143 
144     /**
145      * Gets modify form
146      * @param request the request
147      * @return html content
148      */
149     public String getModifyMailConfiguration( HttpServletRequest request )
150     {
151         String strForm = request.getParameter( PARAMETER_FORM );
152 
153         if ( StringUtils.isBlank( strForm ) )
154         {
155             throw new IllegalArgumentException( "Form not specified." );
156         }
157 
158         MailConfiguration mailConfiguration = MailConfigurationHome.findByPrimaryKey( strForm, getPlugin(  ) );
159 
160         if ( mailConfiguration == null )
161         {
162             throw new IllegalArgumentException( "Form not found for parameter " + strForm );
163         }
164 
165         Map<String, Object> model = new HashMap<String, Object>(  );
166 
167         model.put( MARK_MAIL_CONFIGURATION, mailConfiguration );
168 
169         setPageTitleProperty( PROPERTY_MESSAGE_MODIFY_MAIL_CONFIGURATION_TITLE );
170 
171         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_MAIL_CONFIGURATION, getLocale(  ), model );
172 
173         return getAdminPage( template.getHtml(  ) );
174     }
175 
176     /**
177      * Displays a confirm message
178      * @param request the request
179      * @return confirm message url
180      */
181 
182     /* public String getConfirmRemoveMailConfiguration( HttpServletRequest request )
183     {
184             String strForm = request.getParameter( PARAMETER_FORM );
185     
186             if ( StringUtils.isBlank( strForm ) )
187             {
188                     return getHomeUrl( request );
189             }
190     
191             return AdminMessageService.getMessageUrl( request,
192                             MESSAGE_CONFIRM_REMOVE_MAIL_CONFIGURATION, JSP_REMOVE_MAIL_CONFIGURATION,
193                             AdminMessage.TYPE_CONFIRMATION );
194     } */
195 
196     /**
197      * Modifies the mail config
198      * @param request the request
199      * @return home url, or error message url if any.
200      */
201     public String doModifyMailConfiguration( HttpServletRequest request )
202     {
203         if ( request.getParameter( PARAMETER_CANCEL ) != null )
204         {
205             return getHomeUrl( request );
206         }
207 
208         String strForm = request.getParameter( PARAMETER_FORM );
209 
210         if ( StringUtils.isBlank( strForm ) )
211         {
212             return getHomeUrl( request );
213         }
214 
215         MailConfiguration mailConfiguration = MailConfigurationHome.findByPrimaryKey( strForm, getPlugin(  ) );
216 
217         if ( mailConfiguration == null )
218         {
219             throw new IllegalArgumentException( "No mail configuration found for form " + strForm );
220         }
221 
222         if ( AppLogService.isDebugEnabled(  ) )
223         {
224             describeBean( "Updating mail configuration : ", mailConfiguration );
225         }
226 
227         String strError = getMailConfigurationData( mailConfiguration, request );
228 
229         if ( strError != null )
230         {
231             return strError;
232         }
233 
234         if ( AppLogService.isDebugEnabled(  ) )
235         {
236             describeBean( "To : ", mailConfiguration );
237         }
238 
239         MailConfigurationHome.update( mailConfiguration, getPlugin(  ) );
240 
241         return getHomeUrl( request );
242     }
243 
244     /**
245      * Removes the mail config
246      * @param request the request
247      * @return home url
248      */
249 
250     /* public String doRemoveMailConfiguration( HttpServletRequest request )
251     {
252             String strForm = request.getParameter( PARAMETER_FORM );
253     
254             if ( StringUtils.isBlank( strForm ) )
255             {
256                     return getHomeUrl( request );
257             }
258     
259             MailConfigurationHome.remove( strForm, getPlugin(  ) );
260     
261             return getHomeUrl( request );
262     } */
263 
264     /**
265      * Gets mail configuration data from the request
266      * @param mailConfiguration the mail configuration to fill
267      * @param request the request
268      * @return error message url if any error, <code>null</code> otherwise.
269      */
270     private String getMailConfigurationData( MailConfiguration mailConfiguration, HttpServletRequest request )
271     {
272         String strSenderMail = request.getParameter( PARAMETER_SENDER_MAIL );
273         String strSenderName = request.getParameter( PARAMETER_SENDER_NAME );
274         String strForm = request.getParameter( PARAMETER_FORM );
275         String strObject = request.getParameter( PARAMETER_OBJECT );
276 
277         if ( isBlank( strSenderMail, strSenderName, strForm, strObject ) )
278         {
279             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
280         }
281 
282         if ( !StringUtil.checkEmail( strSenderMail ) )
283         {
284             return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_ERROR_MAIL_FORMAT,
285                 AdminMessage.TYPE_STOP );
286         }
287 
288         mailConfiguration.setForm( strForm );
289         mailConfiguration.setSenderName( strSenderName );
290         mailConfiguration.setSenderMail( strSenderMail );
291         mailConfiguration.setObject( strObject );
292 
293         return null;
294     }
295 
296     /**
297      *
298      * Checks if at least one value is blank
299      * @param values values to checks
300      * @return <code>true</code> if at least one value is blank, <code>false</code> otherwise.
301      * @see StringUtils#isBlank(String)
302      */
303     private boolean isBlank( String... values )
304     {
305         for ( String strValue : values )
306         {
307             if ( StringUtils.isBlank( strValue ) )
308             {
309                 return true;
310             }
311         }
312 
313         return false;
314     }
315 
316     /**
317      * Describes bean infos.<br>
318      * <strong>FOR DEBUGGING PURPOSE ONLY</strong>
319      * @param bean the bean to describe
320      * @param strMessage the message prefixing the description
321      * @see BeanUtils#describe(Object)
322      */
323     private void describeBean( String strMessage, Object bean )
324     {
325         try
326         {
327             AppLogService.debug( strMessage + BeanUtils.describe( bean ) );
328         }
329         catch ( IllegalAccessException e )
330         {
331             AppLogService.error( e );
332         }
333         catch ( InvocationTargetException e )
334         {
335             AppLogService.error( e );
336         }
337         catch ( NoSuchMethodException e )
338         {
339             AppLogService.error( e );
340         }
341     }
342 }