package wenrgise.hrms.ejb.business;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.bean.LOVBean;
import wenrgise.common.ejb.business.HrmCommonBO;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseMessageKeyException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.DateUtility;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.MessageKey;
import wenrgise.common.utility.RecordMetaInfo;
import wenrgise.common.utility.UserInfo;
import wenrgise.common.vo.BaseDetailVO;
import wenrgise.common.vo.BaseHeaderVO;
import wenrgise.common.vo.LovQueryVO;
import wenrgise.common.vo.LovVO;
import wenrgise.ejb.common.helper.DBObject;
import wenrgise.ejb.common.helper.QueryRow;
import wenrgise.ejb.common.helper.QueryValue;
import wenrgise.ejb.common.utility.DBUtilitiesBean;
import wenrgise.hrms.bean.HrmTrngCrsMstDtlBean;
import wenrgise.hrms.bean.HrmTrngCrsMstHdrBean;
import wenrgise.hrms.vo.HrmTrngCrsMstQVO;

public class HrmTrngCrsMstBO extends HrmBaseBO {
  public HrmTrngCrsMstBO() {}
  
  public HrmTrngCrsMstBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmTrngCrsHdrMetaInfo(HrmTrngCrsMstQVO oHrmTrngCrsMstQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, oHrmTrngCrsMstQVO.getTrainingBranch()));
    oParameters.add(new DBObject(2, 1, 12, oHrmTrngCrsMstQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(3, 2, -5));
    oParameters.add(new DBObject(4, 2, 93));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 12));
    oParameters.add(new DBObject(7, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSMST.proc_HrTrngCrsHdrCount(?,?,?,?,?,?,?)");
    RecordMetaInfo oRecordMetaInfo = new RecordMetaInfo();
    DBObject oTimeObject = oOutArray.get(1);
    oRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    DBObject oTotalRecord = oOutArray.get(0);
    oRecordMetaInfo.setRecordCount(((Long)oTotalRecord.getObject()).longValue());
    return oRecordMetaInfo;
  }
  
  public ArrayList getHrmTrngCrsHeaderInfo(HrmTrngCrsMstQVO oHrmTrngCrsMstQVO, long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    ArrayList oHeaderList = null;
    if (oHrmTrngCrsMstQVO == null)
      oHrmTrngCrsMstQVO = new HrmTrngCrsMstQVO(); 
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, -5, new Long(lStartPosition)));
    oParameters.add(new DBObject(2, 1, -5, new Long(lLastPosition)));
    oParameters.add(new DBObject(3, 1, 12, oHrmTrngCrsMstQVO.getTrainingBranch()));
    oParameters.add(new DBObject(4, 1, 12, oHrmTrngCrsMstQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(5, 2, -10));
    oParameters.add(new DBObject(6, 2, 12));
    oParameters.add(new DBObject(7, 2, 12));
    oParameters.add(new DBObject(8, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSMST.proc_GetHrTrngCrsHdrInfo(?,?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      if (count == 0)
        oHeaderList = new ArrayList(); 
      count++;
      oRow = oIt.next();
      HrmTrngCrsMstHdrBean oTrngCrsHeader = new HrmTrngCrsMstHdrBean();
      oTrngCrsHeader.setHeaderPrimaryKey(oRow.get("ID").getString());
      oTrngCrsHeader.setTrainingBranch(oRow.get("BRANCH").getString());
      oTrngCrsHeader.setBranchName(oRow.get("NAME").getString());
      oHeaderList.add(oTrngCrsHeader);
    } 
    return oHeaderList;
  }
  
  public RecordMetaInfo getHrmTrngCrsDetailMetaInfo(String lPrimaryKey) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, lPrimaryKey));
    oParameters.add(new DBObject(2, 2, -5));
    oParameters.add(new DBObject(3, 2, 93));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSMST.proc_HrTrngCrsDtlCount(?,?,?,?,?,?)");
    RecordMetaInfo oRecordMetaInfo = new RecordMetaInfo();
    DBObject oTotalRecord = oOutArray.get(0);
    oRecordMetaInfo.setRecordCount(((Long)oTotalRecord.getObject()).longValue());
    DBObject oTimeObject = oOutArray.get(1);
    oRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    return oRecordMetaInfo;
  }
  
  public ArrayList getHrmTrngCrsDetail(String lPrimaryKey, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList oHrmTrngCrsDetail = null;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, -5, new Long(lDetailFirstPosition)));
    oParameters.add(new DBObject(2, 1, -5, new Long(lDetailLastPosition)));
    oParameters.add(new DBObject(3, 1, 12, lPrimaryKey));
    oParameters.add(new DBObject(4, 2, -10));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 12));
    oParameters.add(new DBObject(7, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSMST.proc_GetHrTrngCrsDtlInfo(?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    QueryRow oRow = null;
    QueryValue oValue = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      if (count == 0)
        oHrmTrngCrsDetail = new ArrayList(); 
      count++;
      oRow = oIt.next();
      HrmTrngCrsMstDtlBean oTrngCrsDetailBean = new HrmTrngCrsMstDtlBean();
      oTrngCrsDetailBean.setDetailId(oRow.get("ID").getString());
      oTrngCrsDetailBean.setTxtCourse(oRow.get("COURSE").getString());
      oTrngCrsDetailBean.setTxtName(oRow.get("NAME").getString());
      oTrngCrsDetailBean.setTxtMaxAttend(oRow.get("Max_Attend").getString());
      oTrngCrsDetailBean.setTxtMinAttend(oRow.get("Min_Attend").getString());
      oHrmTrngCrsDetail.add(oTrngCrsDetailBean);
    } 
    return oHrmTrngCrsDetail;
  }
  
  public ArrayList getHrmTrngCrsHeaderInfoById(String sId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    ArrayList oHeaderList = null;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sId));
    oParameters.add(new DBObject(2, 2, -10));
    oParameters.add(new DBObject(3, 2, 12));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSMST.proc_GetHrTrngCrsHdrInfoById(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      if (count == 0)
        oHeaderList = new ArrayList(); 
      count++;
      oRow = oIt.next();
      HrmTrngCrsMstHdrBean oTrngCrsHeader = new HrmTrngCrsMstHdrBean();
      oTrngCrsHeader.setHeaderPrimaryKey(oRow.get("ID").getString());
      oTrngCrsHeader.setTrainingBranch(oRow.get("BRANCH").getString());
      oTrngCrsHeader.setBranchName(oRow.get("NAME").getString());
      oHeaderList.add(oTrngCrsHeader);
    } 
    return oHeaderList;
  }
  
  public ArrayList getHrmTrngCrsDetailByHdrIdDtlCrs(String sId, String sCourse) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList oHrmTrngCrsDetail = null;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sId));
    oParameters.add(new DBObject(2, 1, 12, sCourse));
    oParameters.add(new DBObject(3, 2, -10));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSMST.proc_GetTrngCrsDtlByCrs(?,?,?,?,?.?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    QueryRow oRow = null;
    QueryValue oValue = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      if (count == 0)
        oHrmTrngCrsDetail = new ArrayList(); 
      count++;
      oRow = oIt.next();
      HrmTrngCrsMstDtlBean oTrngCrsDetailBean = new HrmTrngCrsMstDtlBean();
      oTrngCrsDetailBean.setDetailId(oRow.get("ID").getString());
      oTrngCrsDetailBean.setTxtCourse(oRow.get("COURSE").getString());
      oTrngCrsDetailBean.setTxtName(oRow.get("NAME").getString());
      oHrmTrngCrsDetail.add(oTrngCrsDetailBean);
    } 
    return oHrmTrngCrsDetail;
  }
  
  public LovVO getHrmTrgMstBranchCodeLOVdataQmode(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("trgMstBranchId");
    oHeaderList.add("hrm.HrmTrainingCourseMst.trainingBranch");
    oHeaderList.add("hrm.HrmTrainingCourseMst.branchName");
    oLovVO.setHeaderList(oHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    if (oLovQueryVO.getSearchField2() != null)
      sQuerySearch2 = oLovQueryVO.getSearchField2(); 
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    oParameters.add(new DBObject(2, 1, 12, sQuerySearch2));
    oParameters.add(new DBObject(3, 2, -10));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSMST.proc_GetTrngMstCodeQmode(?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = oList.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        oList = new ArrayList(); 
      count++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("ID").getString());
      oLOVBean.setDetailField2(oRow.get("BRANCH").getString());
      oLOVBean.setDetailField3(oRow.get("NAME").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    HrmTrngCrsMstHdrBean oHrmTrngCrsMstHdrBean = (HrmTrngCrsMstHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "i"));
      oParameters.add(new DBObject(2, 1, 12, oHrmTrngCrsMstHdrBean.getTrainingBranch()));
      oParameters.add(new DBObject(3, 1, 12, oHrmTrngCrsMstHdrBean.getBranchName()));
      oParameters.add(new DBObject(4, 1, 12, "100"));
      oParameters.add(new DBObject(5, 1, 12, "100"));
      oParameters.add(new DBObject(6, 1, 12, null));
      oParameters.add(new DBObject(7, 2, 12));
      oParameters.add(new DBObject(8, 2, 12));
      oParameters.add(new DBObject(9, 2, 12));
      oParameters.add(new DBObject(10, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSMST.proc_UpsertHrTrngCrsHdr(?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
    } else if (ScreenMode.equalsIgnoreCase("U")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "u"));
      oParameters.add(new DBObject(2, 1, 12, oHrmTrngCrsMstHdrBean.getTrainingBranch()));
      oParameters.add(new DBObject(3, 1, 12, oHrmTrngCrsMstHdrBean.getBranchName()));
      oParameters.add(new DBObject(4, 1, 12, "100"));
      oParameters.add(new DBObject(5, 1, 12, "100"));
      oParameters.add(new DBObject(6, 1, 12, oHrmTrngCrsMstHdrBean.getHeaderPrimaryKey()));
      oParameters.add(new DBObject(7, 2, 12));
      oParameters.add(new DBObject(8, 2, 12));
      oParameters.add(new DBObject(9, 2, 12));
      oParameters.add(new DBObject(10, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSMST.proc_UpsertHrTrngCrsHdr(?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
    } else if (ScreenMode.equalsIgnoreCase("D")) {
      oParameters.add(new DBObject(1, 1, 12, oHrmTrngCrsMstHdrBean.getHeaderPrimaryKey()));
      oParameters.add(new DBObject(2, 2, 12));
      oParameters.add(new DBObject(3, 2, 12));
      oParameters.add(new DBObject(4, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSMST.proc_DeleteTrngBranchMst(?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = null;
    } 
    return returnString;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_TRNG_BRANCH_MST";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    if (sScreenName.equalsIgnoreCase("HrmTrngCrsMst"))
      saveHrmTrngCourseDetail(sHeaderPrimaryKey, oDetailBeanArray); 
  }
  
  private void saveHrmTrngCourseDetail(String sHeaderPrimaryKey, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bUpdate = false;
    boolean bDelete = false;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = null;
    DBUtilitiesBean oBean1 = null;
    Iterator oIt = oDetailBeanArray.iterator();
    while (oIt.hasNext()) {
      HrmTrngCrsMstDtlBean oHrmTrngCrsMstDtlBean = oIt.next();
      if (oHrmTrngCrsMstDtlBean.getStatus().equals("N")) {
        if (!bInsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMTRNGCRSMST.proc_UpsertHrTrngCrsDtl(?,?,?,?,?,?,?,?,?)");
          bInsert = true;
        } 
        oParameters = new ArrayList();
        oParameters.add(new DBObject(1, 1, 12, "I"));
        oParameters.add(new DBObject(2, 1, 12, null));
        oParameters.add(new DBObject(3, 1, 12, sHeaderPrimaryKey));
        oParameters.add(new DBObject(4, 1, 12, oHrmTrngCrsMstDtlBean.getTxtCourse()));
        oParameters.add(new DBObject(5, 1, 12, oHrmTrngCrsMstDtlBean.getTxtName()));
        oParameters.add(new DBObject(6, 1, 12, "200"));
        oParameters.add(new DBObject(7, 1, 12, "200"));
        oParameters.add(new DBObject(8, 1, 12, oHrmTrngCrsMstDtlBean.getTxtMaxAttend()));
        oParameters.add(new DBObject(9, 1, 12, oHrmTrngCrsMstDtlBean.getTxtMinAttend()));
        System.out.println(oHrmTrngCrsMstDtlBean.getTxtName());
        oBean.addToBatch(oParameters);
        continue;
      } 
      if (oHrmTrngCrsMstDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMTRNGCRSMST.proc_UpsertHrTrngCrsDtl(?,?,?,?,?,?,?,?,?)");
          bUpdate = true;
        } 
        oParameters = new ArrayList();
        oParameters.add(new DBObject(1, 1, 12, "U"));
        oParameters.add(new DBObject(2, 1, 12, oHrmTrngCrsMstDtlBean.getDetailId()));
        oParameters.add(new DBObject(3, 1, 12, sHeaderPrimaryKey));
        oParameters.add(new DBObject(4, 1, 12, oHrmTrngCrsMstDtlBean.getTxtCourse()));
        oParameters.add(new DBObject(5, 1, 12, oHrmTrngCrsMstDtlBean.getTxtName()));
        oParameters.add(new DBObject(6, 1, 12, "200"));
        oParameters.add(new DBObject(7, 1, 12, "200"));
        oParameters.add(new DBObject(8, 1, 12, oHrmTrngCrsMstDtlBean.getTxtMaxAttend()));
        oParameters.add(new DBObject(9, 1, 12, oHrmTrngCrsMstDtlBean.getTxtMinAttend()));
        oBean.addToBatch(oParameters);
        continue;
      } 
      if (oHrmTrngCrsMstDtlBean.getStatus().equals("D")) {
        if (!bDelete) {
          oBean1 = new DBUtilitiesBean();
          oBean1.createBatch("HRMTRNGCRSMST.proc_DeleteTrngCrsMst(?)");
          bDelete = true;
        } 
        oParameters = new ArrayList();
        System.out.println(oHrmTrngCrsMstDtlBean.getDetailId());
        oParameters.add(new DBObject(1, 1, 12, oHrmTrngCrsMstDtlBean.getDetailId()));
        oBean1.addToBatch(oParameters);
      } 
    } 
    if (bInsert)
      oBean.executeBatch(); 
    if (bUpdate)
      oBean.executeBatch(); 
    if (bDelete)
      oBean1.executeBatch(); 
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {}
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return new String();
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oErrorList = new ArrayList();
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        checkMandatoryHeader((HrmTrngCrsMstHdrBean)oBaseHeaderBean);  
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmTrngCrsMst")) {
        Iterator itrBean1 = oDetailBeanArray.iterator();
        int iCount = 1;
        while (itrBean1.hasNext()) {
          HrmTrngCrsMstDtlBean oHrmTrngCrsMstDtlBean = itrBean1.next();
          if (!oHrmTrngCrsMstDtlBean.getStatus().equalsIgnoreCase("D")) {
            checkMandatory(oHrmTrngCrsMstDtlBean, iCount, oErrorList);
            EnrgiseUtil.checkDuplicate(oDetailBeanArray, "txtCourse", "hrm.HrmTrainingCourseMst.course", oErrorList, true);
          } 
          iCount++;
        } 
      }  
    reportError(oErrorList);
  }
  
  private void checkMandatoryHeader(HrmTrngCrsMstHdrBean oHrmTrngCrsMstHdrBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oErrorList = new ArrayList();
    ArrayList oParams = new ArrayList();
    if (!EnrgiseUtil.checkString(oHrmTrngCrsMstHdrBean.getTrainingBranch())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmTrainingCourseMst.trainingBranch");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(oErrorList);
  }
  
  private void checkMandatory(HrmTrngCrsMstDtlBean oHrmTrngCrsMstDtlBean, int iCount, ArrayList oErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {
    if (!EnrgiseUtil.checkString(oHrmTrngCrsMstDtlBean.getTxtCourse())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmTrainingCourseMst.course");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(new Integer(iCount));
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(oErrorList);
  }
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    HrmTrngCrsMstHdrBean oHrmTrngCrsMstHdrBean = (HrmTrngCrsMstHdrBean)oBaseHeaderBean;
    ArrayList oList = new ArrayList();
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D")) {
        checkUniqueHeader((HrmTrngCrsMstHdrBean)oBaseHeaderBean);
      } else if (sScreenMode.equalsIgnoreCase("D")) {
        HrmCommonBO oCommonBO = new HrmCommonBO();
        String sRefId = oCommonBO.getRefList("HRM_TRNG_CRS_MST", "BRANCH_ID ", oHrmTrngCrsMstHdrBean.getHeaderPrimaryKey(), "ID");
        if (EnrgiseUtil.checkString(sRefId))
          if (oList.isEmpty())
            oList.add(new EnrgiseApplicationException("wenrgise.common.deletion"));  
        String sRefId2 = oCommonBO.getRefList("HRM_TRNG_INST_CRS_MAP", "TRN_CORSMS_ID ", oHrmTrngCrsMstHdrBean.getHeaderPrimaryKey(), "ID");
        if (EnrgiseUtil.checkString(sRefId2))
          if (oList.isEmpty())
            oList.add(new EnrgiseApplicationException("wenrgise.common.deletion"));  
      }  
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmTrngCrsMst")) {
        Iterator oIt1 = oDetailBeanArray.iterator();
        while (oIt1.hasNext()) {
          HrmTrngCrsMstDtlBean oHrmTrngCrsMstDtlBean = oIt1.next();
          if (!oHrmTrngCrsMstDtlBean.getStatus().equalsIgnoreCase("D"))
            continue; 
          if (oHrmTrngCrsMstDtlBean.getStatus().equalsIgnoreCase("D")) {
            HrmCommonBO oCommonBO = new HrmCommonBO();
            String sQuery = String.valueOf(String.valueOf("Select i.ID as ID from HRM_TRNG_INST_CRS_MAP i,hrm_trng_crs_mst m where m.id=i.TRN_CORSMS_ID and m.COURSE='").concat(String.valueOf(oHrmTrngCrsMstDtlBean.getTxtCourse()))).concat(String.valueOf("'"));
            System.out.println(sQuery);
            DBUtilitiesBean oBean = new DBUtilitiesBean();
            ArrayList oList1 = oBean.executeQuery(sQuery);
            if (oList1.size() > 0)
              oList.add(new EnrgiseApplicationException("wenrgise.common.deletion")); 
          } 
        } 
      }  
    reportError(oList);
  }
  
  private void checkUniqueHeader(HrmTrngCrsMstHdrBean oHrmTrngCrsMstHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = String.valueOf(String.valueOf(" Select ID as ID from HRM_TRNG_BRANCH_MST where BRANCH='").concat(String.valueOf(oHrmTrngCrsMstHdrBean.getTrainingBranch()))).concat(String.valueOf("'"));
    System.out.println(sQuery);
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList arylstList = oBean.executeQuery(sQuery);
    Iterator itrBean = arylstList.iterator();
    ArrayList oList = new ArrayList();
    if (itrBean.hasNext()) {
      QueryRow oRow = itrBean.next();
      if (!oRow.get("ID").getString().equalsIgnoreCase(oHrmTrngCrsMstHdrBean.getHeaderPrimaryKey()))
        oList.add(new EnrgiseApplicationException("wenrgise.common.uniqueConstraintViolated")); 
    } 
    reportError(oList);
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
}
