View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.document.business.publication;
35  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  import java.sql.Timestamp;
39  
40  import java.util.ArrayList;
41  import java.util.Collection;
42  import java.util.Date;
43  
44  /**
45   * This class provides Data Access methods for DocumentPublication objects
46   */
47  public class DocumentPublicationDAO implements IDocumentPublicationDAO
48  {
49      private static final String SQL_QUERY_DELETE = " DELETE FROM document_published WHERE id_portlet = ? AND id_document = ? ";
50      private static final String SQL_QUERY_DELETE_FROM_PORTLET_ID = " DELETE FROM document_published WHERE id_portlet = ? ";
51      private static final String SQL_QUERY_DELETE_FROM_DOCUMENT_ID = " DELETE FROM document_published WHERE id_document = ? ";
52      private static final String SQL_QUERY_INSERT = " INSERT INTO document_published ( id_portlet, id_document, document_order, status, date_publishing ) VALUES ( ?, ?, ?, ?, ? )";
53      private static final String SQL_QUERY_UPDATE = " UPDATE document_published SET document_order = ?, status = ?, date_publishing = ? WHERE id_portlet = ? AND id_document = ? ";
54      private static final String SQL_QUERY_SELECT = " SELECT document_order, status, date_publishing FROM document_published WHERE id_portlet = ? AND id_document = ? ORDER BY document_order ASC ";
55      private static final String SQL_QUERY_SELECT_BY_PORTLET_ID = " SELECT id_document, document_order, status, date_publishing FROM document_published WHERE id_portlet = ? ORDER BY document_order ASC ";
56      private static final String SQL_QUERY_SELECT_BY_DOCUMENT_ID = " SELECT id_portlet, document_order, status, date_publishing FROM document_published WHERE id_document = ? ORDER BY document_order ASC ";
57      private static final String SQL_QUERY_SELECT_BY_PORTLET_ID_AND_STATUS = " SELECT id_document, document_order, date_publishing FROM document_published WHERE id_portlet = ? AND status = ? ORDER BY document_order ASC ";
58      private static final String SQL_QUERY_SELECT_BY_DOCUMENT_ID_AND_STATUS = " SELECT id_portlet, document_order, date_publishing FROM document_published WHERE id_document = ? AND status = ? ORDER BY document_order ASC ";
59      private static final String SQL_QUERY_SELECT_BY_DATE_PUBLISHING_AND_STATUS = " SELECT id_portlet, id_document, document_order, date_publishing FROM document_published WHERE date_publishing >= ? AND status = ? ORDER BY document_order ASC ";
60      private static final String SQL_QUERY_MAX_ORDER = "SELECT max(document_order) FROM document_published WHERE id_portlet = ?  ";
61      private static final String SQL_QUERY_MODIFY_ORDER_BY_ID = "SELECT id_document FROM document_published  WHERE document_order = ? AND id_portlet = ?";
62  
63      /**
64       * Insert the documentsPublication object
65       *
66       * @param documentPublication
67       *            The document Publication object
68       */
69      public void insert( DocumentPublication documentPublication )
70      {
71          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
72  
73          daoUtil.setInt( 1, documentPublication.getPortletId( ) );
74          daoUtil.setInt( 2, documentPublication.getDocumentId( ) );
75          daoUtil.setInt( 3, documentPublication.getDocumentOrder( ) );
76          daoUtil.setInt( 4, documentPublication.getStatus( ) );
77          daoUtil.setTimestamp( 5, new Timestamp( documentPublication.getDatePublishing( ).getTime( ) ) );
78  
79          daoUtil.executeUpdate( );
80          daoUtil.free( );
81      }
82  
83      /**
84       * Update the {@link DocumentPublication} object
85       *
86       * @param documentPublication
87       *            The {@link DocumentPublication} object
88       */
89      public void store( DocumentPublication documentPublication )
90      {
91          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
92          daoUtil.setInt( 1, documentPublication.getDocumentOrder( ) );
93          daoUtil.setInt( 2, documentPublication.getStatus( ) ); // FIXME old : daoUtil.setInt( 2, maxOrderDocumentList( nPortletId ) + 1 );
94          daoUtil.setTimestamp( 3, new Timestamp( documentPublication.getDatePublishing( ).getTime( ) ) );
95          daoUtil.setInt( 4, documentPublication.getPortletId( ) );
96          daoUtil.setInt( 5, documentPublication.getDocumentId( ) );
97  
98          daoUtil.executeUpdate( );
99          daoUtil.free( );
100     }
101 
102     /**
103      * Delete records for table document_published specified by portlet id and document id
104      *
105      * @param nPortletId
106      *            the portlet identifier
107      * @param nDocumentId
108      *            the document identifier
109      */
110     public void delete( int nPortletId, int nDocumentId )
111     {
112         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
113         daoUtil.setInt( 1, nPortletId );
114         daoUtil.setInt( 2, nDocumentId );
115         daoUtil.executeUpdate( );
116         daoUtil.free( );
117     }
118 
119     /**
120      * Delete records for table document_published specified by portlet id
121      *
122      * @param nPortletId
123      *            the portlet identifier
124      */
125     public void deleteFromPortletId( int nPortletId )
126     {
127         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_FROM_PORTLET_ID );
128         daoUtil.setInt( 1, nPortletId );
129         daoUtil.executeUpdate( );
130         daoUtil.free( );
131     }
132 
133     /**
134      * Delete records for table document_published specified by portlet id
135      *
136      * @param nDocumentId
137      *            the document identifier
138      */
139     public void deleteFromDocumentId( int nDocumentId )
140     {
141         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_FROM_DOCUMENT_ID );
142         daoUtil.setInt( 1, nDocumentId );
143         daoUtil.executeUpdate( );
144         daoUtil.free( );
145     }
146 
147     ///////////////////////////////////////////////////////////////////////////////
148     // Select
149 
150     /**
151      * Select the {@link DocumentPublication} object specified by the portlet id and document id
152      * 
153      * @param nPortletId
154      *            The portlet identifier
155      * @param nDocumentId
156      *            The document identifier
157      * @return The {@link DocumentPublication} object or null if the object does not exists
158      */
159     public DocumentPublication select( int nPortletId, int nDocumentId )
160     {
161         DocumentPublication documentPublication = null;
162         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
163         daoUtil.setInt( 1, nPortletId );
164         daoUtil.setInt( 2, nDocumentId );
165         daoUtil.executeQuery( );
166 
167         while ( daoUtil.next( ) )
168         {
169             documentPublication = new DocumentPublication( );
170             documentPublication.setPortletId( nPortletId );
171             documentPublication.setDocumentId( nDocumentId );
172             documentPublication.setDocumentOrder( daoUtil.getInt( 1 ) );
173             documentPublication.setStatus( daoUtil.getInt( 2 ) );
174             documentPublication.setDatePublishing( daoUtil.getTimestamp( 3 ) );
175         }
176 
177         daoUtil.free( );
178 
179         return documentPublication;
180     }
181 
182     /**
183      * Select the list of {@link DocumentPublication} objects specified by the portlet id
184      * 
185      * @param nPortletId
186      *            The portlet identifier
187      * @return The {@link DocumentPublication} objects list (empty list if no objects found)
188      */
189     public Collection<DocumentPublication> selectByPortletId( int nPortletId )
190     {
191         Collection<DocumentPublication> listDocumentPublication = new ArrayList<DocumentPublication>( );
192         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_PORTLET_ID );
193         daoUtil.setInt( 1, nPortletId );
194         daoUtil.executeQuery( );
195 
196         while ( daoUtil.next( ) )
197         {
198             DocumentPublicationlication/DocumentPublication.html#DocumentPublication">DocumentPublication documentPublication = new DocumentPublication( );
199             documentPublication.setPortletId( nPortletId );
200             documentPublication.setDocumentId( daoUtil.getInt( 1 ) );
201             documentPublication.setDocumentOrder( daoUtil.getInt( 2 ) );
202             documentPublication.setStatus( daoUtil.getInt( 3 ) );
203             documentPublication.setDatePublishing( daoUtil.getTimestamp( 4 ) );
204             listDocumentPublication.add( documentPublication );
205         }
206 
207         daoUtil.free( );
208 
209         return listDocumentPublication;
210     }
211 
212     /**
213      * Select the list of {@link DocumentPublication} objects specified by the document id
214      * 
215      * @param nDocumentId
216      *            The document identifier
217      * @return The {@link DocumentPublication} objects list (empty list if no objects found)
218      */
219     public Collection<DocumentPublication> selectByDocumentId( int nDocumentId )
220     {
221         Collection<DocumentPublication> listDocumentPublication = new ArrayList<DocumentPublication>( );
222         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_DOCUMENT_ID );
223         daoUtil.setInt( 1, nDocumentId );
224         daoUtil.executeQuery( );
225 
226         while ( daoUtil.next( ) )
227         {
228             DocumentPublicationlication/DocumentPublication.html#DocumentPublication">DocumentPublication documentPublication = new DocumentPublication( );
229             documentPublication.setPortletId( daoUtil.getInt( 1 ) );
230             documentPublication.setDocumentId( nDocumentId );
231             documentPublication.setDocumentOrder( daoUtil.getInt( 2 ) );
232             documentPublication.setStatus( daoUtil.getInt( 3 ) );
233             documentPublication.setDatePublishing( daoUtil.getTimestamp( 4 ) );
234             listDocumentPublication.add( documentPublication );
235         }
236 
237         daoUtil.free( );
238 
239         return listDocumentPublication;
240     }
241 
242     /**
243      * Select the list of {@link DocumentPublication} objects specified by the portlet id and the status
244      * 
245      * @param nPortletId
246      *            The portlet identifier
247      * @param nStatus
248      *            The status
249      * @return The {@link DocumentPublication} objects list (empty list if no objects found)
250      */
251     public Collection<DocumentPublication> selectByPortletIdAndStatus( int nPortletId, int nStatus )
252     {
253         Collection<DocumentPublication> listDocumentPublication = new ArrayList<DocumentPublication>( );
254         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_PORTLET_ID_AND_STATUS );
255         daoUtil.setInt( 1, nPortletId );
256         daoUtil.setInt( 2, nStatus );
257         daoUtil.executeQuery( );
258 
259         while ( daoUtil.next( ) )
260         {
261             DocumentPublicationlication/DocumentPublication.html#DocumentPublication">DocumentPublication documentPublication = new DocumentPublication( );
262             documentPublication.setPortletId( nPortletId );
263             documentPublication.setDocumentId( daoUtil.getInt( 1 ) );
264             documentPublication.setDocumentOrder( daoUtil.getInt( 2 ) );
265             documentPublication.setStatus( nStatus );
266             documentPublication.setDatePublishing( daoUtil.getTimestamp( 3 ) );
267             listDocumentPublication.add( documentPublication );
268         }
269 
270         daoUtil.free( );
271 
272         return listDocumentPublication;
273     }
274 
275     /**
276      * Select the list of {@link DocumentPublication} objects specified by the document id and the status
277      * 
278      * @param nDocumentId
279      *            The document identifier
280      * @param nStatus
281      *            The status
282      * @return The {@link DocumentPublication} objects list (empty list if no objects found)
283      */
284     public Collection<DocumentPublication> selectByDocumentIdAndStatus( int nDocumentId, int nStatus )
285     {
286         Collection<DocumentPublication> listDocumentPublication = new ArrayList<DocumentPublication>( );
287         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_DOCUMENT_ID_AND_STATUS );
288         daoUtil.setInt( 1, nDocumentId );
289         daoUtil.setInt( 2, nStatus );
290         daoUtil.executeQuery( );
291 
292         while ( daoUtil.next( ) )
293         {
294             DocumentPublicationlication/DocumentPublication.html#DocumentPublication">DocumentPublication documentPublication = new DocumentPublication( );
295             documentPublication.setPortletId( daoUtil.getInt( 1 ) );
296             documentPublication.setDocumentId( nDocumentId );
297             documentPublication.setDocumentOrder( daoUtil.getInt( 2 ) );
298             documentPublication.setStatus( nStatus );
299             documentPublication.setDatePublishing( daoUtil.getTimestamp( 3 ) );
300             listDocumentPublication.add( documentPublication );
301         }
302 
303         daoUtil.free( );
304 
305         return listDocumentPublication;
306     }
307 
308     /**
309      * Find the list of {@link DocumentPublication} objects specified the status and published at or after the specified date
310      * 
311      * @param datePublishing
312      *            The publication date
313      * @param nStatus
314      *            The status
315      * @return The {@link DocumentPublication} objects {@link Collection} ordered by documentOrder ascending. The list is empty if no objects found.
316      */
317     public Collection<DocumentPublication> selectSinceDatePublishingAndStatus( Date datePublishing, int nStatus )
318     {
319         Collection<DocumentPublication> listDocumentPublication = new ArrayList<DocumentPublication>( );
320         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_DATE_PUBLISHING_AND_STATUS );
321         daoUtil.setTimestamp( 1, new Timestamp( datePublishing.getTime( ) ) );
322         daoUtil.setInt( 2, nStatus );
323         daoUtil.executeQuery( );
324 
325         while ( daoUtil.next( ) )
326         {
327             DocumentPublicationlication/DocumentPublication.html#DocumentPublication">DocumentPublication documentPublication = new DocumentPublication( );
328             documentPublication.setPortletId( daoUtil.getInt( 1 ) );
329             documentPublication.setDocumentId( daoUtil.getInt( 2 ) );
330             documentPublication.setDocumentOrder( daoUtil.getInt( 3 ) );
331             documentPublication.setStatus( nStatus );
332             documentPublication.setDatePublishing( daoUtil.getTimestamp( 4 ) );
333             listDocumentPublication.add( documentPublication );
334         }
335 
336         daoUtil.free( );
337 
338         return listDocumentPublication;
339     }
340 
341     /**
342      * Select the max order from a list of {@link DocumentPublication} specified by portlet id
343      * 
344      * @param nPortletId
345      *            the portlet identifer
346      * @return The max order of document
347      */
348     public int selectMaxDocumentOrder( int nPortletId )
349     {
350         int nOrder = 0;
351         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_MAX_ORDER );
352         daoUtil.setInt( 1, nPortletId );
353         daoUtil.executeQuery( );
354 
355         if ( daoUtil.next( ) )
356         {
357             nOrder = daoUtil.getInt( 1 );
358         }
359 
360         daoUtil.free( );
361 
362         return nOrder;
363     }
364 
365     /**
366      * Return a document identifier in a distinct order
367      *
368      * @param nDocumentOrder
369      *            The order number
370      * @param nPortletId
371      *            the portlet identifier
372      * @return The order of the Document
373      */
374     public int selectDocumentIdByOrder( int nDocumentOrder, int nPortletId )
375     {
376         // FIXME The document_order column is not a primary key, so this method have to return a collection
377         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_MODIFY_ORDER_BY_ID );
378         int nResult = nDocumentOrder;
379         daoUtil.setInt( 1, nResult );
380         daoUtil.setInt( 2, nPortletId );
381         daoUtil.executeQuery( );
382 
383         if ( !daoUtil.next( ) )
384         {
385             // If number order doesn't exist
386             nResult = 1;
387         }
388         else
389         {
390             nResult = daoUtil.getInt( 1 );
391         }
392 
393         daoUtil.free( );
394 
395         return nResult;
396     }
397 }