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.updatercatalog.business;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.portal.service.util.AppLogService;
38 import fr.paris.lutece.util.sql.DAOUtil;
39 import fr.paris.lutece.util.sql.Transaction;
40
41 import java.sql.SQLException;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46
47
48
49
50 public final class PluginReleaseDAO implements IPluginReleaseDAO
51 {
52
53 private static final String SQL_QUERY_NEW_PK = "SELECT max( id_release ) FROM updatercatalog_plugin_release";
54 private static final String SQL_QUERY_SELECT = "SELECT id_release, plugin_name, plugin_version, url_download, core_version_min, core_version_max FROM updatercatalog_plugin_release WHERE id_release = ?";
55 private static final String SQL_QUERY_INSERT = "INSERT INTO updatercatalog_plugin_release ( id_release, plugin_name, plugin_version, url_download, core_version_min, core_version_max ) VALUES ( ?, ?, ?, ?, ?, ? ) ";
56 private static final String SQL_QUERY_DELETE = "DELETE FROM updatercatalog_plugin_release WHERE id_release = ? ";
57 private static final String SQL_QUERY_DELETE_UPGRADES = "DELETE FROM updatercatalog_plugin_upgrade WHERE id_release = ? ";
58 private static final String SQL_QUERY_UPDATE = "UPDATE updatercatalog_plugin_release SET id_release = ?, plugin_name = ?, plugin_version = ?, url_download = ?, core_version_min = ?, core_version_max = ? WHERE id_release = ?";
59 private static final String SQL_QUERY_SELECTALL = "SELECT id_release, plugin_name, plugin_version, url_download, core_version_min, core_version_max FROM updatercatalog_plugin_release";
60 private static final String SQL_INSERT_UPGRADE = "INSERT INTO updatercatalog_plugin_upgrade ( id_release , version_from , critical_upgrade , url_download ) VALUES( ? , ? , ? , ? )";
61 private static final String SQL_DELETE_UPGRADE = "DELETE FROM updatercatalog_plugin_upgrade WHERE id_release = ? AND version_from = ? ";
62 private static final String SQL_QUERY_SELECT_UPGRADES = "SELECT version_from , critical_upgrade , url_download FROM updatercatalog_plugin_upgrade WHERE id_release = ? ";
63
64
65
66
67
68
69 public int newPrimaryKey( Plugin plugin )
70 {
71 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
72 daoUtil.executeQuery( );
73
74 int nKey;
75
76 if ( !daoUtil.next( ) )
77 {
78
79 nKey = 1;
80 }
81
82 nKey = daoUtil.getInt( 1 ) + 1;
83 daoUtil.free( );
84
85 return nKey;
86 }
87
88
89
90
91
92
93 public void insert( PluginRelease pluginRelease, Plugin plugin )
94 {
95 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
96
97 pluginRelease.setId( newPrimaryKey( plugin ) );
98
99 daoUtil.setInt( 1, pluginRelease.getId( ) );
100 daoUtil.setString( 2, pluginRelease.getPluginName( ) );
101 daoUtil.setString( 3, pluginRelease.getPluginVersion( ) );
102 daoUtil.setString( 4, pluginRelease.getUrlDownload( ) );
103 daoUtil.setString( 5, pluginRelease.getCoreVersionMin( ) );
104 daoUtil.setString( 6, pluginRelease.getCoreVersionMax( ) );
105
106 daoUtil.executeUpdate( );
107 daoUtil.free( );
108 }
109
110
111
112
113
114
115
116 public PluginRelease load( int nId, Plugin plugin )
117 {
118 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
119 daoUtil.setInt( 1, nId );
120 daoUtil.executeQuery( );
121
122 PluginRelease release = null;
123
124 if ( daoUtil.next( ) )
125 {
126 release = new PluginRelease( );
127
128 release.setId( daoUtil.getInt( 1 ) );
129 release.setPluginName( daoUtil.getString( 2 ) );
130 release.setPluginVersion( daoUtil.getString( 3 ) );
131 release.setUrlDownload( daoUtil.getString( 4 ) );
132 release.setCoreVersionMin( daoUtil.getString( 5 ) );
133 release.setCoreVersionMax( daoUtil.getString( 6 ) );
134 }
135
136 daoUtil.free( );
137
138 if ( release != null )
139 {
140 loadUpgrades( release, plugin );
141 }
142
143 return release;
144 }
145
146
147
148
149
150
151 public void delete( int nPluginReleaseId, Plugin plugin )
152 {
153 Transaction transaction = new Transaction( );
154
155 try
156 {
157 transaction.prepareStatement( SQL_QUERY_DELETE_UPGRADES );
158 transaction.getStatement( ).setInt( 1, nPluginReleaseId );
159 transaction.executeStatement( );
160
161 transaction.prepareStatement( SQL_QUERY_DELETE );
162 transaction.getStatement( ).setInt( 1, nPluginReleaseId );
163 transaction.executeStatement( );
164
165 transaction.commit( );
166 }
167 catch ( SQLException e )
168 {
169 transaction.rollback( e );
170 AppLogService.error( "Error deleting Release " + e.getMessage( ), e.getCause( ) );
171 }
172 }
173
174
175
176
177
178
179 public void store( PluginRelease pluginRelease, Plugin plugin )
180 {
181 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
182
183 daoUtil.setInt( 1, pluginRelease.getId( ) );
184 daoUtil.setString( 2, pluginRelease.getPluginName( ) );
185 daoUtil.setString( 3, pluginRelease.getPluginVersion( ) );
186 daoUtil.setString( 4, pluginRelease.getUrlDownload( ) );
187 daoUtil.setString( 5, pluginRelease.getCoreVersionMin( ) );
188 daoUtil.setString( 6, pluginRelease.getCoreVersionMax( ) );
189 daoUtil.setInt( 7, pluginRelease.getId( ) );
190
191 daoUtil.executeUpdate( );
192 daoUtil.free( );
193 }
194
195
196
197
198
199
200 public List<PluginRelease> selectPluginReleasesList( Plugin plugin )
201 {
202 List<PluginRelease> pluginReleaseList = new ArrayList<PluginRelease>( );
203 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
204 daoUtil.executeQuery( );
205
206 while ( daoUtil.next( ) )
207 {
208 PluginRelease pluginRelease = new PluginRelease( );
209
210 pluginRelease.setId( daoUtil.getInt( 1 ) );
211 pluginRelease.setPluginName( daoUtil.getString( 2 ) );
212 pluginRelease.setPluginVersion( daoUtil.getString( 3 ) );
213 pluginRelease.setUrlDownload( daoUtil.getString( 4 ) );
214 pluginRelease.setCoreVersionMin( daoUtil.getString( 5 ) );
215 pluginRelease.setCoreVersionMax( daoUtil.getString( 6 ) );
216
217 pluginReleaseList.add( pluginRelease );
218 }
219
220 daoUtil.free( );
221
222 return pluginReleaseList;
223 }
224
225
226
227
228
229
230
231
232
233
234 public void addUpgrade( int nReleaseId, ReleaseUpgrade upgrade, Plugin plugin )
235 {
236 DAOUtil daoUtil = new DAOUtil( SQL_INSERT_UPGRADE, plugin );
237
238 daoUtil.setInt( 1, nReleaseId );
239 daoUtil.setString( 2, upgrade.getVersionFrom( ) );
240 daoUtil.setInt( 3, upgrade.getCritical( ) );
241 daoUtil.setString( 4, upgrade.getUrlDownload( ) );
242
243 daoUtil.executeUpdate( );
244 daoUtil.free( );
245 }
246
247
248
249
250
251
252
253 public void deleteUpgrade( int nReleaseId, String strVersionFrom, Plugin plugin )
254 {
255 DAOUtil daoUtil = new DAOUtil( SQL_DELETE_UPGRADE, plugin );
256 daoUtil.setInt( 1, nReleaseId );
257 daoUtil.setString( 2, strVersionFrom );
258 daoUtil.executeUpdate( );
259 daoUtil.free( );
260 }
261
262
263
264
265
266
267 private void loadUpgrades( PluginRelease release, Plugin plugin )
268 {
269 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_UPGRADES, plugin );
270 daoUtil.setInt( 1, release.getId( ) );
271 daoUtil.executeQuery( );
272
273 release.getUpgrades( ).clear( );
274
275 while ( daoUtil.next( ) )
276 {
277 ReleaseUpgrade upgrade = new ReleaseUpgrade( );
278
279 upgrade.setVersionFrom( daoUtil.getString( 1 ) );
280 upgrade.setCritical( daoUtil.getInt( 2 ) );
281 upgrade.setUrlDownload( daoUtil.getString( 3 ) );
282
283 release.addUpgrade( upgrade );
284 }
285
286 daoUtil.free( );
287 }
288 }