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.calendar.business.notification;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  
43  /**
44   * 
45   * class TaskCommentConfig
46   * 
47   */
48  public class CalendarNotificationDAO implements ICalendarNotificationDAO
49  {
50      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = " SELECT key_email, email, id_agenda,date_expiry "
51              + " FROM calendar_notify_key WHERE key_email=?";
52      private static final String SQL_QUERY_INSERT = " INSERT INTO calendar_notify_key( "
53              + " key_email, email, id_agenda, date_expiry)" + " VALUES (?,?,?,?)";
54      private static final String SQL_QUERY_UPDATE = "UPDATE calendar_notify_key "
55              + " SET key_email = ?, email = ?, id_agenda = ?,date_expiry = ? WHERE key_email = ?";
56      private static final String SQL_QUERY_DELETE = " DELETE FROM calendar_notify_key WHERE key_email = ? ";
57      private static final String SQL_QUERY_SELECT_EXPIRY = " SELECT key_email, email, id_agenda,date_expiry FROM calendar_notify_key WHERE date_expiry < NOW(  )";
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public synchronized void insert( CalendarNotification calendarNotification, Plugin plugin )
64      {
65          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
66  
67          int nPos = 0;
68  
69          daoUtil.setString( ++nPos, calendarNotification.getKey( ) );
70          daoUtil.setString( ++nPos, calendarNotification.getEmail( ) );
71          daoUtil.setInt( ++nPos, calendarNotification.getIdAgenda( ) );
72          daoUtil.setTimestamp( ++nPos, calendarNotification.getDateExpiry( ) );
73  
74          daoUtil.executeUpdate( );
75          daoUtil.free( );
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public void store( CalendarNotification calendarNotification, Plugin plugin )
83      {
84          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
85  
86          int nPos = 0;
87  
88          daoUtil.setString( ++nPos, calendarNotification.getKey( ) );
89          daoUtil.setString( ++nPos, calendarNotification.getEmail( ) );
90          daoUtil.setInt( ++nPos, calendarNotification.getIdAgenda( ) );
91          daoUtil.setTimestamp( ++nPos, calendarNotification.getDateExpiry( ) );
92  
93          daoUtil.setString( ++nPos, calendarNotification.getKey( ) );
94          daoUtil.executeUpdate( );
95          daoUtil.free( );
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public CalendarNotification load( String strKey, Plugin plugin )
103     {
104         CalendarNotification calendarNotification = null;
105         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
106 
107         daoUtil.setString( 1, strKey );
108         daoUtil.executeQuery( );
109 
110         int nPos = 0;
111 
112         if ( daoUtil.next( ) )
113         {
114             calendarNotification = new CalendarNotification( );
115             calendarNotification.setKey( daoUtil.getString( ++nPos ) );
116             calendarNotification.setEmail( daoUtil.getString( ++nPos ) );
117             calendarNotification.setIdAgenda( daoUtil.getInt( ++nPos ) );
118             calendarNotification.setDateExpiry( daoUtil.getTimestamp( ++nPos ) );
119         }
120 
121         daoUtil.free( );
122 
123         return calendarNotification;
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public void delete( String strKey, Plugin plugin )
131     {
132         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
133 
134         daoUtil.setString( 1, strKey );
135         daoUtil.executeUpdate( );
136         daoUtil.free( );
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public List<CalendarNotification> selectNotificationExpiry( Plugin plugin )
144     {
145         int nPos = 0;
146         List<CalendarNotification> listNotificationExpiry = new ArrayList<CalendarNotification>( );
147         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_EXPIRY, plugin );
148         daoUtil.executeQuery( );
149 
150         while ( daoUtil.next( ) )
151         {
152             nPos = 0;
153 
154             CalendarNotification calendarNotification = new CalendarNotification( );
155             calendarNotification.setKey( daoUtil.getString( ++nPos ) );
156             calendarNotification.setEmail( daoUtil.getString( ++nPos ) );
157             calendarNotification.setIdAgenda( daoUtil.getInt( ++nPos ) );
158             calendarNotification.setDateExpiry( daoUtil.getTimestamp( ++nPos ) );
159             listNotificationExpiry.add( calendarNotification );
160         }
161 
162         daoUtil.free( );
163 
164         return listNotificationExpiry;
165     }
166 }