package core.stlmes.server.sale.dbhelp;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;

import CoreFS.SA01.CoreIComponent;
import CoreFS.SA06.CorePageObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA06.CoreSqlType.CoreOracleType;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

public class DbTransAction extends CoreIComponent
{

	private String connerrMsg = "---";
	private Hashtable<String, Connection> _hsConn = new Hashtable<String, Connection>();
	private static DbTransAction dbTransaction = new DbTransAction();
	private java.sql.Connection conn = null;
	private String daoName = core.stlmes.server.sale.constdefine.ConstDef.daoName;

	private DbTransAction()
	{
		conn = this.getDao(daoName).getConnection();

	}

	private String ErrMsg = "";

	public String getErrMsg()
	{
		return ErrMsg;
	}

	public void setErrMsg(String errMsg)
	{
		ErrMsg = errMsg;
	}

	public CoreReturnObject doGetConnection(String daoName)
			throws java.sql.SQLException
	{
		CoreReturnObject cro = new CoreReturnObject();
		String uuid = java.util.UUID.randomUUID().toString();
		Connection conn = this.getDao(daoName).getConnection();

		if (null == conn)
		{
			return cro;
		}
		conn.setAutoCommit(false);
		this._hsConn.put(uuid, conn);

		cro.setResult(uuid);

		return cro;
	}

	public void removeConnection(String uuid)
	{
		if (this._hsConn.containsKey(uuid))
		{
			try
			{
				this._hsConn.remove(uuid);

			}
			catch (java.lang.Exception ex)
			{

			}

		}
	}

	public java.sql.Connection getConnection(String uuid)
	{
		if (this._hsConn.containsKey(uuid))
		{
			return _hsConn.get(uuid);
		}
		else
			return null;

	}

	public static DbTransAction getDbTransActionInstance()
	{
		return dbTransaction;

	}

	public void commit()
	{
		try
		{
			if (this.getErrMsg().length() > 0) rollback();

			this.conn.commit();
			this.conn.close();
		}
		catch (java.lang.Exception ex)
		{
		}
	}

	public void rollback()
	{
		try
		{
			this.conn.rollback();
			this.conn.close();
		}
		catch (java.lang.Exception ex)
		{
		}

	}

	public java.sql.Connection getConnection()
	{
		return this.conn;
	}

}
