PortalComponentDAO.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.portalcomponent;

  35. import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
  36. import fr.paris.lutece.util.sql.DAOUtil;

  37. /**
  38.  * This class provides Data Access methods for PortalComponent objects
  39.  */
  40. public final class PortalComponentDAO implements IPortalComponentDAO
  41. {
  42.     // Constants
  43.     private static final String SQL_QUERY_SELECT = " SELECT name FROM core_portal_component WHERE id_portal_component = ?";
  44.     private static final String SQL_QUERY_INSERT = " INSERT INTO core_portal_component ( id_portal_component, name ) VALUES ( ?, ? )";
  45.     private static final String SQL_QUERY_DELETE = " DELETE FROM core_portal_component WHERE id_portal_component = ?";
  46.     private static final String SQL_QUERY_UPDATE = " UPDATE core_portal_component SET id_portal_component = ?, name = ? " + " WHERE id_portal_component = ?";
  47.     private static final String SQL_QUERY_SELECTXSL = " SELECT a.id_stylesheet , a.description , a.file_name, a.source "
  48.             + " FROM core_stylesheet a, core_portal_component b, core_style_mode_stylesheet c, core_style d "
  49.             + " WHERE a.id_stylesheet = c.id_stylesheet AND d.id_style = c.id_style "
  50.             + " AND b.id_portal_component = d.id_portal_component AND d.id_portal_component = ? " + " AND c.id_mode = ? ";

  51.     // /////////////////////////////////////////////////////////////////////////////////////
  52.     // Access methods to data

  53.     /**
  54.      * {@inheritDoc}
  55.      */
  56.     public synchronized void insert( PortalComponent portalComponent )
  57.     {
  58.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
  59.         {

  60.             daoUtil.setInt( 1, portalComponent.getId( ) );
  61.             daoUtil.setString( 2, portalComponent.getName( ) );

  62.             daoUtil.executeUpdate( );
  63.         }
  64.     }

  65.     /**
  66.      * {@inheritDoc}
  67.      */
  68.     public PortalComponent load( int nPortalComponentId )
  69.     {
  70.         PortalComponent portalComponent = null;
  71.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  72.         {
  73.             daoUtil.setInt( 1, nPortalComponentId );

  74.             daoUtil.executeQuery( );

  75.             if ( daoUtil.next( ) )
  76.             {
  77.                 portalComponent = new PortalComponent( );

  78.                 portalComponent.setId( nPortalComponentId );
  79.                 portalComponent.setName( daoUtil.getString( 1 ) );
  80.             }

  81.         }

  82.         return portalComponent;
  83.     }

  84.     /**
  85.      * {@inheritDoc}
  86.      */
  87.     public void delete( int nPortalComponentId )
  88.     {
  89.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  90.         {
  91.             daoUtil.setInt( 1, nPortalComponentId );

  92.             daoUtil.executeUpdate( );
  93.         }
  94.     }

  95.     /**
  96.      * {@inheritDoc}
  97.      */
  98.     public void store( PortalComponent portalComponent )
  99.     {
  100.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  101.         {

  102.             daoUtil.setInt( 1, portalComponent.getId( ) );
  103.             daoUtil.setString( 2, portalComponent.getName( ) );
  104.             daoUtil.setInt( 3, portalComponent.getId( ) );

  105.             daoUtil.executeUpdate( );
  106.         }
  107.     }

  108.     /**
  109.      * {@inheritDoc}
  110.      */
  111.     public StyleSheet selectXslFile( int nPortalComponentId, int nIdMode )
  112.     {
  113.         StyleSheet stylesheet = new StyleSheet( );
  114.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTXSL ) )
  115.         {

  116.             daoUtil.setInt( 1, nPortalComponentId );
  117.             daoUtil.setInt( 2, nIdMode );

  118.             daoUtil.executeQuery( );

  119.             if ( daoUtil.next( ) )
  120.             {
  121.                 stylesheet.setId( daoUtil.getInt( 1 ) );
  122.                 stylesheet.setDescription( daoUtil.getString( 2 ) );
  123.                 stylesheet.setFile( daoUtil.getString( 3 ) );
  124.                 stylesheet.setSource( daoUtil.getBytes( 4 ) );
  125.             }

  126.         }

  127.         return stylesheet;
  128.     }
  129. }