source: EDIS/tags/ed/tracking-server-core/src/main/java/gov/va/med/edp/dao/jdbc/JdbcSiteCodeLookUpDao.java@ 1240

Last change on this file since 1240 was 1240, checked in by George Lilly, 13 years ago

new version from the VA

File size: 2.5 KB
Line 
1package gov.va.med.edp.dao.jdbc;
2
3import gov.va.med.edp.dao.SiteCodeLookUpDao;
4
5import java.util.List;
6
7import javax.sql.DataSource;
8
9import org.apache.log4j.Logger;
10import org.springframework.beans.factory.InitializingBean;
11import org.springframework.dao.DataAccessException;
12import org.springframework.jdbc.core.JdbcTemplate;
13import org.springframework.util.Assert;
14
15public 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}
Note: See TracBrowser for help on using the repository browser.