View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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  
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_NEW_PK = "SELECT max( id_file ) FROM core_file";
47      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_file,title,id_physical_file,file_size,mime_type" +
48          " FROM core_file WHERE id_file = ?";
49      private static final String SQL_QUERY_INSERT = "INSERT INTO core_file(id_file,title,id_physical_file,file_size,mime_type)" +
50          " VALUES(?,?,?,?,?)";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM core_file WHERE id_file = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE  core_file SET " +
53          "id_file=?,title=?,id_physical_file=?,file_size=?,mime_type=? WHERE id_file = ?";
54  
55      /**
56       * Generates a new primary key
57       *
58       * @return The new primary key
59       */
60      public int newPrimaryKey(  )
61      {
62          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
63          daoUtil.executeQuery(  );
64  
65          int nKey;
66  
67          if ( !daoUtil.next(  ) )
68          {
69              // if the table is empty
70              nKey = 1;
71          }
72  
73          nKey = daoUtil.getInt( 1 ) + 1;
74          daoUtil.free(  );
75  
76          return nKey;
77      }
78  
79      /**
80       * Insert a new record in the table.
81       *
82       * @param file instance of the File object to insert
83       * @return the id of the new file
84       */
85      public synchronized int insert( File file )
86      {
87          file.setIdFile( newPrimaryKey(  ) );
88  
89          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
90          daoUtil.setString( 2, file.getTitle(  ) );
91  
92          if ( file.getPhysicalFile(  ) != null )
93          {
94              daoUtil.setInt( 3, file.getPhysicalFile(  ).getIdPhysicalFile(  ) );
95          }
96          else
97          {
98              daoUtil.setIntNull( 3 );
99          }
100 
101         daoUtil.setInt( 4, file.getSize(  ) );
102         daoUtil.setString( 5, file.getMimeType(  ) );
103 
104         daoUtil.setInt( 1, file.getIdFile(  ) );
105         daoUtil.executeUpdate(  );
106 
107         daoUtil.free(  );
108 
109         return file.getIdFile(  );
110     }
111 
112     /**
113      * Load the data of the File from the table
114      *
115      * @param nId The identifier of the file
116      * @return the instance of the File
117      */
118     public File load( int nId )
119     {
120         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY );
121         daoUtil.setInt( 1, nId );
122         daoUtil.executeQuery(  );
123 
124         File file = null;
125         PhysicalFile physicalFile = null;
126 
127         if ( daoUtil.next(  ) )
128         {
129             file = new File(  );
130             file.setIdFile( daoUtil.getInt( 1 ) );
131             file.setTitle( daoUtil.getString( 2 ) );
132 
133             if ( daoUtil.getObject( 3 ) != null )
134             {
135                 physicalFile = new PhysicalFile(  );
136                 physicalFile.setIdPhysicalFile( daoUtil.getInt( 3 ) );
137                 file.setPhysicalFile( physicalFile );
138             }
139 
140             file.setSize( daoUtil.getInt( 4 ) );
141             file.setMimeType( daoUtil.getString( 5 ) );
142         }
143 
144         daoUtil.free(  );
145 
146         return file;
147     }
148 
149     /**
150      * Delete a record from the table
151      *
152      * @param nIdFile The identifier of the file
153      */
154     public void delete( int nIdFile )
155     {
156         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
157         daoUtil.setInt( 1, nIdFile );
158         daoUtil.executeUpdate(  );
159         daoUtil.free(  );
160     }
161 
162     /**
163      * Update the file in the table
164      *
165      * @param file instance of the File object to update
166      */
167     public void store( File file )
168     {
169         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
170         daoUtil.setInt( 1, file.getIdFile(  ) );
171         daoUtil.setString( 2, file.getTitle(  ) );
172 
173         if ( file.getPhysicalFile(  ) != null )
174         {
175             daoUtil.setInt( 3, file.getPhysicalFile(  ).getIdPhysicalFile(  ) );
176         }
177         else
178         {
179             daoUtil.setIntNull( 3 );
180         }
181 
182         daoUtil.setInt( 4, file.getSize(  ) );
183         daoUtil.setString( 5, file.getMimeType(  ) );
184         daoUtil.setInt( 6, file.getIdFile(  ) );
185         daoUtil.executeUpdate(  );
186         daoUtil.free(  );
187     }
188 }