StyleSheetDAO.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.stylesheet;

  35. import fr.paris.lutece.portal.service.util.AppException;
  36. import fr.paris.lutece.util.sql.DAOUtil;

  37. import java.sql.Statement;
  38. import java.util.ArrayList;
  39. import java.util.Collection;

  40. /**
  41.  * This class provides Data Access methods for StyleSheet objects
  42.  */
  43. public final class StyleSheetDAO implements IStyleSheetDAO
  44. {
  45.     // Constants
  46.     private static final String SQL_QUERY_SELECT = " SELECT a.description , a.file_name , a.source , b.id_style , b.id_mode " + " FROM core_stylesheet a "
  47.             + " LEFT JOIN core_style_mode_stylesheet b ON a.id_stylesheet = b.id_stylesheet " + " WHERE a.id_stylesheet = ? ";
  48.     private static final String SQL_QUERY_INSERT = " INSERT INTO core_stylesheet ( description , file_name, source ) " + " VALUES ( ? ,?, ? )";
  49.     private static final String SQL_QUERY_DELETE = " DELETE FROM core_stylesheet WHERE id_stylesheet = ? ";
  50.     private static final String SQL_QUERY_UPDATE = " UPDATE core_stylesheet SET id_stylesheet = ?, description = ?, file_name = ?, source = ? WHERE id_stylesheet = ?  ";
  51.     private static final String SQL_QUERY_SELECT_MODEID = " SELECT a.id_mode FROM core_mode a , core_style_mode_stylesheet b  "
  52.             + " WHERE a.id_mode = b.id_mode AND b.id_stylesheet = ?";
  53.     private static final String SQL_QUERY_COUNT_STYLESHEET = " SELECT count(*) FROM core_style_mode_stylesheet WHERE id_style = ? AND id_mode = ? ";
  54.     private static final String SQL_QUERY_INSERT_STYLEMODESTYLESHEET = " INSERT INTO core_style_mode_stylesheet ( id_style , id_mode , id_stylesheet ) "
  55.             + " VALUES ( ?, ? ,? )";
  56.     private static final String SQL_QUERY_UPDATE_STYLEMODESTYLESHEET = " UPDATE core_style_mode_stylesheet SET id_style = ? , id_mode = ?  "
  57.             + " WHERE id_stylesheet = ? ";
  58.     private static final String SQL_QUERY_DELETEE_STYLEMODESTYLESHEET = " DELETE FROM core_style_mode_stylesheet WHERE id_stylesheet = ? ";

  59.     // /////////////////////////////////////////////////////////////////////////////////////
  60.     // Access methods to data

  61.     /**
  62.      * Insert a new record in the table.
  63.      *
  64.      * @param stylesheet
  65.      *            The StyleSheet object
  66.      */
  67.     public void insert( StyleSheet stylesheet )
  68.     {
  69.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
  70.         {

  71.             int nIndex = 1;
  72.             daoUtil.setString( nIndex++, stylesheet.getDescription( ) );
  73.             daoUtil.setString( nIndex++, stylesheet.getFile( ) );
  74.             daoUtil.setBytes( nIndex, stylesheet.getSource( ) );

  75.             daoUtil.executeUpdate( );

  76.             if ( daoUtil.nextGeneratedKey( ) )
  77.             {
  78.                 stylesheet.setId( daoUtil.getGeneratedKeyInt( 1 ) );
  79.             }

  80.             // Update of the table style_mode_stylesheet in the database
  81.             insertStyleModeStyleSheet( stylesheet );
  82.         }
  83.     }

  84.     /**
  85.      * Load the data of Stylesheet from the table
  86.      *
  87.      * @param nIdStylesheet
  88.      *            the identifier of the Stylesheet to load
  89.      * @return stylesheet
  90.      */
  91.     public StyleSheet load( int nIdStylesheet )
  92.     {
  93.         StyleSheet stylesheet = null;
  94.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  95.         {
  96.             daoUtil.setInt( 1, nIdStylesheet );
  97.             daoUtil.executeQuery( );

  98.             if ( daoUtil.next( ) )
  99.             {
  100.                 stylesheet = new StyleSheet( );
  101.                 stylesheet.setId( nIdStylesheet );
  102.                 stylesheet.setDescription( daoUtil.getString( 1 ) );
  103.                 stylesheet.setFile( daoUtil.getString( 2 ) );
  104.                 stylesheet.setSource( daoUtil.getBytes( 3 ) );
  105.                 stylesheet.setStyleId( daoUtil.getInt( 4 ) );
  106.                 stylesheet.setModeId( daoUtil.getInt( 5 ) );
  107.             }

  108.         }

  109.         return stylesheet;
  110.     }

  111.     /**
  112.      * Delete the StyleSheet from the database whose identifier is specified in parameter
  113.      *
  114.      * @param nIdStylesheet
  115.      *            the identifier of the StyleSheet to delete
  116.      */
  117.     public void delete( int nIdStylesheet )
  118.     {
  119.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  120.         {
  121.             daoUtil.setInt( 1, nIdStylesheet );
  122.             daoUtil.executeUpdate( );
  123.         }

  124.         // delete also into style_mode_stylesheet
  125.         deleteStyleModeStyleSheet( nIdStylesheet );
  126.     }

  127.     /**
  128.      * Load the list of stylesheet
  129.      *
  130.      * @param nModeId
  131.      *            The Mode identifier
  132.      * @return the list of the StyleSheet in form of a collection of StyleSheet objects
  133.      */
  134.     public Collection<StyleSheet> selectStyleSheetList( int nModeId )
  135.     {
  136.         Collection<StyleSheet> stylesheetList = new ArrayList<>( );

  137.         String strSelect = " SELECT a.id_stylesheet , a.description , a.file_name ";
  138.         String strFrom = " FROM core_stylesheet a ";

  139.         if ( nModeId != -1 )
  140.         {
  141.             strFrom = " FROM  core_stylesheet a , core_style_mode_stylesheet b " + " WHERE a.id_stylesheet = b.id_stylesheet " + " AND b.id_mode = ? ";
  142.         }

  143.         strFrom += " ORDER BY a.description ";

  144.         String strSQL = strSelect + strFrom;

  145.         try ( DAOUtil daoUtil = new DAOUtil( strSQL ) )
  146.         {

  147.             if ( nModeId != -1 )
  148.             {
  149.                 daoUtil.setInt( 1, nModeId );
  150.             }

  151.             daoUtil.executeQuery( );

  152.             while ( daoUtil.next( ) )
  153.             {
  154.                 StyleSheet stylesheet = new StyleSheet( );

  155.                 stylesheet.setId( daoUtil.getInt( 1 ) );
  156.                 stylesheet.setDescription( daoUtil.getString( 2 ) );
  157.                 stylesheet.setFile( daoUtil.getString( 3 ) );
  158.                 stylesheetList.add( stylesheet );
  159.             }

  160.         }

  161.         return stylesheetList;
  162.     }

  163.     /**
  164.      * Update the record in the table
  165.      *
  166.      * @param stylesheet
  167.      *            The stylesheet
  168.      */
  169.     public void store( StyleSheet stylesheet )
  170.     {
  171.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  172.         {
  173.             daoUtil.setInt( 1, stylesheet.getId( ) );
  174.             daoUtil.setString( 2, stylesheet.getDescription( ) );
  175.             daoUtil.setString( 3, stylesheet.getFile( ) );
  176.             daoUtil.setBytes( 4, stylesheet.getSource( ) );
  177.             daoUtil.setInt( 5, stylesheet.getId( ) );

  178.             daoUtil.executeUpdate( );
  179.         }

  180.         // update the table style_mode_stylesheet
  181.         updateStyleModeStyleSheet( stylesheet );
  182.     }

  183.     /**
  184.      * Insert a new record in the table style_mode_stylesheet
  185.      *
  186.      * @param stylesheet
  187.      *            the instance of StyleSheet to insert in the table
  188.      */
  189.     private void insertStyleModeStyleSheet( StyleSheet stylesheet )
  190.     {
  191.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_STYLEMODESTYLESHEET ) )
  192.         {

  193.             daoUtil.setInt( 1, stylesheet.getStyleId( ) );
  194.             daoUtil.setInt( 2, stylesheet.getModeId( ) );
  195.             daoUtil.setInt( 3, stylesheet.getId( ) );

  196.             daoUtil.executeUpdate( );
  197.         }
  198.     }

  199.     /**
  200.      * Updates the table style_mode_stylesheet with the data of the StyleShhet instance specified in parameter
  201.      *
  202.      * @param stylesheet
  203.      *            the instance of the stylesheet to update
  204.      */
  205.     private void updateStyleModeStyleSheet( StyleSheet stylesheet )
  206.     {
  207.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_STYLEMODESTYLESHEET ) )
  208.         {

  209.             daoUtil.setInt( 1, stylesheet.getStyleId( ) );
  210.             daoUtil.setInt( 2, stylesheet.getModeId( ) );
  211.             daoUtil.setInt( 3, stylesheet.getId( ) );

  212.             daoUtil.executeUpdate( );
  213.         }
  214.     }

  215.     /**
  216.      * Deletes the data in the table style_mode_stylesheet
  217.      *
  218.      * @param nStyleSheetId
  219.      *            the identifier of the stylesheet
  220.      */
  221.     private void deleteStyleModeStyleSheet( int nStyleSheetId )
  222.     {
  223.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETEE_STYLEMODESTYLESHEET ) )
  224.         {
  225.             daoUtil.setInt( 1, nStyleSheetId );
  226.             daoUtil.executeUpdate( );
  227.         }
  228.     }

  229.     /**
  230.      * Returns the number of stylesheets associated to the style and the mode specified in parameter
  231.      *
  232.      * @param nStyleId
  233.      *            the style id
  234.      * @param nModeId
  235.      *            the mode id
  236.      * @return the number of stylesheet associated
  237.      */
  238.     public int selectStyleSheetNbPerStyleMode( int nStyleId, int nModeId )
  239.     {
  240.         int nCount;
  241.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_STYLESHEET ) )
  242.         {

  243.             daoUtil.setInt( 1, nStyleId );
  244.             daoUtil.setInt( 2, nModeId );

  245.             daoUtil.executeQuery( );

  246.             if ( !daoUtil.next( ) )
  247.             {
  248.                 daoUtil.free( );
  249.                 throw new AppException( DAOUtil.MSG_EXCEPTION_SELECT_ERROR + nModeId + " StyleId " + nStyleId );
  250.             }

  251.             nCount = ( daoUtil.getInt( 1 ) );

  252.         }

  253.         return nCount;
  254.     }

  255.     /**
  256.      * Returns the identifier of the mode of the stylesheet whose identifier is specified in parameter
  257.      *
  258.      * @param nIdStylesheet
  259.      *            the identifier of the stylesheet
  260.      * @return the identifier of the mode
  261.      */
  262.     public int selectModeId( int nIdStylesheet )
  263.     {
  264.         int nModeId;
  265.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MODEID ) )
  266.         {

  267.             daoUtil.setInt( 1, nIdStylesheet );
  268.             daoUtil.executeQuery( );

  269.             if ( !daoUtil.next( ) )
  270.             {
  271.                 daoUtil.free( );
  272.                 throw new AppException( DAOUtil.MSG_EXCEPTION_SELECT_ERROR + nIdStylesheet );
  273.             }

  274.             nModeId = ( daoUtil.getInt( 1 ) );

  275.         }

  276.         return nModeId;
  277.     }
  278. }