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