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
40
41
42
43 public final class FileDAO implements IFileDAO
44 {
45
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
57
58
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
70 nKey = 1;
71 }
72
73 nKey = daoUtil.getInt( 1 ) + 1;
74 daoUtil.free( );
75
76 return nKey;
77 }
78
79
80
81
82
83
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
114
115
116
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
151
152
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
164
165
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 }