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          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
83          {
84              daoUtil.setInt( 1, p.getId( ) );
85              daoUtil.setString( 2, p.getDocumentTypeCode( ) );
86  
87              daoUtil.executeUpdate( );
88              insertCategory( portlet );
89          }
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             try ( 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         }
115     }
116 
117     /**
118      * Deletes records for a portlet identifier in the tables document_list_portlet, document_published, document_category_list_portlet
119      *
120      * @param nPortletId
121      *            the portlet identifier
122      */
123     public void delete( int nPortletId )
124     {
125         try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
126         {
127             daoUtil.setInt( 1, nPortletId );
128             daoUtil.executeUpdate( );
129         }
130 
131         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_PUBLISHED_DOCUMENT_PORTLET ) )
132         {
133             daoUtil.setInt( 1, nPortletId );
134             daoUtil.executeUpdate( );
135         }
136 
137         deleteCategories( nPortletId );
138         deleteAutoPublication( nPortletId );
139     }
140 
141     /**
142      * Delete categories for the specified portlet
143      * 
144      * @param nPortletId
145      *            The portlet identifier
146      */
147     private void deleteCategories( int nPortletId )
148     {
149         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_CATEGORY_PORTLET ) )
150         {
151             daoUtil.setInt( 1, nPortletId );
152             daoUtil.executeUpdate( );
153         }
154     }
155 
156     /**
157      * Delete auto publication records for the specified portlet
158      * 
159      * @param nPortletId
160      *            The portlet identifier
161      */
162     private void deleteAutoPublication( int nPortletId )
163     {
164         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_AUTO_PUBLICATION_PORTLET ) )
165         {
166             daoUtil.setInt( 1, nPortletId );
167             daoUtil.executeUpdate( );
168         }
169     }
170 
171     /**
172      * Loads the data of Document List Portlet whose identifier is specified in parameter
173      *
174      * @param nPortletId
175      *            The Portlet identifier
176      * @return theDocumentListPortlet object
177      */
178     public Portlet load( int nPortletId )
179     {
180         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
181         {
182             daoUtil.setInt( 1, nPortletId );
183             daoUtil.executeQuery( );
184 
185             DocumentListPortletbusiness/portlet/DocumentListPortlet.html#DocumentListPortlet">DocumentListPortlet portlet = new DocumentListPortlet( );
186 
187             if ( daoUtil.next( ) )
188             {
189                 portlet.setId( daoUtil.getInt( 1 ) );
190                 portlet.setDocumentTypeCode( daoUtil.getString( 2 ) );
191             }
192             portlet.setIdCategory( loadCategories( nPortletId ) );
193 
194             return portlet;
195         }
196     }
197 
198     /**
199      * Load a list of Id categories
200      * 
201      * @param nPortletId
202      * @return Array of categories
203      */
204     private int [ ] loadCategories( int nPortletId )
205     {
206         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CATEGORY_PORTLET ) )
207         {
208             daoUtil.setInt( 1, nPortletId );
209             daoUtil.executeQuery( );
210 
211             Collection<Integer> nListIdCategory = new ArrayList<>( );
212 
213             while ( daoUtil.next( ) )
214             {
215                 nListIdCategory.add( daoUtil.getInt( 1 ) );
216             }
217 
218             int[] nArrayIdCategory = new int[nListIdCategory.size( )];
219             int i = 0;
220 
221             for ( Integer nIdCategory : nListIdCategory )
222             {
223                 nArrayIdCategory[i++] = nIdCategory.intValue( );
224             }
225 
226             return nArrayIdCategory;
227         }
228     }
229 
230     /**
231      * Update the record in the table
232      *
233      * @param portlet
234      *            A portlet
235      */
236     public void store( Portlet portlet )
237     {
238         DocumentListPortlet/../../../../fr/paris/lutece/plugins/document/business/portlet/DocumentListPortlet.html#DocumentListPortlet">DocumentListPortlet p = (DocumentListPortlet) portlet;
239         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
240         {
241             daoUtil.setInt( 1, p.getId( ) );
242             daoUtil.setString( 2, p.getDocumentTypeCode( ) );
243             daoUtil.setInt( 3, p.getId( ) );
244 
245             daoUtil.executeUpdate( );
246         }
247         deleteCategories( p.getId( ) );
248         insertCategory( p );
249     }
250 
251     /**
252      * Load the list of documentTypes
253      * 
254      * @param nDocumentId
255      *            the document ID
256      * @param strCodeDocumentType
257      *            The code
258      * @param pOrder
259      *            order of the portlets
260      * @return The Collection of the ReferenceItem
261      */
262     public Collection<ReferenceItem> selectByDocumentIdAndDocumentType( int nDocumentId, String strCodeDocumentType, PortletOrder pOrder,
263             PortletFilter pFilter )
264     {
265         StringBuilder strSQl = new StringBuilder( );
266         strSQl.append( SQL_QUERY_SELECT_DOCUMENTS_BY_TYPE_AND_CATEGORY );
267 
268         String strFilter = ( pFilter != null ) ? pFilter.getSQLFilter( ) : null;
269 
270         if ( strFilter != null )
271         {
272             strSQl.append( SQL_QUERY_CASE_AND );
273             strSQl.append( strFilter );
274         }
275 
276         strSQl.append( pOrder.getSQLOrderBy( ) );
277         ReferenceList list = new ReferenceList( );
278         try ( DAOUtil daoUtil = new DAOUtil( strSQl.toString( ) ) )
279         {
280             daoUtil.setInt( 1, nDocumentId );
281             daoUtil.setString( 2, strCodeDocumentType );
282             daoUtil.setInt( 3, nDocumentId );
283 
284             if ( strFilter != null )
285             {
286                 if ( pFilter.getPortletFilterType( ).equals( PortletFilter.PAGE_NAME ) )
287                 {
288                     for ( int i = 0; i < pFilter.getPageName( ).length; i++ )
289                     {
290                         daoUtil.setString( i + 4, "%" + pFilter.getPageName( )[i] + "%" );
291                     }
292                 }
293                 else
294                     if ( pFilter.getPortletFilterType( ).equals( PortletFilter.PORTLET_NAME ) )
295                     {
296                         for ( int i = 0; i < pFilter.getPortletName( ).length; i++ )
297                         {
298                             daoUtil.setString( i + 4, "%" + pFilter.getPortletName( )[i] + "%" );
299                         }
300                     }
301                     else
302                         if ( pFilter.getPortletFilterType( ).equals( PortletFilter.PAGE_ID ) )
303                         {
304                             daoUtil.setInt( 4, pFilter.getIdPage( ) );
305                         }
306             }
307 
308             daoUtil.executeQuery( );
309 
310 
311             while ( daoUtil.next( ) )
312             {
313                 list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
314             }
315         }
316         return list;
317     }
318 
319     /**
320      * Tests if is a portlet is portlet type alias
321      *
322      * @param nPortletId
323      *            The identifier of the document
324      * @return true if the portlet is alias, false otherwise
325      */
326     public boolean checkIsAliasPortlet( int nPortletId )
327     {
328         boolean bIsAlias = false;
329         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_IS_ALIAS ) )
330         {
331             daoUtil.setInt( 1, nPortletId );
332             daoUtil.executeQuery( );
333 
334             if ( daoUtil.next( ) )
335             {
336                 bIsAlias = true;
337             }
338         }
339         return bIsAlias;
340     }
341 }