FileDAO.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.file;

  35. import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
  36. import fr.paris.lutece.util.sql.DAOUtil;

  37. import java.sql.Statement;
  38. import java.sql.Timestamp;
  39. import java.util.Date;

  40. /**
  41.  * This class provides Data Access methods for Field objects
  42.  */
  43. public final class FileDAO implements IFileDAO
  44. {
  45.     // Constants
  46.     private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_file,title,id_physical_file,file_size,mime_type,date_creation,origin"
  47.             + " FROM core_file WHERE id_file = ?";
  48.     private static final String SQL_QUERY_INSERT = "INSERT INTO core_file(title,id_physical_file,file_size,mime_type,date_creation,origin)" + " VALUES(?,?,?,?,?,?)";
  49.     private static final String SQL_QUERY_DELETE = "DELETE FROM core_file WHERE id_file = ? ";
  50.     private static final String SQL_QUERY_UPDATE = "UPDATE  core_file SET " + "id_file=?,title=?,id_physical_file=?,file_size=?,mime_type=?,origin=? WHERE id_file = ?";

  51.     /**
  52.      * Insert a new record in the table.
  53.      *
  54.      * @param file
  55.      *            instance of the File object to insert
  56.      * @return the id of the new file
  57.      */

  58.     @Override
  59.     public int insert( File file )
  60.     {
  61.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
  62.         {
  63.             int nIndex = 1;
  64.             daoUtil.setString( nIndex++, file.getTitle( ) );

  65.             if ( file.getPhysicalFile( ) != null )
  66.             {
  67.                 daoUtil.setInt( nIndex++, file.getPhysicalFile( ).getIdPhysicalFile( ) );
  68.             }
  69.             else
  70.             {
  71.                 daoUtil.setIntNull( nIndex++ );
  72.             }

  73.             daoUtil.setInt( nIndex++, file.getSize( ) );
  74.             daoUtil.setString( nIndex++, file.getMimeType( ) );
  75.             daoUtil.setTimestamp( nIndex++, new Timestamp( new Date( ).getTime( ) ) );
  76.             daoUtil.setString( nIndex, file.getOrigin( ) );
  77.             daoUtil.executeUpdate( );

  78.             if ( daoUtil.nextGeneratedKey( ) )
  79.             {
  80.                 file.setIdFile( daoUtil.getGeneratedKeyInt( 1 ) );
  81.             }

  82.         }

  83.         return file.getIdFile( );
  84.     }

  85.     /**
  86.      * Load the data of the File from the table
  87.      *
  88.      * @param nId
  89.      *            The identifier of the file
  90.      * @return the instance of the File
  91.      */
  92.     @Override
  93.     public File load( int nId )
  94.     {
  95.         File file = null;
  96.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY ) )
  97.         {
  98.             daoUtil.setInt( 1, nId );
  99.             daoUtil.executeQuery( );

  100.             PhysicalFile physicalFile = null;

  101.             if ( daoUtil.next( ) )
  102.             {
  103.                 file = new File( );
  104.                 file.setIdFile( daoUtil.getInt( 1 ) );
  105.                 file.setTitle( daoUtil.getString( 2 ) );

  106.                 if ( daoUtil.getObject( 3 ) != null )
  107.                 {
  108.                     physicalFile = new PhysicalFile( );
  109.                     physicalFile.setIdPhysicalFile( daoUtil.getInt( 3 ) );
  110.                     file.setPhysicalFile( physicalFile );
  111.                 }

  112.                 file.setSize( daoUtil.getInt( 4 ) );
  113.                 file.setMimeType( daoUtil.getString( 5 ) );
  114.                 file.setDateCreation( daoUtil.getTimestamp( 6 ) );
  115.                 file.setOrigin( daoUtil.getString( 7 ) );
  116.             }

  117.         }

  118.         return file;
  119.     }

  120.     /**
  121.      * Delete a record from the table
  122.      *
  123.      * @param nIdFile
  124.      *            The identifier of the file
  125.      */
  126.     @Override
  127.     public void delete( int nIdFile )
  128.     {
  129.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  130.         {
  131.             daoUtil.setInt( 1, nIdFile );
  132.             daoUtil.executeUpdate( );
  133.         }
  134.     }

  135.     /**
  136.      * Update the file in the table
  137.      *
  138.      * @param file
  139.      *            instance of the File object to update
  140.      */
  141.     @Override
  142.     public void store( File file )
  143.     {
  144.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  145.         {
  146.             daoUtil.setInt( 1, file.getIdFile( ) );
  147.             daoUtil.setString( 2, file.getTitle( ) );

  148.             if ( file.getPhysicalFile( ) != null )
  149.             {
  150.                 daoUtil.setInt( 3, file.getPhysicalFile( ).getIdPhysicalFile( ) );
  151.             }
  152.             else
  153.             {
  154.                 daoUtil.setIntNull( 3 );
  155.             }

  156.             daoUtil.setInt( 4, file.getSize( ) );
  157.             daoUtil.setString( 5, file.getMimeType( ) );
  158.             daoUtil.setString( 6, file.getOrigin( ) );
  159.             daoUtil.setInt( 7, file.getIdFile( ) );
  160.             daoUtil.executeUpdate( );
  161.         }
  162.     }
  163. }