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 fr.paris.lutece.plugins.grubusiness.business.notification.INotificationEventDAO;
37  import fr.paris.lutece.plugins.grubusiness.business.notification.NotificationEvent;
38  import fr.paris.lutece.plugins.grubusiness.business.notification.NotificationFilter;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  import java.util.Optional;
43  
44  /**
45   * This class provides Data Access methods for NotificationEvent objects
46   */
47  public final class MockNotificationEventDAO implements INotificationEventDAO
48  {
49      List<NotificationEvent> _eventList = new ArrayList<>( );
50  
51      /**
52       * {@inheritDoc }
53       */
54      @Override
55      public NotificationEvent insert( NotificationEvent notificationEvent )
56      {
57          _eventList.add( notificationEvent );
58  
59          return notificationEvent;
60      }
61  
62      /**
63       * {@inheritDoc }
64       */
65      @Override
66      public Optional<NotificationEvent> loadById( int nKey )
67      {
68          for ( NotificationEvent event : _eventList )
69          {
70              if ( event.getId( ) == nKey )
71                  return Optional.of( event );
72          }
73  
74          return Optional.empty( );
75      }
76  
77      /**
78       * {@inheritDoc }
79       */
80      @Override
81      public void delete( int nKey )
82      {
83          for ( NotificationEvent event : _eventList )
84          {
85              if ( event.getId( ) == nKey )
86              {
87                  _eventList.remove( event );
88                  return;
89              }
90          }
91      }
92  
93      /**
94       * {@inheritDoc }
95       */
96      @Override
97      public List<NotificationEvent> loadByDemand( String strDemandId, String strDemandTypeId )
98      {
99          List<NotificationEvent> eventDemandList = new ArrayList<>( );
100 
101         for ( NotificationEvent event : _eventList )
102         {
103             if ( event.getDemand( ).getId( ).equals( strDemandId ) && event.getDemand( ).getTypeId( ).equals( strDemandTypeId ) )
104             {
105                 eventDemandList.add( event );
106             }
107         }
108 
109         return eventDemandList;
110     }
111 
112     /**
113      * {@inheritDoc }
114      */
115     @Override
116     public List<NotificationEvent> loadByFilter( NotificationFilter filter )
117     {
118         List<NotificationEvent> eventDemandList = new ArrayList<>( );
119 
120         for ( NotificationEvent event : _eventList )
121         {
122             if ( filter.containsDemandTypeId( ) && event.getDemand( ).getTypeId( ).equals( filter.getDemandTypeId( ) ) )
123             {
124                 eventDemandList.add( event );
125             }
126         }
127 
128         return eventDemandList;
129     }
130 
131     /**
132      * {@inheritDoc }
133      */
134     @Override
135     public List<Integer> loadIdsByFilter( NotificationFilter filter )
136     {
137         List<Integer> eventDemandList = new ArrayList<>( );
138 
139         for ( NotificationEvent event : _eventList )
140         {
141             if ( filter.containsDemandTypeId( ) && event.getDemand( ).getTypeId( ).equals( filter.getDemandTypeId( ) ) )
142             {
143                 eventDemandList.add( event.getId( ) );
144             }
145         }
146 
147         return eventDemandList;
148     }
149 
150     @Override
151     public List<NotificationEvent> loadByNotification( String strDemandId, String strDemandTypeId, long lNotificationDate )
152     {
153         List<NotificationEvent> eventList = new ArrayList<>( );
154 
155         for ( NotificationEvent event : _eventList )
156         {
157             if ( event.getDemand( ).getId( ).equals( strDemandId ) && event.getDemand( ).getTypeId( ).equals( strDemandTypeId )
158                     && event.getNotificationDate( ) == lNotificationDate )
159             {
160                 eventList.add( event );
161             }
162         }
163 
164         return eventList;
165     }
166 
167     @Override
168     public List<NotificationEvent> loadByIds( List<Integer> listIds )
169     {
170         List<NotificationEvent> eventList = new ArrayList<>( );
171 
172         for ( NotificationEvent event : _eventList )
173         {
174             if ( listIds.contains( event.getId( ) ) )
175             {
176                 eventList.add( event );
177             }
178         }
179 
180         return eventList;
181     }
182 
183     @Override
184     public String deleteBeforeDate( long lDate )
185     {
186 
187         return "MOCK (no need to purge)";
188     }
189 
190 }