View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.participatorybudget.service.notify;
35  
36  import java.util.HashMap;
37  import java.util.HashSet;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  import java.util.Set;
42  
43  import javax.inject.Inject;
44  import javax.inject.Named;
45  import javax.servlet.http.HttpServletRequest;
46  
47  import org.apache.commons.lang.StringUtils;
48  
49  import fr.paris.lutece.plugins.document.business.Document;
50  import fr.paris.lutece.plugins.document.business.DocumentHome;
51  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
52  import fr.paris.lutece.plugins.extend.service.extender.history.IResourceExtenderHistoryService;
53  import fr.paris.lutece.plugins.participatorybudget.business.notify.TaskNotifyDocumentbpConfig;
54  import fr.paris.lutece.plugins.participatorybudget.service.project.ProjectService;
55  import fr.paris.lutece.plugins.subscribe.business.Subscription;
56  import fr.paris.lutece.plugins.subscribe.business.SubscriptionFilter;
57  import fr.paris.lutece.plugins.subscribe.service.SubscriptionService;
58  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
59  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
60  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
61  import fr.paris.lutece.plugins.workflowcore.service.task.SimpleTask;
62  import fr.paris.lutece.portal.service.mail.MailService;
63  import fr.paris.lutece.portal.service.prefs.UserPreferencesService;
64  import fr.paris.lutece.portal.service.template.AppTemplateService;
65  import fr.paris.lutece.portal.service.util.AppLogService;
66  import fr.paris.lutece.portal.service.util.AppPathService;
67  import fr.paris.lutece.portal.service.util.AppPropertiesService;
68  
69  /**
70   * 
71   * TaskNotifyIdeation Task
72   *
73   */
74  public class TaskNotifyDocumentbp extends SimpleTask
75  {
76      // BEANS CONFIG
77      public static final String CONFIG_SERVICE_BEAN_NAME = "workflow-notifydocumentbp.taskNotifyDocumentbpConfigService";
78  
79      // PARAMETERS
80      private static final String PARAM_BP_EMAIL = "participatorybudget.email";
81  
82      private static final String PROPERTY_LUTECE_PLF_NAME = "lutece.plf.name";
83  
84      // MARKS
85      private static final String MARK_PSEUDO_ABONNEE = "pseudo_abonnee";
86      private static final String MARK_TITRE_PROJET = "titre_projet";
87      private static final String MARK_NUMERO_LUTECE_DOCUMENT = "numero_lutece_document";
88      private static final String MARK_URL_FICHE = "url_fiche";
89      private static final String MARK_PHASE_ACTUELLE_PROJET = "phase_actuelle_projet";
90      private static final String MARK_MESSAGE_NOTIFICATION = "message_notification";
91  
92      public static final String CONSTANT_RESOURCE_TYPE = "DOCUMENT_BP";
93  
94      private static final String DSKEY_MAIL_RECIPIENT = "workflow-notifydocumentbp.site_property.admin_mail.recipient";
95      private static final String DSKEY_MAIL_SEND_ACTIVATED = "workflow-notifydocumentbp.site_property.admin_mail.activated";
96      private static final String DSKEY_MAIL_SENDER = "workflow-notifydocumentbp.site_property.admin_mail.sender";
97  
98      // ERRORS
99      private static final String ERROR_GET_EMAIL = "Ideation, failed to get email";
100 
101     @Inject
102     private IResourceHistoryService _resourceHistoryService;
103     @Inject
104     @Named( CONFIG_SERVICE_BEAN_NAME )
105     private ITaskConfigService _taskNotifyIdeationConfigService;
106     @Inject
107     private IResourceExtenderHistoryService _resourceExtenderHistoryService;
108 
109     private StringBuffer _strLog = new StringBuffer( );
110 
111     @Override
112     public String getTitle( Locale locale )
113     {
114         return "Document Bp Notify";
115     }
116 
117     @Override
118     public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
119     {
120         _strLog = new StringBuffer( "	    - Processing task 'TaskNotifyDocumentbp' for resourceHistory #" + nIdResourceHistory + "\n" );
121 
122         TaskNotifyDocumentbpConfig config = _taskNotifyIdeationConfigService.findByPrimaryKey( this.getId( ) );
123 
124         ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
125 
126         if ( ( resourceHistory != null ) && CONSTANT_RESOURCE_TYPE.equals( resourceHistory.getResourceType( ) ) )
127         {
128             Document document = DocumentHome.findByPrimaryKey( resourceHistory.getIdResource( ) );
129             _strLog.append( "	    - Document number is #" + document.getId( ) + "\n" );
130 
131             // Get realization message
132             String realizationNotify = "";
133             DocumentAttribute realizationNotifyAttr = document.getAttribute( "realization_notify" );
134             if ( realizationNotifyAttr == null )
135             {
136                 _strLog.append( "ERROR : Unable to find attribute 'realization_notify' for document #" + document.getId( ) + " !" + "\n" );
137             }
138             else
139             {
140                 realizationNotify = realizationNotifyAttr.getTextValue( ).trim( );
141             }
142 
143             // Get list of user guid to notify
144             Set<String> userGuids = new HashSet<String>( );
145 
146             Set<String> userGuidDocumentFollowers = new HashSet<String>( );
147 
148             if ( config.isAbonnes( ) )
149             {
150                 userGuidDocumentFollowers.addAll( getActiveSubscribersGuid( ProjectService.getInstance( ).getUniqueUserGuidsProjectFollowers( document ) ) );
151             }
152 
153             userGuids.addAll( userGuidDocumentFollowers );
154 
155             // Process notify
156             sendMailToUserGuidSet( document.getId( ), document.getTitle( ), realizationNotify, userGuids, config, locale );
157 
158             _strLog.append( "	      Nb notifiable users (possible duplicates) :" + "\n" );
159             _strLog.append( "	        --> Followers    of project       = " + userGuidDocumentFollowers.size( ) + "\n" );
160             _strLog.append( "	      Nb notified users (without duplicates) = " + userGuids.size( ) + "\n" );
161         }
162         else
163         {
164             _strLog.append( "ERROR : unable to find resourceHistory '" + nIdResourceHistory + "' !" + "\n" );
165         }
166 
167         AppLogService.info( _strLog.toString( ) );
168         sendAdminMail( _strLog.toString( ) );
169     }
170 
171     // *********************************************************************************************
172     // * SUBSCRIBER SUBSCRIBER SUBSCRIBER SUBSCRIBER SUBSCRIBER SUBSCRIBER SUBSCRIBER SUBSCRIBER S *
173     // * SUBSCRIBER SUBSCRIBER SUBSCRIBER SUBSCRIBER SUBSCRIBER SUBSCRIBER SUBSCRIBER SUBSCRIBER S *
174     // *********************************************************************************************
175 
176     /**
177      * Returns a Set wchich contains only user the notify of whom is activated.
178      */
179     private Set<String> getActiveSubscribersGuid( Set<String> userGuids )
180     {
181 
182         Set<String> activeSubscriberGuids = new HashSet<String>( );
183 
184         SubscriptionFilter filterSubUpdate = new SubscriptionFilter( );
185         filterSubUpdate.setSubscriptionProvider( "participatoryideation.subscriptionProviderName" );
186         filterSubUpdate.setSubscriptionKey( "updateOnRealization" );
187         List<Subscription> listSubUpdate = SubscriptionService.getInstance( ).findByFilter( filterSubUpdate );
188 
189         for ( Subscription subscription : listSubUpdate )
190         {
191             if ( userGuids.contains( subscription.getUserId( ) ) )
192             {
193                 activeSubscriberGuids.add( subscription.getUserId( ) );
194             }
195         }
196 
197         return activeSubscriberGuids;
198     }
199 
200     // ***********************************************************************************
201     // * SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND *
202     // * SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND SEND *
203     // ***********************************************************************************
204 
205     private void sendMailToUserGuidSet( int documentId, String documentTitle, String realizationNotify, Set<String> userGuidSet,
206             TaskNotifyDocumentbpConfig config, Locale locale )
207     {
208         for ( String userGuid : userGuidSet )
209         {
210             sendMailToUserGuid( documentId, documentTitle, realizationNotify, userGuid, config, locale );
211         }
212     }
213 
214     private void sendMailToUserGuid( int documentId, String documentTitle, String realizationNotify, String userGuid, TaskNotifyDocumentbpConfig config,
215             Locale locale )
216     {
217 
218         String strSenderName = config.getSenderName( );
219         String strRecipientBcc = config.getRecipientsBcc( );
220         String strRecipientCc = config.getRecipientsCc( );
221         String strSubject = config.getSubject( );
222 
223         String strMessage = StringUtils.EMPTY;
224 
225         String strEmail = StringUtils.EMPTY;
226         String strSenderEmail = config.getSenderEmail( );
227 
228         String strNickNameUser = UserPreferencesService.instance( ).getNickname( userGuid );
229 
230         _strLog.append( "	    - Generating mail for user '" + userGuid + "'..." );
231 
232         Map<String, Object> model = new HashMap<String, Object>( );
233         model.put( MARK_PSEUDO_ABONNEE, strNickNameUser );
234         model.put( MARK_TITRE_PROJET, documentTitle );
235         model.put( MARK_NUMERO_LUTECE_DOCUMENT, documentId );
236         model.put( MARK_URL_FICHE,
237                 AppPathService.getProdUrl( "https://budgetparticipatif.paris.fr/bp" ) + "jsp/site/Portal.jsp?document_id=" + documentId + "&portlet_id=158" );
238         model.put( MARK_PHASE_ACTUELLE_PROJET, "" );
239         model.put( MARK_MESSAGE_NOTIFICATION, realizationNotify );
240 
241         strMessage = AppTemplateService.getTemplateFromStringFtl( config.getMessage( ), locale, model ).getHtml( );
242 
243         _strLog.append( " done." + "\n" );
244 
245         try
246         {
247             strEmail = UserPreferencesService.instance( ).get( userGuid, PARAM_BP_EMAIL, StringUtils.EMPTY );
248         }
249         catch( Exception e )
250         {
251             throw new RuntimeException( ERROR_GET_EMAIL, e );
252         }
253 
254         MailService.sendMailHtml( strEmail, strRecipientCc, strRecipientBcc, strSenderName, strSenderEmail, strSubject, strMessage );
255     }
256 
257     // ***********************************************************************************
258     // * RESULT_MAIL RESULT_MAIL RESULT_MAIL RESULT_MAIL RESULT_MAIL RESULT_MAIL RESULT_ *
259     // * RESULT_MAIL RESULT_MAIL RESULT_MAIL RESULT_MAIL RESULT_MAIL RESULT_MAIL RESULT_ *
260     // ***********************************************************************************
261 
262     private void sendAdminMail( String str )
263     {
264         String strMailActivated = AppPropertiesService.getProperty( DSKEY_MAIL_SEND_ACTIVATED, "0" );
265 
266         if ( "1".equals( strMailActivated ) )
267         {
268             String lutecePlfName = AppPropertiesService.getProperty( PROPERTY_LUTECE_PLF_NAME, "unidentified Lutece platform" );
269             String strRecipient = AppPropertiesService.getProperty( DSKEY_MAIL_RECIPIENT, "ddct-mpc-site-bp@paris.fr" );
270             String strSenderEmail = AppPropertiesService.getProperty( DSKEY_MAIL_SENDER, "no-reply@paris.fr" );
271 
272             String strTitle = "BP (" + lutecePlfName + ") - Mailing about realization (from UpdateSuiviSyntProjetsFromEudonet)";
273 
274             MailService.sendMailText( strRecipient, "No Reply", strSenderEmail, strTitle, str );
275         }
276     }
277 }