View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.crm.service.notification;
35  
36  import fr.paris.lutece.plugins.crm.business.notification.MokeNotification;
37  import fr.paris.lutece.plugins.crm.business.notification.Notification;
38  import fr.paris.lutece.plugins.crm.business.notification.NotificationFilter;
39  import fr.paris.lutece.plugins.crm.business.notification.NotificationHome;
40  
41  import java.sql.Timestamp;
42  
43  import java.util.ArrayList;
44  import java.util.Date;
45  import java.util.List;
46  
47  /**
48   *
49   * MokeNotificationService
50   *
51   */
52  public class MokeNotificationService extends NotificationService
53  {
54      private static final int ID_NOTIFICATION1 = 1;
55      private static final int ID_NOTIFICATION2 = 2;
56  
57      /**
58       * {@inheritDoc}
59       */
60      public Notification findByPrimaryKey( int strIdNotification )
61      {
62          Notification notification = new MokeNotification( ID_NOTIFICATION1 );
63          System.out.println( trace( notification ) );
64  
65          return notification;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      public int create( Notification notification )
72      {
73          notification.setIdNotification( ID_NOTIFICATION1 );
74          notification.setIsRead( false );
75          notification.setDateCreation( new Timestamp( new Date( ).getTime( ) ) );
76  
77          System.out.println( trace( notification ) );
78  
79          return notification.getIdNotification( );
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      public void update( Notification notification )
86      {
87          if ( notification != null )
88          {
89              notification.setDateCreation( new Timestamp( new Date( ).getTime( ) ) );
90          }
91  
92          System.out.println( trace( notification ) );
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      public void remove( int nIdNotification )
99      {
100         System.out.println( trace( ) );
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     public void removeByIdDemand( int nIdDemand )
107     {
108         System.out.println( trace( ) );
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     public List<Notification> findAll( )
115     {
116         List<Notification> listNotifications = new ArrayList<Notification>( );
117         listNotifications.add( new MokeNotification( ID_NOTIFICATION1 ) );
118         listNotifications.add( new MokeNotification( ID_NOTIFICATION2 ) );
119 
120         System.out.println( trace( listNotifications ) );
121 
122         return listNotifications;
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     public List<Notification> findByFilter( NotificationFilter nFilter )
129     {
130         List<Notification> listNotifications = new ArrayList<Notification>( );
131         listNotifications.add( new MokeNotification( ID_NOTIFICATION1 ) );
132         listNotifications.add( new MokeNotification( ID_NOTIFICATION2 ) );
133 
134         System.out.println( trace( listNotifications, nFilter ) );
135 
136         return listNotifications;
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     public int getNumberUnreadNotifications( int nIdDemand )
143     {
144         NotificationFilter nFilter = new NotificationFilter( );
145         nFilter.setIdDemand( nIdDemand );
146         nFilter.setIsRead( false );
147 
148         List<Notification> listNotifications = NotificationHome.findByFilter( nFilter );
149 
150         System.out.println( trace( listNotifications ) );
151 
152         return listNotifications.size( );
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     public int getNumberNotifications( int nIdDemand )
159     {
160         NotificationFilter nFilter = new NotificationFilter( );
161         nFilter.setIdDemand( nIdDemand );
162 
163         List<Notification> listNotifications = NotificationHome.findByFilter( nFilter );
164 
165         System.out.println( trace( listNotifications ) );
166 
167         return listNotifications.size( );
168     }
169 
170     /**
171      * Trace
172      * 
173      * @return trace
174      */
175     private String trace( )
176     {
177         return trace( null, null );
178     }
179 
180     /**
181      * Trace
182      * 
183      * @param notification
184      *            notification
185      * @return trace
186      */
187     private String trace( Notification notification )
188     {
189         List<Notification> listNotifications = new ArrayList<Notification>( );
190         listNotifications.add( notification );
191 
192         return trace( listNotifications, null );
193     }
194 
195     /**
196      * Trace
197      * 
198      * @param listNotifications
199      *            the list of notifications
200      * @return trace
201      */
202     private String trace( List<Notification> listNotifications )
203     {
204         return trace( listNotifications, null );
205     }
206 
207     /**
208      * Trace
209      * 
210      * @param listNotifications
211      *            the list of notifications
212      * @return trace
213      */
214     private String trace( List<Notification> listNotifications, NotificationFilter nFilter )
215     {
216         StringBuilder sbTrace = new StringBuilder( );
217         sbTrace.append( "\n ---------------------- Notification Service -------------------" );
218         sbTrace.append( "\nMethod name : " + Thread.currentThread( ).getStackTrace( ) [2].getMethodName( ) );
219 
220         if ( ( listNotifications != null ) && ( listNotifications.size( ) > 0 ) )
221         {
222             for ( Notification notification : listNotifications )
223             {
224                 sbTrace.append( "\n-- Notification --" );
225                 sbTrace.append( "\nid_notification : " + notification.getIdNotification( ) );
226                 sbTrace.append( "\ndate_creation : " + notification.getDateCreation( ) );
227                 sbTrace.append( "\nid_demand : " + notification.getIdDemand( ) );
228                 sbTrace.append( "\nis_read : " + notification.isRead( ) );
229                 sbTrace.append( "\nmessage : " + notification.getMessage( ) );
230                 sbTrace.append( "\nobject : " + notification.getObject( ) );
231                 sbTrace.append( "\nsender : " + notification.getSender( ) );
232             }
233         }
234 
235         if ( nFilter != null )
236         {
237             sbTrace.append( "\n-- Notification Filter --" );
238             sbTrace.append( "\nid_demand : " + nFilter.getIdDemand( ) );
239             sbTrace.append( "\nis_wide_search : " + nFilter.getIsWideSearch( ) );
240         }
241 
242         sbTrace.append( "\n --------------------------------------------------------------------" );
243 
244         return sbTrace.toString( );
245     }
246 }