View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.pluginwizard.business.model;
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.Collection;
41  
42  /**
43   * This class provides Data Access methods for PluginModel objects
44   */
45  public final class PluginModelDAO implements IPluginModelDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_plugin ) FROM pluginwizard_plugin_id";
49      private static final String SQL_QUERY_SELECT = "SELECT id_plugin, plugin_name, plugin_class, plugin_description, plugin_documentation, plugin_installation, plugin_changes, plugin_user_guide, plugin_version, plugin_copyright, plugin_icon_url, plugin_provider, plugin_provider_url, plugin_db_pool_required FROM pluginwizard_plugin_id WHERE id_plugin = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO pluginwizard_plugin_id ( id_plugin, plugin_name, plugin_class, plugin_description, plugin_documentation, plugin_installation, plugin_changes, plugin_user_guide, plugin_version, plugin_copyright, plugin_icon_url, plugin_provider, plugin_provider_url, plugin_db_pool_required ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM pluginwizard_plugin_id WHERE id_plugin = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE pluginwizard_plugin_id SET id_plugin = ?, plugin_name = ?, plugin_class = ?, plugin_description = ?, plugin_documentation = ?, plugin_installation = ?, plugin_changes = ?, plugin_user_guide = ?, plugin_version = ?, plugin_copyright = ?, plugin_icon_url = ?, plugin_provider = ?, plugin_provider_url = ?, plugin_db_pool_required = ? WHERE id_plugin = ?";
53      private static final String SQL_QUERY_SELECTALL = "SELECT id_plugin, plugin_name, plugin_class, plugin_description, plugin_documentation, plugin_installation, plugin_changes, plugin_user_guide, plugin_version, plugin_copyright, plugin_icon_url, plugin_provider, plugin_provider_url, plugin_db_pool_required FROM pluginwizard_plugin_id";
54      private static final String SQL_QUERY_FIND_BY_NAME = "SELECT id_plugin FROM pluginwizard_plugin_id WHERE plugin_name= ?";
55      private static final String SQL_QUERY_COUNT_FIND_BY_NAME = "SELECT COUNT(id_plugin) FROM pluginwizard_plugin_id WHERE plugin_name= ?";
56  
57      /**
58       * Generates a new primary key
59       * 
60       * @param plugin
61       *            The Plugin
62       * @return The new primary key
63       */
64      public int newPrimaryKey( Plugin plugin )
65      {
66          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ) )
67          {
68              daoUtil.executeQuery( );
69  
70              int nKey;
71  
72              if ( !daoUtil.next( ) )
73              {
74                  // if the table is empty
75                  nKey = 1;
76              }
77  
78              nKey = daoUtil.getInt( 1 ) + 1;
79              daoUtil.free( );
80  
81              return nKey;
82          }
83      }
84  
85      /**
86       * Insert a new record in the table.
87       * 
88       * @param pluginModel
89       *            instance of the PluginModel object to insert
90       * @param plugin
91       *            The plugin
92       */
93      public void insert( PluginModel pluginModel, Plugin plugin )
94      {
95          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
96          {
97  
98              pluginModel.setIdPlugin( newPrimaryKey( plugin ) );
99  
100             daoUtil.setInt( 1, pluginModel.getIdPlugin( ) );
101             daoUtil.setString( 2, pluginModel.getPluginName( ) );
102             daoUtil.setString( 3, pluginModel.getPluginClass( ) );
103             daoUtil.setString( 4, pluginModel.getPluginDescription( ) );
104             daoUtil.setString( 5, pluginModel.getPluginDocumentation( ) );
105             daoUtil.setString( 6, pluginModel.getPluginInstallation( ) );
106             daoUtil.setString( 7, pluginModel.getPluginChanges( ) );
107             daoUtil.setString( 8, pluginModel.getPluginUserGuide( ) );
108             daoUtil.setString( 9, pluginModel.getPluginVersion( ) );
109             daoUtil.setString( 10, pluginModel.getPluginCopyright( ) );
110             daoUtil.setString( 11, pluginModel.getPluginIconUrl( ) );
111             daoUtil.setString( 12, pluginModel.getPluginProvider( ) );
112             daoUtil.setString( 13, pluginModel.getPluginProviderUrl( ) );
113             daoUtil.setString( 14, pluginModel.getPluginDbPoolRequired( ) );
114 
115             daoUtil.executeUpdate( );
116             daoUtil.free( );
117         }
118     }
119 
120     /**
121      * Load the data of the pluginModel from the table
122      * 
123      * @param nId
124      *            The identifier of the pluginModel
125      * @param plugin
126      *            The plugin
127      * @return the instance of the PluginModel
128      */
129     public PluginModel load( int nId, Plugin plugin )
130     {
131         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
132         {
133             daoUtil.setInt( 1, nId );
134             daoUtil.executeQuery( );
135 
136             PluginModel pluginModel = null;
137 
138             if ( daoUtil.next( ) )
139             {
140                 pluginModel = new PluginModel( );
141 
142                 pluginModel.setIdPlugin( daoUtil.getInt( 1 ) );
143                 pluginModel.setPluginName( daoUtil.getString( 2 ) );
144                 pluginModel.setPluginClass( daoUtil.getString( 3 ) );
145                 pluginModel.setPluginDescription( daoUtil.getString( 4 ) );
146                 pluginModel.setPluginDocumentation( daoUtil.getString( 5 ) );
147                 pluginModel.setPluginInstallation( daoUtil.getString( 6 ) );
148                 pluginModel.setPluginChanges( daoUtil.getString( 7 ) );
149                 pluginModel.setPluginUserGuide( daoUtil.getString( 8 ) );
150                 pluginModel.setPluginVersion( daoUtil.getString( 9 ) );
151                 pluginModel.setPluginCopyright( daoUtil.getString( 10 ) );
152                 pluginModel.setPluginIconUrl( daoUtil.getString( 11 ) );
153                 pluginModel.setPluginProvider( daoUtil.getString( 12 ) );
154                 pluginModel.setPluginProviderUrl( daoUtil.getString( 13 ) );
155                 pluginModel.setPluginDbPoolRequired( daoUtil.getString( 14 ) );
156 
157                 // //TODO Portlets pluginModel.setPluginPortlets( PluginPortletHome.findByPlugin( nId, plugin ) );
158             }
159 
160             daoUtil.free( );
161 
162             return pluginModel;
163         }
164     }
165 
166     /**
167      * Delete a record from the table
168      * 
169      * @param nPluginModelId
170      *            The identifier of the pluginModel
171      * @param plugin
172      *            The plugin
173      */
174     public void delete( int nPluginModelId, Plugin plugin )
175     {
176         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
177         {
178             daoUtil.setInt( 1, nPluginModelId );
179             daoUtil.executeUpdate( );
180             daoUtil.free( );
181         }
182     }
183 
184     /**
185      * Update the record in the table
186      * 
187      * @param pluginModel
188      *            The reference of the pluginModel
189      * @param plugin
190      *            The plugin
191      */
192     public void store( PluginModel pluginModel, Plugin plugin )
193     {
194         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
195         {
196 
197             daoUtil.setInt( 1, pluginModel.getIdPlugin( ) );
198             daoUtil.setString( 2, pluginModel.getPluginName( ) );
199             daoUtil.setString( 3, pluginModel.getPluginClass( ) );
200             daoUtil.setString( 4, pluginModel.getPluginDescription( ) );
201             daoUtil.setString( 5, pluginModel.getPluginDocumentation( ) );
202             daoUtil.setString( 6, pluginModel.getPluginInstallation( ) );
203             daoUtil.setString( 7, pluginModel.getPluginChanges( ) );
204             daoUtil.setString( 8, pluginModel.getPluginUserGuide( ) );
205             daoUtil.setString( 9, pluginModel.getPluginVersion( ) );
206             daoUtil.setString( 10, pluginModel.getPluginCopyright( ) );
207             daoUtil.setString( 11, pluginModel.getPluginIconUrl( ) );
208             daoUtil.setString( 12, pluginModel.getPluginProvider( ) );
209             daoUtil.setString( 13, pluginModel.getPluginProviderUrl( ) );
210             daoUtil.setString( 14, pluginModel.getPluginDbPoolRequired( ) );
211             daoUtil.setInt( 15, pluginModel.getIdPlugin( ) );
212 
213             daoUtil.executeUpdate( );
214             daoUtil.free( );
215         }
216     }
217 
218     /**
219      * Load the data of all the pluginModels and returns them as a collection
220      * 
221      * @param plugin
222      *            The plugin
223      * @return The Collection which contains the data of all the pluginModels
224      */
225     public Collection<PluginModel> selectPluginModelsList( Plugin plugin )
226     {
227         Collection<PluginModel> pluginModelList = new ArrayList<>( );
228         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
229         {
230             daoUtil.executeQuery( );
231 
232             while ( daoUtil.next( ) )
233             {
234                 PluginModelnwizard/business/model/PluginModel.html#PluginModel">PluginModel pluginModel = new PluginModel( );
235 
236                 pluginModel.setIdPlugin( daoUtil.getInt( 1 ) );
237                 pluginModel.setPluginName( daoUtil.getString( 2 ) );
238                 pluginModel.setPluginClass( daoUtil.getString( 3 ) );
239                 pluginModel.setPluginDescription( daoUtil.getString( 4 ) );
240                 pluginModel.setPluginDocumentation( daoUtil.getString( 5 ) );
241                 pluginModel.setPluginInstallation( daoUtil.getString( 6 ) );
242                 pluginModel.setPluginChanges( daoUtil.getString( 7 ) );
243                 pluginModel.setPluginUserGuide( daoUtil.getString( 8 ) );
244                 pluginModel.setPluginVersion( daoUtil.getString( 9 ) );
245                 pluginModel.setPluginCopyright( daoUtil.getString( 10 ) );
246                 pluginModel.setPluginIconUrl( daoUtil.getString( 11 ) );
247                 pluginModel.setPluginProvider( daoUtil.getString( 12 ) );
248                 pluginModel.setPluginProviderUrl( daoUtil.getString( 13 ) );
249                 pluginModel.setPluginDbPoolRequired( daoUtil.getString( 14 ) );
250 
251                 pluginModelList.add( pluginModel );
252             }
253 
254             daoUtil.free( );
255 
256             return pluginModelList;
257         }
258     }
259 
260     /**
261      * Returns the identifier of the generated plugin
262      * 
263      * @param plugin
264      *            The Plugin
265      * @param strPluginName
266      *            The name of the generated plugin
267      * @return the identifier of the generated plugin
268      */
269     public int selectPluginModelByName( Plugin plugin, String strPluginName )
270     {
271         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_NAME, plugin ) )
272         {
273             daoUtil.setString( 1, strPluginName );
274             daoUtil.executeQuery( );
275 
276             int nPluginId = 0;
277 
278             if ( daoUtil.next( ) )
279             {
280                 nPluginId = daoUtil.getInt( 1 );
281             }
282 
283             daoUtil.free( );
284 
285             return nPluginId;
286         }
287     }
288 
289     /**
290      * Verifies whether the plugin exists
291      * 
292      * @param strPluginName
293      *            The name given to the plugin
294      * @param plugin
295      *            The plugin
296      * @return A boolean value telling whether a plugin with this name exists
297      */
298     public boolean pluginExists( String strPluginName, Plugin plugin )
299     {
300         boolean bValue = false;
301         int nCount = 0;
302         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_FIND_BY_NAME, plugin ) )
303         {
304             daoUtil.setString( 1, strPluginName );
305             daoUtil.executeQuery( );
306 
307             if ( daoUtil.next( ) )
308             {
309                 nCount = daoUtil.getInt( 1 );
310             }
311 
312             daoUtil.free( );
313 
314             if ( nCount > 0 )
315             {
316                 bValue = true;
317             }
318 
319             return bValue;
320         }
321     }
322 }