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