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.plugins.appstore.business;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.util.sql.DAOUtil;
38
39 import java.util.ArrayList;
40 import java.util.List;
41
42
43
44
45
46
47 public class IconDAO implements IIconDAO
48 {
49 private static final String SQL_QUERY_NEW_PK = "SELECT max( id_icon ) FROM appstore_icon";
50 private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_icon,name,mime_type,file_value,width,height" + " FROM appstore_icon WHERE id_icon=?";
51 private static final String SQL_QUERY_SELECT_ICON = "SELECT id_icon,name,mime_type,width,height" + " FROM appstore_icon ORDER BY name DESC ";
52 private static final String SQL_QUERY_INSERT = "INSERT INTO appstore_icon " + "(id_icon,name,mime_type,file_value,width,height)VALUES(?,?,?,?,?,?)";
53 private static final String SQL_QUERY_UPDATE = "UPDATE appstore_icon SET id_icon=?,name=?,mime_type=?,file_value=?,width=?,height=?" + " WHERE id_icon=?";
54 private static final String SQL_QUERY_UPDATE_METADATA = "UPDATE appstore_icon SET id_icon=?,name=?,width=?,height=?" + " WHERE id_icon=?";
55 private static final String SQL_QUERY_DELETE = "DELETE FROM appstore_icon WHERE id_icon=? ";
56
57
58
59
60
61
62
63
64 private int newPrimaryKey( Plugin plugin )
65 {
66 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
67 daoUtil.executeQuery( );
68
69 int nKey;
70
71 if ( !daoUtil.next( ) )
72 {
73
74 nKey = 1;
75 }
76
77 nKey = daoUtil.getInt( 1 ) + 1;
78 daoUtil.free( );
79
80 return nKey;
81 }
82
83
84
85
86
87
88
89 public synchronized void insert( Icon icon, Plugin plugin )
90 {
91 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
92
93 int nPos = 0;
94 icon.setId( newPrimaryKey( plugin ) );
95
96 daoUtil.setInt( ++nPos, icon.getId( ) );
97 daoUtil.setString( ++nPos, icon.getName( ) );
98 daoUtil.setString( ++nPos, icon.getMimeType( ) );
99 daoUtil.setBytes( ++nPos, icon.getValue( ) );
100 daoUtil.setInt( ++nPos, icon.getWidth( ) );
101 daoUtil.setInt( ++nPos, icon.getHeight( ) );
102 daoUtil.executeUpdate( );
103 daoUtil.free( );
104 }
105
106
107
108
109
110
111
112 public void store( Icon icon, Plugin plugin )
113 {
114 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
115
116 int nPos = 0;
117
118 daoUtil.setInt( ++nPos, icon.getId( ) );
119 daoUtil.setString( ++nPos, icon.getName( ) );
120 daoUtil.setString( ++nPos, icon.getMimeType( ) );
121 daoUtil.setBytes( ++nPos, icon.getValue( ) );
122 daoUtil.setInt( ++nPos, icon.getWidth( ) );
123 daoUtil.setInt( ++nPos, icon.getHeight( ) );
124
125 daoUtil.setInt( ++nPos, icon.getId( ) );
126 daoUtil.executeUpdate( );
127 daoUtil.free( );
128 }
129
130 public void storeMetadata( Icon icon, Plugin plugin )
131 {
132 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_METADATA, plugin );
133
134 int nPos = 0;
135
136 daoUtil.setInt( ++nPos, icon.getId( ) );
137 daoUtil.setString( ++nPos, icon.getName( ) );
138 daoUtil.setInt( ++nPos, icon.getWidth( ) );
139 daoUtil.setInt( ++nPos, icon.getHeight( ) );
140 daoUtil.setInt( ++nPos, icon.getId( ) );
141 daoUtil.executeUpdate( );
142 daoUtil.free( );
143 }
144
145
146
147
148
149
150 public Icon load( int nIdIcon, Plugin plugin )
151 {
152 Icon icon = null;
153 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
154
155 daoUtil.setInt( 1, nIdIcon );
156
157 daoUtil.executeQuery( );
158
159 int nPos = 0;
160
161 if ( daoUtil.next( ) )
162 {
163 icon = new Icon( );
164 icon.setId( daoUtil.getInt( ++nPos ) );
165 icon.setName( daoUtil.getString( ++nPos ) );
166 icon.setMimeType( daoUtil.getString( ++nPos ) );
167 icon.setValue( daoUtil.getBytes( ++nPos ) );
168 icon.setWidth( daoUtil.getInt( ++nPos ) );
169 icon.setHeight( daoUtil.getInt( ++nPos ) );
170 }
171
172 daoUtil.free( );
173
174 return icon;
175 }
176
177
178
179
180
181
182 public void delete( int nIdIcon, Plugin plugin )
183 {
184 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
185
186 daoUtil.setInt( 1, nIdIcon );
187 daoUtil.executeUpdate( );
188 daoUtil.free( );
189 }
190
191
192
193
194
195
196 public List<Icon> selectAll( Plugin plugin )
197 {
198 Icon icon = null;
199 List<Icon> listIcon = new ArrayList<Icon>( );
200
201 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ICON, plugin );
202 daoUtil.executeQuery( );
203
204 int nPos;
205
206 while ( daoUtil.next( ) )
207 {
208 nPos = 0;
209 icon = new Icon( );
210 icon.setId( daoUtil.getInt( ++nPos ) );
211 icon.setName( daoUtil.getString( ++nPos ) );
212 icon.setMimeType( daoUtil.getString( ++nPos ) );
213 icon.setWidth( daoUtil.getInt( ++nPos ) );
214 icon.setHeight( daoUtil.getInt( ++nPos ) );
215
216 listIcon.add( icon );
217 }
218
219 daoUtil.free( );
220
221 return listIcon;
222 }
223 }