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.workflow.business.resource;
35  
36  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
37  import fr.paris.lutece.plugins.workflowcore.business.resource.IResourceUserHistoryDAO;
38  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceUserHistory;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  /**
42   * This class provides Data Access methods for ResourceUserHistory objects
43   */
44  public final class ResourceUserHistoryDAO implements IResourceUserHistoryDAO
45  {
46      // Constants
47      private static final String SQL_QUERY_SELECT = "SELECT  id_history, user_access_code, email, first_name, last_name, realm FROM workflow_resource_user_history WHERE id_history = ?";
48      private static final String SQL_QUERY_INSERT = "INSERT INTO workflow_resource_user_history ( id_history, user_access_code, email, first_name, last_name, realm ) VALUES ( ?, ?, ?, ?, ?, ? ) ";
49      private static final String SQL_QUERY_DELETE = "DELETE FROM workflow_resource_user_history WHERE id_history = ? ";
50      private static final String SQL_QUERY_UPDATE = "UPDATE workflow_resource_user_history SET user_access_code = ?, email = ?, first_name = ?, last_name = ?, realm = ? WHERE id_history = ?";
51  
52      /**
53       * {@inheritDoc }
54       */
55      @Override
56      public void insert( ResourceUserHistory resourceUserHistory )
57      {
58          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, WorkflowUtils.getPlugin( ) ) )
59          {
60              int nIndex = 1;
61              daoUtil.setInt( nIndex++, resourceUserHistory.getIdHistory( ) );
62              daoUtil.setString( nIndex++, resourceUserHistory.getUserAccessCode( ) );
63              daoUtil.setString( nIndex++, resourceUserHistory.getEmail( ) );
64              daoUtil.setString( nIndex++, resourceUserHistory.getFirstName( ) );
65              daoUtil.setString( nIndex++, resourceUserHistory.getLastName( ) );
66              daoUtil.setString( nIndex++, resourceUserHistory.getRealm( ) );
67  
68              daoUtil.executeUpdate( );
69          }
70  
71      }
72  
73      /**
74       * {@inheritDoc }
75       */
76      @Override
77      public ResourceUserHistory load( int nKey )
78      {
79          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, WorkflowUtils.getPlugin( ) ) )
80          {
81              daoUtil.setInt( 1, nKey );
82              daoUtil.executeQuery( );
83              ResourceUserHistory resourceUserHistory = null;
84  
85              if ( daoUtil.next( ) )
86              {
87                  resourceUserHistory = new ResourceUserHistory( );
88                  int nIndex = 1;
89                  resourceUserHistory.setIdHistory( daoUtil.getInt( nIndex++ ) );
90                  resourceUserHistory.setUserAccessCode( daoUtil.getString( nIndex++ ) );
91                  resourceUserHistory.setEmail( daoUtil.getString( nIndex++ ) );
92                  resourceUserHistory.setFirstName( daoUtil.getString( nIndex++ ) );
93                  resourceUserHistory.setLastName( daoUtil.getString( nIndex++ ) );
94                  resourceUserHistory.setRealm( daoUtil.getString( nIndex ) );
95              }
96  
97              daoUtil.free( );
98              return resourceUserHistory;
99          }
100     }
101 
102     /**
103      * {@inheritDoc }
104      */
105     @Override
106     public void delete( int nKey )
107     {
108         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, WorkflowUtils.getPlugin( ) ) )
109         {
110             daoUtil.setInt( 1, nKey );
111             daoUtil.executeUpdate( );
112             daoUtil.free( );
113         }
114     }
115 
116     @Override
117     public void store( ResourceUserHistory resourceUserHistory )
118     {
119         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, WorkflowUtils.getPlugin( ) ) )
120         {
121             int nPos = 0;
122 
123             daoUtil.setString( ++nPos, resourceUserHistory.getUserAccessCode( ) );
124             daoUtil.setString( ++nPos, resourceUserHistory.getEmail( ) );
125             daoUtil.setString( ++nPos, resourceUserHistory.getFirstName( ) );
126             daoUtil.setString( ++nPos, resourceUserHistory.getLastName( ) );
127             daoUtil.setString( ++nPos, resourceUserHistory.getRealm( ) );
128 
129             daoUtil.setInt( ++nPos, resourceUserHistory.getIdHistory( ) );
130 
131             daoUtil.executeUpdate( );
132         }
133     }
134 }