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.Notification;
37  import fr.paris.lutece.plugins.crm.business.notification.NotificationFilter;
38  import fr.paris.lutece.plugins.crm.business.notification.NotificationHome;
39  import fr.paris.lutece.portal.service.spring.SpringContextService;
40  
41  import java.sql.Timestamp;
42  
43  import java.util.Date;
44  import java.util.List;
45  
46  /**
47   *
48   * NotificationService
49   *
50   */
51  public class NotificationService
52  {
53      private static final String BEAN_CRM_NOTIFICATIONSERVICE = "crm.notificationService";
54  
55      /**
56       * Constructor
57       */
58      protected NotificationService( )
59      {
60      }
61  
62      /**
63       * Get the instance of {@link NotificationService}
64       * 
65       * @return the instance of {@link NotificationService}
66       */
67      public static NotificationService getService( )
68      {
69          return SpringContextService.getBean( BEAN_CRM_NOTIFICATIONSERVICE );
70      }
71  
72      /**
73       * Find the notification by its primary key
74       * 
75       * @param nIdNotification
76       *            the id notification
77       * @return a {@link Notification}
78       */
79      public Notification findByPrimaryKey( int nIdNotification )
80      {
81          return NotificationHome.findByPrimaryKey( nIdNotification );
82      }
83  
84      /**
85       * Create a notification
86       * 
87       * @param notification
88       *            the notification
89       * @return the newly created notification id
90       */
91      public int create( Notification notification )
92      {
93          int nNewPrimaryKey = -1;
94  
95          if ( notification != null )
96          {
97              notification.setIsRead( false );
98              notification.setDateCreation( new Timestamp( new Date( ).getTime( ) ) );
99              nNewPrimaryKey = NotificationHome.create( notification );
100         }
101 
102         return nNewPrimaryKey;
103     }
104 
105     /**
106      * Update the notification
107      * 
108      * @param notification
109      *            the notification
110      */
111     public void update( Notification notification )
112     {
113         if ( notification != null )
114         {
115             NotificationHome.update( notification );
116         }
117     }
118 
119     /**
120      * Remove the notification
121      * 
122      * @param nIdNotification
123      *            the id notification
124      */
125     public void remove( int nIdNotification )
126     {
127         NotificationHome.remove( nIdNotification );
128     }
129 
130     /**
131      * Remove the notifications from a given ID demand
132      * 
133      * @param nIdDemand
134      *            the id demand
135      */
136     public void removeByIdDemand( int nIdDemand )
137     {
138         NotificationHome.removeByIdDemand( nIdDemand );
139     }
140 
141     /**
142      * Find all notifications
143      * 
144      * @return a list of {@link Notification}
145      */
146     public List<Notification> findAll( )
147     {
148         return NotificationHome.findAll( );
149     }
150 
151     /**
152      * Find notifications by a filter
153      * 
154      * @param nFilter
155      *            the filter
156      * @return a list of {@link Notification}
157      */
158     public List<Notification> findByFilter( NotificationFilter nFilter )
159     {
160         return NotificationHome.findByFilter( nFilter );
161     }
162 
163     /**
164      * Get the number of unread notifications of a demand
165      * 
166      * @param nIdDemand
167      *            the id demand
168      * @return the number of unread notifications
169      */
170     public int getNumberUnreadNotifications( int nIdDemand )
171     {
172         NotificationFilterness/notification/NotificationFilter.html#NotificationFilter">NotificationFilter nFilter = new NotificationFilter( );
173         nFilter.setIdDemand( nIdDemand );
174         nFilter.setIsRead( false );
175 
176         List<Notification> listNotifications = NotificationHome.findByFilter( nFilter );
177 
178         return listNotifications.size( );
179     }
180 
181     /**
182      * Get the number of notifications of a demand
183      * 
184      * @param nIdDemand
185      *            the id demand
186      * @return the number of notifications
187      */
188     public int getNumberNotifications( int nIdDemand )
189     {
190         NotificationFilterness/notification/NotificationFilter.html#NotificationFilter">NotificationFilter nFilter = new NotificationFilter( );
191         nFilter.setIdDemand( nIdDemand );
192 
193         List<Notification> listNotifications = NotificationHome.findByFilter( nFilter );
194 
195         return listNotifications.size( );
196     }
197 }