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.grubusiness.business.mock;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import fr.paris.lutece.plugins.grubusiness.business.notification.INotificationDAO;
40  import fr.paris.lutece.plugins.grubusiness.business.notification.Notification;
41  import fr.paris.lutece.plugins.grubusiness.business.notification.NotificationFilter;
42  import java.util.HashMap;
43  import java.util.Map;
44  import java.util.Optional;
45  
46  /**
47   * This class is a mock implementation of {@link INotificationDAO}
48   */
49  public class MockNotificationDAO implements INotificationDAO
50  {
51  
52      private final List<Notification> _listMockNotification = new ArrayList<Notification>( );
53      private int _lastId;
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public List<Notification> loadByDemand( String strDemandId, String strDemandTypeId )
60      {
61          List<Notification> listResult = new ArrayList<Notification>( );
62          for ( Notification notification : _listMockNotification )
63          {
64              if ( notification.getDemand( ) != null && notification.getDemand( ).getId( ).equals( strDemandId )
65                      && notification.getDemand( ).getTypeId( ).equals( strDemandTypeId ) )
66              {
67                  listResult.add( notification );
68              }
69          }
70          return listResult;
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public List<Notification> loadByFilter( NotificationFilter notificationFilter )
78      {
79          List<Notification> listResult = new ArrayList<Notification>( );
80          for ( Notification notification : _listMockNotification )
81          {
82              boolean bAddNotif = true;
83              if ( notificationFilter.containsDemandId( ) )
84              {
85                  bAddNotif = notification.getDemand( ) != null && notificationFilter.getDemandId( ).equals( notification.getDemand( ).getId( ) );
86              }
87              if ( bAddNotif && notificationFilter.containsDemandTypeId( ) )
88              {
89                  bAddNotif = notification.getDemand( ) != null && notificationFilter.getDemandTypeId( ).equals( notification.getDemand( ).getTypeId( ) );
90              }
91              if ( bAddNotif && notificationFilter.containsBackofficeNotificationType( ) )
92              {
93                  bAddNotif = notification.getBackofficeNotification( ) != null;
94              }
95              if ( bAddNotif && notificationFilter.containsBroadcastEmailNotificationType( ) )
96              {
97                  bAddNotif = notification.getBroadcastEmail( ) != null && !notification.getBroadcastEmail( ).isEmpty( );
98              }
99              if ( bAddNotif && notificationFilter.containsCustomerEmailNotificationType( ) )
100             {
101                 bAddNotif = notification.getEmailNotification( ) != null;
102             }
103             if ( bAddNotif && notificationFilter.containsMyDashboardNotificationType( ) )
104             {
105                 bAddNotif = notification.getMyDashboardNotification( ) != null;
106             }
107             if ( bAddNotif && notificationFilter.containsSmsNotificationType( ) )
108             {
109                 bAddNotif = notification.getSmsNotification( ) != null;
110             }
111             if ( bAddNotif )
112             {
113                 listResult.add( notification );
114             }
115         }
116         return listResult;
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public Notification insert( Notification notification )
124     {
125         notification.setId( _lastId++ );
126         _listMockNotification.add( notification );
127         return notification;
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public void deleteByDemand( String strDemandId, String strDemandTypeId )
135     {
136         int nIndexDelete = -1;
137         for ( int nIndex = 0; nIndex < _listMockNotification.size( ); nIndex++ )
138         {
139             Notification notification = _listMockNotification.get( nIndex );
140             if ( notification.getDemand( ) != null && notification.getDemand( ).getId( ).equals( strDemandId )
141                     && notification.getDemand( ).getTypeId( ).equals( strDemandTypeId ) )
142             {
143                 nIndexDelete = nIndex;
144                 break;
145             }
146         }
147         if ( nIndexDelete > -1 )
148         {
149             _listMockNotification.remove( nIndexDelete );
150         }
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public List<Integer> loadIdsByFilter( NotificationFilter notificationFilter )
158     {
159         List<Integer> listResult = new ArrayList<>( );
160         for ( Notification notification : _listMockNotification )
161         {
162             boolean bAddNotif = true;
163             if ( notificationFilter.containsDemandId( ) )
164             {
165                 bAddNotif = notification.getDemand( ) != null && notificationFilter.getDemandId( ).equals( notification.getDemand( ).getId( ) );
166             }
167             if ( bAddNotif && notificationFilter.containsDemandTypeId( ) )
168             {
169                 bAddNotif = notification.getDemand( ) != null && notificationFilter.getDemandTypeId( ).equals( notification.getDemand( ).getTypeId( ) );
170             }
171             if ( bAddNotif && notificationFilter.containsBackofficeNotificationType( ) )
172             {
173                 bAddNotif = notification.getBackofficeNotification( ) != null;
174             }
175             if ( bAddNotif && notificationFilter.containsBroadcastEmailNotificationType( ) )
176             {
177                 bAddNotif = notification.getBroadcastEmail( ) != null && !notification.getBroadcastEmail( ).isEmpty( );
178             }
179             if ( bAddNotif && notificationFilter.containsCustomerEmailNotificationType( ) )
180             {
181                 bAddNotif = notification.getEmailNotification( ) != null;
182             }
183             if ( bAddNotif && notificationFilter.containsMyDashboardNotificationType( ) )
184             {
185                 bAddNotif = notification.getMyDashboardNotification( ) != null;
186             }
187             if ( bAddNotif && notificationFilter.containsSmsNotificationType( ) )
188             {
189                 bAddNotif = notification.getSmsNotification( ) != null;
190             }
191             if ( bAddNotif )
192             {
193                 listResult.add( notification.getId( ) );
194             }
195         }
196         return listResult;
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     @Override
203     public Optional<Notification> loadById( int nId )
204     {
205 
206         for ( Notification notification : _listMockNotification )
207         {
208             if ( notification.getDemand( ) != null && notification.getId( ) == nId )
209             {
210                 return Optional.ofNullable( notification );
211             }
212         }
213         return Optional.empty( );
214     }
215 
216     @Override
217     public List<Notification> loadByDemandAndDate( String strDemandId, String strDemandTypeId, long lDate )
218     {
219         List<Notification> listResult = new ArrayList<Notification>( );
220         for ( Notification notification : _listMockNotification )
221         {
222             if ( notification.getDemand( ) != null && notification.getDemand( ).getId( ).equals( strDemandId )
223                     && notification.getDemand( ).getTypeId( ).equals( strDemandTypeId ) && notification.getDate( ) == lDate )
224             {
225                 listResult.add( notification );
226             }
227         }
228         return listResult;
229     }
230 
231     @Override
232     public List<String> loadDistinctDemandTypeIds( )
233     {
234         Map<String, String> mapDemandTypeIds = new HashMap<>( );
235         for ( Notification notification : _listMockNotification )
236         {
237             mapDemandTypeIds.put( String.valueOf( notification.getId( ) ), null );
238         }
239 
240         return new ArrayList<>( mapDemandTypeIds.keySet( ) );
241     }
242 
243     @Override
244     public List<Notification> loadByIds( List<Integer> listIds )
245     {
246 
247         List<Notification> list = new ArrayList<>( );
248 
249         for ( Notification notification : _listMockNotification )
250         {
251             if ( listIds.contains( notification.getId( ) ) )
252             {
253                 list.add( notification );
254             }
255         }
256         return list;
257     }
258 
259     @Override
260     public void delete( int id )
261     {
262         for ( Notification notification : _listMockNotification )
263         {
264             if ( id == notification.getId( ) )
265             {
266                 _listMockNotification.remove( notification );
267                 break;
268             }
269         }
270 
271     }
272 
273     @Override
274     public List<Notification> loadByDemandIdTypeIdCustomerId( String strDemandId, String strDemandTypeId, String strCustomerId )
275     {
276         return null;
277     }
278 
279     @Override
280     public Notification loadLastNotifByDemandIdAndDemandTypeId( String strDemandId, String strDemandTypeId )
281     {
282         return null;
283     }
284 }