View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of 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.announce.modules.workflow.service;
35  
36  import fr.paris.lutece.plugins.announce.business.Announce;
37  import fr.paris.lutece.plugins.announce.business.AnnounceHome;
38  import fr.paris.lutece.plugins.announce.modules.workflow.business.TaskNotifyAnnounceConfig;
39  import fr.paris.lutece.plugins.genericattributes.business.Response;
40  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
41  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
42  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
43  import fr.paris.lutece.plugins.workflowcore.service.task.SimpleTask;
44  import fr.paris.lutece.portal.service.mail.MailService;
45  import fr.paris.lutece.portal.service.security.LuteceUser;
46  import fr.paris.lutece.portal.service.security.LuteceUserService;
47  import fr.paris.lutece.portal.service.template.AppTemplateService;
48  import fr.paris.lutece.util.string.StringUtil;
49  
50  import org.apache.commons.lang.StringUtils;
51  
52  import java.util.HashMap;
53  import java.util.List;
54  import java.util.Locale;
55  import java.util.Map;
56  
57  import javax.inject.Inject;
58  import javax.inject.Named;
59  import javax.servlet.http.HttpServletRequest;
60  
61  /**
62   * Workflow task to notify a user of an announce
63   */
64  public class TaskNotifyAnnounce extends SimpleTask
65  {
66      /**
67       * Name of the bean of the config service of this task
68       */
69      public static final String CONFIG_SERVICE_BEAN_NAME = "announce-workflow.taskNotifyAnnounceConfigService";
70  
71      // TEMPLATES
72      private static final String TEMPLATE_TASK_NOTIFY_MAIL = "admin/plugins/announce/modules/workflow/task_notify_announce_mail.html";
73  
74      // MARKS
75      private static final String MARK_ANNOUNCE = "announce";
76      private static final String MARK_MESSAGE = "message";
77      private static final String MARK_LIST_RESPONSE = "listResponse";
78  
79      // SERVICES
80      @Inject
81      private IResourceHistoryService _resourceHistoryService;
82      @Inject
83      @Named( CONFIG_SERVICE_BEAN_NAME )
84      private ITaskConfigService _taskNotifyAnnounceConfigService;
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
91      {
92          ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
93          TaskNotifyAnnounceConfig config = _taskNotifyAnnounceConfigService.findByPrimaryKey( this.getId( ) );
94          Announce announce = AnnounceHome.findByPrimaryKey( resourceHistory.getIdResource( ) );
95  
96          LuteceUser user = LuteceUserService.getLuteceUserFromName( announce.getUserName( ) );
97  
98          String strEmail = user != null ? user.getEmail( ) : announce.getUserName( );
99          if ( StringUtil.checkEmail( strEmail ) )
100         {
101             this.sendEmail( announce, resourceHistory, request, locale, config, strEmail );
102         }
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public void doRemoveConfig( )
110     {
111         _taskNotifyAnnounceConfigService.remove( this.getId( ) );
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public String getTitle( Locale locale )
119     {
120         TaskNotifyAnnounceConfig config = _taskNotifyAnnounceConfigService.findByPrimaryKey( this.getId( ) );
121 
122         if ( config != null )
123         {
124             return config.getSubject( );
125         }
126 
127         return StringUtils.EMPTY;
128     }
129 
130     /**
131      * Send an email to a user
132      * 
133      * @param announce
134      *            The announce
135      * @param resourceHistory
136      *            The resource history
137      * @param request
138      *            The request
139      * @param locale
140      *            The locale
141      * @param notifyAnnounceConfig
142      *            The task configuration
143      * @param strEmail
144      *            The address to send the email to
145      */
146     @SuppressWarnings( "deprecation" )
147     public void sendEmail( Announce announce, ResourceHistory resourceHistory, HttpServletRequest request, Locale locale,
148             TaskNotifyAnnounceConfig notifyAnnounceConfig, String strEmail )
149     {
150         if ( ( notifyAnnounceConfig != null ) && ( resourceHistory != null ) && Announce.RESOURCE_TYPE.equals( resourceHistory.getResourceType( ) )
151                 && ( announce != null ) )
152         {
153             if ( StringUtils.isEmpty( notifyAnnounceConfig.getSenderEmail( ) ) || !StringUtil.checkEmail( notifyAnnounceConfig.getSenderEmail( ) ) )
154             {
155                 notifyAnnounceConfig.setSenderEmail( MailService.getNoReplyEmail( ) );
156             }
157 
158             if ( StringUtils.isBlank( notifyAnnounceConfig.getSenderName( ) ) )
159             {
160                 notifyAnnounceConfig.setSenderName( notifyAnnounceConfig.getSenderEmail( ) );
161             }
162 
163             Map<String, Object> model = new HashMap<String, Object>( );
164 
165             model.put( MARK_ANNOUNCE, announce );
166             model.put( MARK_MESSAGE, notifyAnnounceConfig.getMessage( ) );
167 
168             List<Response> listResponse = AnnounceHome.findListResponse( announce.getId( ), false );
169             model.put( MARK_LIST_RESPONSE, listResponse );
170 
171             String strSubject = AppTemplateService.getTemplateFromStringFtl( notifyAnnounceConfig.getSubject( ), locale, model ).getHtml( );
172 
173             boolean bHasRecipients = ( StringUtils.isNotBlank( notifyAnnounceConfig.getRecipientsBcc( ) )
174                     || StringUtils.isNotBlank( notifyAnnounceConfig.getRecipientsCc( ) ) );
175 
176             String strContent = AppTemplateService
177                     .getTemplateFromStringFtl( AppTemplateService.getTemplate( TEMPLATE_TASK_NOTIFY_MAIL, locale, model ).getHtml( ), locale, model )
178                     .getHtml( );
179 
180             if ( bHasRecipients )
181             {
182                 MailService.sendMailHtml( strEmail, notifyAnnounceConfig.getRecipientsCc( ), notifyAnnounceConfig.getRecipientsBcc( ),
183                         notifyAnnounceConfig.getSenderName( ), notifyAnnounceConfig.getSenderEmail( ), strSubject, strContent );
184             }
185             else
186             {
187                 MailService.sendMailHtml( strEmail, notifyAnnounceConfig.getSenderName( ), notifyAnnounceConfig.getSenderEmail( ), strSubject, strContent );
188             }
189         }
190     }
191 }