1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
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
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
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
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
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 }