All pastes #263045 Raw Edit

Stuff

public text v1 · immutable
·published 2006-12-01 03:45 UTC
rendered paste body
package net.forcedata.common;

import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.sqlmap.client.SqlMapClient;




public class BaseDAO {
  private static Log log = LogFactory.getLog(BaseDAO.class);
  private static SqlMapClient sqlMap = null;
    
  /** 
   *  Added to allow all DAO's to access this as the updated BaseDAO(c) now takes in a param
   */
  public BaseDAO() {
    
  }
  
  
  int clientId = 0;
  String clientName = "";
  
  public BaseDAO(String c) {
    //static {
    
//    for(int i=0; i<clients.length; i++) {
    
    //String c = clients[i];                                                    //prefix the client name with a dash
    if(!c.equals("")) {
      c = "-" + c;
    }
    
    try {
      String resource = "net/forcedata/common/conf/sql-map-config" + c + ".xml";
      Reader reader = Resources.getResourceAsReader(resource);
      log.debug("reader = "+reader );
      sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
      log.debug("sqlMap = "+sqlMap );
      reader.close();
    } catch (Exception ex) {
      log.error("BaseDAO static block: " + ex);
      throw new RuntimeException("Error Initializing BaseDAO :" + ex);
    }
    //  }
    //}
  }
  
  
  
  /* ORIGINAL
  static {
    try {
      String resource = "net/forcedata/common/conf/sql-map-config.xml";
      Reader reader = Resources.getResourceAsReader(resource);
      log.debug("reader = "+reader );
      sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
      log.debug("sqlMap = "+sqlMap );
      reader.close();
   
   
    } catch (Exception ex) {
      log.error("BaseDAO static block: " + ex);
      throw new RuntimeException("Error Initializing BaseDAO :" + ex);
    }
  }
   */
  
  
  
  
  public Map getMap(String statementName, Object parameterObject, String s1, String s2) throws DaoException {
    //List list = null;
    Map map;// = null;
    
    try {
      sqlMap.startTransaction();
      map = sqlMap.queryForMap(statementName, parameterObject, s1, s2);
      sqlMap.commitTransaction();
    } catch (SQLException e) {
      try {
        sqlMap.endTransaction();
      } catch (SQLException ex) {
        throw new DaoException(ex.fillInStackTrace());
      }
      throw new DaoException(e.fillInStackTrace());
    } finally {
      try {
        sqlMap.endTransaction();
      } catch(SQLException sqle) {}
    }
    return map;
  }
  
  
  
  
  
  
  
  //selectLoginFromSearch
  public List getList(String statementName, Object parameterObject) throws DaoException {
    List list = null;
    
    try {
      sqlMap.startTransaction();
      list = sqlMap.queryForList(statementName, parameterObject);
      sqlMap.commitTransaction();
    } catch (SQLException e) {
      try {
        sqlMap.endTransaction();
      } catch (SQLException ex) {
        throw new DaoException(ex.fillInStackTrace());
      }
      throw new DaoException(e.fillInStackTrace());
    } finally {
      try {
        sqlMap.endTransaction();
      } catch(SQLException sqle) {}
    }
    return list;
  }
  
  
  
  public Object getObject(String statementName, Object parameterObject) throws DaoException {
    Object result = null;
    
    try {
      sqlMap.startTransaction();
      result = sqlMap.queryForObject(statementName, parameterObject);
      sqlMap.commitTransaction();
    } catch (SQLException e) {
      try {
        sqlMap.endTransaction();
      } catch (SQLException ex) {
        throw new DaoException(ex.fillInStackTrace());
      }
      throw new DaoException(e.fillInStackTrace());
    } finally {
      try {
        sqlMap.endTransaction();
      } catch(SQLException sqle) {}
      
    }
    return result;
  }
  
