PageTemplateDAO.java

  1. /*
  2.  * Copyright (c) 2002-2022, 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.portal.business.style;

  35. import fr.paris.lutece.util.sql.DAOUtil;

  36. import java.sql.Statement;
  37. import java.util.ArrayList;
  38. import java.util.List;

  39. /**
  40.  * This class provides Data Access methods for PageTemplate objects
  41.  */
  42. public final class PageTemplateDAO implements IPageTemplateDAO
  43. {
  44.     // Constants
  45.     private static final String SQL_QUERY_SELECT = " SELECT id_template, description, file_name, picture FROM core_page_template WHERE id_template = ?";
  46.     private static final String SQL_QUERY_INSERT = " INSERT INTO core_page_template ( description, file_name, picture ) VALUES ( ?, ?, ? )";
  47.     private static final String SQL_QUERY_DELETE = " DELETE FROM core_page_template WHERE id_template = ?";
  48.     private static final String SQL_QUERY_UPDATE = " UPDATE core_page_template SET id_template = ?, description = ?, file_name = ?, picture = ? "
  49.             + " WHERE id_template = ?";
  50.     private static final String SQL_QUERY_SELECTALL = " SELECT id_template , description, file_name, picture FROM core_page_template ORDER BY id_template ";
  51.     private static final String SQL_CHECK_PAGE_TEMPLATE_IS_USED = " SELECT id_template FROM core_page WHERE id_template = ? ";

  52.     // /////////////////////////////////////////////////////////////////////////////////////
  53.     // Access methods to data

  54.     /**
  55.      * Insert a new record in the table.
  56.      *
  57.      * @param pageTemplate
  58.      *            The Instance of the object PageTemplate
  59.      */
  60.     public void insert( PageTemplate pageTemplate )
  61.     {
  62.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
  63.         {
  64.             int nIndex = 1;
  65.             daoUtil.setString( nIndex++, pageTemplate.getDescription( ) );
  66.             daoUtil.setString( nIndex++, pageTemplate.getFile( ) );
  67.             daoUtil.setString( nIndex, pageTemplate.getPicture( ) );

  68.             daoUtil.executeUpdate( );

  69.             if ( daoUtil.nextGeneratedKey( ) )
  70.             {
  71.                 pageTemplate.setId( daoUtil.getGeneratedKeyInt( 1 ) );
  72.             }
  73.         }
  74.     }

  75.     /**
  76.      * load the data of PageTemplate from the table
  77.      *
  78.      * @param nPageTemplateId
  79.      *            The indentifier of the object PageTemplate
  80.      * @return The Instance of the object PageTemplate
  81.      */
  82.     public PageTemplate load( int nPageTemplateId )
  83.     {
  84.         PageTemplate pageTemplate = null;
  85.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  86.         {
  87.             daoUtil.setInt( 1, nPageTemplateId );

  88.             daoUtil.executeQuery( );

  89.             if ( daoUtil.next( ) )
  90.             {
  91.                 pageTemplate = new PageTemplate( );
  92.                 pageTemplate.setId( daoUtil.getInt( 1 ) );
  93.                 pageTemplate.setDescription( daoUtil.getString( 2 ) );
  94.                 pageTemplate.setFile( daoUtil.getString( 3 ) );
  95.                 pageTemplate.setPicture( daoUtil.getString( 4 ) );
  96.             }

  97.         }

  98.         return pageTemplate;
  99.     }

  100.     /**
  101.      * Delete a record from the table
  102.      *
  103.      * @param nPageTemplateId
  104.      *            The indentifier of the object PageTemplate
  105.      */
  106.     public void delete( int nPageTemplateId )
  107.     {
  108.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  109.         {
  110.             daoUtil.setInt( 1, nPageTemplateId );
  111.             daoUtil.executeUpdate( );
  112.         }
  113.     }

  114.     /**
  115.      * Update the record in the table
  116.      *
  117.      * @param pageTemplate
  118.      *            The instance of the PageTemplate to update
  119.      */
  120.     public void store( PageTemplate pageTemplate )
  121.     {
  122.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  123.         {

  124.             daoUtil.setInt( 1, pageTemplate.getId( ) );
  125.             daoUtil.setString( 2, pageTemplate.getDescription( ) );
  126.             daoUtil.setString( 3, pageTemplate.getFile( ) );
  127.             daoUtil.setString( 4, pageTemplate.getPicture( ) );
  128.             daoUtil.setInt( 5, pageTemplate.getId( ) );

  129.             daoUtil.executeUpdate( );
  130.         }
  131.     }

  132.     /**
  133.      * Returns a list of all the page templates
  134.      *
  135.      * @return A list of PageTemplates objects
  136.      */
  137.     public List<PageTemplate> selectPageTemplatesList( )
  138.     {
  139.         List<PageTemplate> listPageTemplates = new ArrayList<>( );
  140.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
  141.         {
  142.             daoUtil.executeQuery( );

  143.             while ( daoUtil.next( ) )
  144.             {
  145.                 PageTemplate pageTemplate = new PageTemplate( );

  146.                 pageTemplate.setId( daoUtil.getInt( 1 ) );
  147.                 pageTemplate.setDescription( daoUtil.getString( 2 ) );
  148.                 pageTemplate.setFile( daoUtil.getString( 3 ) );
  149.                 pageTemplate.setPicture( daoUtil.getString( 4 ) );
  150.                 listPageTemplates.add( pageTemplate );
  151.             }

  152.         }

  153.         return listPageTemplates;
  154.     }

  155.     /**
  156.      * Checks if a page template has been used by a page
  157.      *
  158.      * @param nPageTemplateId
  159.      *            The identifier of the page template
  160.      * @return true if a page template is used by a page, false otherwise
  161.      */
  162.     public boolean checkPageTemplateIsUsed( int nPageTemplateId )
  163.     {
  164.         boolean check = false;
  165.         try ( DAOUtil daoUtil = new DAOUtil( SQL_CHECK_PAGE_TEMPLATE_IS_USED ) )
  166.         {

  167.             daoUtil.setInt( 1, nPageTemplateId );
  168.             daoUtil.executeQuery( );

  169.             if ( !daoUtil.next( ) )
  170.             {
  171.                 check = true;
  172.             }

  173.         }

  174.         return check;
  175.     }
  176. }