View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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  
35  /*
36   * Copyright (c) 2002-2020, Mairie de Paris
37   * All rights reserved.
38   *
39   * Redistribution and use in source and binary forms, with or without
40   * modification, are permitted provided that the following conditions
41   * are met:
42   *
43   *  1. Redistributions of source code must retain the above copyright notice
44   *     and the following disclaimer.
45   *
46   *  2. Redistributions in binary form must reproduce the above copyright notice
47   *     and the following disclaimer in the documentation and/or other materials
48   *     provided with the distribution.
49   *
50   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
51   *     contributors may be used to endorse or promote products derived from
52   *     this software without specific prior written permission.
53   *
54   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
55   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
58   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64   * POSSIBILITY OF SUCH DAMAGE.
65   *
66   * License 1.0
67   */
68  
69  package fr.paris.lutece.plugins.releaser.business;
70  
71  import fr.paris.lutece.portal.service.plugin.Plugin;
72  import fr.paris.lutece.util.ReferenceList;
73  import fr.paris.lutece.util.sql.DAOUtil;
74  import java.sql.Statement;
75  
76  import java.util.ArrayList;
77  import java.util.List;
78  
79  /**
80   * This class provides Data Access methods for WorkflowDeployContext objects
81   */
82  public final class WorkflowContextHistoryDAO implements IWorkflowContextHistoryDAO
83  {
84      // Constants
85      private static final String SQL_QUERY_SELECT = "SELECT id_wf_context,  date_begin, date_end, artifact_id,data, user_name FROM releaser_workflow_context_history WHERE id_wf_context = ?";
86      private static final String SQL_QUERY_INSERT = "INSERT INTO releaser_workflow_context_history (  date_begin, date_end, artifact_id,data, user_name ) VALUES ( ?, ?, ?, ?, ?) ";
87      private static final String SQL_QUERY_DELETE = "DELETE FROM releaser_workflow_context_history WHERE id_wf_context = ? ";
88      private static final String SQL_QUERY_UPDATE = "UPDATE releaser_workflow_context_history SET id_wf_context = ?, date_begin = ?, date_end = ?, artifact_id= ?,  data = ?, user_name = ? WHERE id_wf_context = ?";
89      private static final String SQL_QUERY_SELECTALL = "SELECT id_wf_context,  date_begin, date_end, artifact_id ,data, user_name FROM releaser_workflow_context_history";
90      private static final String FILTER_BY_ARTIFACT_ID = " WHERE artifact_id= ?";
91  
92      /**
93       * {@inheritDoc }
94       */
95      @Override
96      public void insert( WorkflowContextHistory workflowContext, Plugin plugin )
97      {
98          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
99          {
100             int nIndex = 1;
101             daoUtil.setTimestamp( nIndex++, workflowContext.getDateBegin( ) );
102             daoUtil.setTimestamp( nIndex++, workflowContext.getDateEnd( ) );
103             daoUtil.setString( nIndex++, workflowContext.getArtifactId( ) );
104             daoUtil.setString( nIndex++, workflowContext.getData( ) );
105             daoUtil.setString( nIndex++, workflowContext.getUser( ) );
106 
107             daoUtil.executeUpdate( );
108             if ( daoUtil.nextGeneratedKey( ) )
109             {
110                 workflowContext.setId( daoUtil.getGeneratedKeyInt( 1 ) );
111             }
112         }
113 
114     }
115 
116     /**
117      * {@inheritDoc }
118      */
119     @Override
120     public WorkflowContextHistory load( int nKey, Plugin plugin )
121     {
122         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
123         {
124             daoUtil.setInt( 1, nKey );
125             daoUtil.executeQuery( );
126             WorkflowContextHistory workflowContext = null;
127 
128             if ( daoUtil.next( ) )
129             {
130                 workflowContext = new WorkflowContextHistory( );
131                 int nIndex = 1;
132 
133                 workflowContext.setId( daoUtil.getInt( nIndex++ ) );
134 
135                 workflowContext.setDateBegin( daoUtil.getTimestamp( nIndex++ ) );
136                 workflowContext.setDateEnd( daoUtil.getTimestamp( nIndex++ ) );
137                 workflowContext.setArtifactId( daoUtil.getString( nIndex++ ) );
138                 workflowContext.setData( daoUtil.getString( nIndex++ ) );
139                 workflowContext.setUser( daoUtil.getString( nIndex ) );
140             }
141 
142             daoUtil.free( );
143             return workflowContext;
144         }
145     }
146 
147     /**
148      * {@inheritDoc }
149      */
150     @Override
151     public void delete( int nKey, Plugin plugin )
152     {
153         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
154         {
155             daoUtil.setInt( 1, nKey );
156             daoUtil.executeUpdate( );
157             daoUtil.free( );
158         }
159     }
160 
161     /**
162      * {@inheritDoc }
163      */
164     @Override
165     public void store( WorkflowContextHistory workflowContext, Plugin plugin )
166     {
167         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
168         {
169             int nIndex = 1;
170 
171             daoUtil.setInt( nIndex++, workflowContext.getId( ) );
172 
173             daoUtil.setTimestamp( nIndex++, workflowContext.getDateBegin( ) );
174             daoUtil.setTimestamp( nIndex++, workflowContext.getDateEnd( ) );
175             daoUtil.setString( nIndex++, workflowContext.getArtifactId( ) );
176             daoUtil.setString( nIndex++, workflowContext.getData( ) );
177             daoUtil.setString( nIndex++, workflowContext.getUser( ) );
178             daoUtil.setInt( nIndex, workflowContext.getId( ) );
179 
180             daoUtil.executeUpdate( );
181             daoUtil.free( );
182         }
183     }
184 
185     /**
186      * {@inheritDoc }
187      */
188     @Override
189     public List<WorkflowContextHistory> selectWorkflowDeployContextsList( Plugin plugin )
190     {
191         List<WorkflowContextHistory> workflowContextList = new ArrayList<>( );
192         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
193         {
194             daoUtil.executeQuery( );
195 
196             while ( daoUtil.next( ) )
197             {
198                 WorkflowContextHistorylowContextHistory.html#WorkflowContextHistory">WorkflowContextHistory workflowContext = new WorkflowContextHistory( );
199                 int nIndex = 1;
200 
201                 workflowContext.setId( daoUtil.getInt( nIndex++ ) );
202                 workflowContext.setDateBegin( daoUtil.getTimestamp( nIndex++ ) );
203                 workflowContext.setDateEnd( daoUtil.getTimestamp( nIndex++ ) );
204                 workflowContext.setArtifactId( daoUtil.getString( nIndex++ ) );
205                 workflowContext.setData( daoUtil.getString( nIndex++ ) );
206                 workflowContext.setUser( daoUtil.getString( nIndex ) );
207 
208                 workflowContextList.add( workflowContext );
209             }
210 
211             daoUtil.free( );
212             return workflowContextList;
213         }
214     }
215 
216     /**
217      * {@inheritDoc }
218      */
219     @Override
220     public List<WorkflowContextHistory> selectWorkflowDeployContextsListByArtifactId( Plugin plugin, String strArtifactId )
221     {
222         List<WorkflowContextHistory> workflowContextList = new ArrayList<>( );
223         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL + FILTER_BY_ARTIFACT_ID, plugin ) )
224         {
225 
226             daoUtil.setString( 1, strArtifactId );
227             daoUtil.executeQuery( );
228             while ( daoUtil.next( ) )
229             {
230                 WorkflowContextHistorylowContextHistory.html#WorkflowContextHistory">WorkflowContextHistory workflowContext = new WorkflowContextHistory( );
231                 int nIndex = 1;
232 
233                 workflowContext.setId( daoUtil.getInt( nIndex++ ) );
234                 workflowContext.setDateBegin( daoUtil.getTimestamp( nIndex++ ) );
235                 workflowContext.setDateEnd( daoUtil.getTimestamp( nIndex++ ) );
236                 workflowContext.setArtifactId( daoUtil.getString( nIndex++ ) );
237                 workflowContext.setData( daoUtil.getString( nIndex++ ) );
238                 workflowContext.setUser( daoUtil.getString( nIndex ) );
239 
240                 workflowContextList.add( workflowContext );
241             }
242 
243             daoUtil.free( );
244             return workflowContextList;
245         }
246     }
247 
248 }