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.portlet;
35  
36  import fr.paris.lutece.portal.business.portlet.Portlet;
37  import fr.paris.lutece.util.ReferenceItem;
38  import fr.paris.lutece.util.ReferenceList;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  import java.util.ArrayList;
42  import java.util.Collection;
43  
44  /**
45   * This class provides Data Access methods for ArticlesListPortlet objects
46   */
47  public final class DocumentListPortletDAO implements IDocumentListPortletDAO
48  {
49      private static final String SQL_QUERY_INSERT = "INSERT INTO document_list_portlet ( id_portlet , code_document_type ) VALUES ( ? , ? )";
50      private static final String SQL_QUERY_SELECT = "SELECT id_portlet , code_document_type FROM document_list_portlet WHERE id_portlet = ? ";
51      private static final String SQL_QUERY_UPDATE = "UPDATE document_list_portlet SET id_portlet = ?, code_document_type = ? WHERE id_portlet = ? ";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM document_list_portlet WHERE id_portlet= ? ";
53      private static final String SQL_QUERY_DELETE_PUBLISHED_DOCUMENT_PORTLET = " DELETE FROM document_published WHERE id_portlet = ? ";
54      private static final String SQL_QUERY_SELECT_DOCUMENTS_BY_TYPE_AND_CATEGORY = "SELECT DISTINCT b.id_portlet , a.name, a.date_update "
55              + "FROM document_list_portlet b " + "LEFT JOIN document_published c ON b.id_portlet = c.id_portlet AND c.id_document= ? "
56              + "INNER JOIN core_portlet a ON b.id_portlet = a.id_portlet " + "LEFT OUTER JOIN document_category_list_portlet d ON b.id_portlet = d.id_portlet "
57              + "INNER JOIN core_page f ON a.id_page = f.id_page "
58              + "WHERE c.id_portlet IS NULL AND b.code_document_type = ? AND (d.id_category IN (SELECT e.id_category "
59              + "FROM document_category_link e WHERE e.id_document = ?) OR d.id_category IS NULL) ";
60      private static final String SQL_QUERY_CHECK_IS_ALIAS = "SELECT id_alias FROM core_portlet_alias WHERE id_alias = ?";
61  
62      // Category
63      private static final String SQL_QUERY_INSERT_CATEGORY_PORTLET = "INSERT INTO document_category_list_portlet ( id_portlet , id_category ) VALUES ( ? , ? )";
64      private static final String SQL_QUERY_DELETE_CATEGORY_PORTLET = " DELETE FROM document_category_list_portlet WHERE id_portlet = ? ";
65      private static final String SQL_QUERY_DELETE_AUTO_PUBLICATION_PORTLET = " DELETE FROM document_auto_publication WHERE id_portlet = ? ";
66      private static final String SQL_QUERY_SELECT_CATEGORY_PORTLET = "SELECT id_category FROM document_category_list_portlet WHERE id_portlet = ? ";
67      private static final String SQL_QUERY_CASE_AND = " AND ";
68  
69      ///////////////////////////////////////////////////////////////////////////////////////
70      // Access methods to data
71  
72      /**
73       * Insert a new record in the table portlet_articles_list
74       *
75       * @param portlet
76       *            the instance of the Portlet object to insert
77       */
78      public void insert( Portlet portlet )
79      {
80          DocumentListPortlet/../../../../fr/paris/lutece/plugins/document/business/portlet/DocumentListPortlet.html#DocumentListPortlet">DocumentListPortlet p = (DocumentListPortlet) portlet;
81  
82          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
83          daoUtil.setInt( 1, p.getId( ) );
84          daoUtil.setString( 2, p.getDocumentTypeCode( ) );
85  
86          daoUtil.executeUpdate( );
87          daoUtil.free( );
88  
89          insertCategory( portlet );
90      }
91  
92      /**
93       * Insert a list of category for a specified portlet
94       * 
95       * @param portlet
96       *            the DocumentListPortlet to insert
97       */
98      private void insertCategory( Portlet portlet )
99      {
100         DocumentListPortlet/../../../../fr/paris/lutece/plugins/document/business/portlet/DocumentListPortlet.html#DocumentListPortlet">DocumentListPortlet p = (DocumentListPortlet) portlet;
101 
102         if ( p.getIdCategory( ) != null )
103         {
104             DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_CATEGORY_PORTLET );
105 
106             for ( int nIdCategory : p.getIdCategory( ) )
107             {
108                 daoUtil.setInt( 1, p.getId( ) );
109                 daoUtil.setInt( 2, nIdCategory );
110 
111                 daoUtil.executeUpdate( );
112             }
113 
114             daoUtil.free( );
115         }
116     }
117 
118     /**
119      * Deletes records for a portlet identifier in the tables document_list_portlet, document_published, document_category_list_portlet
120      *
121      * @param nPortletId
122      *            the portlet identifier
123      */
124     public void delete( int nPortletId )
125     {
126         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
127         daoUtil.setInt( 1, nPortletId );
128         daoUtil.executeUpdate( );
129         daoUtil.free( );
130 
131         daoUtil = new DAOUtil( SQL_QUERY_DELETE_PUBLISHED_DOCUMENT_PORTLET );
132         daoUtil.setInt( 1, nPortletId );
133         daoUtil.executeUpdate( );
134         daoUtil.free( );
135 
136         deleteCategories( nPortletId );
137         deleteAutoPublication( nPortletId );
138     }
139 
140     /**
141      * Delete categories for the specified portlet
142      * 
143      * @param nPortletId
144      *            The portlet identifier
145      */
146     private void deleteCategories( int nPortletId )
147     {
148         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_CATEGORY_PORTLET );
149         daoUtil.setInt( 1, nPortletId );
150         daoUtil.executeUpdate( );
151         daoUtil.free( );
152     }
153 
154     /**
155      * Delete auto publication records for the specified portlet
156      * 
157      * @param nPortletId
158      *            The portlet identifier
159      */
160     private void deleteAutoPublication( int nPortletId )
161     {
162         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_AUTO_PUBLICATION_PORTLET );
163         daoUtil.setInt( 1, nPortletId );
164         daoUtil.executeUpdate( );
165         daoUtil.free( );
166     }
167 
168     /**
169      * Loads the data of Document List Portlet whose identifier is specified in parameter
170      *
171      * @param nPortletId
172      *            The Portlet identifier
173      * @return theDocumentListPortlet object
174      */
175     public Portlet load( int nPortletId )
176     {
177         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
178         daoUtil.setInt( 1, nPortletId );
179         daoUtil.executeQuery( );
180 
181         DocumentListPortletbusiness/portlet/DocumentListPortlet.html#DocumentListPortlet">DocumentListPortlet portlet = new DocumentListPortlet( );
182 
183         if ( daoUtil.next( ) )
184         {
185             portlet.setId( daoUtil.getInt( 1 ) );
186             portlet.setDocumentTypeCode( daoUtil.getString( 2 ) );
187         }
188 
189         daoUtil.free( );
190 
191         portlet.setIdCategory( loadCategories( nPortletId ) );
192 
193         return portlet;
194     }
195 
196     /**
197      * Load a list of Id categories
198      * 
199      * @param nPortletId
200      * @return Array of categories
201      */
202     private int [ ] loadCategories( int nPortletId )
203     {
204         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CATEGORY_PORTLET );
205         daoUtil.setInt( 1, nPortletId );
206         daoUtil.executeQuery( );
207 
208         Collection<Integer> nListIdCategory = new ArrayList<Integer>( );
209 
210         while ( daoUtil.next( ) )
211         {
212             nListIdCategory.add( daoUtil.getInt( 1 ) );
213         }
214 
215         daoUtil.free( );
216 
217         int [ ] nArrayIdCategory = new int [ nListIdCategory.size( )];
218         int i = 0;
219 
220         for ( Integer nIdCategory : nListIdCategory )
221         {
222             nArrayIdCategory [i++] = nIdCategory.intValue( );
223         }
224 
225         return nArrayIdCategory;
226     }
227 
228     /**
229      * Update the record in the table
230      *
231      * @param portlet
232      *            A portlet
233      */
234     public void store( Portlet portlet )
235     {
236         DocumentListPortlet/../../../../fr/paris/lutece/plugins/document/business/portlet/DocumentListPortlet.html#DocumentListPortlet">DocumentListPortlet p = (DocumentListPortlet) portlet;
237         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
238         daoUtil.setInt( 1, p.getId( ) );
239         daoUtil.setString( 2, p.getDocumentTypeCode( ) );
240         daoUtil.setInt( 3, p.getId( ) );
241 
242         daoUtil.executeUpdate( );
243 
244         daoUtil.free( );
245 
246         deleteCategories( p.getId( ) );
247         insertCategory( p );
248     }
249 
250     /**
251      * Load the list of documentTypes
252      * 
253      * @param nDocumentId
254      *            the document ID
255      * @param strCodeDocumentType
256      *            The code
257      * @param pOrder
258      *            order of the portlets
259      * @return The Collection of the ReferenceItem
260      */
261     public Collection<ReferenceItem> selectByDocumentIdAndDocumentType( int nDocumentId, String strCodeDocumentType, PortletOrder pOrder,
262             PortletFilter pFilter )
263     {
264         StringBuilder strSQl = new StringBuilder( );
265         strSQl.append( SQL_QUERY_SELECT_DOCUMENTS_BY_TYPE_AND_CATEGORY );
266 
267         String strFilter = ( pFilter != null ) ? pFilter.getSQLFilter( ) : null;
268 
269         if ( strFilter != null )
270         {
271             strSQl.append( SQL_QUERY_CASE_AND );
272             strSQl.append( strFilter );
273         }
274 
275         strSQl.append( pOrder.getSQLOrderBy( ) );
276 
277         DAOUtil daoUtil = new DAOUtil( strSQl.toString( ) );
278 
279         daoUtil.setInt( 1, nDocumentId );
280         daoUtil.setString( 2, strCodeDocumentType );
281         daoUtil.setInt( 3, nDocumentId );
282 
283         if ( strFilter != null )
284         {
285             if ( pFilter.getPortletFilterType( ).equals( PortletFilter.PAGE_NAME ) )
286             {
287                 for ( int i = 0; i < pFilter.getPageName( ).length; i++ )
288                 {
289                     daoUtil.setString( i + 4, "%" + pFilter.getPageName( ) [i] + "%" );
290                 }
291             }
292             else
293                 if ( pFilter.getPortletFilterType( ).equals( PortletFilter.PORTLET_NAME ) )
294                 {
295                     for ( int i = 0; i < pFilter.getPortletName( ).length; i++ )
296                     {
297                         daoUtil.setString( i + 4, "%" + pFilter.getPortletName( ) [i] + "%" );
298                     }
299                 }
300                 else
301                     if ( pFilter.getPortletFilterType( ).equals( PortletFilter.PAGE_ID ) )
302                     {
303                         daoUtil.setInt( 4, pFilter.getIdPage( ) );
304                     }
305         }
306 
307         daoUtil.executeQuery( );
308 
309         ReferenceList list = new ReferenceList( );
310 
311         while ( daoUtil.next( ) )
312         {
313             list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
314         }
315 
316         daoUtil.free( );
317 
318         return list;
319     }
320 
321     /**
322      * Tests if is a portlet is portlet type alias
323      *
324      * @param nPortletId
325      *            The identifier of the document
326      * @return true if the portlet is alias, false otherwise
327      */
328     public boolean checkIsAliasPortlet( int nPortletId )
329     {
330         boolean bIsAlias = false;
331         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_IS_ALIAS );
332 
333         daoUtil.setInt( 1, nPortletId );
334         daoUtil.executeQuery( );
335 
336         if ( daoUtil.next( ) )
337         {
338             bIsAlias = true;
339         }
340 
341         daoUtil.free( );
342 
343         return bIsAlias;
344     }
345 }