View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.appcenter.modules.fastdeployapplication.business.fastaction;
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.List;
41  
42  /**
43   * This class provides Data Access methods for ActionHistory objects
44   */
45  public final class ActionHistoryDAO implements IActionHistoryDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_action_history ) FROM appcenter_fastdeployapplication_action_history";
49      private static final String SQL_QUERY_SELECTALL = "SELECT id_action_history, id_front_user, code, i18n_key_name, server_type, server_code, server_i18n_key_name, id_application, environment_code, action_history_data, creation_date, status FROM appcenter_fastdeployapplication_action_history";
50      private static final String SQL_QUERY_SELECT = "SELECT id_action_history, id_front_user, code, i18n_key_name, server_type, server_code, server_i18n_key_name, id_application, environment_code, action_history_data, creation_date, status FROM appcenter_fastdeployapplication_action_history WHERE id_action_history = ? ";
51      private static final String SQL_QUERY_SELECT_BY_APP_BY_ENV = "SELECT id_action_history, id_front_user, code, i18n_key_name, server_type, server_code, server_i18n_key_name, id_application, environment_code, action_history_data, creation_date, status FROM appcenter_fastdeployapplication_action_history WHERE id_application = ? AND environment_code = ? order by creation_date desc ";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO appcenter_fastdeployapplication_action_history ( id_action_history, id_front_user, code, i18n_key_name, server_type, server_code, server_i18n_key_name, id_application, environment_code, action_history_data, creation_date, status ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM appcenter_fastdeployapplication_action_history WHERE id_action_history = ? ";
54      private static final String SQL_QUERY_UPDATE = "UPDATE appcenter_fastdeployapplication_action_history SET id_front_user = ?, code = ?, i18n_key_name = ?, server_type = ?, server_code = ?, server_i18n_key_name = ?, id_application = ?, environment_code = ?, action_history_data = ?, creation_date = ?, status = ? WHERE id_action_history = ?";
55  
56      /**
57       * Generates a new primary key
58       * 
59       * @param plugin
60       *            The Plugin
61       * @return The new primary key
62       */
63      public int newPrimaryKey( Plugin plugin )
64      {
65          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
66          daoUtil.executeQuery( );
67          int nKey = 1;
68  
69          if ( daoUtil.next( ) )
70          {
71              nKey = daoUtil.getInt( 1 ) + 1;
72          }
73  
74          daoUtil.free( );
75          return nKey;
76      }
77  
78      /**
79       * {@inheritDoc }
80       */
81      @Override
82      public void insert( ActionHistory actionHistory, Plugin plugin )
83      {
84          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
85          actionHistory.setId( newPrimaryKey( plugin ) );
86          int nIndex = 1;
87  
88          daoUtil.setInt( nIndex++, actionHistory.getId( ) );
89          daoUtil.setString( nIndex++, actionHistory.getIdUserFront( ) );
90          daoUtil.setString( nIndex++, actionHistory.getCode( ) );
91          daoUtil.setString( nIndex++, actionHistory.getI18nKeyName( ) );
92          daoUtil.setString( nIndex++, actionHistory.getServerType( ) );
93          daoUtil.setString( nIndex++, actionHistory.getServerCode( ) );
94          daoUtil.setString( nIndex++, actionHistory.getServerName( ) );
95          daoUtil.setInt( nIndex++, actionHistory.getIdApplication( ) );
96          daoUtil.setString( nIndex++, actionHistory.getCodeEnvironment( ) );
97          daoUtil.setString( nIndex++, actionHistory.getActionData( ) );
98          daoUtil.setTimestamp( nIndex++, actionHistory.getCreationDate( ) );
99          daoUtil.setBoolean( nIndex++, actionHistory.getStatus( ) );
100 
101         daoUtil.executeUpdate( );
102         daoUtil.free( );
103     }
104 
105     /**
106      * {@inheritDoc }
107      */
108     @Override
109     public ActionHistory load( int nKey, Plugin plugin )
110     {
111         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
112         daoUtil.setInt( 1, nKey );
113         daoUtil.executeQuery( );
114         ActionHistory actionHistory = null;
115 
116         if ( daoUtil.next( ) )
117         {
118             actionHistory = new ActionHistory( );
119             int nIndex = 1;
120 
121             actionHistory.setId( daoUtil.getInt( nIndex++ ) );
122             actionHistory.setIdUserFront( daoUtil.getString( nIndex++ ) );
123             actionHistory.setCode( daoUtil.getString( nIndex++ ) );
124             actionHistory.setI18nKeyName( daoUtil.getString( nIndex++ ) );
125             actionHistory.setServerType( daoUtil.getString( nIndex++ ) );
126             actionHistory.setServerCode( daoUtil.getString( nIndex++ ) );
127             actionHistory.setServerName( daoUtil.getString( nIndex++ ) );
128             actionHistory.setIdApplication( daoUtil.getInt( nIndex++ ) );
129             actionHistory.setCodeEnvironment( daoUtil.getString( nIndex++ ) );
130             actionHistory.setActionData( daoUtil.getString( nIndex++ ) );
131             actionHistory.setCreationDate( daoUtil.getTimestamp( nIndex++ ) );
132             actionHistory.setStatus( daoUtil.getBoolean( nIndex++ ) );
133         }
134 
135         daoUtil.free( );
136         return actionHistory;
137     }
138 
139     /**
140      * {@inheritDoc }
141      */
142     @Override
143     public void delete( int nKey, Plugin plugin )
144     {
145         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
146         daoUtil.setInt( 1, nKey );
147         daoUtil.executeUpdate( );
148         daoUtil.free( );
149     }
150 
151     /**
152      * {@inheritDoc }
153      */
154     @Override
155     public void store( ActionHistory actionHistory, Plugin plugin )
156     {
157         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
158         int nIndex = 1;
159 
160         daoUtil.setString( nIndex++, actionHistory.getIdUserFront( ) );
161         daoUtil.setString( nIndex++, actionHistory.getCode( ) );
162         daoUtil.setString( nIndex++, actionHistory.getI18nKeyName( ) );
163         daoUtil.setString( nIndex++, actionHistory.getServerType( ) );
164         daoUtil.setString( nIndex++, actionHistory.getServerCode( ) );
165         daoUtil.setString( nIndex++, actionHistory.getServerName( ) );
166         daoUtil.setInt( nIndex++, actionHistory.getIdApplication( ) );
167         daoUtil.setString( nIndex++, actionHistory.getCodeEnvironment( ) );
168         daoUtil.setString( nIndex++, actionHistory.getActionData( ) );
169         daoUtil.setTimestamp( nIndex++, actionHistory.getCreationDate( ) );
170         daoUtil.setBoolean( nIndex++, actionHistory.getStatus( ) );
171 
172         daoUtil.setInt( nIndex, actionHistory.getId( ) );
173 
174         daoUtil.executeUpdate( );
175         daoUtil.free( );
176     }
177 
178     /**
179      * {@inheritDoc }
180      */
181     @Override
182     public List<ActionHistory> selectActionHistorysList( Plugin plugin )
183     {
184         List<ActionHistory> actionHistoryList = new ArrayList<>( );
185         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
186         daoUtil.executeQuery( );
187 
188         while ( daoUtil.next( ) )
189         {
190             ActionHistorycenter/modules/fastdeployapplication/business/fastaction/ActionHistory.html#ActionHistory">ActionHistory actionHistory = new ActionHistory( );
191             int nIndex = 1;
192 
193             actionHistory.setId( daoUtil.getInt( nIndex++ ) );
194             actionHistory.setIdUserFront( daoUtil.getString( nIndex++ ) );
195             actionHistory.setCode( daoUtil.getString( nIndex++ ) );
196             actionHistory.setI18nKeyName( daoUtil.getString( nIndex++ ) );
197             actionHistory.setServerType( daoUtil.getString( nIndex++ ) );
198             actionHistory.setServerCode( daoUtil.getString( nIndex++ ) );
199             actionHistory.setServerName( daoUtil.getString( nIndex++ ) );
200             actionHistory.setIdApplication( daoUtil.getInt( nIndex++ ) );
201             actionHistory.setCodeEnvironment( daoUtil.getString( nIndex++ ) );
202             actionHistory.setActionData( daoUtil.getString( nIndex++ ) );
203             actionHistory.setCreationDate( daoUtil.getTimestamp( nIndex++ ) );
204             actionHistory.setStatus( daoUtil.getBoolean( nIndex++ ) );
205 
206             actionHistoryList.add( actionHistory );
207         }
208 
209         daoUtil.free( );
210         return actionHistoryList;
211     }
212 
213     /**
214      * {@inheritDoc }
215      */
216     @Override
217     public List<ActionHistory> selectActionHistorysListByAppByEnv( int nIdApplication, String strCodeEnvironment, Plugin plugin )
218     {
219         List<ActionHistory> actionHistoryList = new ArrayList<>( );
220         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_APP_BY_ENV, plugin );
221         daoUtil.setInt( 1, nIdApplication );
222         daoUtil.setString( 2, strCodeEnvironment );
223         daoUtil.executeQuery( );
224 
225         while ( daoUtil.next( ) )
226         {
227             ActionHistorycenter/modules/fastdeployapplication/business/fastaction/ActionHistory.html#ActionHistory">ActionHistory actionHistory = new ActionHistory( );
228             int nIndex = 1;
229 
230             actionHistory.setId( daoUtil.getInt( nIndex++ ) );
231             actionHistory.setIdUserFront( daoUtil.getString( nIndex++ ) );
232             actionHistory.setCode( daoUtil.getString( nIndex++ ) );
233             actionHistory.setI18nKeyName( daoUtil.getString( nIndex++ ) );
234             actionHistory.setServerType( daoUtil.getString( nIndex++ ) );
235             actionHistory.setServerCode( daoUtil.getString( nIndex++ ) );
236             actionHistory.setServerName( daoUtil.getString( nIndex++ ) );
237             actionHistory.setIdApplication( daoUtil.getInt( nIndex++ ) );
238             actionHistory.setCodeEnvironment( daoUtil.getString( nIndex++ ) );
239             actionHistory.setActionData( daoUtil.getString( nIndex++ ) );
240             actionHistory.setCreationDate( daoUtil.getTimestamp( nIndex++ ) );
241             actionHistory.setStatus( daoUtil.getBoolean( nIndex++ ) );
242 
243             actionHistoryList.add( actionHistory );
244         }
245 
246         daoUtil.free( );
247         return actionHistoryList;
248     }
249 }