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