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  package fr.paris.lutece.plugins.mylutece.modules.database.authentication.business.key;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  /**
40   *
41   * DatabaseUserKeyDAO
42   *
43   */
44  public class DatabaseUserKeyDAO implements IDatabaseUserKeyDAO
45  {
46      private static final String SQL_QUERY_SELECT = " SELECT mylutece_database_user_key, mylutece_database_user_id FROM mylutece_database_key WHERE mylutece_database_user_key = ? ";
47      private static final String SQL_QUERY_INSERT = " INSERT INTO mylutece_database_key (mylutece_database_user_key, mylutece_database_user_id) VALUES ( ?,? ) ";
48      private static final String SQL_QUERY_DELETE = " DELETE FROM mylutece_database_key ";
49      private static final String SQL_WHERE = " WHERE ";
50      private static final String SQL_USER_KEY = " mylutece_database_user_key = ? ";
51      private static final String SQL_USER_ID = " mylutece_database_user_id = ? ";
52      private static final String SQL_QUERY_SELECT_BY_LOGIN = " SELECT mdk.mylutece_database_user_key, mdk.mylutece_database_user_id FROM mylutece_database_key mdk LEFT JOIN mylutece_database_user mdu ON (mdu.mylutece_database_user_id = mdk.mylutece_database_user_id) WHERE mdu.login = ? ";
53  
54      /**
55       * {@inheritDoc}
56       */
57      public void insert( DatabaseUserKey userKey, Plugin plugin )
58      {
59          int nIndex = 0;
60          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
61          {
62              daoUtil.setString( ++nIndex, userKey.getKey( ) );
63              daoUtil.setInt( ++nIndex, userKey.getUserId( ) );
64  
65              daoUtil.executeUpdate( );
66          }
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      public DatabaseUserKey load( String strKey, Plugin plugin )
73      {
74          DatabaseUserKey userKey = null;
75  
76          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
77          {
78              daoUtil.setString( 1, strKey );
79              daoUtil.executeQuery( );
80  
81              if ( daoUtil.next( ) )
82              {
83                  int nIndex = 0;
84                  userKey = new DatabaseUserKey( );
85                  userKey.setKey( daoUtil.getString( ++nIndex ) );
86                  userKey.setUserId( daoUtil.getInt( ++nIndex ) );
87              }
88          }
89          return userKey;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      public void delete( String strKey, Plugin plugin )
96      {
97          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE + SQL_WHERE + SQL_USER_KEY, plugin ) )
98          {
99              daoUtil.setString( 1, strKey );
100             daoUtil.executeUpdate( );
101         }
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     public void deleteByIdUser( int nUserId, Plugin plugin )
108     {
109         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE + SQL_WHERE + SQL_USER_ID, plugin ) )
110         {
111             daoUtil.setInt( 1, nUserId );
112             daoUtil.executeUpdate( );
113         }
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     public DatabaseUserKey selectKeyByLogin( String login, Plugin plugin )
120     {
121         DatabaseUserKey userKey = null;
122 
123         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_LOGIN, plugin ) )
124         {
125             daoUtil.setString( 1, login );
126             daoUtil.executeQuery( );
127 
128             if ( daoUtil.next( ) )
129             {
130                 int nIndex = 0;
131                 userKey = new DatabaseUserKey( );
132                 userKey.setKey( daoUtil.getString( ++nIndex ) );
133                 userKey.setUserId( daoUtil.getInt( ++nIndex ) );
134             }
135         }
136         return userKey;
137     }
138 }