View Javadoc
1   package fr.ifremer.quadrige3.core.dao.administration.user;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Client Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * <p>
52   * DepartmentJdbcDaoImpl class.
53   * </p>
54   * 
55   * @see fr.ifremer.quadrige3.core.dao.administration.user.Quser
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  	 * Constructor used by Spring
72  	 * 
73  	 * @param dataSource
74  	 *            a {@link javax.sql.DataSource} object.
75  	 */
76  	@Autowired
77  	public DepartmentJdbcDaoImpl(DataSource dataSource) {
78  		super(dataSource);
79  	}
80  
81  	/**
82  	 * Constructor without Spring (e.g. for synchro)
83  	 */
84  	public DepartmentJdbcDaoImpl() {
85  		this((Properties) null);
86  	}
87  
88  	/**
89  	 * Constructor without Spring (e.g. for synchro)
90  	 * 
91  	 * @param connectionProperties
92  	 *            a {@link java.util.Properties} object.
93  	 */
94  	public DepartmentJdbcDaoImpl(Properties connectionProperties) {
95  		super();
96  		this.connectionProperties = connectionProperties;
97  
98  		// Load properties
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 		// Check all queries
115 		checkAllQueriesExists();
116 	}
117 
118 	/** {@inheritDoc} */
119 	@Override
120 	public void afterPropertiesSet() {
121 		checkAllQueriesExists();
122 	}
123 
124 	/** {@inheritDoc} */
125 	@Override
126 	public DepartmentVO getDepartmentById(int depId) {
127 		return getDepartmentById(connectionProperties, depId);
128 	}
129 
130 	/** {@inheritDoc} */
131 	@Override
132 	public DepartmentVO getDepartmentById(Properties connectionProperties, int depId) {
133 		// Find the user, by id
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 	/** {@inheritDoc} */
143 	@Override
144 	public List<DepartmentVO> getDepartmentsByIds(List<Integer> ids) {
145 		return getDepartmentsByIds(connectionProperties, ids);
146 	}
147 
148 	/** {@inheritDoc} */
149 	@Override
150 	public List<DepartmentVO> getDepartmentsByIds(Properties connectionProperties, List<Integer> ids) {
151 		// Find the user, by id
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 	/* -- Internal methods -- */
173 
174 	/**
175 	 * <p>
176 	 * toDepartmentVO.
177 	 * </p>
178 	 * 
179 	 * @param source
180 	 *            a {@link java.sql.ResultSet} object.
181 	 * @return a {@link fr.ifremer.quadrige3.core.vo.administration.user.DepartmentVO} object.
182 	 * @throws java.sql.SQLException
183 	 *             if any.
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 	 * <p>
205 	 * checkAllQueriesExists.
206 	 * </p>
207 	 */
208 	protected void checkAllQueriesExists() {
209 		checkQueryExists("departmentById");
210 	}
211 
212 	/**
213 	 * <p>
214 	 * checkQueryExists.
215 	 * </p>
216 	 * 
217 	 * @param queryName
218 	 *            a {@link java.lang.String} object.
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 }