package org.openbravo.activiti;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import javax.sql.DataSource;

import org.openbravo.base.ConnectionProviderContextListener;
import org.openbravo.dal.core.DalContextListener;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.exception.NoConnectionAvailableException;

public class ActivitiDataSource implements DataSource {
  private ConnectionProvider pool;

  public ActivitiDataSource() {
    pool = (ConnectionProvider) DalContextListener.getServletContext().getAttribute(
        ConnectionProviderContextListener.POOL_ATTRIBUTE);
  }

  @Override
  public PrintWriter getLogWriter() throws SQLException {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public int getLoginTimeout() throws SQLException {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public Logger getParentLogger() throws SQLFeatureNotSupportedException {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public void setLogWriter(PrintWriter arg0) throws SQLException {
    // TODO Auto-generated method stub

  }

  @Override
  public void setLoginTimeout(int arg0) throws SQLException {
    // TODO Auto-generated method stub

  }

  @Override
  public boolean isWrapperFor(Class<?> arg0) throws SQLException {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public <T> T unwrap(Class<T> arg0) throws SQLException {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Connection getConnection() throws SQLException {
    // TODO Auto-generated method stub
    try {
      Connection con = pool.getConnection();
      con.setAutoCommit(false);
      return con;
    } catch (NoConnectionAvailableException e) {
      // TODO Auto-generated catch block
      throw new SQLException(e);
    }
  }

  @Override
  public Connection getConnection(String username, String password) throws SQLException {
    return getConnection();
  }

}
