View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.announce.service.daemon;
35  
36  import fr.paris.lutece.plugins.announce.business.*;
37  import fr.paris.lutece.plugins.announce.service.AnnounceSubscriptionProvider;
38  import fr.paris.lutece.plugins.module.announce.subscribe.business.AnnounceSubscribtionDTO;
39  import fr.paris.lutece.portal.service.daemon.Daemon;
40  import fr.paris.lutece.portal.service.mail.MailService;
41  import fr.paris.lutece.portal.service.template.AppTemplateService;
42  import fr.paris.lutece.portal.service.util.AppPathService;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  
46  import java.util.HashMap;
47  import java.util.List;
48  import java.util.Locale;
49  import java.util.Map;
50  import java.util.stream.Collectors;
51  
52  import static java.util.Optional.ofNullable;
53  
54  /**
55   * Daemon to send notification to users when subscribed announces are created
56   */
57  public class AnnounceSubscriptionDaemon extends Daemon
58  {
59      private static final String MARK_ANNOUNCE = "announce";
60      private static final String PROPERTY_SUBSCRIPTION_NOTIFICATION_SUBJECT = "announce.subscription.notificationSubject";
61      private static final String PROPERTY_SUBSCRIPTION_NOTIFICATION_NUMBER = "announce.subscription.notificationNumber";
62      private static final String TEMPLATE_EMAIL_ANNOUNCES = "skin/plugins/announce/email_notify_announces.html";
63  
64      public static final String MARK_PROD_URL = "prod_url";
65      public static final String MARK_CATEGORY = "category";
66      public static final String MARK_URL_SUBSCRIPTION = "url_subscription";
67      public static final String MARK_QUERY_SPECIFIC_ANNOUNCE = "page=announce&action=view_announce&announce_id=";
68      public static final String MARK_QUERY_URL_SUBSCRIPTION = "page=announce&action=view_subscriptions";
69  
70      private static final String PROPERTY_BASE_URL = "lutece.base.url";
71      private static final String BASE_URL = AppPropertiesService.getProperty( PROPERTY_BASE_URL ) + "/" + AppPathService.getPortalUrl( ) + "?";
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public void run( )
78      {
79  
80          String numberOfAnnounceToNotify = AppPropertiesService.getProperty( PROPERTY_SUBSCRIPTION_NOTIFICATION_NUMBER );
81          List<AnnounceNotify> listAnnounceToNotify = AnnounceNotifyHome.slecteAll( );
82          if ( listAnnounceToNotify == null )
83          {
84              return;
85          }
86  
87          for ( AnnounceNotify announceNotify : listAnnounceToNotify )
88          {
89              Announce announce = AnnounceHome.findByPrimaryKey( announceNotify.getIdAnnounce( ) );
90              if ( announce == null )
91              {
92                  continue;
93              }
94              processAnnounce( announce, numberOfAnnounceToNotify );
95              AnnounceNotifyHome.delete( announceNotify.getId( ) );
96          }
97      }
98  
99      private void processAnnounce( Announce announce, String numberOfAnnounceToNotify )
100     {
101         List<AnnounceSubscribtionDTO> listSubscriptions = AnnounceSubscriptionProvider.getService( )
102                 .getSubscriptionsByAnnounce( Integer.toString( announce.getCategory( ).getId( ) ), announce.getUserName( ), numberOfAnnounceToNotify );
103         listSubscriptions = ofNullable( listSubscriptions ).map(
104                 lst -> lst.stream( ).filter( e -> e.getEmailSubscribes( ) != null && !e.getEmailSubscribes( ).isEmpty( ) ).collect( Collectors.toList( ) ) )
105                 .orElse( null );
106 
107         if ( listSubscriptions != null )
108         {
109             String strSubject = AppPropertiesService.getProperty( PROPERTY_SUBSCRIPTION_NOTIFICATION_SUBJECT );
110             String strSenderName = MailService.getNoReplyEmail( );
111             String strSenderEmail = strSenderName;
112             StringBuilder emailSubscribes = new StringBuilder( "" );
113 
114             int countElst = 1;
115             int sizeLstSubNotify = listSubscriptions.size( );
116             for ( AnnounceSubscribtionDTO subscription : listSubscriptions )
117             {
118                 if ( subscription != null )
119                 {
120                     emailSubscribes.append( subscription.getEmailSubscribes( ) );
121                 }
122                 if ( sizeLstSubNotify > countElst )
123                 {
124                     countElst++;
125                     emailSubscribes.append( ";" );
126                 }
127             }
128             notifyUser( announce, strSubject, strSenderName, strSenderEmail, emailSubscribes.toString( ) );
129         }
130     }
131 
132     /**
133      * Notify a user that announces ha has subscribed to have been published
134      * 
135      * @param strUserName
136      *            The name of the user to notify
137      * @param announcesToNotify
138      *            published announce
139      * @param strSubject
140      *            The subject of the email to send
141      * @param strSenderName
142      *            The name of the sender of the email
143      * @param strSenderEmail
144      *            The email address of the sender of the email
145      */
146 
147     private void notifyUser( Announce announcesToNotify, String strSubject, String strSenderName, String strSenderEmail, String strUserEmail )
148     {
149 
150         Map<String, Object> model = new HashMap<>( );
151         model.put( MARK_ANNOUNCE, announcesToNotify );
152 
153         model.put( MARK_PROD_URL, BASE_URL + MARK_QUERY_SPECIFIC_ANNOUNCE );
154         model.put( MARK_CATEGORY, CategoryHome.findByPrimaryKey( announcesToNotify.getCategory( ).getId( ) ) );
155         model.put( MARK_URL_SUBSCRIPTION, BASE_URL + MARK_QUERY_URL_SUBSCRIPTION );
156 
157         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_EMAIL_ANNOUNCES, Locale.getDefault( ), model );
158 
159         if ( !strSenderEmail.equals( " " ) )
160         {
161             MailService.sendMailHtml( (String) null, (String) null, strUserEmail, strSenderName, strSenderEmail, strSubject, template.getHtml( ) );
162         }
163 
164     }
165 }