View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.newsletter.business;
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   * This class provides Data Access methods for SendingNewsLetter objects
44   */
45  public final class SendingNewsLetterDAO implements ISendingNewsLetterDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_INSERT = "INSERT INTO newsletter_sending ( id_sending, id_newsletter , date_sending, subscriber_count, html, email_subject ) VALUES ( ?, ?, ?, ?, ?, ? )";
49      private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_sending WHERE id_sending = ? ";
50      private static final String SQL_QUERY_SELECT = "SELECT id_newsletter, date_sending, subscriber_count, html, email_subject FROM newsletter_sending WHERE id_sending = ? ";
51      private static final String SQL_QUERY_SELECT_LAST_SENDIND_BY_NEWSLETTER = " SELECT id_sending, id_newsletter, date_sending, subscriber_count, html, email_subject FROM newsletter_sending WHERE id_newsletter = ? ORDER BY date_sending DESC";
52      private static final String SQL_QUERY_SELECT_ALL_SENDINDS = "SELECT id_sending, id_newsletter, date_sending, subscriber_count, html, email_subject FROM newsletter_sending ORDER BY date_sending DESC";
53      private static final String SQL_QUERY_UPDATE = "UPDATE newsletter_sending SET date_sending = ? WHERE id_sending = ?";
54      private static final String SQL_QUERY_CHECK_PRIMARY_KEY = "SELECT id_sending FROM newsletter_sending WHERE id_sending = ?";
55      private static final String SQL_QUERY_NEW_PRIMARY_KEY = "SELECT max(id_sending) FROM newsletter_sending ";
56  
57      ///////////////////////////////////////////////////////////////////////////////////////
58      // Access methods to data
59  
60      /**
61       * Insert a new record in the table.
62       * 
63       * @param sending
64       *            the object to be inserted
65       * @param plugin
66       *            the Plugin
67       */
68      public void insert( SendingNewsLetter sending, Plugin plugin )
69      {
70          int nNewPrimaryKey = newPrimaryKey( plugin );
71          sending.setId( nNewPrimaryKey );
72  
73          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
74  
75          daoUtil.setInt( 1, sending.getId( ) );
76          daoUtil.setInt( 2, sending.getNewsLetterId( ) );
77          daoUtil.setTimestamp( 3, sending.getDate( ) );
78          daoUtil.setInt( 4, sending.getCountSubscribers( ) );
79          daoUtil.setString( 5, sending.getHtml( ) );
80          daoUtil.setString( 6, sending.getEmailSubject( ) );
81          daoUtil.executeUpdate( );
82          daoUtil.free( );
83      }
84  
85      /**
86       * Delete the record in the database
87       * 
88       * @param nSendingId
89       *            the sending Identifier
90       * @param plugin
91       *            the Plugin
92       */
93      public void delete( int nSendingId, Plugin plugin )
94      {
95          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
96          daoUtil.setInt( 1, nSendingId );
97          daoUtil.executeUpdate( );
98          daoUtil.free( );
99      }
100 
101     /**
102      * Loads the data from the database
103      * 
104      * @param nSendingId
105      *            the sending identifier
106      * @param plugin
107      *            the Plugin
108      * @return an object SendingNewsLetter
109      */
110     public SendingNewsLetter load( int nSendingId, Plugin plugin )
111     {
112         SendingNewsLetter sending = null;
113 
114         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
115 
116         daoUtil.setInt( 1, nSendingId );
117         daoUtil.executeQuery( );
118 
119         if ( daoUtil.next( ) )
120         {
121             sending = new SendingNewsLetter( );
122             sending.setId( nSendingId );
123             sending.setNewsLetterId( daoUtil.getInt( 1 ) );
124             sending.setDate( daoUtil.getTimestamp( 2 ) );
125             sending.setCountSubscribers( daoUtil.getInt( 3 ) );
126             sending.setHtml( daoUtil.getString( 4 ) );
127             sending.setEmailSubject( daoUtil.getString( 5 ) );
128         }
129 
130         daoUtil.free( );
131 
132         return sending;
133     }
134 
135     /**
136      * Update the record in the table
137      * 
138      * @param sending
139      *            an instance of the class SendingNewsLetter
140      * @param plugin
141      *            the Plugin
142      */
143     public void store( SendingNewsLetter sending, Plugin plugin )
144     {
145         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
146         daoUtil.setTimestamp( 1, sending.getDate( ) );
147         daoUtil.setInt( 2, sending.getId( ) );
148         daoUtil.executeUpdate( );
149         daoUtil.free( );
150     }
151 
152     /**
153      * Check the unicity of a primary key
154      * 
155      * @param nKey
156      *            the identifier to check
157      * @param plugin
158      *            the Plugin
159      * @return true if the identifier exist and false if not
160      */
161     boolean checkPrimaryKey( int nKey, Plugin plugin )
162     {
163         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PRIMARY_KEY, plugin );
164         daoUtil.setInt( 1, nKey );
165         daoUtil.executeQuery( );
166 
167         if ( !daoUtil.next( ) )
168         {
169             daoUtil.free( );
170 
171             return false;
172         }
173 
174         daoUtil.free( );
175 
176         return true;
177     }
178 
179     /**
180      * Generate a new primary key to add a new sending
181      * 
182      * @param plugin
183      *            the Plugin
184      * @return the primary key
185      */
186     int newPrimaryKey( Plugin plugin )
187     {
188         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin );
189         int nKey;
190 
191         daoUtil.executeQuery( );
192 
193         if ( !daoUtil.next( ) )
194         {
195             // If the table is empty
196             nKey = 1;
197         }
198 
199         nKey = daoUtil.getInt( 1 ) + 1;
200 
201         daoUtil.free( );
202 
203         return nKey;
204     }
205 
206     /**
207      * Returns the last sending performed for the newsletter of given id
208      * 
209      * @param newsletterId
210      *            the newsletter id for wich we need the last sending
211      * @param plugin
212      *            the plugin
213      * @return the last sending for the given newsletter id - null if no sending found
214      */
215     public SendingNewsLetter selectLastSendingForNewsletterId( int newsletterId, Plugin plugin )
216     {
217         SendingNewsLetter sending = null;
218 
219         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LAST_SENDIND_BY_NEWSLETTER, plugin );
220         daoUtil.setInt( 1, newsletterId );
221         daoUtil.executeQuery( );
222 
223         if ( daoUtil.next( ) )
224         {
225             sending = new SendingNewsLetter( );
226             sending.setId( daoUtil.getInt( 1 ) );
227             sending.setNewsLetterId( daoUtil.getInt( 2 ) );
228             sending.setDate( daoUtil.getTimestamp( 3 ) );
229             sending.setCountSubscribers( daoUtil.getInt( 4 ) );
230             sending.setHtml( daoUtil.getString( 5 ) );
231             sending.setEmailSubject( daoUtil.getString( 6 ) );
232         }
233 
234         daoUtil.free( );
235 
236         return sending;
237     }
238 
239     /**
240      * Returns all the last sending performed for the newsletter of given id
241      * 
242      * @param newsletterId
243      *            the newsletter id for wich we need the last sending
244      * @param plugin
245      *            the plugin
246      * @return the last sending for the given newsletter id - null if no sending found
247      */
248     public List<SendingNewsLetter> selectAllLastSendingForNewsletterId( int newsletterId, Plugin plugin )
249     {
250         SendingNewsLetter sending = null;
251         List<SendingNewsLetter> results = new ArrayList<SendingNewsLetter>( );
252         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LAST_SENDIND_BY_NEWSLETTER, plugin );
253         daoUtil.setInt( 1, newsletterId );
254         daoUtil.executeQuery( );
255 
256         while ( daoUtil.next( ) )
257         {
258             sending = new SendingNewsLetter( );
259             sending.setId( daoUtil.getInt( 1 ) );
260             sending.setNewsLetterId( daoUtil.getInt( 2 ) );
261             sending.setDate( daoUtil.getTimestamp( 3 ) );
262             sending.setCountSubscribers( daoUtil.getInt( 4 ) );
263             sending.setHtml( daoUtil.getString( 5 ) );
264             sending.setEmailSubject( daoUtil.getString( 6 ) );
265 
266             results.add( sending );
267         }
268 
269         daoUtil.free( );
270 
271         return results;
272     }
273 
274     /**
275      * Returns all the sendings in the database.
276      * 
277      * @param plugin
278      *            the plugin
279      * @return a list of SendingNewsLetter objects.
280      */
281     public List<SendingNewsLetter> findAllSendings( Plugin plugin )
282     {
283         List<SendingNewsLetter> results = new ArrayList<SendingNewsLetter>( );
284 
285         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_SENDINDS, plugin );
286         daoUtil.executeQuery( );
287 
288         while ( daoUtil.next( ) )
289         {
290             SendingNewsLetter/business/SendingNewsLetter.html#SendingNewsLetter">SendingNewsLetter sending = new SendingNewsLetter( );
291             sending.setId( daoUtil.getInt( 1 ) );
292             sending.setNewsLetterId( daoUtil.getInt( 2 ) );
293             sending.setDate( daoUtil.getTimestamp( 3 ) );
294             sending.setCountSubscribers( daoUtil.getInt( 4 ) );
295             sending.setHtml( daoUtil.getString( 5 ) );
296             sending.setEmailSubject( daoUtil.getString( 6 ) );
297 
298             results.add( sending );
299         }
300 
301         daoUtil.free( );
302 
303         return results;
304     }
305 
306     /**
307      * Returns all the sendings in the database.
308      * 
309      * @return a list of SendingNewsLetter objects.
310      * @param listId
311      *            A list of newsletter id
312      * @param plugin
313      *            the plugin
314      */
315     public ArrayList<SendingNewsLetter> findSendingsByIds( ArrayList<Integer> listId, Plugin plugin )
316     {
317         ArrayList<SendingNewsLetter> results = new ArrayList<SendingNewsLetter>( );
318         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_SENDINDS, plugin );
319         daoUtil.executeQuery( );
320 
321         while ( daoUtil.next( ) )
322         {
323             if ( listId.contains( Integer.valueOf( daoUtil.getInt( 1 ) ) ) )
324             {
325                 SendingNewsLetter/business/SendingNewsLetter.html#SendingNewsLetter">SendingNewsLetter sending = new SendingNewsLetter( );
326                 sending.setId( daoUtil.getInt( 1 ) );
327                 sending.setNewsLetterId( daoUtil.getInt( 2 ) );
328                 sending.setDate( daoUtil.getTimestamp( 3 ) );
329                 sending.setCountSubscribers( daoUtil.getInt( 4 ) );
330                 sending.setHtml( daoUtil.getString( 5 ) );
331                 sending.setEmailSubject( daoUtil.getString( 6 ) );
332 
333                 results.add( sending );
334             }
335         }
336 
337         daoUtil.free( );
338 
339         return results;
340     }
341 }