View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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  
35  
36  package fr.paris.lutece.plugins.easyrulesbot.modules.sitebuilder.service;
37  
38  import fr.paris.lutece.plugins.easyrulesbot.service.bot.BotExecutor;
39  import fr.paris.lutece.plugins.easyrulesbot.modules.sitebuilder.Constants;
40  import fr.paris.lutece.plugins.easyrulesbot.service.response.exceptions.ResponseProcessingException;
41  import fr.paris.lutece.plugins.easyrulesbot.service.response.processors.AbstractProcessor;
42  import fr.paris.lutece.plugins.easyrulesbot.service.response.processors.ResponseProcessor;
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.template.AppTemplateService;
46  import fr.paris.lutece.portal.web.LocalVariables;
47  import fr.paris.lutece.portal.web.l10n.LocaleService;
48  import fr.paris.lutece.util.html.HtmlTemplate;
49  import fr.paris.lutece.util.mail.FileAttachment;
50  import java.util.ArrayList;
51  import java.util.List;
52  import java.util.Locale;
53  import java.util.Map;
54  import javax.servlet.http.HttpServletRequest;
55  import javax.servlet.http.HttpSession;
56  
57  /**
58   * PomBuilderProcessor
59   */
60  public class PomBuilderProcessor extends AbstractProcessor implements ResponseProcessor
61  {
62      private static final String PROPERTY_LAST_MESSAGE = "module.easyrulesbot.sitebuilder.lastMessage";
63      private static final String PROPERTY_MAIL_SENDER = "module.easyrulesbot.sitebuilder.mail.sender.name";
64      private static final String PROPERTY_MAIL_SENDER_EMAIL = "module.easyrulesbot.sitebuilder.mail.sender.email";
65      private static final String PROPERTY_MAIL_SUBJECT = "module.easyrulesbot.sitebuilder.mail.subject";
66      private static final String PROPERTY_MAIL_MESSAGE = "module.easyrulesbot.sitebuilder.mail.message";
67      private PomBuilder _pomBuilder;
68      
69      /**
70       * Set the pom builder
71       * 
72       * @param pomBuilder
73       *            The pom builder
74       */
75      public void setPomBuilder( PomBuilder pomBuilder )
76      {
77          _pomBuilder = pomBuilder;
78      }
79  
80      /**
81       * {@inheritDoc }
82       */
83      @Override
84      public String processResponse( String strResponse, Locale locale, Map mapData ) throws ResponseProcessingException
85      {
86          String strPom = _pomBuilder.buildPom( mapData );
87          HttpServletRequest request = LocalVariables.getRequest();
88          HttpSession session = request.getSession( true );
89          session.setAttribute( Constants.SESSION_ATTRIBUTE_POM, strPom );
90  
91          String strEmail = (String) mapData.get( BotExecutor.DATA_USER_EMAIL );
92          if ( strEmail != null )
93          {
94              sendMail( strEmail, strPom, locale, mapData );
95          }
96          String strMessage = I18nService.getLocalizedString( PROPERTY_LAST_MESSAGE, locale );
97  
98          return strMessage;
99      }
100 
101     /**
102      * Send the pom.xml file by mail
103      * 
104      * @param strRecipient
105      *            The recipient
106      * @param strPom
107      *            The POM content
108      * @param locale
109      *            The locale
110      * @param mapData
111      *            The data
112      */
113     private void sendMail( String strRecipient, String strPom, Locale locale, Map<String, String> mapData )
114     {
115         String strSender = I18nService.getLocalizedString( PROPERTY_MAIL_SENDER, locale );
116         String strSenderEmail = I18nService.getLocalizedString( PROPERTY_MAIL_SENDER_EMAIL, locale );
117         String strSubject = I18nService.getLocalizedString( PROPERTY_MAIL_SUBJECT, locale );
118         String strMessageTemplate = I18nService.getLocalizedString( PROPERTY_MAIL_MESSAGE, locale );
119         HtmlTemplate template = AppTemplateService.getTemplateFromStringFtl( strMessageTemplate, LocaleService.getDefault( ), mapData );
120         String strMessage = template.getHtml( );
121         FileAttachment file = new FileAttachment( "pom.xml", strPom.getBytes( ), "text/plain" );
122         List<FileAttachment> filesAttachement = new ArrayList<FileAttachment>( );
123         filesAttachement.add( file );
124         MailService.sendMailMultipartHtml( strRecipient, null, null, strSender, strSenderEmail, strSubject, strMessage, null, filesAttachement );
125     }
126 }