View Javadoc
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  
36  import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.sql.Statement;
40  import java.sql.Timestamp;
41  import java.util.Date;
42  
43  /**
44   * This class provides Data Access methods for Field objects
45   */
46  public final class FileDAO implements IFileDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_file,title,id_physical_file,file_size,mime_type,date_creation,origin"
50              + " FROM core_file WHERE id_file = ?";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO core_file(title,id_physical_file,file_size,mime_type,date_creation,origin)" + " VALUES(?,?,?,?,?,?)";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM core_file WHERE id_file = ? ";
53      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 = ?";
54  
55      /**
56       * Insert a new record in the table.
57       *
58       * @param file
59       *            instance of the File object to insert
60       * @return the id of the new file
61       */
62  
63      @Override
64      public int insert( File file )
65      {
66          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
67          {
68              int nIndex = 1;
69              daoUtil.setString( nIndex++, file.getTitle( ) );
70  
71              if ( file.getPhysicalFile( ) != null )
72              {
73                  daoUtil.setInt( nIndex++, file.getPhysicalFile( ).getIdPhysicalFile( ) );
74              }
75              else
76              {
77                  daoUtil.setIntNull( nIndex++ );
78              }
79  
80              daoUtil.setInt( nIndex++, file.getSize( ) );
81              daoUtil.setString( nIndex++, file.getMimeType( ) );
82              daoUtil.setTimestamp( nIndex++, new Timestamp( new Date( ).getTime( ) ) );
83              daoUtil.setString( nIndex, file.getOrigin( ) );
84              daoUtil.executeUpdate( );
85  
86              if ( daoUtil.nextGeneratedKey( ) )
87              {
88                  file.setIdFile( daoUtil.getGeneratedKeyInt( 1 ) );
89              }
90  
91          }
92  
93          return file.getIdFile( );
94      }
95  
96      /**
97       * Load the data of the File from the table
98       *
99       * @param nId
100      *            The identifier of the file
101      * @return the instance of the File
102      */
103     @Override
104     public File load( int nId )
105     {
106         File file = null;
107         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY ) )
108         {
109             daoUtil.setInt( 1, nId );
110             daoUtil.executeQuery( );
111 
112             PhysicalFile physicalFile = null;
113 
114             if ( daoUtil.next( ) )
115             {
116                 file = new File( );
117                 file.setIdFile( daoUtil.getInt( 1 ) );
118                 file.setTitle( daoUtil.getString( 2 ) );
119 
120                 if ( daoUtil.getObject( 3 ) != null )
121                 {
122                     physicalFile = new PhysicalFile( );
123                     physicalFile.setIdPhysicalFile( daoUtil.getInt( 3 ) );
124                     file.setPhysicalFile( physicalFile );
125                 }
126 
127                 file.setSize( daoUtil.getInt( 4 ) );
128                 file.setMimeType( daoUtil.getString( 5 ) );
129                 file.setDateCreation( daoUtil.getTimestamp( 6 ) );
130                 file.setOrigin( daoUtil.getString( 7 ) );
131             }
132 
133         }
134 
135         return file;
136     }
137 
138     /**
139      * Delete a record from the table
140      *
141      * @param nIdFile
142      *            The identifier of the file
143      */
144     @Override
145     public void delete( int nIdFile )
146     {
147         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
148         {
149             daoUtil.setInt( 1, nIdFile );
150             daoUtil.executeUpdate( );
151         }
152     }
153 
154     /**
155      * Update the file in the table
156      *
157      * @param file
158      *            instance of the File object to update
159      */
160     @Override
161     public void store( File file )
162     {
163         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
164         {
165             daoUtil.setInt( 1, file.getIdFile( ) );
166             daoUtil.setString( 2, file.getTitle( ) );
167 
168             if ( file.getPhysicalFile( ) != null )
169             {
170                 daoUtil.setInt( 3, file.getPhysicalFile( ).getIdPhysicalFile( ) );
171             }
172             else
173             {
174                 daoUtil.setIntNull( 3 );
175             }
176 
177             daoUtil.setInt( 4, file.getSize( ) );
178             daoUtil.setString( 5, file.getMimeType( ) );
179             daoUtil.setString( 6, file.getOrigin( ) );
180             daoUtil.setInt( 7, file.getIdFile( ) );
181             daoUtil.executeUpdate( );
182         }
183     }
184 }