  /*
  public int insert(String statementName, Object parameterObject) throws DaoException {
    Integer result;
    System.out.println("INSIDE THE INSERT NOW");
    try {
      sqlMap.startTransaction();
      result = (Integer) sqlMap.insert(statementName, parameterObject);
      sqlMap.commitTransaction();
      log.info("inserted record");
    } catch (SQLException e) {
      try {
        sqlMap.endTransaction();
      } catch (SQLException ex) {
        throw new DaoException(ex.fillInStackTrace());
      }
      throw new DaoException(e.fillInStackTrace());
    } finally {
      try {
        sqlMap.endTransaction();
      } catch(SQLException sqle) {}
    }
   
    System.out.println("RESULT: " + result);
    System.out.println("result value: " + result.intValue());
    return result.intValue();
  }
   */
  
  public int insert(String statementName, Object parameterObject) throws DaoException {
    Integer result;
    
    System.out.println("INSIDE THE INSERT NOW");
    try {
      result = (Integer) sqlMap.insert(statementName, parameterObject);
      log.info("inserted record");
    } catch (SQLException e) {
      throw new DaoException(e.fillInStackTrace());
    }
    //System.out.println("RESULT: " + result);
    //System.out.println("result value: " + result.intValue());
    //return result.intValue();
    return result.intValue();
  }
  
  
  
  
  
  public int update(String statementName, Object parameterObject) throws DaoException {
    int result = 0;
    
    try {
      sqlMap.startTransaction();
      result = sqlMap.update(statementName, parameterObject);
      sqlMap.commitTransaction();
      log.info("updated record");
    } catch (SQLException e) {
      try {
        sqlMap.endTransaction();
      } catch (SQLException ex) {
        throw new DaoException(ex.fillInStackTrace());
      }
      
      throw new DaoException(e.fillInStackTrace());
    } finally {
      try {
        sqlMap.endTransaction();
      } catch(SQLException sqle) {}
      
    }
    return result;
  }
  
  
  public int delete(String statementName, Object parameterObject) throws DaoException {
    int result = 0;
    
    try {
      sqlMap.startTransaction();
      result = sqlMap.delete(statementName, parameterObject);
      sqlMap.commitTransaction();
      log.info("deleted record");
    } catch (SQLException e) {
      try {
        sqlMap.endTransaction();
      } catch (SQLException ex) {
        throw new DaoException(ex.fillInStackTrace());
      }
      
      throw new DaoException(e.fillInStackTrace());
    } finally {
      try {
        sqlMap.endTransaction();
      } catch(SQLException sqle) {}
      
    }
    return result;
  }
  
  
  /**
   * Update a list of bean objects and insert them into the db
   * @param statementName
   * @param parameterObject
   * @return
   * @throws DaoException
   */
  public int updateList(String statementName, Object parameterObject) throws DaoException {
    int result = 0;
    List list = (List) parameterObject;
    Object obj = null;
    try {
      sqlMap.startTransaction();
      sqlMap.startBatch();
      for(int i=0; i<list.size(); i++) {
        obj =  list.get(i);
        result = sqlMap.update(statementName, (Object) list.get(i));
      }
      sqlMap.executeBatch();
      sqlMap.commitTransaction();
    } catch (SQLException e) {
      try {
        sqlMap.endTransaction();
      } catch (SQLException ex) {
        throw new DaoException(ex.fillInStackTrace());
      }
      throw new DaoException(e.fillInStackTrace());
    } finally {
      try {
        sqlMap.endTransaction();
      } catch(SQLException sqle) {}
      
    }
    return result;
  }
  
  
  public int insertList(String statementName, Object parameterObject) throws DaoException {
    int result = 0;
    ArrayList list = (ArrayList) parameterObject;
    try {
      sqlMap.startBatch();
      for(int i=0; i<list.size(); i++) {
        //result = sqlMap.insert(statementName, (Object) list.get(i));
        result = sqlMap.update(statementName, (Object) list.get(i));
        //System.out.println("Object inserted: " + list.get(i));
      }
      sqlMap.executeBatch();
    } catch (SQLException e) {
      try {
        sqlMap.endTransaction();
      } catch (SQLException ex) {
        throw new DaoException(ex.fillInStackTrace());
      }
      throw new DaoException(e.fillInStackTrace());
    }
    return result;
  }
  
  
  
  
  
  
  
  
  
  
}