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.oauth.business;
35
36 import fr.paris.lutece.plugins.mylutece.modules.oauth.authentication.OAuthAuthentication;
37 import fr.paris.lutece.portal.service.plugin.Plugin;
38 import fr.paris.lutece.util.sql.DAOUtil;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43
44
45
46
47
48 public class OAuthAuthenticationDAO implements IOAuthAuthenticationDAO
49 {
50 private static final String SQL_QUERY_DELETE = "DELETE FROM mylutece_oauth_authentication WHERE auth_name = ? ";
51 private static final String SQL_QUERY_INSERT = "INSERT INTO mylutece_oauth_authentication " +
52 "(auth_name, auth_service_name, auth_icon_url, request_token_url, access_token_url, authorize_url, consumer_key, consumer_secret, credential_url, credential_format ) " +
53 " VALUES(?,?,?,?,?,?,?,?,?,?)";
54 private static final String SQL_QUERY_UPDATE = "UPDATE mylutece_oauth_authentication SET " +
55 "auth_service_name = ?, auth_icon_url = ?, request_token_url = ?, access_token_url = ?, authorize_url = ?, consumer_key = ?, consumer_secret = ?, credential_url = ?, credential_format = ?" +
56 "WHERE auth_name = ?";
57 private static final String SQL_QUERY_SELECT = "SELECT auth_name, auth_service_name, auth_icon_url, request_token_url, " +
58 "access_token_url, authorize_url, consumer_key, consumer_secret, credential_url, credential_format FROM mylutece_oauth_authentication ";
59 private static final String SQL_QUERY_SELECT_ALL = SQL_QUERY_SELECT;
60 private static final String SQL_QUERY_SELECT_BY_PRIMARY_KEY = SQL_QUERY_SELECT + " WHERE auth_name = ?";
61
62
63
64
65
66 public void delete( String strIdAuthentication, Plugin plugin )
67 {
68 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
69
70 daoUtil.setString( 1, strIdAuthentication );
71
72 daoUtil.executeUpdate( );
73
74 daoUtil.free( );
75 }
76
77
78
79
80
81 public void insert( OAuthAuthentication authentication, Plugin plugin )
82 {
83 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
84
85 daoUtil.setString( 1, authentication.getName( ) );
86 setInsertOrUpdateValues( 2, authentication, daoUtil );
87
88 daoUtil.executeUpdate( );
89
90 daoUtil.free( );
91 }
92
93
94
95
96
97 public OAuthAuthentication load( String strIdAuthentication, Plugin plugin )
98 {
99 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_PRIMARY_KEY, plugin );
100
101 daoUtil.setString( 1, strIdAuthentication );
102
103 daoUtil.executeQuery( );
104
105 OAuthAuthentication authAuthentication = null;
106
107 if ( daoUtil.next( ) )
108 {
109 authAuthentication = new OAuthAuthentication( );
110 load( authAuthentication, daoUtil );
111 }
112
113 daoUtil.free( );
114
115 return authAuthentication;
116 }
117
118
119
120
121
122 public List<OAuthAuthentication> selectListAuthentication( Plugin plugin )
123 {
124 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin );
125
126 daoUtil.executeQuery( );
127
128 List<OAuthAuthentication> listAuthentication = new ArrayList<OAuthAuthentication>( );
129
130 while ( daoUtil.next( ) )
131 {
132 OAuthAuthentication authentication = new OAuthAuthentication( );
133 load( authentication, daoUtil );
134 listAuthentication.add( authentication );
135 }
136
137 daoUtil.free( );
138
139 return listAuthentication;
140 }
141
142
143
144
145
146 public void store( OAuthAuthentication authentication, Plugin plugin )
147 {
148 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
149
150 int nIndex = setInsertOrUpdateValues( 1, authentication, daoUtil );
151 daoUtil.setString( nIndex, authentication.getName( ) );
152
153 daoUtil.executeUpdate( );
154
155 daoUtil.free( );
156 }
157
158
159
160
161
162
163
164
165 private int setInsertOrUpdateValues( int nStartIndex, OAuthAuthentication authAuthentication, DAOUtil daoUtil )
166 {
167 int nIndex = nStartIndex;
168
169
170 daoUtil.setString( nIndex++, authAuthentication.getAuthServiceName( ) );
171 daoUtil.setString( nIndex++, authAuthentication.getIconUrl( ) );
172 daoUtil.setString( nIndex++, authAuthentication.getRequestTokenEndpointUrl( ) );
173 daoUtil.setString( nIndex++, authAuthentication.getAccessTokenEndpointUrl( ) );
174 daoUtil.setString( nIndex++, authAuthentication.getAuthorizeWebsiteUrl( ) );
175 daoUtil.setString( nIndex++, authAuthentication.getConsumerKey( ) );
176 daoUtil.setString( nIndex++, authAuthentication.getConsumerSecret( ) );
177 daoUtil.setString( nIndex++, authAuthentication.getCredentialUrl( ) );
178 daoUtil.setString( nIndex++, authAuthentication.getCredentialFormat( ) );
179
180 return nIndex;
181 }
182
183
184
185
186
187
188 private void load( OAuthAuthentication authAuthentication, DAOUtil daoUtil )
189 {
190 int nIndex = 1;
191
192 authAuthentication.setName( daoUtil.getString( nIndex++ ) );
193 authAuthentication.setAuthServiceName( daoUtil.getString( nIndex++ ) );
194 authAuthentication.setIconUrl( daoUtil.getString( nIndex++ ) );
195 authAuthentication.setRequestTokenEndpointUrl( daoUtil.getString( nIndex++ ) );
196 authAuthentication.setAccessTokenEndpointUrl( daoUtil.getString( nIndex++ ) );
197 authAuthentication.setAuthorizeWebsiteUrl( daoUtil.getString( nIndex++ ) );
198 authAuthentication.setConsumerKey( daoUtil.getString( nIndex++ ) );
199 authAuthentication.setConsumerSecret( daoUtil.getString( nIndex++ ) );
200 authAuthentication.setCredentialUrl( daoUtil.getString( nIndex++ ) );
201 authAuthentication.setCredentialFormat( daoUtil.getString( nIndex++ ) );
202 }
203 }