StyleDAO.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.portal.business.stylesheet.StyleSheet;
  36. import fr.paris.lutece.util.ReferenceList;
  37. import fr.paris.lutece.util.sql.DAOUtil;

  38. import java.util.ArrayList;
  39. import java.util.Collection;

  40. /**
  41.  * This class provides Data Access methods for style objects
  42.  */
  43. public final class StyleDAO implements IStyleDAO
  44. {
  45.     // Constants
  46.     private static final String SQL_QUERY_SELECT = " SELECT DISTINCT a.id_portlet_type , a.id_portal_component, a.description_style , "
  47.             + " b.name, c.name , id_style FROM core_style a " + " INNER JOIN core_portal_component c ON a.id_portal_component = c.id_portal_component "
  48.             + " LEFT JOIN core_portlet_type b ON a.id_portlet_type = b.id_portlet_type WHERE a.id_style = ? ";
  49.     private static final String SQL_QUERY_INSERT = " INSERT INTO core_style ( id_style , id_portlet_type , id_portal_component, description_style ) "
  50.             + " VALUES ( ?, ?, ?, ? )";
  51.     private static final String SQL_QUERY_DELETE = " DELETE FROM core_style WHERE id_style = ? ";
  52.     private static final String SQL_QUERY_UPDATE = " UPDATE core_style SET  id_portlet_type = ?, id_portal_component = ?, description_style = ? "
  53.             + " WHERE id_style = ?";
  54.     private static final String SQL_QUERY_SELECTALL = " SELECT a.id_style , a.id_portlet_type , a.id_portal_component, a.description_style , "
  55.             + " b.name, c.name FROM core_style a " + " INNER JOIN core_portal_component c ON a.id_portal_component = c.id_portal_component "
  56.             + " LEFT JOIN core_portlet_type b ON a.id_portlet_type = b.id_portlet_type " + " ORDER BY a.id_portal_component, a.id_style ";
  57.     private static final String SQL_QUERY_SELECT_STYLESHEET = " SELECT a.id_stylesheet, a.description, a.file_name "
  58.             + " FROM  core_stylesheet a , core_style_mode_stylesheet b" + " WHERE b.id_stylesheet = a.id_stylesheet " + " AND b.id_style = ? ";
  59.     private static final String SQL_CHECK_STYLE_PORTLETCOMPONENT = " SELECT id_style FROM core_style WHERE id_portal_component = ? ";
  60.     private static final String SQL_QUERY_SELECT_PORTLETCOMPONENT = " SELECT id_portal_component , name FROM core_portal_component ORDER BY name ";

  61.     // /////////////////////////////////////////////////////////////////////////////////////
  62.     // Access methods to data

  63.     /**
  64.      * Insert a new record in the table.
  65.      *
  66.      * @param style
  67.      *            The Instance of the object Style
  68.      */
  69.     public void insert( Style style )
  70.     {
  71.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
  72.         {

  73.             daoUtil.setInt( 1, style.getId( ) );
  74.             daoUtil.setString( 2, style.getPortletTypeId( ) );
  75.             daoUtil.setInt( 3, style.getPortalComponentId( ) );
  76.             daoUtil.setString( 4, style.getDescription( ) );

  77.             daoUtil.executeUpdate( );
  78.         }
  79.     }

  80.     /**
  81.      * load the data of the Style whose identifier is specified in parameter from the table
  82.      *
  83.      * @param nStyleId
  84.      *            The identifier of the Style
  85.      * @return an instance of the Style which has been created
  86.      */
  87.     public Style load( int nStyleId )
  88.     {
  89.         Style style = null;
  90.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  91.         {
  92.             daoUtil.setInt( 1, nStyleId );

  93.             daoUtil.executeQuery( );

  94.             if ( daoUtil.next( ) )
  95.             {
  96.                 style = new Style( );
  97.                 style.setId( nStyleId );
  98.                 style.setPortletTypeId( daoUtil.getString( 1 ) );
  99.                 style.setPortalComponentId( daoUtil.getInt( 2 ) );
  100.                 style.setDescription( daoUtil.getString( 3 ) );
  101.                 style.setPortletTypeName( daoUtil.getString( 4 ) );
  102.                 style.setPortalComponentName( daoUtil.getString( 5 ) );
  103.             }

  104.         }

  105.         return style;
  106.     }

  107.     /**
  108.      * Delete a record from the table
  109.      *
  110.      * @param nStyleId
  111.      *            the identifier of the style to delete
  112.      */
  113.     public void delete( int nStyleId )
  114.     {
  115.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  116.         {
  117.             daoUtil.setInt( 1, nStyleId );
  118.             daoUtil.executeUpdate( );
  119.         }
  120.     }

  121.     /**
  122.      * Update the record in the table
  123.      *
  124.      * @param style
  125.      *            The instance of the Style to update
  126.      */
  127.     public void store( Style style )
  128.     {
  129.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  130.         {

  131.             daoUtil.setString( 1, style.getPortletTypeId( ) );
  132.             daoUtil.setInt( 2, style.getPortalComponentId( ) );
  133.             daoUtil.setString( 3, style.getDescription( ) );
  134.             daoUtil.setInt( 4, style.getId( ) );

  135.             daoUtil.executeUpdate( );
  136.         }
  137.     }

  138.     /**
  139.      * Load the list of styles stored in the database
  140.      *
  141.      * @return The styles list in form of a Collection object
  142.      */
  143.     public Collection<Style> selectStylesList( )
  144.     {
  145.         Collection<Style> styleList = new ArrayList<>( );
  146.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
  147.         {
  148.             daoUtil.executeQuery( );

  149.             while ( daoUtil.next( ) )
  150.             {
  151.                 Style style = new Style( );

  152.                 style.setId( daoUtil.getInt( 1 ) );
  153.                 style.setPortletTypeId( daoUtil.getString( 2 ) );
  154.                 style.setPortalComponentId( daoUtil.getInt( 3 ) );
  155.                 style.setDescription( daoUtil.getString( 4 ) );
  156.                 style.setPortletTypeName( daoUtil.getString( 5 ) );
  157.                 style.setPortalComponentName( daoUtil.getString( 6 ) );

  158.                 styleList.add( style );
  159.             }

  160.         }

  161.         return styleList;
  162.     }

  163.     /**
  164.      * Returns the list of the portal component in form of a ReferenceList
  165.      *
  166.      * @return the list of the portal component
  167.      */
  168.     public ReferenceList selectPortalComponentList( )
  169.     {
  170.         ReferenceList portletComponentList = new ReferenceList( );
  171.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PORTLETCOMPONENT ) )
  172.         {
  173.             daoUtil.executeQuery( );

  174.             while ( daoUtil.next( ) )
  175.             {
  176.                 portletComponentList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
  177.             }

  178.         }

  179.         return portletComponentList;
  180.     }

  181.     /**
  182.      * load the data of the StyleSheet which re associated to the given style
  183.      *
  184.      * @param nStyleId
  185.      *            The identifier of the Style
  186.      * @return an instance of the Style which has been created
  187.      */
  188.     public Collection<StyleSheet> selectStyleSheetList( int nStyleId )
  189.     {
  190.         Collection<StyleSheet> stylesheetList = new ArrayList<>( );
  191.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_STYLESHEET ) )
  192.         {

  193.             daoUtil.setInt( 1, nStyleId );
  194.             daoUtil.executeQuery( );

  195.             while ( daoUtil.next( ) )
  196.             {
  197.                 StyleSheet stylesheet = new StyleSheet( );

  198.                 stylesheet.setId( daoUtil.getInt( 1 ) );
  199.                 stylesheet.setDescription( daoUtil.getString( 2 ) );
  200.                 stylesheet.setFile( daoUtil.getString( 3 ) );

  201.                 stylesheetList.add( stylesheet );
  202.             }

  203.         }

  204.         return stylesheetList;
  205.     }

  206.     /**
  207.      * Checks if a style has been created in the database with the given portal componenet
  208.      *
  209.      * @param nPortalComponentId
  210.      *            The identifier of the portal component
  211.      * @return true if a style has been created for this portal component, false otherwise
  212.      */
  213.     public boolean checkStylePortalComponent( int nPortalComponentId )
  214.     {
  215.         boolean check = false;
  216.         try ( DAOUtil daoUtil = new DAOUtil( SQL_CHECK_STYLE_PORTLETCOMPONENT ) )
  217.         {

  218.             daoUtil.setInt( 1, nPortalComponentId );
  219.             daoUtil.executeQuery( );

  220.             if ( daoUtil.next( ) )
  221.             {
  222.                 check = true;
  223.             }

  224.         }

  225.         return check;
  226.     }
  227. }