1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
45
46 public final class FileDAO implements IFileDAO
47 {
48
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
57
58
59
60
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
98
99
100
101
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
140
141
142
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
156
157
158
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 }