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.Maps;
34 import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
35 import fr.ifremer.quadrige2.core.dao.technical.jdbc.OptionalDataSourceJdbcDaoSupport;
36 import fr.ifremer.quadrige2.core.vo.administration.user.DepartmentVO;
37 import org.apache.commons.io.IOUtils;
38 import org.apache.commons.lang3.StringUtils;
39 import org.springframework.beans.factory.InitializingBean;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.context.annotation.Lazy;
42 import org.springframework.dao.DataAccessException;
43 import org.springframework.jdbc.core.ResultSetExtractor;
44 import org.springframework.jdbc.core.RowMapper;
45 import org.springframework.stereotype.Repository;
46
47 import javax.annotation.Resource;
48 import javax.sql.DataSource;
49 import java.io.IOException;
50 import java.io.InputStream;
51 import java.sql.ResultSet;
52 import java.sql.SQLException;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.Properties;
56
57
58
59
60
61
62
63
64 @Repository("departmentJdbcDao")
65 @Lazy
66 public class DepartmentJdbcDaoImpl
67 extends OptionalDataSourceJdbcDaoSupport
68 implements DepartmentJdbcDao, InitializingBean {
69
70 private final static String QUERIES_FILE_PATH = "queries.jdbc.xml";
71
72 @Resource(name = "queriesJdbcProperties")
73 protected Properties queriesJdbcProperties;
74
75 protected Properties connectionProperties;
76
77
78
79
80
81
82
83 @Autowired
84 public DepartmentJdbcDaoImpl(DataSource dataSource) {
85 super(dataSource);
86 }
87
88
89
90
91 public DepartmentJdbcDaoImpl() {
92 this((Properties) null);
93 }
94
95
96
97
98
99
100
101 public DepartmentJdbcDaoImpl(Properties connectionProperties) {
102 super();
103 this.connectionProperties = connectionProperties;
104
105
106 Properties properties = new Properties();
107 InputStream is = null;
108 try {
109 is = getClass().getClassLoader().getResourceAsStream(QUERIES_FILE_PATH);
110 properties.loadFromXML(is);
111
112 this.queriesJdbcProperties = properties;
113 } catch (IOException e) {
114 throw new Quadrige2TechnicalException(
115 String.format("Unable to read file [%s] from classpath", QUERIES_FILE_PATH),
116 e);
117 } finally {
118 IOUtils.closeQuietly(is);
119 }
120
121
122 checkAllQueriesExists();
123 }
124
125
126 @Override
127 public void afterPropertiesSet() throws Exception {
128 checkAllQueriesExists();
129 }
130
131
132 @Override
133 public DepartmentVO getDepartmentById(int depId) {
134 return getDepartmentById(connectionProperties, depId);
135 }
136
137
138 @Override
139 public DepartmentVO getDepartmentById(Properties connectionProperties, int depId) {
140
141 String sql = queriesJdbcProperties.getProperty("departmentById");
142
143 Map<String, Object> paramMap = Maps.newHashMap();
144 paramMap.put("depId", depId);
145
146 return query(connectionProperties, sql, paramMap, new ResultSetExtractor<DepartmentVO>() {
147 @Override
148 public DepartmentVO extractData(ResultSet rs) throws SQLException, DataAccessException {
149 return toDepartmentVO(rs);
150 }
151 });
152 }
153
154
155 @Override
156 public List<DepartmentVO> getDepartmentsByIds(List<Integer> ids) {
157 return getDepartmentsByIds(connectionProperties, ids);
158 }
159
160
161 @Override
162 public List<DepartmentVO> getDepartmentsByIds(Properties connectionProperties, List<Integer> ids) {
163
164 String sql = queriesJdbcProperties.getProperty("departmentsByIds");
165 sql = sql.replace(":ids", Joiner.on(',').skipNulls().join(ids));
166
167 Map<String, Object> paramMap = Maps.newHashMap();
168
169 return query(connectionProperties, sql, paramMap, new RowMapper<DepartmentVO>() {
170 @Override
171 public DepartmentVO mapRow(ResultSet rs, int rowNum) throws SQLException, DataAccessException {
172 return toDepartmentVO(rs);
173 }
174 });
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 protected DepartmentVO toDepartmentVO(ResultSet source) throws SQLException {
191 DepartmentVO target = new DepartmentVO();
192 int col = 1;
193 target.setDepId(safeGetInteger(source, col++));
194 target.setDepCd(source.getString(col++));
195 target.setDepNm(source.getString(col++));
196 target.setDepEMail(source.getString(col++));
197 target.setDepAddress(source.getString(col++));
198 target.setDepPhone(source.getString(col++));
199 target.setDepLdapPresent(source.getString(col++));
200 target.setDepCreationDt(source.getDate(col++));
201 target.setUpdateDt(source.getTimestamp(col++));
202 target.setParentDepartmentId(source.getInt(col++));
203 target.setStatusCd(source.getString(col++));
204
205 return target;
206 }
207
208
209
210
211
212
213 protected void checkAllQueriesExists() {
214 checkQueryExists("departmentById");
215 }
216
217
218
219
220
221
222
223
224
225 protected void checkQueryExists(String queryName) {
226 Preconditions.checkArgument(
227 StringUtils.isNotBlank(queriesJdbcProperties.getProperty(queryName)),
228 "Property with name [%s] not exists on JDBC queries file");
229 }
230 }