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.grustoragedb.modules.broadcast.service;
35  
36  import fr.paris.lutece.plugins.grubusiness.business.demand.DemandService;
37  import fr.paris.lutece.plugins.grubusiness.business.notification.NotificationEvent;
38  import fr.paris.lutece.plugins.grustoragedb.modules.broadcast.business.Subscription;
39  import fr.paris.lutece.plugins.grustoragedb.modules.broadcast.business.SubscriptionHome;
40  import fr.paris.lutece.portal.service.mail.MailService;
41  import fr.paris.lutece.portal.service.spring.SpringContextService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Locale;
48  import java.util.Map;
49  import java.sql.Timestamp;
50  import java.time.LocalDateTime;
51  import java.util.ArrayList;
52  import java.util.Map.Entry;
53  
54  /**
55   *
56   * @author seboo
57   */
58  public class NotificationAlertBroadcastService
59  {
60  
61      // constants
62      private static String DEMAND_SERVICE_BEAN_NAME = "grusupply.storageService";
63      private static String STATUS_FAILED = "FAILED";
64      private static String KEY_NOTIFICATION_EVENT_LIST = "notification_event_list";
65      private static String KEY_START = "start";
66      private static String KEY_END = "end";
67      
68      private static String PROPERTY_GRU_ALERTS_FROM_NAME ="grustoragedb-broadcast.mail.from.name";
69      private static String PROPERTY_GRU_ALERTS_FROM_MAIL ="grustoragedb-broadcast.mail.from.mail";
70      private static String PROPERTY_GRU_ALERTS_SUBJECT   ="grustoragedb-broadcast.mail.subject";
71      private static String GRU_ALERTS_FROM_NAME = AppPropertiesService.getProperty( PROPERTY_GRU_ALERTS_FROM_NAME, "GRU ESB Notification alerts Daemon" );
72      private static String GRU_ALERTS_FROM_MAIL = AppPropertiesService.getProperty( PROPERTY_GRU_ALERTS_FROM_MAIL, "no-reply@paris.fr" );
73      private static String GRU_ALERTS_SUBJECT = AppPropertiesService.getProperty( PROPERTY_GRU_ALERTS_SUBJECT, "GRU ESB notifications alerts - demand type : %s" );
74      
75      // Templates
76      private static final String TEMPLATE_MAIL = "/admin/plugins/grustoragedb/modules/broadcast/mail.html";
77  
78      public static String broadcast( Locale defaultLocale )
79      {
80  
81          // get subscripters list
82          List<Subscription> listSub = SubscriptionHome.getSubscriptionsList( );
83  
84          if ( listSub.isEmpty( ) )
85          {
86              return "no subscribers";
87          }
88  
89          // get concerned demand_type_id
90          Map<String, Subscription> mapBroadcastFeeds = new HashMap<>( );
91          Map<String, List<String>> mapBroadcastFeedRecipients = new HashMap<>( );
92  
93          for ( Subscription sub : listSub )
94          {
95              String strBroadcastFeedKey = getKey( sub );
96  
97              if ( !mapBroadcastFeeds.containsKey( strBroadcastFeedKey ) )
98              {
99                  Subscriptionoragedb/modules/broadcast/business/Subscription.html#Subscription">Subscription broadcastFeed = new Subscription( );
100                 broadcastFeed.setDemandTypeId( sub.getDemandTypeId( ) );
101                 broadcastFeed.setFrequency( sub.getFrequency( ) );
102 
103                 mapBroadcastFeeds.put( strBroadcastFeedKey, broadcastFeed );
104                 mapBroadcastFeedRecipients.put( strBroadcastFeedKey, new ArrayList<String>( ) );
105             }
106 
107             mapBroadcastFeedRecipients.get( strBroadcastFeedKey ).add( sub.getMail( ) );
108         }
109 
110         // select last alerts by demand_type_id by frequency
111         // & send alert list to subscribers
112         DemandService storageService = SpringContextService.getBean( DEMAND_SERVICE_BEAN_NAME );
113         int nbMailSent = 0;
114         int nbEvent = 0;
115 
116         for ( Map.Entry<String, Subscription> entry : mapBroadcastFeeds.entrySet( ) )
117         {
118             Subscription broadcastFeed = entry.getValue( );
119 
120             LocalDateTime ldtNow = LocalDateTime.now( );
121             long endPeriod = Timestamp.valueOf( ldtNow ).getTime( );
122             long startPeriod = Timestamp.valueOf( ldtNow.minusHours( broadcastFeed.getFrequency( ) ) ).getTime( );
123 
124             List<NotificationEvent> listEvent = storageService.findEventsByDateAndDemandTypeIdAndStatus(startPeriod, endPeriod,
125                     String.valueOf( broadcastFeed.getDemandTypeId( ) ), STATUS_FAILED );
126 
127             if ( !listEvent.isEmpty( ) )
128             {
129                 Map<String, Object> model = new HashMap<>( );
130                 model.put( KEY_START, startPeriod );
131                 model.put( KEY_END, endPeriod );
132                 
133                 model.put( KEY_NOTIFICATION_EVENT_LIST, listEvent );
134 
135                 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MAIL, defaultLocale, model );
136                 String strToList = String.join( ",", mapBroadcastFeedRecipients.get( entry.getKey( ) ) );
137 
138                 MailService.sendMailHtml( strToList, GRU_ALERTS_FROM_NAME, GRU_ALERTS_FROM_MAIL,
139                 		String.format(GRU_ALERTS_SUBJECT, String.valueOf( broadcastFeed.getDemandTypeId( ) ) ), template.getHtml( ) );
140 
141                 nbMailSent++;
142                 nbEvent += listEvent.size( );
143             }
144         }
145 
146         return nbMailSent + " mail(s) sent.";
147     }
148 
149     /**
150      * get unique key
151      * 
152      * @param sub
153      * @return the key as String
154      */
155     private static String getKey( Subscription sub )
156     {
157         return sub.getDemandTypeId( ) + "|" + sub.getFrequency( );
158     }
159 }