[1240] | 1 | package gov.va.med.edp.dao.jdbc;
|
---|
| 2 |
|
---|
| 3 | import gov.va.med.edp.dao.SiteCodeLookUpDao;
|
---|
| 4 |
|
---|
| 5 | import java.util.List;
|
---|
| 6 |
|
---|
| 7 | import javax.sql.DataSource;
|
---|
| 8 |
|
---|
| 9 | import org.apache.log4j.Logger;
|
---|
| 10 | import org.springframework.beans.factory.InitializingBean;
|
---|
| 11 | import org.springframework.dao.DataAccessException;
|
---|
| 12 | import org.springframework.jdbc.core.JdbcTemplate;
|
---|
| 13 | import org.springframework.util.Assert;
|
---|
| 14 |
|
---|
| 15 | public class JdbcSiteCodeLookUpDao implements SiteCodeLookUpDao, InitializingBean {
|
---|
| 16 |
|
---|
| 17 | private JdbcTemplate jdbcTemplate;
|
---|
| 18 | private static Logger logger = Logger.getLogger(JdbcSiteCodeLookUpDao.class);
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | public String getSiteCode(String machineName) throws DataAccessException {
|
---|
| 22 | String sql = "select siteCode from EDP.SiteMapper where sitePrefix=?";
|
---|
| 23 | String siteCode = "";
|
---|
| 24 | List siteCodeList = null;
|
---|
| 25 |
|
---|
| 26 | if (logger.isDebugEnabled()){
|
---|
| 27 | logger.debug("Executing SQL: " + sql + " with parameter: " + machineName);
|
---|
| 28 | }
|
---|
| 29 | siteCodeList = jdbcTemplate.queryForList(sql, new Object[]{machineName} , String.class);
|
---|
| 30 | if (!(siteCodeList!= null && siteCodeList.size() == 1)){
|
---|
| 31 | String sitePrefix = parseSitePrefix(machineName);
|
---|
| 32 | if (logger.isDebugEnabled()){
|
---|
| 33 | logger.debug("Matching site code was not found for the full machineName. Now executing SQL query:" + sql + " with parameter: " + sitePrefix);
|
---|
| 34 | }
|
---|
| 35 | siteCodeList = jdbcTemplate.queryForList(sql, new Object[]{sitePrefix} , String.class);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | if (siteCodeList!= null && siteCodeList.size() == 1){
|
---|
| 39 | siteCode = siteCodeList.get(0).toString();
|
---|
| 40 | if (logger.isDebugEnabled()) logger.debug("SQL Query executed successfully. The Site Code is: " + siteCode);
|
---|
| 41 | return siteCode;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | if (logger.isDebugEnabled()) logger.debug("No Results were found for the SQL Query. Returning null for siteCode");
|
---|
| 45 | return null;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public void setDataSource(DataSource dataSource) {
|
---|
| 49 | this.jdbcTemplate = new JdbcTemplate(dataSource);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | public void afterPropertiesSet() throws Exception {
|
---|
| 54 | Assert.notNull(jdbcTemplate, "jdbcTemplate must not be null");
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | private String parseSitePrefix(String machineName){
|
---|
| 58 | String sitePrefix = "";
|
---|
| 59 | if (machineName.startsWith("VHA")){
|
---|
| 60 | sitePrefix = machineName.substring(3, 6);
|
---|
| 61 |
|
---|
| 62 | } else {
|
---|
| 63 | sitePrefix = machineName.substring(0, 3);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | if (logger.isDebugEnabled()){
|
---|
| 67 | logger.debug("Parsed sitePrefix from the machineName: " + machineName + " is: " + sitePrefix);
|
---|
| 68 | }
|
---|
| 69 | return sitePrefix;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | }
|
---|