View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.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   * This class provides Data Access methods for PluginRelease objects
49   */
50  public final class PluginReleaseDAO implements IPluginReleaseDAO
51  {
52      // Constants
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       * Generates a new primary key
66       * @param plugin The Plugin
67       * @return The new primary key
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              // if the table is empty
79              nKey = 1;
80          }
81  
82          nKey = daoUtil.getInt( 1 ) + 1;
83          daoUtil.free(  );
84  
85          return nKey;
86      }
87  
88      /**
89       * Insert a new record in the table.
90       * @param pluginRelease instance of the PluginRelease object to insert
91       * @param plugin The plugin
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      * Load the data of the pluginRelease from the table
112      * @param nId The identifier of the pluginRelease
113      * @param plugin The plugin
114      * @return the instance of the PluginRelease
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      * Delete a record from the table
148      * @param nPluginReleaseId The identifier of the pluginRelease
149      * @param plugin The plugin
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      * Update the record in the table
176      * @param pluginRelease The reference of the pluginRelease
177      * @param plugin The plugin
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      * Load the data of all the pluginReleases and returns them as a List
197      * @param plugin The plugin
198      * @return The List which contains the data of all the pluginReleases
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     // Upgrades
227 
228     /**
229      * Add an upgrade to a release
230      * @param nReleaseId The identifier of the pluginRelease
231      * @param upgrade The version
232      * @param plugin The plugin
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      * Delete an update from a release
249      * @param nReleaseId The identifier of the pluginRelease
250      * @param strVersionFrom The version
251      * @param plugin The plugin
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      * Load upgrades for a given release
264      * @param release The release
265      * @param plugin The plugin
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 }