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.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   * <p>
59   * DepartmentJdbcDaoImpl class.
60   * </p>
61   * 
62   * @see Quser
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  	 * Constructor used by Spring
79  	 * 
80  	 * @param dataSource
81  	 *            a {@link javax.sql.DataSource} object.
82  	 */
83  	@Autowired
84  	public DepartmentJdbcDaoImpl(DataSource dataSource) {
85  		super(dataSource);
86  	}
87  
88  	/**
89  	 * Constructor without Spring (e.g. for synchro)
90  	 */
91  	public DepartmentJdbcDaoImpl() {
92  		this((Properties) null);
93  	}
94  
95  	/**
96  	 * Constructor without Spring (e.g. for synchro)
97  	 * 
98  	 * @param connectionProperties
99  	 *            a {@link java.util.Properties} object.
100 	 */
101 	public DepartmentJdbcDaoImpl(Properties connectionProperties) {
102 		super();
103 		this.connectionProperties = connectionProperties;
104 
105 		// Load properties
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 		// Check all queries
122 		checkAllQueriesExists();
123 	}
124 
125 	/** {@inheritDoc} */
126 	@Override
127 	public void afterPropertiesSet() throws Exception {
128 		checkAllQueriesExists();
129 	}
130 
131 	/** {@inheritDoc} */
132 	@Override
133 	public DepartmentVO getDepartmentById(int depId) {
134 		return getDepartmentById(connectionProperties, depId);
135 	}
136 
137 	/** {@inheritDoc} */
138 	@Override
139 	public DepartmentVO getDepartmentById(Properties connectionProperties, int depId) {
140 		// Find the user, by id
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 	/** {@inheritDoc} */
155 	@Override
156 	public List<DepartmentVO> getDepartmentsByIds(List<Integer> ids) {
157 		return getDepartmentsByIds(connectionProperties, ids);
158 	}
159 
160 	/** {@inheritDoc} */
161 	@Override
162 	public List<DepartmentVO> getDepartmentsByIds(Properties connectionProperties, List<Integer> ids) {
163 		// Find the user, by id
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 	/* -- Internal methods -- */
178 
179 	/**
180 	 * <p>
181 	 * toDepartmentVO.
182 	 * </p>
183 	 * 
184 	 * @param source
185 	 *            a {@link java.sql.ResultSet} object.
186 	 * @return a {@link fr.ifremer.quadrige2.core.vo.administration.user.DepartmentVO} object.
187 	 * @throws java.sql.SQLException
188 	 *             if any.
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 	 * <p>
210 	 * checkAllQueriesExists.
211 	 * </p>
212 	 */
213 	protected void checkAllQueriesExists() {
214 		checkQueryExists("departmentById");
215 	}
216 
217 	/**
218 	 * <p>
219 	 * checkQueryExists.
220 	 * </p>
221 	 * 
222 	 * @param queryName
223 	 *            a {@link java.lang.String} object.
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 }