1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
72
73
74 public class TaskNotifyDocumentbp extends SimpleTask
75 {
76
77 public static final String CONFIG_SERVICE_BEAN_NAME = "workflow-notifydocumentbp.taskNotifyDocumentbpConfigService";
78
79
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
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
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
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
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
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
173
174
175
176
177
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
202
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
259
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 }