package com.hunantv.fw.db;

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

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.hunantv.fw.Application;
import com.hunantv.fw.utils.FwLogger;

public class DB {
	protected static FwLogger logger = new FwLogger(DB.class);
	private JdbcTemplate jdbcTemplate;

	public DB() {
		this("jdbcTemplate");
	}

	public DB(String name) {
//		jdbcTemplate = Application.getInstance().getSpringCtx().getBean(name, JdbcTemplate.class);
		jdbcTemplate = new JdbcTemplate();
//		DataSource ds = (DataSource) Application.getInstance().getSpringCtx().getBean("dataSource");
		DataSource ds = C3P0.instance().getDataSource();
		jdbcTemplate.setDataSource(ds);
	}

	public Transaction beginTransaction() {
		return this.beginTransaction("txManager");
	}

	public Transaction beginTransaction(String transactionName) {
		return new Transaction(transactionName);
	}

	public List<Map<String, Object>> query(String sql) {
		List<Map<String, Object>> relt = this.jdbcTemplate.queryForList(sql);
		return relt;
	}

	public List<Map<String, Object>> query(String sql, Object... args) {
		return this.jdbcTemplate.queryForList(sql, args);
	}
	
	public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) {
		return this.jdbcTemplate.query(sql, args, rowMapper);
	}

	public void query(String sql, RowCallbackHandler rowCallbackHandler, Object... args) {
		this.jdbcTemplate.query(sql, args, rowCallbackHandler);
	}

	public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) {
		return this.jdbcTemplate.queryForObject(sql, rowMapper, args);
	}

	public <T> T queryForObject(String sql, Class<T> requiredType, Object... args) {
		return this.jdbcTemplate.queryForObject(sql, requiredType, args);
	}

	public Map<String, Object> get(String sql) {
		try {
			return this.jdbcTemplate.queryForMap(sql);
		} catch (EmptyResultDataAccessException ex) {
			return null;
		}
	}

	public Map<String, Object> get(String sql, Object... args) {
		try {
			return this.jdbcTemplate.queryForMap(sql, args);
		} catch (EmptyResultDataAccessException ex) {
			return null;
		}
	}
	
	public int execute(String sql) {
		return this.jdbcTemplate.update(sql);
	}

	public int execute(String sql, Object... args) {
		return this.jdbcTemplate.update(sql, args);
	}

	public int[] batchExecute(String... sql) {
		return this.jdbcTemplate.batchUpdate(sql);
	}

	public int[] batchExecute(String sql, List<Object[]> args) throws DataAccessException {
		return this.jdbcTemplate.batchUpdate(sql, args, new int[0]);
	}

	public class Transaction {
		private DefaultTransactionDefinition def;
		private DataSourceTransactionManager transactionManager;
		private TransactionStatus status;

		private Transaction(String transactionName) {
//			transactionManager = Application.getInstance().getSpringCtx()
//			        .getBean(transactionName, DataSourceTransactionManager.class);
			transactionManager = new DataSourceTransactionManager();
//			DataSource ds = (DataSource) Application.getInstance().getSpringCtx().getBean("dataSource");
			DataSource ds = C3P0.instance().getDataSource();
			transactionManager.setDataSource(ds);
			def = new DefaultTransactionDefinition();
			def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
			status = transactionManager.getTransaction(def);
		}

		public void rollback() {
			transactionManager.rollback(status);
		}

		public void commit() {
			transactionManager.commit(status);
		}
	}
}

@@ -1023,237 +1023,8 @@
 ) %7B%0A
-//%09%09jdbcTemplate = Application.getInstance().getSpringCtx().getBean(name, JdbcTemplate.class);%0A%09%09jdbcTemplate = new JdbcTemplate();%0A//%09%09DataSource ds = (DataSource) Application.getInstance().getSpringCtx().getBean(%22dataSource%22);%0A
 %09%09Da
@@ -1084,29 +1084,34 @@
 Template
-.setDataSourc
+ = new JdbcTemplat
 e(ds);%0A%09
@@ -3338,68 +3338,8 @@
 s);%0A
-%09%09%09transactionManager = new DataSourceTransactionManager();%0A
 //%09%09
@@ -3505,20 +3505,23 @@
 nManager
-.set
+ = new 
 DataSour
@@ -3522,16 +3522,34 @@
 taSource
+TransactionManager
 (ds);%0A%09%09

重构，去掉多余的代码