com.tt.utils.DBUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.tt.utils.DBUtils.java

Source

package com.tt.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import com.tt.domain.ActivityInfo;
import com.tt.domain.SearchCriteria;

public class DBUtils {

    private SimpleJdbcTemplate simplejdbcTemplate;
    private String ACCESS_TOKEN = "timetrade.service.gateway.username";

    private static Map<String, SearchCriteria> cachedata = new HashMap<String, SearchCriteria>();

    public void setDataSource(DataSource dataSource) {
        this.simplejdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

    public List<ActivityInfo> getActivityInfo(SearchCriteria searchCriteria) {

        List<ActivityInfo> result = null;
        try {
            result = simplejdbcTemplate.query(
                    DBQueryMap.getQueryString(DBQueryMap.GET_ACTIVITY_INFO_BY_LOCATION_EXTERNALID),
                    ParameterizedBeanPropertyRowMapper.newInstance(ActivityInfo.class),
                    new BeanPropertySqlParameterSource(searchCriteria));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

        return result;

    }

    public int getLicenseeInfo() {
        int id = -9999;
        Map<String, String> params = new HashMap<String, String>();
        params.put("AccessToken", System.getProperty(ACCESS_TOKEN, "default"));
        try {
            id = simplejdbcTemplate.queryForInt(DBQueryMap.getQueryString(DBQueryMap.GET_ACCESS_ID), params);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }

        return id;
    }

    public int getLocationInfo(SearchCriteria sc) {
        int id = -9999;
        Map<String, String> params = new HashMap<String, String>();
        params.put("licenseeId", new Integer(sc.getLicenseeId()).toString());
        params.put("locationExternalId", sc.getLocationExternalId());
        try {
            id = simplejdbcTemplate.queryForInt(DBQueryMap.getQueryString(DBQueryMap.GET_LOCATION_INFO), params);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }

        return id;
    }

    public SearchCriteria getCachedData(String locationExternalId) {
        SearchCriteria sc = null;
        if (cachedata.containsKey(locationExternalId)) {
            sc = cachedata.get(locationExternalId);
        } else {
            sc = new SearchCriteria();
            sc.setLocationExternalId(locationExternalId);
            sc.setLicenseeId(getLicenseeInfo());
            sc.setLocationId(getLocationInfo(sc));
            cachedata.put(locationExternalId, sc);

        }

        return sc;
    }
}