View Javadoc
1   /*
2    * Copyright (c) 2002-2024, 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.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   * This class provides instances management methods (create, find, ...) for NotificationContent objects
70   */
71  
72  public final class NotificationContentHome
73  {
74  
75      // Static variable pointed at the DAO instance
76  
77      private static INotificationContentDAO../fr/paris/lutece/plugins/notificationstore/business/INotificationContentDAO.html#INotificationContentDAO">INotificationContentDAO _dao = (INotificationContentDAO) SpringContextService.getBean( "notificationstore.notificationContentDao" );
78  
79      /**
80       * Private constructor - this class need not be instantiated
81       */
82  
83      private NotificationContentHome( )
84      {
85      }
86  
87      /**
88       * Create an instance of the notificationContent class
89       * 
90       * @param notificationContent
91       *            The instance of the NotificationContent which contains the informations to store
92       * @param plugin
93       *            the Plugin
94       * @return The instance of notificationContent which has been created with its primary key.
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      * Create an instance of the notificationContent class
106      * 
107      * @param notificationContent
108      *            The instance of the NotificationContent which contains the informations to store
109      * @param plugin
110      *            the Plugin
111      * @return The instance of notificationContent which has been created with its primary key.
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      * Update of the notificationContent which is specified in parameter
127      * 
128      * @param notificationContent
129      *            The instance of the NotificationContent which contains the data to store
130      * @param plugin
131      *            the Plugin
132      * @return The instance of the notificationContent which has been updated
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      * Remove the notificationContent whose identifier is specified in parameter
144      * 
145      * @param nNotificationContentId
146      *            The notificationContent Id
147      * @param plugin
148      *            the Plugin
149      */
150 
151     public static void remove( int nNotificationContentId )
152     {
153         _dao.delete( nNotificationContentId, NotificationStorePlugin.getPlugin( ) );
154     }
155 
156     ///////////////////////////////////////////////////////////////////////////
157     // Finders
158 
159     /**
160      * Returns an instance of a notificationContent whose identifier is specified in parameter
161      * 
162      * @param nKey
163      *            The notificationContent primary key
164      * @param plugin
165      *            the Plugin
166      * @return an instance of NotificationContent
167      */
168 
169     public static NotificationContent findByPrimaryKey( int nKey )
170     {
171         return _dao.load( nKey, NotificationStorePlugin.getPlugin( ) );
172     }
173 
174     /**
175      * Load the data of all the notificationContent objects and returns them in form of a list
176      * 
177      * @return the list which contains the data of all the notificationContent objects
178      */
179 
180     public static List<NotificationContent> getNotificationContentsList( )
181     {
182         return _dao.selectNotificationContentsList( NotificationStorePlugin.getPlugin( ) );
183     }
184 
185     /**
186      * Load the data by id notification and returns them in form of a list
187      * 
188      * @param nIdNotification
189      *            the id notification
190      * @return the list which contains the data by id notification
191      */
192 
193     public static List<NotificationContent> getNotificationContentsByIdNotification( int nIdNotification )
194     {
195         return _dao.selectNotificationContentsByIdNotification( nIdNotification, NotificationStorePlugin.getPlugin( ) );
196     }
197 
198     /**
199      * Load the data by id notification and notification type
200      * 
201      * @param nIdNotification
202      *            the id notification
203      * @return the list which contains the data by id notification
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      * Init Notification content
269      * 
270      * @param nNotificationId
271      * @param notificationType
272      * @param strNotificationContent
273      * @throws IOException
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      * Save notification content in file store
291      * @param notification
292      * @param notificationType
293      * @param strNotificationContent
294      * @return file id
295      * @throws IOException
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         //Convert notification content to bytes
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         //Create file
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             //Save file
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      * Get status for mydashboard notification
339      * 
340      * @param notification
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 }