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;
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 ConfigurationKey objects
44   */
45  public final class ConfigurationKeyDAO implements IConfigurationKeyDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_key ) FROM pluginwizard_configuration_key";
49      private static final String SQL_QUERY_SELECT = "SELECT id_key, key_description, key_value FROM pluginwizard_configuration_key WHERE id_key = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO pluginwizard_configuration_key ( id_key, key_description, key_value ) VALUES ( ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM pluginwizard_configuration_key WHERE id_key = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE pluginwizard_configuration_key SET id_key = ?, key_description = ?, key_value = ? WHERE id_key = ?";
53      private static final String SQL_QUERY_SELECTALL = "SELECT id_key, key_description, key_value FROM pluginwizard_configuration_key";
54  
55      /**
56       * Generates a new primary key
57       * 
58       * @param plugin
59       *            The Plugin
60       * @return The new primary key
61       */
62      public int newPrimaryKey( Plugin plugin )
63      {
64          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ) )
65          {
66              daoUtil.executeQuery( );
67  
68              int nKey;
69  
70              if ( !daoUtil.next( ) )
71              {
72                  // if the table is empty
73                  nKey = 1;
74              }
75  
76              nKey = daoUtil.getInt( 1 ) + 1;
77              daoUtil.free( );
78  
79              return nKey;
80          }
81      }
82  
83      /**
84       * Insert a new record in the table.
85       * 
86       * @param configurationKey
87       *            instance of the ConfigurationKey object to insert
88       * @param plugin
89       *            The plugin
90       */
91      public void insert( ConfigurationKey configurationKey, Plugin plugin )
92      {
93          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
94          {
95  
96              configurationKey.setIdKey( newPrimaryKey( plugin ) );
97  
98              daoUtil.setInt( 1, configurationKey.getIdKey( ) );
99              daoUtil.setString( 2, configurationKey.getKeyDescription( ) );
100             daoUtil.setString( 3, configurationKey.getKeyValue( ) );
101 
102             daoUtil.executeUpdate( );
103             daoUtil.free( );
104         }
105     }
106 
107     /**
108      * Load the data of the configurationKey from the table
109      * 
110      * @param nId
111      *            The identifier of the configurationKey
112      * @param plugin
113      *            The plugin
114      * @return the instance of the ConfigurationKey
115      */
116     public ConfigurationKey load( int nId, Plugin plugin )
117     {
118         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
119         {
120             daoUtil.setInt( 1, nId );
121             daoUtil.executeQuery( );
122 
123             ConfigurationKey configurationKey = null;
124 
125             if ( daoUtil.next( ) )
126             {
127                 configurationKey = new ConfigurationKey( );
128 
129                 configurationKey.setIdKey( daoUtil.getInt( 1 ) );
130                 configurationKey.setKeyDescription( daoUtil.getString( 2 ) );
131                 configurationKey.setKeyValue( daoUtil.getString( 3 ) );
132             }
133 
134             daoUtil.free( );
135 
136             return configurationKey;
137         }
138     }
139 
140     /**
141      * Delete a record from the table
142      * 
143      * @param nConfigurationKeyId
144      *            The identifier of the configurationKey
145      * @param plugin
146      *            The plugin
147      */
148     public void delete( int nConfigurationKeyId, Plugin plugin )
149     {
150         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
151         {
152             daoUtil.setInt( 1, nConfigurationKeyId );
153             daoUtil.executeUpdate( );
154             daoUtil.free( );
155         }
156     }
157 
158     /**
159      * Update the record in the table
160      * 
161      * @param configurationKey
162      *            The reference of the configurationKey
163      * @param plugin
164      *            The plugin
165      */
166     public void store( ConfigurationKey configurationKey, Plugin plugin )
167     {
168         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
169         {
170 
171             daoUtil.setInt( 1, configurationKey.getIdKey( ) );
172             daoUtil.setString( 2, configurationKey.getKeyDescription( ) );
173             daoUtil.setString( 3, configurationKey.getKeyValue( ) );
174             daoUtil.setInt( 4, configurationKey.getIdKey( ) );
175 
176             daoUtil.executeUpdate( );
177             daoUtil.free( );
178         }
179     }
180 
181     /**
182      * Load the data of all the configurationKeys and returns them as a collection
183      * 
184      * @param plugin
185      *            The plugin
186      * @return The Collection which contains the data of all the configurationKeys
187      */
188     public Collection<ConfigurationKey> selectConfigurationKeysList( Plugin plugin )
189     {
190         Collection<ConfigurationKey> configurationKeyList = new ArrayList<>( );
191         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
192         {
193             daoUtil.executeQuery( );
194 
195             while ( daoUtil.next( ) )
196             {
197                 ConfigurationKeyess/ConfigurationKey.html#ConfigurationKey">ConfigurationKey configurationKey = new ConfigurationKey( );
198 
199                 configurationKey.setIdKey( daoUtil.getInt( 1 ) );
200                 configurationKey.setKeyDescription( daoUtil.getString( 2 ) );
201                 configurationKey.setKeyValue( daoUtil.getString( 3 ) );
202 
203                 configurationKeyList.add( configurationKey );
204             }
205 
206             daoUtil.free( );
207 
208             return configurationKeyList;
209         }
210     }
211 }