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.calendar.service;
35  
36  import fr.paris.lutece.plugins.calendar.business.CalendarSubscriber;
37  import fr.paris.lutece.plugins.calendar.business.CalendarSubscriberHome;
38  import fr.paris.lutece.plugins.calendar.business.Event;
39  import fr.paris.lutece.plugins.calendar.business.notification.CalendarNotification;
40  import fr.paris.lutece.plugins.calendar.business.notification.CalendarNotificationHome;
41  import fr.paris.lutece.plugins.calendar.web.Constants;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
43  import fr.paris.lutece.portal.service.mail.MailService;
44  import fr.paris.lutece.portal.service.message.SiteMessage;
45  import fr.paris.lutece.portal.service.message.SiteMessageException;
46  import fr.paris.lutece.portal.service.message.SiteMessageService;
47  import fr.paris.lutece.portal.service.plugin.Plugin;
48  import fr.paris.lutece.portal.service.plugin.PluginService;
49  import fr.paris.lutece.portal.service.template.AppTemplateService;
50  import fr.paris.lutece.portal.service.util.AppPathService;
51  import fr.paris.lutece.portal.service.util.AppPropertiesService;
52  import fr.paris.lutece.portal.web.LocalVariables;
53  import fr.paris.lutece.portal.web.constants.Messages;
54  import fr.paris.lutece.util.html.HtmlTemplate;
55  import fr.paris.lutece.util.string.StringUtil;
56  import fr.paris.lutece.util.url.UrlItem;
57  
58  import java.sql.Timestamp;
59  import java.util.Calendar;
60  import java.util.Collection;
61  import java.util.Date;
62  import java.util.GregorianCalendar;
63  import java.util.HashMap;
64  import java.util.UUID;
65  
66  import javax.servlet.ServletConfig;
67  import javax.servlet.http.HttpServletRequest;
68  import javax.servlet.http.HttpServletResponse;
69  
70  import org.apache.commons.lang.StringUtils;
71  
72  
73  /**
74   * The class responsible for the subscription and unsubscription process
75   */
76  public final class AgendaSubscriberService
77  {
78      public static final String JSP_URL_DO_UNSUBSCRIBE = "/jsp/site/plugins/calendar/DoUnsubscribeCalendar.jsp";
79  
80      //Messages
81      private static final String PROPERTY_INVALID_MAIL_TITLE_MESSAGE = "calendar.siteMessage.invalid_mail.title";
82      private static final String PROPERTY_INVALID_MAIL_ERROR_MESSAGE = "calendar.siteMessage.invalid_mail.message";
83      private static final String PROPERTY_SUBSCRIPTION_OK_TITLE_MESSAGE = "calendar.siteMessage.subscription_ok.title";
84      private static final String PROPERTY_SUBSCRIPTION_OK_ALERT_MESSAGE = "calendar.siteMessage.subscription_ok.message";
85      private static final String PROPERTY_UNSUBSCRIPTION_OK_TITLE_MESSAGE = "calendar.siteMessage.unsubscription_ok.title";
86      private static final String PROPERTY_UNSUBSCRIPTION_OK_ALERT_MESSAGE = "calendar.siteMessage.unsubscription_ok.message";
87      private static final String PROPERTY_NO_CALENDAR_CHOSEN_TITLE_MESSAGE = "calendar.siteMessage.no_calendar_chosen.title";
88      private static final String PROPERTY_NO_CALENDAR_CHOSEN_ERROR_MESSAGE = "calendar.siteMessage.no_calendar_chosen.message";
89      private static final String PROPERTY_EXPIRATION_SUBSCRIPTION_TITLE_MESSAGE = "calendar.siteMessage.expiration_subscription.title";
90      private static final String PROPERTY_EXPIRATION_SUBSCRIPTION_ALERT_MESSAGE = "calendar.siteMessage.expiration_subscription.message";
91      private static final String PROPERTY_INVALID_KEY_SUBSCRIPTION_TITLE_MESSAGE = "calendar.siteMessage.invalid_key_subscription.title";
92      private static final String PROPERTY_INVALID_KEY_SUBSCRIPTION_ALERT_MESSAGE = "calendar.siteMessage.invalid_key_subscription.message";
93      private static final String PROPERTY_SUBSCRIPTION_MAIL_SEND_TITLE_MESSAGE = "calendar.siteMessage.mail_send_subscription.title";
94      private static final String PROPERTY_SUBSCRIPTION_MAIL_SEND_ALERT_MESSAGE = "calendar.siteMessage.mail_send_subscription.message";
95      private static final String PROPERTY_REDIRECTION_TITLE_MESSAGE = "calendar.siteMessage.redirection_subscription.title";
96      private static final String PROPERTY_REDIRECTION_ALERT_MESSAGE = "calendar.siteMessage.redirection_subscription.message";
97  
98      //Templates
99      private static final String TEMPLATE_SEND_NOTIFICATION_MAIL = "skin/plugins/calendar/notification_email.html";
100     private static final String TEMPLATE_NOTIFY_SUBSCRIPTION_MAIL = "skin/plugins/calendar/subscription_notification_mail.html";
101     private static final String MARK_SUBSCRIBER_EMAIL = "subscriber_email";
102     private static final String MARK_BASE_URL = "base_url";
103     private static final String MARK_SENDER_MESSAGE = "sender_message";
104     private static final String MARK_EMAIL_CONTENT = "email_content";
105     private static final String MARK_UNSUBSCRIBE_LINK = "unsubscribe_link";
106     private static final String MARK_EVENT = "event";
107     private static final String MARK_LINK = "link";
108     private static final String MARK_MESSAGE = "message";
109 
110     //properties
111     private static final String PROPERTY_UNSUBSCRIBE_LINK = "calendar.unsubscribe.link";
112     private static final String PROPERTY_SENDER_NAME = "calendar.sender.name";
113     private static final String PROPERTY_SENDER_EMAIL = "calendar.sender.email";
114     private static final String PROPERTY_EMAIL_SUBSCRIBER_CONTENT = "calendar.subscriber.email.content";
115     private static final String PROPERTY_EMAIL_SUBSCRIBER_OBJECT = "calendar.subscriber.email.object";
116     private static final String PROPERTY_EMAIL_FRIEND_OBJECT = "calendar.friend.email.object";
117     private static final String PROPERTY_SUBSCRIBE_HTML_LINK = "calendar.notification.subscription.link";
118     private static final String PROPERTY_SUBSCRIBE_HTML_MESSAGE = "calendar.notification.subscription.message";
119     private static final String PROPERTY_SUBSCRIBE_HTML_OBJECT = "calendar.notification.subscription.object";
120 
121     //Parameters
122     private static final String HTML_LINK_CLOSE = "</a>";
123     private static final String HTML_LINK_OPEN_1 = "<a href=\"";
124     private static final String HTML_LINK_OPEN_2 = "\" >";
125 
126     //JSP
127     private static final String URL_JSP_RETURN_SEND_FRIEND_MAIL = "../../Portal.jsp?page=calendar&action=get_friend_email_page";
128     private static final String URL_JSP_SUBSCRIPTION_NOTIFICATION = "jsp/site/plugins/calendar/DoSubscriptionCalendar.jsp";
129     private static final String URL_JSP_SUBSCRIPTION_REDIRECTION = "plugins/calendar/DoSubscriptionCalendar.jsp";
130     private static final String URL_JSP_PAGE_PORTAL = "jsp/site/Portal.jsp";
131 
132     /**
133      * The registration service
134      */
135     private static AgendaSubscriberService _singleton;
136 
137     /**
138      * Constructor
139      */
140     private AgendaSubscriberService( )
141     {
142         if ( _singleton == null )
143         {
144             _singleton = this;
145         }
146     }
147 
148     /**
149      * Fetches the singleton instance
150      * @return The singleton instance
151      */
152     public static AgendaSubscriberService getInstance( )
153     {
154         if ( _singleton == null )
155         {
156             _singleton = new AgendaSubscriberService( );
157         }
158 
159         return _singleton;
160     }
161 
162     /**
163      * Performs the subscription process
164      * Throw a SiteMessage
165      * @param request The Http request
166      * @throws fr.paris.lutece.portal.service.message.SiteMessageException The
167      *             error message thrown to the user
168      */
169     public void doSubscription( HttpServletRequest request ) throws SiteMessageException
170     {
171         String strEmail = request.getParameter( Constants.PARAMETER_EMAIL );
172         String strAgenda = request.getParameter( Constants.PARAM_AGENDA );
173         String strPluginName = request.getParameter( Constants.PARAMETER_PLUGIN_NAME );
174         strPluginName = ( !( strPluginName == null ) ) ? strPluginName : Constants.PLUGIN_NAME;
175 
176         if ( ( strEmail == null ) || !StringUtil.checkEmail( strEmail ) )
177         {
178             SiteMessageService.setMessage( request, PROPERTY_INVALID_MAIL_ERROR_MESSAGE,
179                     PROPERTY_INVALID_MAIL_TITLE_MESSAGE, SiteMessage.TYPE_STOP );
180         }
181 
182         Plugin plugin = PluginService.getPlugin( strPluginName );
183 
184         if ( ( strAgenda == null ) || ( strAgenda.equals( "" ) ) )
185         {
186             SiteMessageService.setMessage( request, PROPERTY_NO_CALENDAR_CHOSEN_ERROR_MESSAGE,
187                     PROPERTY_NO_CALENDAR_CHOSEN_TITLE_MESSAGE, SiteMessage.TYPE_STOP );
188         }
189 
190         //Checks if a subscriber with the same email address doesn't exist yet
191         CalendarSubscriber subscriber = CalendarSubscriberHome.findByEmail( strEmail, plugin );
192 
193         if ( subscriber == null )
194         {
195             // The email doesn't exist, so create a new subcriber
196             subscriber = new CalendarSubscriber( );
197             subscriber.setEmail( strEmail );
198             CalendarSubscriberHome.create( subscriber, plugin );
199         }
200         CalendarSubscriberHome.addSubscriber( Integer.parseInt( strAgenda ), subscriber.getId( ), new Timestamp(
201                 new Date( ).getTime( ) ), plugin );
202 
203         SiteMessageService.setMessage( request, PROPERTY_SUBSCRIPTION_OK_ALERT_MESSAGE,
204                 PROPERTY_SUBSCRIPTION_OK_TITLE_MESSAGE, SiteMessage.TYPE_INFO );
205     }
206 
207     /**
208      * Performs unsubscription process
209      * Throw a SiteMessage
210      * @param request The http request
211      * @param plugin The plugin
212      * @throws fr.paris.lutece.portal.service.message.SiteMessageException The
213      *             error message handled by the front office
214      */
215     public void doUnSubscribe( HttpServletRequest request, Plugin plugin ) throws SiteMessageException
216     {
217         String strEmail = request.getParameter( Constants.PARAMETER_EMAIL );
218         String strAgenda = request.getParameter( Constants.PARAM_AGENDA );
219         if ( StringUtils.isNotBlank( strEmail ) )
220         {
221             if ( StringUtils.isNotBlank( strAgenda ) && StringUtils.isNumeric( strAgenda ) )
222             {
223                 CalendarSubscriber subscriber = CalendarSubscriberHome.findByEmail( strEmail, plugin );
224 
225                 CalendarSubscriberHome.removeSubscriber( subscriber.getId( ), Integer.parseInt( strAgenda ), plugin );
226                 if ( !CalendarSubscriberHome.isUserSubscribed( subscriber.getId( ), plugin ) )
227                 {
228                     CalendarSubscriberHome.remove( subscriber.getId( ), plugin );
229                 }
230 
231                 SiteMessageService.setMessage( request, PROPERTY_UNSUBSCRIPTION_OK_ALERT_MESSAGE,
232                         PROPERTY_UNSUBSCRIPTION_OK_TITLE_MESSAGE, SiteMessage.TYPE_INFO );
233             }
234             else
235             {
236                 SiteMessageService.setMessage( request, PROPERTY_NO_CALENDAR_CHOSEN_ERROR_MESSAGE,
237                         PROPERTY_NO_CALENDAR_CHOSEN_TITLE_MESSAGE, SiteMessage.TYPE_STOP );
238             }
239         }
240         else
241         {
242             SiteMessageService.setMessage( request, PROPERTY_INVALID_MAIL_ERROR_MESSAGE,
243                     PROPERTY_INVALID_MAIL_TITLE_MESSAGE, SiteMessage.TYPE_STOP );
244         }
245     }
246 
247     /**
248      * Send the mail
249      * @param strRecipientTo Subscriber email
250      * @param strSenderName The sender name.
251      * @param strSenderEmail The sender email address.
252      * @param strSubject The message subject.
253      * @param strMessage The message.
254      */
255     public void doSendMail( String strRecipientTo, String strSenderName, String strSenderEmail, String strSubject,
256             String strMessage )
257     {
258         MailService.sendMailHtml( strRecipientTo, strSenderName, strSenderEmail, strSubject, strMessage );
259     }
260 
261     /**
262      * Send an event to a list of subscribers
263      * @param request the http request
264      * @param listSubscribers a Collection<CalendarSubscriber>
265      * @param event the event
266      * @param nCalendarId The id of the calendar
267      */
268     public void sendSubscriberMail( HttpServletRequest request, Collection<CalendarSubscriber> listSubscribers,
269             Event event, int nCalendarId )
270     {
271         String strUnsubscribelink = AppPropertiesService.getProperty( PROPERTY_UNSUBSCRIBE_LINK );
272         String strSenderName = AppPropertiesService.getProperty( PROPERTY_SENDER_NAME );
273         String strSenderEmail = AppPropertiesService.getProperty( PROPERTY_SENDER_EMAIL );
274         String strContent = I18nService.getLocalizedString( PROPERTY_EMAIL_SUBSCRIBER_CONTENT, request.getLocale( ) );
275         String strObject = I18nService.getLocalizedString( PROPERTY_EMAIL_SUBSCRIBER_OBJECT, request.getLocale( ) );
276 
277         String strBaseUrl = AppPathService.getBaseUrl( request );
278 
279         HashMap<String, Object> emailModel = new HashMap<String, Object>( );
280 
281         for ( CalendarSubscriber subscriber : listSubscribers )
282         {
283             emailModel.put( MARK_UNSUBSCRIBE_LINK, strUnsubscribelink );
284             emailModel.put( MARK_EMAIL_CONTENT, strContent );
285             emailModel.put( MARK_BASE_URL, strBaseUrl );
286             emailModel.put( MARK_EVENT, event );
287             emailModel.put( Constants.MARK_CALENDAR_ID, nCalendarId );
288             emailModel.put( Constants.MARK_EVENT_ID, event.getId( ) );
289             emailModel.put( MARK_SUBSCRIBER_EMAIL, subscriber.getEmail( ) );
290             emailModel.put( Constants.PARAMETER_ACTION, Constants.ACTION_SHOW_RESULT );
291 
292             HtmlTemplate templateAgenda = AppTemplateService.getTemplate( TEMPLATE_SEND_NOTIFICATION_MAIL,
293                     request.getLocale( ), emailModel );
294             String strNewsLetterCode = templateAgenda.getHtml( );
295 
296             MailService.sendMailHtml( subscriber.getEmail( ), strSenderName, strSenderEmail, strObject,
297                     strNewsLetterCode );
298             emailModel.clear( );
299         }
300     }
301 
302     /**
303      * Send the calendar to a friend
304      * @param request the http request
305      * @return The next URL to redirect to
306      * @throws SiteMessageException To display an error
307      */
308     public String sendFriendMail( HttpServletRequest request ) throws SiteMessageException
309     {
310         //Form parameters
311         String strFriendEmail = request.getParameter( Constants.PARAMETER_SENDER_FRIEND_EMAIL );
312         String strSenderFirstName = request.getParameter( Constants.PARAMETER_SENDER_FIRST_NAME );
313         String strSenderLastName = request.getParameter( Constants.PARAMETER_SENDER_LAST_NAME );
314         String strSenderEmail = request.getParameter( Constants.PARAMETER_SENDER_EMAIL );
315         String strSenderMessage = request.getParameter( Constants.PARAMETER_SENDER_MESSAGE );
316 
317         // Mandatory field
318         if ( StringUtils.isNotBlank( strFriendEmail ) && StringUtils.isNotBlank( strSenderFirstName )
319                 && StringUtils.isNotBlank( strSenderLastName ) && StringUtils.isNotBlank( strSenderEmail )
320                 && StringUtils.isNotBlank( strSenderMessage ) )
321         {
322             if ( StringUtil.checkEmail( strFriendEmail ) )
323             {
324                 if ( StringUtil.checkEmail( strSenderEmail ) )
325                 {
326                     String strSenderName = strSenderFirstName + Constants.SPACE + strSenderLastName;
327 
328                     //Properties
329                     String strObject = I18nService.getLocalizedString( PROPERTY_EMAIL_FRIEND_OBJECT,
330                             request.getLocale( ) );
331                     String strBaseUrl = AppPathService.getBaseUrl( request );
332 
333                     HashMap<String, Object> emailModel = new HashMap<String, Object>( );
334                     emailModel.put( MARK_SENDER_MESSAGE, strSenderMessage );
335                     emailModel.put( MARK_BASE_URL, strBaseUrl );
336 
337                     HtmlTemplate templateAgenda = AppTemplateService.getTemplate( TEMPLATE_SEND_NOTIFICATION_MAIL,
338                             request.getLocale( ), emailModel );
339 
340                     String strNewsLetterCode = templateAgenda.getHtml( );
341 
342                     MailService.sendMailHtml( strFriendEmail, strSenderName, strSenderEmail, strObject,
343                             strNewsLetterCode );
344                 }
345                 else
346                 {
347                     Object[] args = { strSenderEmail };
348                     SiteMessageService.setMessage( request, PROPERTY_INVALID_MAIL_ERROR_MESSAGE, args,
349                             PROPERTY_INVALID_MAIL_TITLE_MESSAGE, SiteMessage.TYPE_STOP );
350                 }
351             }
352             else
353             {
354                 Object[] args = { strFriendEmail };
355                 SiteMessageService.setMessage( request, PROPERTY_INVALID_MAIL_ERROR_MESSAGE, args,
356                         PROPERTY_INVALID_MAIL_TITLE_MESSAGE, SiteMessage.TYPE_STOP );
357             }
358         }
359         else
360         {
361             SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, Messages.MANDATORY_FIELDS,
362                     SiteMessage.TYPE_STOP );
363         }
364 
365         return URL_JSP_RETURN_SEND_FRIEND_MAIL;
366     }
367 
368     /**
369      * Notify the subscription
370      * @param agenda The agenda
371      * @param request HttpServletRequest
372      * @param plugin The plugin
373      * @throws SiteMessageException site message exception
374      */
375     public void doNotificationSubscription( AgendaResource agenda, HttpServletRequest request, Plugin plugin )
376             throws SiteMessageException
377     {
378         String strEmail = request.getParameter( Constants.PARAMETER_EMAIL );
379 
380         CalendarNotification calendarNotification = new CalendarNotification( );
381         //Generate key
382         UUID key = java.util.UUID.randomUUID( );
383         calendarNotification.setKey( key.toString( ) );
384         calendarNotification.setEmail( strEmail );
385         calendarNotification.setIdAgenda( Integer.parseInt( agenda.getId( ) ) );
386         Calendar calendar = GregorianCalendar.getInstance( );
387         if ( agenda.isNotify( ) )
388         {
389             calendar.add( Calendar.DAY_OF_MONTH, agenda.getPeriodValidity( ) );
390         }
391         else
392         {
393             calendar.add( Calendar.DAY_OF_MONTH, 1 );
394         }
395         calendarNotification.setDateExpiry( new Timestamp( calendar.getTimeInMillis( ) ) );
396         CalendarNotificationHome.create( calendarNotification, plugin );
397         if ( agenda.isNotify( ) )
398         {
399             String strSenderName = AppPropertiesService.getProperty( PROPERTY_SENDER_NAME );
400             String strSenderEmail = AppPropertiesService.getProperty( PROPERTY_SENDER_EMAIL );
401             String strObject = I18nService.getLocalizedString( PROPERTY_SUBSCRIBE_HTML_OBJECT, request.getLocale( ) );
402 
403             String strBaseUrl = AppPathService.getBaseUrl( request ) + URL_JSP_SUBSCRIPTION_NOTIFICATION;
404             UrlItem url = new UrlItem( strBaseUrl );
405             url.addParameter( Constants.MARK_KEY, key.toString( ) );
406 
407             String strlink = I18nService.getLocalizedString( PROPERTY_SUBSCRIBE_HTML_LINK, request.getLocale( ) );
408             String strMessage = I18nService.getLocalizedString( PROPERTY_SUBSCRIBE_HTML_MESSAGE, request.getLocale( ) );
409             String linkHtml = HTML_LINK_OPEN_1 + url.getUrl( ) + HTML_LINK_OPEN_2 + strlink + HTML_LINK_CLOSE;
410 
411             HashMap<String, String> emailModel = new HashMap<String, String>( );
412             emailModel.put( MARK_MESSAGE, strMessage );
413             emailModel.put( MARK_LINK, linkHtml );
414 
415             HtmlTemplate templateAgenda = AppTemplateService.getTemplate( TEMPLATE_NOTIFY_SUBSCRIPTION_MAIL,
416                     request.getLocale( ), emailModel );
417             String strEmailCode = templateAgenda.getHtml( );
418 
419             MailService.sendMailHtml( strEmail, strSenderName, strSenderEmail, strObject, strEmailCode );
420             emailModel.clear( );
421 
422             SiteMessageService.setMessage( request, PROPERTY_SUBSCRIPTION_MAIL_SEND_ALERT_MESSAGE,
423                     PROPERTY_SUBSCRIPTION_MAIL_SEND_TITLE_MESSAGE, SiteMessage.TYPE_INFO );
424         }
425         else
426         {
427             ServletConfig config = LocalVariables.getConfig( );
428             HttpServletResponse response = LocalVariables.getResponse( );
429             try
430             {
431                 String strAdresse = URL_JSP_SUBSCRIPTION_REDIRECTION + Constants.INTERROGATION_MARK
432                         + Constants.MARK_KEY + Constants.EQUAL + key;
433                 response.sendRedirect( strAdresse );
434             }
435             catch ( Exception e )
436             {
437                 SiteMessageService.setMessage( request, PROPERTY_REDIRECTION_TITLE_MESSAGE,
438                         PROPERTY_REDIRECTION_ALERT_MESSAGE, SiteMessage.TYPE_INFO );
439             }
440             LocalVariables.setLocal( config, request, response );
441         }
442     }
443 
444     /**
445      * Validate subscription
446      * @param request HttpServletRequest
447      * @throws SiteMessageException site message exception
448      */
449     public void doValidationSubscription( HttpServletRequest request ) throws SiteMessageException
450     {
451         String strKey = request.getParameter( Constants.MARK_KEY );
452         String strPluginName = request.getParameter( Constants.PARAMETER_PLUGIN_NAME );
453         strPluginName = !( strPluginName == null ) ? strPluginName : Constants.PLUGIN_NAME;
454 
455         Plugin plugin = PluginService.getPlugin( strPluginName );
456         CalendarNotification calendarNotification = CalendarNotificationHome.findByPrimaryKey( strKey, plugin );
457 
458         if ( calendarNotification != null )
459         {
460             Calendar calendar = GregorianCalendar.getInstance( );
461             Timestamp todaydate = new Timestamp( calendar.getTimeInMillis( ) );
462             if ( todaydate.before( calendarNotification.getDateExpiry( ) ) )
463             {
464                 String strEmail = calendarNotification.getEmail( );
465                 int nIdAgenda = calendarNotification.getIdAgenda( );
466 
467                 //Checks if a subscriber with the same email address doesn't exist yet
468                 CalendarSubscriber subscriber = CalendarSubscriberHome.findByEmail( strEmail, plugin );
469 
470                 if ( subscriber == null )
471                 {
472                     // The email doesn't exist, so create a new subcriber
473                     subscriber = new CalendarSubscriber( );
474                     subscriber.setEmail( strEmail );
475                     CalendarSubscriberHome.create( subscriber, plugin );
476                 }
477                 CalendarSubscriberHome.addSubscriber( nIdAgenda, subscriber.getId( ),
478                         new Timestamp( new Date( ).getTime( ) ), plugin );
479 
480                 CalendarNotificationHome.remove( strKey, plugin );
481 
482                 SiteMessageService.setMessage( request, PROPERTY_SUBSCRIPTION_OK_ALERT_MESSAGE, SiteMessage.TYPE_INFO,
483                         AppPathService.getBaseUrl( request ) + URL_JSP_PAGE_PORTAL,
484                         PROPERTY_SUBSCRIPTION_OK_TITLE_MESSAGE, null );
485             }
486             else
487             {
488                 SiteMessageService.setMessage( request, PROPERTY_EXPIRATION_SUBSCRIPTION_ALERT_MESSAGE,
489                         SiteMessage.TYPE_ERROR, AppPathService.getBaseUrl( request ) + URL_JSP_PAGE_PORTAL,
490                         PROPERTY_EXPIRATION_SUBSCRIPTION_TITLE_MESSAGE, null );
491             }
492         }
493         else
494         {
495             SiteMessageService.setMessage( request, PROPERTY_INVALID_KEY_SUBSCRIPTION_ALERT_MESSAGE,
496                     SiteMessage.TYPE_ERROR, AppPathService.getBaseUrl( request ) + URL_JSP_PAGE_PORTAL,
497                     PROPERTY_INVALID_KEY_SUBSCRIPTION_TITLE_MESSAGE, null );
498         }
499     }
500 
501     /**
502      * Get the number of subscribers of a calendar
503      * @param nCalendarId The id of the calendar to get the number of
504      *            subscribers of
505      * @param plugin The plugin
506      * @return The number of subscribers of the calendar
507      */
508     public int getSubscriberNumber( int nCalendarId, Plugin plugin )
509     {
510         return CalendarSubscriberHome.findSubscriberNumber( nCalendarId, plugin );
511     }
512 
513     /**
514      * Get the list of subscribers of a calendar
515      * @param nCalendarId The id of the calendar to get the list of subscribers
516      *            of
517      * @param plugin The plugin
518      * @return The list of subscribers of the calendar
519      */
520     public Collection<CalendarSubscriber> getSubscribers( int nCalendarId, Plugin plugin )
521     {
522         return CalendarSubscriberHome.findSubscribers( nCalendarId, plugin );
523     }
524 }