1
2
3
4
5
6 package fr.ifremer.quadrige2.core.dao.administration.user;
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 import com.google.common.base.Joiner;
32 import com.google.common.base.Preconditions;
33 import com.google.common.collect.Lists;
34 import com.google.common.collect.Maps;
35 import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
36 import fr.ifremer.quadrige2.core.dao.referential.StatusCode;
37 import fr.ifremer.quadrige2.core.dao.technical.jdbc.OptionalDataSourceJdbcDaoSupport;
38 import fr.ifremer.quadrige2.core.vo.administration.program.ProgramVO;
39 import fr.ifremer.quadrige2.core.vo.administration.user.LightQuserVO;
40 import fr.ifremer.quadrige2.core.vo.administration.user.PrivilegeVO;
41 import fr.ifremer.quadrige2.core.vo.administration.user.QuserVO;
42 import org.apache.commons.io.IOUtils;
43 import org.apache.commons.lang3.StringUtils;
44 import org.springframework.beans.factory.InitializingBean;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.context.annotation.Lazy;
47 import org.springframework.dao.DataAccessException;
48 import org.springframework.jdbc.core.ResultSetExtractor;
49 import org.springframework.jdbc.core.RowCallbackHandler;
50 import org.springframework.jdbc.core.RowMapper;
51 import org.springframework.stereotype.Repository;
52
53 import javax.annotation.Resource;
54 import javax.sql.DataSource;
55 import java.io.IOException;
56 import java.io.InputStream;
57 import java.math.BigDecimal;
58 import java.sql.ResultSet;
59 import java.sql.SQLException;
60 import java.util.List;
61 import java.util.Map;
62 import java.util.Properties;
63
64
65
66
67
68
69
70
71 @Repository("quserJdbcDao")
72 @Lazy
73 public class QuserJdbcDaoImpl
74 extends OptionalDataSourceJdbcDaoSupport
75 implements QuserJdbcDao, InitializingBean {
76
77 private final static String QUERIES_FILE_PATH = "queries.jdbc.xml";
78
79 @Resource(name = "queriesJdbcProperties")
80 protected Properties queriesJdbcProperties;
81
82 protected Properties connectionProperties;
83
84
85
86
87
88
89
90 @Autowired
91 public QuserJdbcDaoImpl(DataSource dataSource) {
92 super(dataSource);
93 }
94
95
96
97
98 public QuserJdbcDaoImpl() {
99 this((Properties) null);
100 }
101
102
103
104
105
106
107
108 public QuserJdbcDaoImpl(Properties connectionProperties) {
109 super();
110 this.connectionProperties = connectionProperties;
111
112
113 Properties properties = new Properties();
114 InputStream is = null;
115 try {
116 is = getClass().getClassLoader().getResourceAsStream(QUERIES_FILE_PATH);
117 properties.loadFromXML(is);
118
119 this.queriesJdbcProperties = properties;
120 } catch (IOException e) {
121 throw new Quadrige2TechnicalException(
122 String.format("Unable to read file [%s] from classpath", QUERIES_FILE_PATH),
123 e);
124 } finally {
125 IOUtils.closeQuietly(is);
126 }
127
128
129 checkAllQueriesExists();
130 }
131
132
133 @Override
134 public void afterPropertiesSet() throws Exception {
135
136 checkAllQueriesExists();
137 }
138
139
140 @Override
141 public Integer getUserIdByUsername(String username) {
142 return getUserIdByUsername(connectionProperties, username);
143 }
144
145
146 @Override
147 public Integer getUserIdByUsername(Properties connectionProperties, String username) {
148
149 String sql = queriesJdbcProperties.getProperty("quserIdByLoginAndStatus");
150
151 Map<String, Object> paramMap = Maps.newHashMap();
152 paramMap.put("username", username);
153 paramMap.put("statusCd", StatusCode.ENABLE.getValue());
154
155 return query(connectionProperties, sql, paramMap, new ResultSetExtractor<Integer>() {
156 @Override
157 public Integer extractData(ResultSet rs) throws SQLException, DataAccessException {
158 Object value = rs.getObject(1);
159 if (value == null) {
160 return null;
161 }
162
163
164 if (value instanceof BigDecimal) {
165 return ((BigDecimal) value).intValue();
166 }
167
168
169 return Integer.parseInt(value.toString());
170 }
171 });
172 }
173
174
175 @Override
176 public LightQuserVO getLightUserById(int personId) {
177 return getLightUserById(connectionProperties, personId);
178 }
179
180
181 @Override
182 public LightQuserVO getLightUserById(Properties connectionProperties, int quserId) {
183
184 String sql = queriesJdbcProperties.getProperty("lightQuserById");
185
186 Map<String, Object> paramMap = Maps.newHashMap();
187 paramMap.put("quserId", quserId);
188
189 return query(connectionProperties, sql, paramMap, new ResultSetExtractor<LightQuserVO>() {
190 @Override
191 public LightQuserVO extractData(ResultSet rs) throws SQLException, DataAccessException {
192 return toLightQuserVO(rs);
193 }
194 });
195 }
196
197
198 @Override
199 public List<LightQuserVO> getUsersByIds(List<Integer> ids) {
200 return getUsersByIds(connectionProperties, ids);
201 }
202
203
204 @Override
205 public List<LightQuserVO> getUsersByIds(Properties connectionProperties, List<Integer> ids) {
206
207 String sql = queriesJdbcProperties.getProperty("lightQusersByIds");
208 sql = sql.replace(":ids", Joiner.on(",").skipNulls().join(ids));
209
210 Map<String, Object> paramMap = Maps.newHashMap();
211
212 return query(connectionProperties, sql, paramMap, new RowMapper<LightQuserVO>() {
213 @Override
214 public LightQuserVO mapRow(ResultSet rs, int rowNum) throws SQLException, DataAccessException {
215 return toLightQuserVO(rs);
216 }
217 });
218 }
219
220
221 @Override
222 public QuserVO getUserById(int userId) {
223 return getUserById(connectionProperties, userId);
224 }
225
226
227 @Override
228 public QuserVO getUserById(Properties connectionProperties, int quserId) {
229
230 String sql = queriesJdbcProperties.getProperty("quserById");
231
232 Map<String, Object> paramMap = Maps.newHashMap();
233 paramMap.put("quserId", quserId);
234
235 return query(connectionProperties, sql, paramMap, new ResultSetExtractor<QuserVO>() {
236 @Override
237 public QuserVO extractData(ResultSet rs) throws SQLException, DataAccessException {
238 return toUserVO(rs);
239 }
240 });
241 }
242
243
244 @Override
245 public List<PrivilegeVO> getPrivilegesByUserId(int userId) {
246 return getPrivilegesByUserId(connectionProperties, userId);
247 }
248
249
250 @Override
251 public List<PrivilegeVO> getPrivilegesByUserId(Properties connectionProperties, int userId) {
252
253 String sql = queriesJdbcProperties.getProperty("privilegesByQuserId");
254
255 Map<String, Object> paramMap = Maps.newHashMap();
256 paramMap.put("quserId", userId);
257
258 final List<PrivilegeVO> result = Lists.newArrayList();
259
260
261 query(connectionProperties, sql, paramMap, new RowCallbackHandler() {
262
263 @Override
264 public void processRow(ResultSet source) throws SQLException {
265 PrivilegeVO target = toPrivilegeVO(source);
266 result.add(target);
267 }
268 });
269
270 return result;
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 protected LightQuserVO toLightQuserVO(ResultSet source) throws SQLException {
287 int row = 1;
288 return new LightQuserVO(
289 safeGetInteger(source, row++),
290 source.getString(row++),
291 source.getString(row++));
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305 protected QuserVO toUserVO(ResultSet source) throws SQLException {
306 QuserVO target = new QuserVO();
307
308 int row = 1;
309 target.setQuserId(safeGetInteger(source, row++));
310 target.setQuserLastNm(source.getString(row++));
311 target.setQuserFirstNm(source.getString(row++));
312 target.setQuserAddress(source.getString(row++));
313 target.setQuserCreationDt(source.getDate(row++));
314 target.setUpdateDt(source.getTimestamp(row++));
315 target.setStatusCd(source.getString(row++));
316 target.setDepId(safeGetInteger(source, row++));
317 target.setQuserIntranetLg(source.getString(row++));
318 target.setQuserExtranetLg(source.getString(row++));
319 target.setQuserEMail(source.getString(row++));
320 target.setQuserPhone(source.getString(row++));
321 target.setQuserOrgan(source.getString(row++));
322 target.setQuserAdminCenter(source.getString(row++));
323 target.setQuserSite(source.getString(row++));
324 target.setQuserLdapPresent(source.getString(row++));
325
326 return target;
327 }
328
329
330
331
332
333
334
335
336
337
338
339
340 protected ProgramVO toProgramVO(ResultSet source) throws SQLException {
341 ProgramVO target = new ProgramVO();
342
343 int row = 1;
344 target.setProgCd(source.getString(row++));
345 target.setProgNm(source.getString(row++));
346
347 return target;
348 }
349
350
351
352
353
354
355
356
357
358
359
360
361 protected PrivilegeVO toPrivilegeVO(ResultSet source) throws SQLException {
362 PrivilegeVO target = new PrivilegeVO();
363
364 int row = 1;
365 target.setPrivilegeCd(source.getString(row++));
366 target.setPrivilegeNm(source.getString(row++));
367 target.setPrivilegeDc(source.getString(row++));
368 target.setStatusCd(source.getString(row++));
369 target.setUpdateDt(source.getTimestamp(row++));
370
371 return target;
372 }
373
374
375
376
377 protected void checkAllQueriesExists() {
378 Preconditions.checkNotNull(queriesJdbcProperties);
379
380 checkQueryExists("quserIdByLoginAndStatus");
381 checkQueryExists("lightQuserById");
382 checkQueryExists("quserById");
383 checkQueryExists("privilegesByQuserId");
384 }
385
386
387
388
389
390
391
392 protected void checkQueryExists(String queryName) {
393 if (StringUtils.isBlank(queriesJdbcProperties.getProperty(queryName))) {
394 throw new Quadrige2TechnicalException(String.format("Property with name [%s] not exists on JDBC queries file", queryName));
395 }
396 }
397 }