View Javadoc
1   // license-header java merge-point
2   //
3   // Attention: Generated code! Do not modify by hand!
4   // Generated by: SpringHibernateDaoImpl.vsl in andromda-spring-cartridge.
5   //
6   package fr.ifremer.quadrige2.core.dao.administration.user;
7   
8   /*-
9    * #%L
10   * Quadrige2 Core :: Quadrige2 Server Core
11   * $Id:$
12   * $HeadURL:$
13   * %%
14   * Copyright (C) 2017 Ifremer
15   * %%
16   * This program is free software: you can redistribute it and/or modify
17   * it under the terms of the GNU Affero General Public License as published by
18   * the Free Software Foundation, either version 3 of the License, or
19   * (at your option) any later version.
20   * 
21   * This program is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24   * GNU General Public License for more details.
25   * 
26   * You should have received a copy of the GNU Affero General Public License
27   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28   * #L%
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   * <p>
66   * QuserJdbcDaoImpl class.
67   * </p>
68   * 
69   * @see Quser
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  	 * Constructor used by Spring
86  	 * 
87  	 * @param dataSource
88  	 *            a {@link javax.sql.DataSource} object.
89  	 */
90  	@Autowired
91  	public QuserJdbcDaoImpl(DataSource dataSource) {
92  		super(dataSource);
93  	}
94  
95  	/**
96  	 * Constructor without Spring (e.g. for synchro)
97  	 */
98  	public QuserJdbcDaoImpl() {
99  		this((Properties) null/* use default connection properties */);
100 	}
101 
102 	/**
103 	 * Constructor without Spring (e.g. for synchro)
104 	 * 
105 	 * @param connectionProperties
106 	 *            a {@link java.util.Properties} object.
107 	 */
108 	public QuserJdbcDaoImpl(Properties connectionProperties) {
109 		super();
110 		this.connectionProperties = connectionProperties;
111 
112 		// Load properties
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 		// Check all queries
129 		checkAllQueriesExists();
130 	}
131 
132 	/** {@inheritDoc} */
133 	@Override
134 	public void afterPropertiesSet() throws Exception {
135 		// Check queries exists on queries file
136 		checkAllQueriesExists();
137 	}
138 
139 	/** {@inheritDoc} */
140 	@Override
141 	public Integer getUserIdByUsername(String username) {
142 		return getUserIdByUsername(connectionProperties, username);
143 	}
144 
145 	/** {@inheritDoc} */
146 	@Override
147 	public Integer getUserIdByUsername(Properties connectionProperties, String username) {
148 		// Find the user, by id
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 				// Oracle DBMS return a BigDecimal from NUMBER(10) columns
164 				if (value instanceof BigDecimal) {
165 					return ((BigDecimal) value).intValue();
166 				}
167 
168 				// for all other java types: convert into Integer
169 				return Integer.parseInt(value.toString());
170 			}
171 		});
172 	}
173 
174 	/** {@inheritDoc} */
175 	@Override
176 	public LightQuserVO getLightUserById(int personId) {
177 		return getLightUserById(connectionProperties, personId);
178 	}
179 
180 	/** {@inheritDoc} */
181 	@Override
182 	public LightQuserVO getLightUserById(Properties connectionProperties, int quserId) {
183 		// Find the user, by id
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 	/** {@inheritDoc} */
198 	@Override
199 	public List<LightQuserVO> getUsersByIds(List<Integer> ids) {
200 		return getUsersByIds(connectionProperties, ids);
201 	}
202 
203 	/** {@inheritDoc} */
204 	@Override
205 	public List<LightQuserVO> getUsersByIds(Properties connectionProperties, List<Integer> ids) {
206 		// Find users, by ids
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 	/** {@inheritDoc} */
221 	@Override
222 	public QuserVO getUserById(int userId) {
223 		return getUserById(connectionProperties, userId);
224 	}
225 
226 	/** {@inheritDoc} */
227 	@Override
228 	public QuserVO getUserById(Properties connectionProperties, int quserId) {
229 		// Find the user, by id
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 	/** {@inheritDoc} */
244 	@Override
245 	public List<PrivilegeVO> getPrivilegesByUserId(int userId) {
246 		return getPrivilegesByUserId(connectionProperties, userId);
247 	}
248 
249 	/** {@inheritDoc} */
250 	@Override
251 	public List<PrivilegeVO> getPrivilegesByUserId(Properties connectionProperties, int userId) {
252 		// Find the user, by id
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 		// Execute the query, and fill the result list
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 	/* -- Internal methods -- */
274 
275 	/**
276 	 * <p>
277 	 * toLightQuserVO.
278 	 * </p>
279 	 * 
280 	 * @param source
281 	 *            a {@link java.sql.ResultSet} object.
282 	 * @return a {@link fr.ifremer.quadrige2.core.vo.administration.user.LightQuserVO} object.
283 	 * @throws java.sql.SQLException
284 	 *             if any.
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 	 * <p>
296 	 * toUserVO.
297 	 * </p>
298 	 * 
299 	 * @param source
300 	 *            a {@link java.sql.ResultSet} object.
301 	 * @return a {@link fr.ifremer.quadrige2.core.vo.administration.user.QuserVO} object.
302 	 * @throws java.sql.SQLException
303 	 *             if any.
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 	 * <p>
331 	 * toProgramVO.
332 	 * </p>
333 	 * 
334 	 * @param source
335 	 *            a {@link java.sql.ResultSet} object.
336 	 * @return a {@link fr.ifremer.quadrige2.core.vo.administration.program.ProgramVO} object.
337 	 * @throws java.sql.SQLException
338 	 *             if any.
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 	 * <p>
352 	 * toPrivilegeVO.
353 	 * </p>
354 	 * 
355 	 * @param source
356 	 *            a {@link java.sql.ResultSet} object.
357 	 * @return a {@link fr.ifremer.quadrige2.core.vo.administration.user.PrivilegeVO} object.
358 	 * @throws java.sql.SQLException
359 	 *             if any.
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 	 * Check queries exists on queries file
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 	 * Check if a query exists on the queries properties file
388 	 * 
389 	 * @param queryName
390 	 *            a {@link java.lang.String} object.
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 }