View Javadoc

1   /*
2    * Copyright (c) 2002-2014, 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  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   * NotificationService
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      * NotificationService
81      *
82      */
83      private NotificationService(  )
84      {
85      }
86  
87      /**
88       * Notifaction when a notice change
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                 // let indexing fail explicitly
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                         // FIXME : wildcard group and role are not handled
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                 // mailSender = null; // not needed anymore
175             }
176         }
177     }
178 
179     /**
180      * Return the mail content
181      * @param notice Notice
182      * @return the mail content
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 }