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.phraseanet.business.account;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.util.sql.DAOUtil;
38
39 import java.util.ArrayList;
40 import java.util.List;
41
42
43
44
45
46 public final class AccountDAO implements IAccountDAO
47 {
48
49 private static final String SQL_QUERY_NEW_PK = "SELECT max( id_account ) FROM phraseanet_account";
50 private static final String SQL_QUERY_SELECT = "SELECT id_account, name, description, access_url, customer_id, customer_secret, autthorize_end_point, access_end_point, phraseanet_id, password, token FROM phraseanet_account WHERE id_account = ?";
51 private static final String SQL_QUERY_SELECT_TOKEN = "SELECT token FROM phraseanet_account WHERE id_account = ?";
52 private static final String SQL_QUERY_INSERT = "INSERT INTO phraseanet_account ( id_account, name, description, access_url, customer_id, customer_secret, autthorize_end_point, access_end_point, phraseanet_id, password, token ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ";
53 private static final String SQL_QUERY_DELETE = "DELETE FROM phraseanet_account WHERE id_account = ? ";
54 private static final String SQL_QUERY_UPDATE = "UPDATE phraseanet_account SET name = ?, description = ?, access_url = ?, customer_id = ?, customer_secret = ?, autthorize_end_point = ?, access_end_point = ?, phraseanet_id = ?, password = ?, token = ? WHERE id_account = ?";
55 private static final String SQL_QUERY_UPDATE_TOKEN = "UPDATE phraseanet_account SET token = ? WHERE id_account = ?";
56 private static final String SQL_QUERY_SELECTALL = "SELECT id_account, name, description, access_url, customer_id, customer_secret, autthorize_end_point, access_end_point, phraseanet_id, password, token FROM phraseanet_account";
57
58
59
60
61
62
63 public int newPrimaryKey( Plugin plugin )
64 {
65 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
66 daoUtil.executeQuery( );
67
68 int nKey;
69
70 if ( !daoUtil.next( ) )
71 {
72
73 nKey = 1;
74 }
75
76 nKey = daoUtil.getInt( 1 ) + 1;
77 daoUtil.free( );
78
79 return nKey;
80 }
81
82
83
84
85
86
87 public void insert( Account account, Plugin plugin )
88 {
89 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
90
91 account.setId( newPrimaryKey( plugin ) );
92
93 daoUtil.setInt( 1, account.getId( ) );
94 daoUtil.setString( 2, account.getName( ) );
95 daoUtil.setString( 3, account.getDescription( ) );
96 daoUtil.setString( 4, account.getAccessURL( ) );
97 daoUtil.setString( 5, account.getCustomerId( ) );
98 daoUtil.setString( 6, account.getCustomerSecret( ) );
99 daoUtil.setString( 7, account.getAuthorizeEndPoint( ) );
100 daoUtil.setString( 8, account.getAccessEndPoint( ) );
101 daoUtil.setString( 9, account.getPhraseanetId( ) );
102 daoUtil.setString( 10, account.getPassword( ) );
103 daoUtil.setString( 11, account.getToken( ) );
104
105 daoUtil.executeUpdate( );
106 daoUtil.free( );
107 }
108
109
110
111
112
113
114
115 public Account load( int nId, Plugin plugin )
116 {
117 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
118 daoUtil.setInt( 1, nId );
119 daoUtil.executeQuery( );
120
121 Account account = null;
122
123 if ( daoUtil.next( ) )
124 {
125 account = new Account( );
126
127 account.setId( daoUtil.getInt( 1 ) );
128 account.setName( daoUtil.getString( 2 ) );
129 account.setDescription( daoUtil.getString( 3 ) );
130 account.setAccessURL( daoUtil.getString( 4 ) );
131 account.setCustomerId( daoUtil.getString( 5 ) );
132 account.setCustomerSecret( daoUtil.getString( 6 ) );
133 account.setAuthorizeEndPoint( daoUtil.getString( 7 ) );
134 account.setAccessEndPoint( daoUtil.getString( 8 ) );
135 account.setPhraseanetId( daoUtil.getString( 9 ) );
136 account.setPassword( daoUtil.getString( 10 ) );
137 account.setToken( daoUtil.getString( 11 ) );
138 }
139
140 daoUtil.free( );
141
142 return account;
143 }
144
145
146
147
148
149
150 public void delete( int nAccountId, Plugin plugin )
151 {
152 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
153 daoUtil.setInt( 1, nAccountId );
154 daoUtil.executeUpdate( );
155 daoUtil.free( );
156 }
157
158
159
160
161
162
163 public void store( Account account, Plugin plugin )
164 {
165 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
166
167 daoUtil.setString( 1, account.getName( ) );
168 daoUtil.setString( 2, account.getDescription( ) );
169 daoUtil.setString( 3, account.getAccessURL( ) );
170 daoUtil.setString( 4, account.getCustomerId( ) );
171 daoUtil.setString( 5, account.getCustomerSecret( ) );
172 daoUtil.setString( 6, account.getAuthorizeEndPoint( ) );
173 daoUtil.setString( 7, account.getAccessEndPoint( ) );
174 daoUtil.setString( 8, account.getPhraseanetId( ) );
175 daoUtil.setString( 9, account.getPassword( ) );
176 daoUtil.setString( 10, account.getToken( ) );
177 daoUtil.setInt( 11, account.getId( ) );
178
179 daoUtil.executeUpdate( );
180 daoUtil.free( );
181 }
182
183
184
185
186
187
188 public void updateToken( Account account, Plugin plugin )
189 {
190 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_TOKEN, plugin );
191
192 daoUtil.setString( 1, account.getToken( ) );
193 daoUtil.setInt( 2, account.getId( ) );
194
195 daoUtil.executeUpdate( );
196 daoUtil.free( );
197 }
198
199
200
201
202
203
204 public List<Account> selectAccountsList( Plugin plugin )
205 {
206 List<Account> accountList = new ArrayList<Account>( );
207 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
208 daoUtil.executeQuery( );
209
210 while ( daoUtil.next( ) )
211 {
212 Account account = new Account( );
213
214 account.setId( daoUtil.getInt( 1 ) );
215 account.setName( daoUtil.getString( 2 ) );
216 account.setDescription( daoUtil.getString( 3 ) );
217 account.setAccessURL( daoUtil.getString( 4 ) );
218 account.setCustomerId( daoUtil.getString( 5 ) );
219 account.setCustomerSecret( daoUtil.getString( 6 ) );
220 account.setAuthorizeEndPoint( daoUtil.getString( 7 ) );
221 account.setAccessEndPoint( daoUtil.getString( 8 ) );
222 account.setPhraseanetId( daoUtil.getString( 9 ) );
223 account.setPassword( daoUtil.getString( 10 ) );
224 account.setToken( daoUtil.getString( 11 ) );
225
226 accountList.add( account );
227 }
228
229 daoUtil.free( );
230
231 return accountList;
232 }
233 }