View Javadoc
1   /*
2    * Copyright (c) 2002-2019, 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.botpress.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.ReferenceList;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.sql.Statement;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  
45  /**
46   * This class provides Data Access methods for BPBot objects
47   */
48  public final class BPBotDAO implements IBPBotDAO
49  {
50      // Constants
51      private static final String SQL_QUERY_SELECT = "SELECT id_b_p_bot, bot_key, botpress_key, name, description, avatar_url, avatar_renderer_key, language, bot_status, is_standalone, welcome_message, error_message, server_url, api_version FROM botpress_bots WHERE id_b_p_bot = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO botpress_bots ( bot_key, botpress_key, name, description, avatar_url, avatar_renderer_key,language, bot_status, is_standalone, welcome_message, error_message, server_url, api_version ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM botpress_bots WHERE id_b_p_bot = ? ";
54      private static final String SQL_QUERY_UPDATE = "UPDATE botpress_bots SET id_b_p_bot = ?, bot_key = ?, botpress_key = ?, name = ?, description = ?, avatar_url = ?, avatar_renderer_key = ?,language = ?, bot_status = ?, is_standalone = ?, welcome_message = ?, error_message = ?, server_url = ?, api_version = ? WHERE id_b_p_bot = ?";
55      private static final String SQL_QUERY_SELECTALL = "SELECT id_b_p_bot, bot_key, botpress_key, name, description, avatar_url, avatar_renderer_key, language, bot_status, is_standalone, welcome_message, error_message, server_url, api_version FROM botpress_bots";
56  
57      /**
58       * {@inheritDoc }
59       */
60      @Override
61      public void insert( BPBot bPBot, Plugin plugin )
62      {
63          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin );
64  
65          try
66          {
67              int nIndex = 1;
68              daoUtil.setString( nIndex++, bPBot.getBotKey( ) );
69              daoUtil.setString( nIndex++, bPBot.getBotPressKey( ) );
70              daoUtil.setString( nIndex++, bPBot.getName( ) );
71              daoUtil.setString( nIndex++, bPBot.getDescription( ) );
72              daoUtil.setString( nIndex++, bPBot.getAvatarUrl( ) );
73              daoUtil.setString( nIndex++, bPBot.getAvatarRendererKey( ) );
74              daoUtil.setString( nIndex++, bPBot.getLanguage( ) );
75              daoUtil.setInt( nIndex++, bPBot.getBotStatus( ) );
76              daoUtil.setInt( nIndex++, bPBot.getIsStandalone( ) );
77              daoUtil.setString( nIndex++, bPBot.getWelcomeMessage( ) );
78              daoUtil.setString( nIndex++, bPBot.getErrorMessage( ) );
79              daoUtil.setString( nIndex++, bPBot.getServerUrl( ) );
80              daoUtil.setInt( nIndex++, bPBot.getApiVersion( ) );
81  
82              daoUtil.executeUpdate( );
83  
84              if ( daoUtil.nextGeneratedKey( ) )
85              {
86                  bPBot.setId( daoUtil.getGeneratedKeyInt( 1 ) );
87              }
88          }
89          finally
90          {
91              daoUtil.free( );
92          }
93      }
94  
95      /**
96       * {@inheritDoc }
97       */
98      @Override
99      public BPBot load( int nKey, Plugin plugin )
100     {
101         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
102         daoUtil.setInt( 1, nKey );
103         daoUtil.executeQuery( );
104 
105         BPBot bPBot = null;
106 
107         if ( daoUtil.next( ) )
108         {
109             bPBot = new BPBot( );
110 
111             int nIndex = 1;
112 
113             bPBot.setId( daoUtil.getInt( nIndex++ ) );
114             bPBot.setBotKey( daoUtil.getString( nIndex++ ) );
115             bPBot.setBotPressKey( daoUtil.getString( nIndex++ ) );
116             bPBot.setName( daoUtil.getString( nIndex++ ) );
117             bPBot.setDescription( daoUtil.getString( nIndex++ ) );
118             bPBot.setAvatarUrl( daoUtil.getString( nIndex++ ) );
119             bPBot.setAvatarRendererKey( daoUtil.getString( nIndex++ ) );
120             bPBot.setLanguage( daoUtil.getString( nIndex++ ) );
121             bPBot.setBotStatus( daoUtil.getInt( nIndex++ ) );
122             bPBot.setIsStandalone( daoUtil.getInt( nIndex++ ) );
123             bPBot.setWelcomeMessage( daoUtil.getString( nIndex++ ) );
124             bPBot.setErrorMessage( daoUtil.getString( nIndex++ ) );
125             bPBot.setServerUrl( daoUtil.getString( nIndex++ ) );
126             bPBot.setApiVersion( daoUtil.getInt( nIndex++ ) );
127         }
128 
129         daoUtil.free( );
130 
131         return bPBot;
132     }
133 
134     /**
135      * {@inheritDoc }
136      */
137     @Override
138     public void delete( int nKey, Plugin plugin )
139     {
140         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
141         daoUtil.setInt( 1, nKey );
142         daoUtil.executeUpdate( );
143         daoUtil.free( );
144     }
145 
146     /**
147      * {@inheritDoc }
148      */
149     @Override
150     public void store( BPBot bPBot, Plugin plugin )
151     {
152         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
153         int nIndex = 1;
154 
155         daoUtil.setInt( nIndex++, bPBot.getId( ) );
156         daoUtil.setString( nIndex++, bPBot.getBotKey( ) );
157         daoUtil.setString( nIndex++, bPBot.getBotPressKey( ) );
158         daoUtil.setString( nIndex++, bPBot.getName( ) );
159         daoUtil.setString( nIndex++, bPBot.getDescription( ) );
160         daoUtil.setString( nIndex++, bPBot.getAvatarUrl( ) );
161         daoUtil.setString( nIndex++, bPBot.getAvatarRendererKey( ) );
162         daoUtil.setString( nIndex++, bPBot.getLanguage( ) );
163         daoUtil.setInt( nIndex++, bPBot.getBotStatus( ) );
164         daoUtil.setInt( nIndex++, bPBot.getIsStandalone( ) );
165         daoUtil.setString( nIndex++, bPBot.getWelcomeMessage( ) );
166         daoUtil.setString( nIndex++, bPBot.getErrorMessage( ) );
167         daoUtil.setString( nIndex++, bPBot.getServerUrl( ) );
168         daoUtil.setInt( nIndex++, bPBot.getApiVersion( ) );
169         daoUtil.setInt( nIndex, bPBot.getId( ) );
170 
171         daoUtil.executeUpdate( );
172         daoUtil.free( );
173     }
174 
175     /**
176      * {@inheritDoc }
177      */
178     @Override
179     public List<BPBot> selectBPBotsList( Plugin plugin )
180     {
181         List<BPBot> bPBotList = new ArrayList<BPBot>( );
182         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
183         daoUtil.executeQuery( );
184 
185         while ( daoUtil.next( ) )
186         {
187             BPBotins/botpress/business/BPBot.html#BPBot">BPBot bPBot = new BPBot( );
188             int nIndex = 1;
189 
190             bPBot.setId( daoUtil.getInt( nIndex++ ) );
191             bPBot.setBotKey( daoUtil.getString( nIndex++ ) );
192             bPBot.setBotPressKey( daoUtil.getString( nIndex++ ) );
193             bPBot.setName( daoUtil.getString( nIndex++ ) );
194             bPBot.setDescription( daoUtil.getString( nIndex++ ) );
195             bPBot.setAvatarUrl( daoUtil.getString( nIndex++ ) );
196             bPBot.setAvatarRendererKey( daoUtil.getString( nIndex++ ) );
197             bPBot.setLanguage( daoUtil.getString( nIndex++ ) );
198             bPBot.setBotStatus( daoUtil.getInt( nIndex++ ) );
199             bPBot.setIsStandalone( daoUtil.getInt( nIndex++ ) );
200             bPBot.setWelcomeMessage( daoUtil.getString( nIndex++ ) );
201             bPBot.setErrorMessage( daoUtil.getString( nIndex++ ) );
202             bPBot.setServerUrl( daoUtil.getString( nIndex++ ) );
203             bPBot.setApiVersion( daoUtil.getInt( nIndex++ ) );
204 
205             bPBotList.add( bPBot );
206         }
207 
208         daoUtil.free( );
209 
210         return bPBotList;
211     }
212 
213     /**
214      * {@inheritDoc }
215      */
216     @Override
217     public ReferenceList selectBPBotsReferenceList( Plugin plugin )
218     {
219         ReferenceList bPBotList = new ReferenceList( );
220         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
221         daoUtil.executeQuery( );
222 
223         while ( daoUtil.next( ) )
224         {
225             bPBotList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
226         }
227 
228         daoUtil.free( );
229 
230         return bPBotList;
231     }
232 }