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.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
57
58 public class NotificationAlertBroadcastService
59 {
60
61
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
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
82 List<Subscription> listSub = SubscriptionHome.getSubscriptionsList( );
83
84 if ( listSub.isEmpty( ) )
85 {
86 return "no subscribers";
87 }
88
89
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
111
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
151
152
153
154
155 private static String getKey( Subscription sub )
156 {
157 return sub.getDemandTypeId( ) + "|" + sub.getFrequency( );
158 }
159 }