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.formengine.service.notice;
35
36 import fr.paris.lutece.plugins.formengine.business.Notice;
37 import fr.paris.lutece.plugins.formengine.business.NoticeFilter;
38 import fr.paris.lutece.plugins.formengine.business.NoticeGroup;
39 import fr.paris.lutece.plugins.formengine.business.NoticeGroupHome;
40 import fr.paris.lutece.plugins.formengine.business.NoticeHome;
41 import fr.paris.lutece.plugins.formengine.service.output.MailOutputProcessor;
42 import fr.paris.lutece.plugins.formengine.service.output.TransactionMailOutputProcessor;
43 import fr.paris.lutece.portal.business.mailinglist.MailingList;
44 import fr.paris.lutece.portal.business.mailinglist.MailingListHome;
45 import fr.paris.lutece.portal.business.mailinglist.MailingListUsersFilter;
46 import fr.paris.lutece.portal.business.user.AdminUser;
47 import fr.paris.lutece.portal.business.workgroup.AdminWorkgroupHome;
48 import fr.paris.lutece.portal.service.i18n.I18nService;
49 import fr.paris.lutece.portal.service.mail.MailService;
50 import fr.paris.lutece.portal.service.plugin.Plugin;
51 import fr.paris.lutece.portal.service.plugin.PluginService;
52 import fr.paris.lutece.portal.service.template.AppTemplateService;
53 import fr.paris.lutece.portal.service.util.AppException;
54 import fr.paris.lutece.util.html.HtmlTemplate;
55
56 import java.util.Collection;
57 import java.util.HashMap;
58 import java.util.List;
59 import java.util.Locale;
60 import java.util.Map;
61
62
63
64
65
66 public final class NotificationService
67 {
68 private static final String CONSTANTE_PLUGIN_NAME_FORM = "formengine";
69 private static final String PROPERTY_MAIL_ENABLE_SUBJECT = "formengine.notice.mail.notification.enable.subject";
70 private static final String PROPERTY_MAIL_ENABLE_SENDERMAIL = "formengine.notice.mail.notification.enable.sender_mail";
71 private static final String PROPERTY_MAIL_ENABLE_SENDERNAME = "formengine.notice.mail.notification.enable.sender_name";
72 private static final String PROPERTY_MAIL_DISABLE_SUBJECT = "formengine.notice.mail.notification.disable.subject";
73 private static final String PROPERTY_MAIL_DISABLE_SENDERMAIL = "formengine.notice.mail.notification.disable.sender_mail";
74 private static final String PROPERTY_MAIL_DISABLE_SENDERNAME = "formengine.notice.mail.notification.disable.sender_name";
75 private static final String MARK_NOTICE = "notice";
76 private static final String TEMPLATE_MAIL_ACTIVE = "admin/plugins/formengine/mail/notification_mail_enable.html";
77 private static final String TEMPLATE_MAIL_INACTIVE = "admin/plugins/formengine/mail/notification_mail_disable.html";
78
79
80
81
82
83 private NotificationService( )
84 {
85 }
86
87
88
89
90 public static void notification( )
91 {
92 Plugin plugin = PluginService.getPlugin( CONSTANTE_PLUGIN_NAME_FORM );
93 NoticeFilter filter = new NoticeFilter( );
94 List<Notice> listNotice = NoticeHome.getNoticeList( filter, plugin );
95 boolean isModify = false;
96
97 for ( Notice notice : listNotice )
98 {
99 MailOutputProcessor mailSender = null;
100
101 NoticeGroup noticeGroup = NoticeGroupHome.findByPrimaryKey( notice.getIdNoticeGroup( ), plugin );
102
103 if ( noticeGroup == null )
104 {
105
106 throw new AppException( "NoticeGroup not found for id=" + notice.getIdNoticeGroup( ) +
107 " and notice id=" + notice.getIdNotice( ) );
108 }
109
110 if ( notice.getIdNotification( ) == 0 )
111 {
112 if ( notice.hasValidDate( ) && notice.isEnabled( ) && ( notice.getIdDiffusion( ) != -1 ) )
113 {
114 mailSender = new TransactionMailOutputProcessor( );
115 mailSender.setEmailSubject( I18nService.getLocalizedString( PROPERTY_MAIL_ENABLE_SUBJECT,
116 Locale.getDefault( ) ) );
117 mailSender.setSenderEmail( I18nService.getLocalizedString( PROPERTY_MAIL_ENABLE_SENDERMAIL,
118 Locale.getDefault( ) ) );
119 mailSender.setSenderName( I18nService.getLocalizedString( PROPERTY_MAIL_ENABLE_SENDERNAME,
120 Locale.getDefault( ) ) );
121 isModify = true;
122 }
123 }
124 else if ( notice.getIdNotification( ) == 1 )
125 {
126 if ( ( !notice.hasValidDate( ) || !notice.isEnabled( ) ) && ( notice.getIdDiffusion( ) != -1 ) )
127 {
128 mailSender = new TransactionMailOutputProcessor( );
129 mailSender.setEmailSubject( I18nService.getLocalizedString( PROPERTY_MAIL_DISABLE_SUBJECT,
130 Locale.getDefault( ) ) );
131 mailSender.setSenderEmail( I18nService.getLocalizedString( PROPERTY_MAIL_DISABLE_SENDERMAIL,
132 Locale.getDefault( ) ) );
133 mailSender.setSenderName( I18nService.getLocalizedString( PROPERTY_MAIL_DISABLE_SENDERNAME,
134 Locale.getDefault( ) ) );
135 isModify = true;
136 }
137 }
138 else if ( notice.getIdNotification( ) == 2 )
139 {
140 isModify = true;
141 }
142
143 if ( mailSender != null )
144 {
145 int nIdDiffusion = notice.getIdDiffusion( );
146
147 if ( nIdDiffusion != -1 )
148 {
149 MailingList mailingList = MailingListHome.findByPrimaryKey( nIdDiffusion + 1 );
150 Collection<MailingListUsersFilter> users = mailingList.getFilters( );
151
152 for ( MailingListUsersFilter group : users )
153 {
154
155 Collection<AdminUser> colAdminUser = AdminWorkgroupHome.getUserListForWorkgroup( group.getWorkgroup( ) );
156
157 for ( AdminUser user : colAdminUser )
158 {
159 mailSender.setRecipientEmail( user.getEmail( ) );
160 MailService.sendMailHtml( mailSender.getRecipientEmail( ), mailSender.getSenderName( ),
161 mailSender.getSenderEmail( ), mailSender.getEmailSubject( ),
162 NotificationService.getMailContent( notice ) );
163 }
164 }
165 }
166 }
167
168 if ( isModify )
169 {
170 notice.setIdNotification( ( notice.getIdNotification( ) + 1 ) % 3 );
171 NoticeHome.update( notice, plugin );
172 isModify = false;
173
174
175 }
176 }
177 }
178
179
180
181
182
183
184 public static String getMailContent( Notice notice )
185 {
186 Map<String, Object> model = new HashMap<String, Object>( );
187 model.put( MARK_NOTICE, notice );
188
189 if ( notice.getIdNotification( ) == 0 )
190 {
191 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MAIL_ACTIVE, Locale.getDefault( ), model );
192
193 return template.getHtml( );
194 }
195 else if ( notice.getIdNotification( ) == 1 )
196 {
197 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MAIL_INACTIVE, Locale.getDefault( ), model );
198
199 return template.getHtml( );
200 }
201
202 return "";
203 }
204 }