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.notificationstore.business;
35
36 import fr.paris.lutece.plugins.grubusiness.business.demand.Demand;
37 import fr.paris.lutece.plugins.grubusiness.business.demand.DemandStatus;
38 import fr.paris.lutece.plugins.grubusiness.business.notification.EnumNotificationType;
39 import fr.paris.lutece.plugins.grubusiness.business.notification.Notification;
40 import fr.paris.lutece.plugins.grubusiness.business.web.rs.EnumGenericStatus;
41 import fr.paris.lutece.plugins.notificationstore.service.NotificationStorePlugin;
42 import fr.paris.lutece.plugins.notificationstore.utils.NotificationStoreConstants;
43 import fr.paris.lutece.plugins.notificationstore.utils.NotificationStoreUtils;
44 import fr.paris.lutece.portal.business.file.File;
45 import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
46 import fr.paris.lutece.portal.service.file.FileService;
47 import fr.paris.lutece.portal.service.file.FileServiceException;
48 import fr.paris.lutece.portal.service.spring.SpringContextService;
49 import fr.paris.lutece.portal.service.util.AppLogService;
50 import fr.paris.lutece.portal.service.util.AppPropertiesService;
51 import fr.paris.lutece.util.string.StringUtil;
52
53 import java.io.IOException;
54 import java.nio.charset.StandardCharsets;
55 import java.util.ArrayList;
56 import java.util.Date;
57 import java.util.List;
58 import java.util.Optional;
59
60 import javax.ws.rs.core.MediaType;
61
62 import org.apache.commons.collections.CollectionUtils;
63 import org.apache.commons.lang3.StringUtils;
64
65 import com.fasterxml.jackson.core.JsonProcessingException;
66 import com.fasterxml.jackson.databind.ObjectMapper;
67
68
69
70
71
72 public final class NotificationContentHome
73 {
74
75
76
77 private static INotificationContentDAO../fr/paris/lutece/plugins/notificationstore/business/INotificationContentDAO.html#INotificationContentDAO">INotificationContentDAO _dao = (INotificationContentDAO) SpringContextService.getBean( "notificationstore.notificationContentDao" );
78
79
80
81
82
83 private NotificationContentHome( )
84 {
85 }
86
87
88
89
90
91
92
93
94
95
96
97 public static NotificationContent/../../fr/paris/lutece/plugins/notificationstore/business/NotificationContent.html#NotificationContent">NotificationContent create( NotificationContent notificationContent )
98 {
99 _dao.insert( notificationContent, NotificationStorePlugin.getPlugin( ) );
100
101 return notificationContent;
102 }
103
104
105
106
107
108
109
110
111
112
113
114 public static List<NotificationContent> create( Notification notification )
115 {
116 List<NotificationContent> listNotificationContent = getListNotificationContent( notification );
117 for ( NotificationContent content : listNotificationContent )
118 {
119 _dao.insert( content, NotificationStorePlugin.getPlugin( ) );
120 }
121
122 return listNotificationContent;
123 }
124
125
126
127
128
129
130
131
132
133
134
135 public static NotificationContent/../../fr/paris/lutece/plugins/notificationstore/business/NotificationContent.html#NotificationContent">NotificationContent update( NotificationContent notificationContent )
136 {
137 _dao.store( notificationContent, NotificationStorePlugin.getPlugin( ) );
138
139 return notificationContent;
140 }
141
142
143
144
145
146
147
148
149
150
151 public static void remove( int nNotificationContentId )
152 {
153 _dao.delete( nNotificationContentId, NotificationStorePlugin.getPlugin( ) );
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public static NotificationContent findByPrimaryKey( int nKey )
170 {
171 return _dao.load( nKey, NotificationStorePlugin.getPlugin( ) );
172 }
173
174
175
176
177
178
179
180 public static List<NotificationContent> getNotificationContentsList( )
181 {
182 return _dao.selectNotificationContentsList( NotificationStorePlugin.getPlugin( ) );
183 }
184
185
186
187
188
189
190
191
192
193 public static List<NotificationContent> getNotificationContentsByIdNotification( int nIdNotification )
194 {
195 return _dao.selectNotificationContentsByIdNotification( nIdNotification, NotificationStorePlugin.getPlugin( ) );
196 }
197
198
199
200
201
202
203
204
205
206 public static List<NotificationContent> getNotificationContentsByIdAndTypeNotification( int nIdNotification,
207 List<EnumNotificationType> listNotificationType )
208 {
209 return _dao.selectNotificationContentsByIdAndTypeNotification( nIdNotification, listNotificationType, NotificationStorePlugin.getPlugin( ) );
210 }
211
212 private static List<NotificationContent> getListNotificationContent( Notification notification )
213 {
214 List<NotificationContent> listNotificationContent = new ArrayList<>( );
215
216 try
217 {
218 ObjectMapper mapperr = NotificationStoreUtils.getMapper( );
219 Demand demand = notification.getDemand( );
220
221 if ( notification.getSmsNotification( ) != null )
222 {
223 listNotificationContent.add(
224 initNotificationContent( notification, EnumNotificationType.SMS, mapperr.writeValueAsString( notification.getSmsNotification( ) ) ) );
225 }
226
227 if ( notification.getBackofficeNotification( ) != null )
228 {
229 listNotificationContent.add( initNotificationContent( notification, EnumNotificationType.BACKOFFICE,
230 mapperr.writeValueAsString( notification.getBackofficeNotification( ) ) ) );
231 }
232
233 if ( CollectionUtils.isNotEmpty( notification.getBroadcastEmail( ) ) )
234 {
235 listNotificationContent.add( initNotificationContent( notification, EnumNotificationType.BROADCAST_EMAIL,
236 mapperr.writeValueAsString( notification.getBroadcastEmail( ) ) ) );
237 }
238
239 if ( notification.getMyDashboardNotification( ) != null )
240 {
241 demand.setStatusId( getStatusGenericId( notification, EnumNotificationType.MYDASHBOARD ) );
242 listNotificationContent.add( initNotificationContent( notification, EnumNotificationType.MYDASHBOARD,
243 mapperr.writeValueAsString( notification.getMyDashboardNotification( ) ) ) );
244 }
245
246 if ( notification.getEmailNotification( ) != null )
247 {
248 listNotificationContent.add( initNotificationContent( notification, EnumNotificationType.CUSTOMER_EMAIL,
249 mapperr.writeValueAsString( notification.getEmailNotification( ) ) ) );
250 }
251 demand.setModifyDate( new Date( ).getTime( ) );
252 DemandHome.update( demand );
253
254 }
255 catch( JsonProcessingException e )
256 {
257 AppLogService.error( "Error while writing JSON of notification", e );
258 }
259 catch( IOException e )
260 {
261 AppLogService.error( "Error while compressing or writing JSON of notification", e );
262 }
263
264 return listNotificationContent;
265 }
266
267
268
269
270
271
272
273
274
275 private static NotificationContent initNotificationContent( Notification notification, EnumNotificationType notificationType,
276 String strNotificationContent ) throws IOException
277 {
278 NotificationContentss/NotificationContent.html#NotificationContent">NotificationContent notificationContent = new NotificationContent( );
279 notificationContent.setIdNotification( notification.getId( ) );
280 notificationContent.setNotificationType( notificationType.name( ) );
281 notificationContent.setStatusId( getStatusId( notification, EnumNotificationType.MYDASHBOARD ) );
282 notificationContent.setGenericStatusId( getStatusGenericId( notification, EnumNotificationType.MYDASHBOARD ) );
283 notificationContent.setFileKey( saveContentInFileStore(notification, notificationType, strNotificationContent ) );
284 notificationContent.setFileStore( NotificationStoreConstants.FILE_STORE_PROVIDER );
285
286 return notificationContent;
287 }
288
289
290
291
292
293
294
295
296
297 private static String saveContentInFileStore( Notification notification, EnumNotificationType notificationType,
298 String strNotificationContent) throws IOException
299 {
300 strNotificationContent = strNotificationContent.replaceAll( NotificationStoreConstants.CHARECTER_REGEXP_FILTER, "" );
301
302
303 byte [ ] bytes;
304
305 if ( AppPropertiesService.getPropertyBoolean( NotificationStoreConstants.PROPERTY_COMPRESS_NOTIFICATION, false ) )
306 {
307 bytes = StringUtil.compress( strNotificationContent );
308 }
309 else
310 {
311 bytes = strNotificationContent.getBytes( StandardCharsets.UTF_8 );
312 }
313
314
315 File file = new File( );
316 file.setTitle( notification.getDemand( ).getId( ) + "_" + notificationType.name( ) + "_" + notification.getDemand( ).getCustomer( ).getConnectionId( ) );
317 file.setSize( bytes.length );
318 file.setMimeType( MediaType.APPLICATION_JSON );
319
320 PhysicalFile physiqueFile = new PhysicalFile( );
321 physiqueFile.setValue( bytes );
322
323 file.setPhysicalFile( physiqueFile );
324
325 try
326 {
327
328 return FileService.getInstance( ).getFileStoreServiceProvider( NotificationStoreConstants.FILE_STORE_PROVIDER ).storeFile( file );
329
330 } catch ( FileServiceException e )
331 {
332 AppLogService.error( "An error occurred while saving the notification content, demand_id {}", notification.getDemand( ).getId( ) , e.getMessage( ) );
333 }
334 return StringUtils.EMPTY;
335 }
336
337
338
339
340
341
342 private static Integer getStatusGenericId( Notification notification, EnumNotificationType statusType )
343 {
344 if ( EnumNotificationType.MYDASHBOARD.equals( statusType ) && notification.getMyDashboardNotification( ) != null )
345 {
346 if ( EnumGenericStatus.exists( notification.getMyDashboardNotification( ).getStatusId( ) ) )
347 {
348 return notification.getMyDashboardNotification( ).getStatusId( );
349 }
350 else
351 {
352 Optional<DemandStatus> status = StatusHome.findByStatus( notification.getMyDashboardNotification( ).getStatusText( ) );
353 if ( status.isPresent( ) && status.get( ).getGenericStatus( ) != null )
354 {
355 return status.get( ).getGenericStatus( ).getStatusId( );
356 }
357 return -1;
358 }
359 }
360
361 return null;
362 }
363
364 private static Integer getStatusId ( Notification notification, EnumNotificationType statusType )
365 {
366 if ( EnumNotificationType.MYDASHBOARD.equals( statusType ) && notification.getMyDashboardNotification( ) != null )
367 {
368 Optional<DemandStatus> status = StatusHome.findByStatus( notification.getMyDashboardNotification( ).getStatusText( ) );
369
370 if ( status.isPresent( ) )
371 {
372 return status.get( ).getId( );
373 }
374 else
375 {
376 DemandStatus newStatus = new DemandStatus( );
377 newStatus.setStatus( notification.getMyDashboardNotification( ).getStatusText( ) );
378
379 newStatus = StatusHome.create( newStatus );
380
381 return newStatus.getId( );
382 }
383 }
384 return -1;
385 }
386
387 }