XslExportDAO.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.xsl;

  35. import fr.paris.lutece.portal.business.file.File;
  36. import fr.paris.lutece.portal.service.plugin.Plugin;
  37. import fr.paris.lutece.util.sql.DAOUtil;

  38. import java.sql.Statement;
  39. import java.util.ArrayList;
  40. import java.util.List;

  41. /**
  42.  * This class provides Data Access methods for XSL objects
  43.  */
  44. public final class XslExportDAO implements IXslExportDAO
  45. {
  46.     // Constants
  47.     private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_xsl_export,title,description,extension,id_file,plugin FROM core_xsl_export WHERE id_xsl_export = ?";
  48.     private static final String SQL_QUERY_INSERT = "INSERT INTO core_xsl_export( title,description,extension,id_file,plugin) VALUES(?,?,?,?,?)";
  49.     private static final String SQL_QUERY_DELETE = "DELETE FROM core_xsl_export WHERE id_xsl_export = ? ";
  50.     private static final String SQL_QUERY_UPDATE = "UPDATE core_xsl_export SET id_xsl_export=?,title=?,description=?,extension=?,id_file=?,plugin=? WHERE id_xsl_export = ? ";
  51.     private static final String SQL_QUERY_SELECT = "SELECT id_xsl_export,title,description,extension,id_file,plugin FROM core_xsl_export ";
  52.     private static final String SQL_WHERE = " WHERE ";
  53.     private static final String SQL_FILTER_PLUGIN = " plugin = ? ";

  54.     /**
  55.      * {@inheritDoc}
  56.      */
  57.     @Override
  58.     public void insert( XslExport xslExport )
  59.     {
  60.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
  61.         {
  62.             int nIndex = 1;
  63.             daoUtil.setString( nIndex++, xslExport.getTitle( ) );
  64.             daoUtil.setString( nIndex++, xslExport.getDescription( ) );
  65.             daoUtil.setString( nIndex++, xslExport.getExtension( ) );

  66.             if ( xslExport.getFile( ) != null )
  67.             {
  68.                 daoUtil.setInt( nIndex++, xslExport.getFile( ).getIdFile( ) );
  69.             }
  70.             else
  71.             {
  72.                 daoUtil.setIntNull( nIndex++ );
  73.             }

  74.             daoUtil.setString( nIndex, xslExport.getPlugin( ) );

  75.             daoUtil.executeUpdate( );

  76.             if ( daoUtil.nextGeneratedKey( ) )
  77.             {
  78.                 xslExport.setIdXslExport( daoUtil.getGeneratedKeyInt( 1 ) );
  79.             }
  80.         }
  81.     }

  82.     /**
  83.      * {@inheritDoc}
  84.      */
  85.     @Override
  86.     public XslExport load( int nId )
  87.     {
  88.         XslExport xslExport = null;
  89.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY ) )
  90.         {
  91.             daoUtil.setInt( 1, nId );
  92.             daoUtil.executeQuery( );

  93.             File file = null;

  94.             if ( daoUtil.next( ) )
  95.             {
  96.                 xslExport = new XslExport( );
  97.                 xslExport.setIdXslExport( daoUtil.getInt( 1 ) );
  98.                 xslExport.setTitle( daoUtil.getString( 2 ) );
  99.                 xslExport.setDescription( daoUtil.getString( 3 ) );
  100.                 xslExport.setExtension( daoUtil.getString( 4 ) );

  101.                 if ( daoUtil.getObject( 5 ) != null )
  102.                 {
  103.                     file = new File( );
  104.                     file.setIdFile( daoUtil.getInt( 5 ) );
  105.                     xslExport.setFile( file );
  106.                 }

  107.                 xslExport.setPlugin( daoUtil.getString( 6 ) );
  108.             }

  109.         }

  110.         return xslExport;
  111.     }

  112.     /**
  113.      * {@inheritDoc}
  114.      */
  115.     @Override
  116.     public void delete( int nIdXslExport )
  117.     {
  118.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  119.         {
  120.             daoUtil.setInt( 1, nIdXslExport );
  121.             daoUtil.executeUpdate( );
  122.         }
  123.     }

  124.     /**
  125.      * {@inheritDoc}
  126.      */
  127.     @Override
  128.     public void store( XslExport xslExport )
  129.     {
  130.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  131.         {
  132.             daoUtil.setInt( 1, xslExport.getIdXslExport( ) );
  133.             daoUtil.setString( 2, xslExport.getTitle( ) );
  134.             daoUtil.setString( 3, xslExport.getDescription( ) );
  135.             daoUtil.setString( 4, xslExport.getExtension( ) );

  136.             if ( xslExport.getFile( ) != null )
  137.             {
  138.                 daoUtil.setInt( 5, xslExport.getFile( ).getIdFile( ) );
  139.             }
  140.             else
  141.             {
  142.                 daoUtil.setIntNull( 5 );
  143.             }

  144.             daoUtil.setString( 6, xslExport.getPlugin( ) );

  145.             daoUtil.setInt( 7, xslExport.getIdXslExport( ) );
  146.             daoUtil.executeUpdate( );
  147.         }
  148.     }

  149.     /**
  150.      * {@inheritDoc}
  151.      */
  152.     @Override
  153.     public List<XslExport> selectList( )
  154.     {
  155.         List<XslExport> listXslExport = new ArrayList<>( );
  156.         XslExport xslExport = null;
  157.         File file = null;

  158.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  159.         {

  160.             daoUtil.executeQuery( );

  161.             while ( daoUtil.next( ) )
  162.             {
  163.                 xslExport = new XslExport( );
  164.                 xslExport.setIdXslExport( daoUtil.getInt( 1 ) );
  165.                 xslExport.setTitle( daoUtil.getString( 2 ) );
  166.                 xslExport.setDescription( daoUtil.getString( 3 ) );
  167.                 xslExport.setExtension( daoUtil.getString( 4 ) );

  168.                 if ( daoUtil.getObject( 5 ) != null )
  169.                 {
  170.                     file = new File( );
  171.                     file.setIdFile( daoUtil.getInt( 5 ) );
  172.                     xslExport.setFile( file );
  173.                 }

  174.                 xslExport.setPlugin( daoUtil.getString( 6 ) );

  175.                 listXslExport.add( xslExport );
  176.             }

  177.         }

  178.         return listXslExport;
  179.     }

  180.     /**
  181.      * {@inheritDoc}
  182.      */
  183.     @Override
  184.     public List<XslExport> selectListByPlugin( Plugin plugin )
  185.     {
  186.         List<XslExport> listXslExport = new ArrayList<>( );
  187.         XslExport xslExport = null;
  188.         File file = null;

  189.         StringBuilder sbSql = new StringBuilder( SQL_QUERY_SELECT );
  190.         sbSql.append( SQL_WHERE );
  191.         sbSql.append( SQL_FILTER_PLUGIN );

  192.         try ( DAOUtil daoUtil = new DAOUtil( sbSql.toString( ) ) )
  193.         {
  194.             daoUtil.setString( 1, plugin.getName( ) );
  195.             daoUtil.executeQuery( );

  196.             while ( daoUtil.next( ) )
  197.             {
  198.                 xslExport = new XslExport( );
  199.                 xslExport.setIdXslExport( daoUtil.getInt( 1 ) );
  200.                 xslExport.setTitle( daoUtil.getString( 2 ) );
  201.                 xslExport.setDescription( daoUtil.getString( 3 ) );
  202.                 xslExport.setExtension( daoUtil.getString( 4 ) );

  203.                 if ( daoUtil.getObject( 5 ) != null )
  204.                 {
  205.                     file = new File( );
  206.                     file.setIdFile( daoUtil.getInt( 5 ) );
  207.                     xslExport.setFile( file );
  208.                 }

  209.                 xslExport.setPlugin( daoUtil.getString( 6 ) );

  210.                 listXslExport.add( xslExport );
  211.             }

  212.         }

  213.         return listXslExport;
  214.     }
  215. }