package wenrgise.hrms.ejb.business;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
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.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.HrmTrngCrsBudDtlBean;
import wenrgise.hrms.bean.HrmTrngCrsBudHdrBean;
import wenrgise.hrms.vo.HrmTrngCrsBudQVO;

public class HrmTrngCrsBudBO extends HrmBaseBO {
  public HrmTrngCrsBudBO() {}
  
  public HrmTrngCrsBudBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmTrngCrsBudHdrMetaInfo(HrmTrngCrsBudQVO oHrmTrngCrsBudQVO) 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, oHrmTrngCrsBudQVO.getCalendarId()));
    oParameters.add(new DBObject(2, 1, 12, oHrmTrngCrsBudQVO.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, "HRMTRNGCRSBUD.proc_HrTrngCrsBudHdrCount(?,?,?,?,?,?,?)");
    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 getHrmTrngCrsBudHeaderInfo(HrmTrngCrsBudQVO oHrmTrngCrsBudQVO, 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 (oHrmTrngCrsBudQVO == null)
      oHrmTrngCrsBudQVO = new HrmTrngCrsBudQVO(); 
    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, oHrmTrngCrsBudQVO.getCalendarId()));
    oParameters.add(new DBObject(4, 1, 12, oHrmTrngCrsBudQVO.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, "HRMTRNGCRSBUD.proc_GetHrTrngCrsBudHdr(?,?,?,?,?,?,?,?)");
    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();
      HrmTrngCrsBudHdrBean oTrngCrsBudHeader = new HrmTrngCrsBudHdrBean();
      oTrngCrsBudHeader.setHeaderPrimaryKey(oRow.get("ID").getString());
      oTrngCrsBudHeader.setCalendarId(oRow.get("TRNG_CAL_ID").getString());
      oTrngCrsBudHeader.setCalendarName(oRow.get("NAME").getString());
      oTrngCrsBudHeader.setApprovedBudget(oRow.get("APPROVED_BUDGET").getString());
      oTrngCrsBudHeader.setAvailableBudget(oRow.get("AVAILABLE_BUDGET").getString());
      oTrngCrsBudHeader.setSpentTillDate(oRow.get("SPENT_TILL_DATE").getString());
      oHeaderList.add(oTrngCrsBudHeader);
    } 
    return oHeaderList;
  }
  
  public RecordMetaInfo getHrmTrngCrsBudDtlMetaInfo(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, "HRMTRNGCRSBUD.proc_HrTrngCrsBudDtlCount(?,?,?,?,?,?)");
    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 getHrmTrngCrsBudDetailInfo(String lPrimaryKey, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList oHrmTrngCrsBudDetail = 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, "HRMTRNGCRSBUD.proc_GetHrTrngCrsBudDtl(?,?,?,?,?,?,?)");
    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();
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    while (oIt.hasNext()) {
      if (count == 0)
        oHrmTrngCrsBudDetail = new ArrayList(); 
      count++;
      oRow = oIt.next();
      HrmTrngCrsBudDtlBean oTrngCrsBudDetailBean = new HrmTrngCrsBudDtlBean();
      oTrngCrsBudDetailBean.setDetailId(oRow.get("ID").getString());
      oTrngCrsBudDetailBean.setTxtCourseId(oRow.get("trn_corsms_id").getString());
      oTrngCrsBudDetailBean.setTxtCourseName(oRow.get("name").getString());
      oTrngCrsBudDetailBean.setTxtFromDate(EnrgiseUtil.convertToString(oRow.get("start_date").getDate()));
      oTrngCrsBudDetailBean.setTxtToDate(EnrgiseUtil.convertToString(oRow.get("end_date").getDate()));
      oTrngCrsBudDetailBean.setTxtTravel(oRow.get("travel").getString());
      oTrngCrsBudDetailBean.setTxtAccomodation(oRow.get("accomodation").getString());
      oTrngCrsBudDetailBean.setTxtDailyAllowances(oRow.get("daily_allowances").getString());
      oTrngCrsBudDetailBean.setTxtFood(oRow.get("food_expenses").getString());
      oTrngCrsBudDetailBean.setTxtFees(oRow.get("training_fees").getString());
      oTrngCrsBudDetailBean.setTxtMisc(oRow.get("misc").getString());
      oTrngCrsBudDetailBean.setTxtTotal(oRow.get("total").getString());
      oHrmTrngCrsBudDetail.add(oTrngCrsBudDetailBean);
    } 
    return oHrmTrngCrsBudDetail;
  }
  
  public LovVO getHrmTrngBudCodeLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderNames = new ArrayList();
    arylstHeaderNames.add("Course Id");
    arylstHeaderNames.add("hrm.HrmTrainingBudget.course");
    arylstHeaderNames.add("hrm.HrmTrainingBudget.courseName");
    oLovVO.setHeaderList(arylstHeaderNames);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = new ArrayList();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    if (oLovQueryVO.getSearchField2() != null)
      sQuerySearch2 = oLovQueryVO.getSearchField2(); 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    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, "HRMTRNGCRSBUD.proc_GetTrngCourseLOV(?,?,?,?,?,?)");
    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("COURSE").getString());
      oLOVBean.setDetailField3(oRow.get("NAME").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public LovVO getHrmTrngCalNameLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderNames = new ArrayList();
    arylstHeaderNames.add("Id");
    arylstHeaderNames.add("hrm.HrmFacTrngDtl.calendarName");
    arylstHeaderNames.add("hrm.HrmFacTrngDtl.startDate");
    arylstHeaderNames.add("hrm.HrmFacTrngDtl.endDate");
    oLovVO.setHeaderList(arylstHeaderNames);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = new ArrayList();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    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, "HRMTRNGCRSBUD.proc_GetTrngCalNameLOV(?,?,?,?,?)");
    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("NAME").getString());
      oLOVBean.setDetailField3(EnrgiseUtil.convertToString(oRow.get("START_DATE").getDate()));
      oLOVBean.setDetailField4(EnrgiseUtil.convertToString(oRow.get("END_DATE").getDate()));
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public LovVO getHrmTrngSiteLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderNames = new ArrayList();
    arylstHeaderNames.add("Id");
    arylstHeaderNames.add("hrm.HrmFacTrngDtl.site");
    arylstHeaderNames.add("hrm.HrmFacTrngDtl.siteName");
    oLovVO.setHeaderList(arylstHeaderNames);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = new ArrayList();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    if (oLovQueryVO.getSearchField2() != null)
      sQuerySearch2 = oLovQueryVO.getSearchField2(); 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    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, "HRMTRNGCRSBUD.proc_GetTrngSiteLOV(?,?,?,?,?,?)");
    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("CODE").getString());
      oLOVBean.setDetailField3(oRow.get("NAME").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public LovVO getHrmTrngBudCalendarCodeLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderNames = new ArrayList();
    arylstHeaderNames.add("Calendar Id");
    arylstHeaderNames.add("hrm.HrmTrainingBudget.calendarName");
    oLovVO.setHeaderList(arylstHeaderNames);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = new ArrayList();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    if (oLovQueryVO.getSearchField2() != null)
      sQuerySearch2 = oLovQueryVO.getSearchField2(); 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    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, "HRMTRNGCRSBUD.proc_GetTrngCalendarLOV(?,?,?,?,?,?)");
    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("NAME").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public LovVO getHrmTrngBudCourseCodeLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderNames = new ArrayList();
    arylstHeaderNames.add("Course Id");
    arylstHeaderNames.add("hrm.HrmTrainingBudget.courseName");
    oLovVO.setHeaderList(arylstHeaderNames);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = new ArrayList();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    if (oLovQueryVO.getProperty("calendarId") != null)
      sQuerySearch2 = oLovQueryVO.getProperty("calendarId"); 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    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, "HRMTRNGCRSBUD.proc_GetTrngBudCourseLOV(?,?,?,?,?,?)");
    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("COURSE_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();
    String sAvailableBudget = new String();
    String sSpentTillDate = new String();
    HrmTrngCrsBudHdrBean oHrmTrngCrsBudHdrBean = (HrmTrngCrsBudHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "i"));
      oParameters.add(new DBObject(2, 1, 12, oHrmTrngCrsBudHdrBean.getCalendarId()));
      oParameters.add(new DBObject(3, 1, 12, oHrmTrngCrsBudHdrBean.getAvailableBudget()));
      oParameters.add(new DBObject(4, 1, 12, oHrmTrngCrsBudHdrBean.getApprovedBudget()));
      oParameters.add(new DBObject(5, 1, 12, oHrmTrngCrsBudHdrBean.getSpentTillDate()));
      oParameters.add(new DBObject(6, 1, 12, this.oUserInfo.getUserTypeId()));
      oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(8, 1, 12, null));
      oParameters.add(new DBObject(9, 2, 12));
      oParameters.add(new DBObject(10, 2, 12));
      oParameters.add(new DBObject(11, 2, 12));
      oParameters.add(new DBObject(12, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSBUD.proc_UpsertHrTrngBudHdr(?,?,?,?,?,?,?,?,?,?,?,?)");
      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, oHrmTrngCrsBudHdrBean.getCalendarId()));
      oParameters.add(new DBObject(3, 1, 12, sAvailableBudget));
      oParameters.add(new DBObject(4, 1, 12, oHrmTrngCrsBudHdrBean.getApprovedBudget()));
      oParameters.add(new DBObject(5, 1, 12, sSpentTillDate));
      oParameters.add(new DBObject(6, 1, 12, this.oUserInfo.getUserTypeId()));
      oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(8, 1, 12, oHrmTrngCrsBudHdrBean.getHeaderPrimaryKey()));
      oParameters.add(new DBObject(9, 2, 12));
      oParameters.add(new DBObject(10, 2, 12));
      oParameters.add(new DBObject(11, 2, 12));
      oParameters.add(new DBObject(12, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGCRSBUD.proc_UpsertHrTrngBudHdr(?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      updateHeaderField1(oHrmTrngCrsBudHdrBean);
    } 
    return returnString;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_TRNG_BUD_HDR";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    if (sScreenName.equalsIgnoreCase("HrmTrngBudget"))
      saveHrmTrngBudDetail(sHeaderPrimaryKey, oDetailBeanArray); 
  }
  
  private void saveHrmTrngBudDetail(String sHeaderPrimaryKey, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bUpdate = false;
    boolean bDelete = false;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = null;
    String sTotal = new String();
    Iterator oIt = oDetailBeanArray.iterator();
    while (oIt.hasNext()) {
      HrmTrngCrsBudDtlBean oHrmTrngCrsBudDtlBean = oIt.next();
      sTotal = calculateTotalExp(oHrmTrngCrsBudDtlBean);
      if (oHrmTrngCrsBudDtlBean.getStatus().equals("N")) {
        if (!bInsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMTRNGCRSBUD.proc_UpsertHrTrngBudDtl(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
          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, oHrmTrngCrsBudDtlBean.getTxtCourseId()));
        oParameters.add(new DBObject(5, 1, 12, oHrmTrngCrsBudDtlBean.getTxtTravel()));
        oParameters.add(new DBObject(6, 1, 12, oHrmTrngCrsBudDtlBean.getTxtAccomodation()));
        oParameters.add(new DBObject(7, 1, 12, oHrmTrngCrsBudDtlBean.getTxtDailyAllowances()));
        oParameters.add(new DBObject(8, 1, 12, oHrmTrngCrsBudDtlBean.getTxtFood()));
        oParameters.add(new DBObject(9, 1, 12, oHrmTrngCrsBudDtlBean.getTxtFees()));
        oParameters.add(new DBObject(10, 1, 12, oHrmTrngCrsBudDtlBean.getTxtMisc()));
        oParameters.add(new DBObject(11, 1, 12, sTotal));
        oParameters.add(new DBObject(12, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmTrngCrsBudDtlBean.getTxtFromDate())));
        oParameters.add(new DBObject(13, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmTrngCrsBudDtlBean.getTxtToDate())));
        oParameters.add(new DBObject(14, 1, 12, this.oUserInfo.getUserTypeId()));
        oParameters.add(new DBObject(15, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(oParameters);
        continue;
      } 
      if (oHrmTrngCrsBudDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMTRNGCRSBUD.proc_UpsertHrTrngBudDtl(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
          bUpdate = true;
        } 
        oParameters = new ArrayList();
        oParameters.add(new DBObject(1, 1, 12, "U"));
        oParameters.add(new DBObject(2, 1, 12, oHrmTrngCrsBudDtlBean.getDetailId()));
        oParameters.add(new DBObject(3, 1, 12, sHeaderPrimaryKey));
        oParameters.add(new DBObject(4, 1, 12, oHrmTrngCrsBudDtlBean.getTxtCourseId()));
        oParameters.add(new DBObject(5, 1, 12, oHrmTrngCrsBudDtlBean.getTxtTravel()));
        oParameters.add(new DBObject(6, 1, 12, oHrmTrngCrsBudDtlBean.getTxtAccomodation()));
        oParameters.add(new DBObject(7, 1, 12, oHrmTrngCrsBudDtlBean.getTxtDailyAllowances()));
        oParameters.add(new DBObject(8, 1, 12, oHrmTrngCrsBudDtlBean.getTxtFood()));
        oParameters.add(new DBObject(9, 1, 12, oHrmTrngCrsBudDtlBean.getTxtFees()));
        oParameters.add(new DBObject(10, 1, 12, oHrmTrngCrsBudDtlBean.getTxtMisc()));
        oParameters.add(new DBObject(11, 1, 12, sTotal));
        oParameters.add(new DBObject(12, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmTrngCrsBudDtlBean.getTxtFromDate())));
        oParameters.add(new DBObject(13, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmTrngCrsBudDtlBean.getTxtToDate())));
        oParameters.add(new DBObject(14, 1, 12, this.oUserInfo.getUserTypeId()));
        oParameters.add(new DBObject(15, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(oParameters);
        continue;
      } 
      if (oHrmTrngCrsBudDtlBean.getStatus().equals("D")) {
        if (!bDelete) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMTRNGCRSBUD.proc_DeleteHrTrngBudDtl(?)");
          bDelete = true;
        } 
        oParameters = new ArrayList();
        oParameters.add(new DBObject(1, 1, 12, oHrmTrngCrsBudDtlBean.getDetailId()));
        oBean.addToBatch(oParameters);
      } 
    } 
    if (bInsert)
      oBean.executeBatch(); 
    if (bUpdate)
      oBean.executeBatch(); 
    if (bDelete)
      oBean.executeBatch(); 
    updateHeaderField(sHeaderPrimaryKey, oDetailBeanArray);
  }
  
  private void updateHeaderField(String sHeaderPrimaryKey, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    ArrayList arylstOutList = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sTotal = new String();
    float fSpent = 0.0F;
    Iterator oIt = oDetailBeanArray.iterator();
    while (oIt.hasNext()) {
      HrmTrngCrsBudDtlBean oHrmTrngCrsBudDtlBean = oIt.next();
      sTotal = calculateTotalExp(oHrmTrngCrsBudDtlBean);
      fSpent += Float.valueOf(sTotal).floatValue();
    } 
    arylstParameters.add(new DBObject(1, 1, 12, sHeaderPrimaryKey));
    arylstParameters.add(new DBObject(2, 1, 12, String.valueOf(fSpent)));
    arylstParameters.add(new DBObject(3, 2, 12));
    arylstParameters.add(new DBObject(4, 2, 12));
    arylstParameters.add(new DBObject(5, 2, 4));
    arylstOutList = oBean.callProc(arylstParameters, "HRMTRNGCRSBUD.procUpdateHeaderField(?,?,?,?,?)");
    DBObject oObj = arylstOutList.get(0);
    String sErrorLOg = oObj.toString();
  }
  
  private void updateHeaderField1(HrmTrngCrsBudHdrBean oHrmTrngCrsBudHdrBean) throws EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    ArrayList arylstOutList = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sSpent = new String();
    float fSpent = Float.valueOf(oHrmTrngCrsBudHdrBean.getSpentTillDate()).floatValue();
    arylstParameters.add(new DBObject(1, 1, 12, oHrmTrngCrsBudHdrBean.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(2, 1, 12, String.valueOf(fSpent)));
    arylstParameters.add(new DBObject(3, 2, 12));
    arylstParameters.add(new DBObject(4, 2, 12));
    arylstParameters.add(new DBObject(5, 2, 4));
    arylstOutList = oBean.callProc(arylstParameters, "HRMTRNGCRSBUD.procUpdateHeaderField(?,?,?,?,?)");
    DBObject oObj = arylstOutList.get(0);
    String sErrorLOg = oObj.toString();
  }
  
  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((HrmTrngCrsBudHdrBean)oBaseHeaderBean);  
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmTrngBudget")) {
        Iterator itrBean1 = oDetailBeanArray.iterator();
        int iCount = 1;
        while (itrBean1.hasNext()) {
          HrmTrngCrsBudDtlBean oHrmTrngCrsBudDtlBean = itrBean1.next();
          if (!oHrmTrngCrsBudDtlBean.getStatus().equalsIgnoreCase("D"))
            checkMandatory(oHrmTrngCrsBudDtlBean, iCount, oErrorList); 
          iCount++;
        } 
      }  
    reportError(oErrorList);
  }
  
  private void checkMandatory(HrmTrngCrsBudDtlBean oHrmTrngCrsBudDtlBean, int iCount, ArrayList oErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {
    if (!EnrgiseUtil.checkString(oHrmTrngCrsBudDtlBean.getTxtCourseName())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmTrainingBudget.courseName");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(new Integer(iCount));
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmTrngCrsBudDtlBean.getTxtFromDate())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmTrainingBudget.fromDate");
      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 additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oErrorList = new ArrayList();
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmTrngBudget")) {
        Iterator oIt1 = oDetailBeanArray.iterator();
        while (oIt1.hasNext()) {
          HrmTrngCrsBudDtlBean oHrmTrngCrsBudDtlBean = oIt1.next();
          if (!oHrmTrngCrsBudDtlBean.getStatus().equalsIgnoreCase("D"))
            compareFields(oDetailBeanArray, oBaseHeaderBean, oErrorList); 
        } 
      }  
    reportError(oErrorList);
  }
  
  private void checkMandatoryHeader(HrmTrngCrsBudHdrBean oHrmTrngCrsBudHdrBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oErrorList = new ArrayList();
    if (!EnrgiseUtil.checkString(oHrmTrngCrsBudHdrBean.getCalendarName())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmTrainingBudget.calendarName");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmTrngCrsBudHdrBean.getApprovedBudget())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmTrainingBudget.approvedBudget");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(oErrorList);
  }
  
  private void compareFields(ArrayList oDetailBeanArray, BaseHeaderBean oBaseHeaderBean, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = null;
    String sTotal = new String();
    float fSpent = 0.0F;
    Iterator oIt = oDetailBeanArray.iterator();
    while (oIt.hasNext()) {
      HrmTrngCrsBudDtlBean oHrmTrngCrsBudDtlBean = oIt.next();
      sTotal = calculateTotalExp(oHrmTrngCrsBudDtlBean);
      fSpent += Float.valueOf(sTotal).floatValue();
    } 
    sTotal = String.valueOf(fSpent);
    HrmTrngCrsBudHdrBean oHrmTrngCrsBudHdrBean = (HrmTrngCrsBudHdrBean)oBaseHeaderBean;
    if (Float.valueOf(sTotal).floatValue() > Float.valueOf(oHrmTrngCrsBudHdrBean.getApprovedBudget()).floatValue()) {
      arylstParam = new ArrayList();
      arylstParam.add("Training Budget: Spent Amount");
      arylstParam.add(new Integer(0));
      oErrorList.add(new EnrgiseMessageKeyException("hrm.HrmTrainingBudget.compareAmount", arylstParam));
    } 
    reportError(oErrorList);
  }
  
  private void checkDtlFieldsConsistency(ArrayList oDetailBeanArray, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oList = new ArrayList();
    int count = 0;
    Iterator oIt = oDetailBeanArray.iterator();
    int n = oDetailBeanArray.size();
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    String sOldToDate = null;
    while (oIt.hasNext()) {
      HrmTrngCrsBudDtlBean oBean = oIt.next();
      String sFromDate = oBean.getTxtFromDate();
      if (EnrgiseUtil.compareDates(dateFormat, sFromDate, sOldToDate) == -1)
        oErrorList.add(new EnrgiseApplicationException("wenrgise.common.PrevRowdateConstraintViolated")); 
      sOldToDate = oBean.getTxtToDate();
    } 
  }
  
  private String calculateTotalExp(HrmTrngCrsBudDtlBean oHrmTrngCrsBudDtlBean) throws EnrgiseSystemException {
    ArrayList oList = new ArrayList();
    float fTravel = 0.0F;
    float fAccomodation = 0.0F;
    float fDailyAllowances = 0.0F;
    float fFood = 0.0F;
    float fFees = 0.0F;
    float fMisc = 0.0F;
    if (EnrgiseUtil.checkString(oHrmTrngCrsBudDtlBean.getTxtTravel()))
      fTravel = Float.valueOf(oHrmTrngCrsBudDtlBean.getTxtTravel()).floatValue(); 
    if (EnrgiseUtil.checkString(oHrmTrngCrsBudDtlBean.getTxtAccomodation()))
      fAccomodation = Float.valueOf(oHrmTrngCrsBudDtlBean.getTxtAccomodation()).floatValue(); 
    if (EnrgiseUtil.checkString(oHrmTrngCrsBudDtlBean.getTxtDailyAllowances()))
      fDailyAllowances = Float.valueOf(oHrmTrngCrsBudDtlBean.getTxtDailyAllowances()).floatValue(); 
    if (EnrgiseUtil.checkString(oHrmTrngCrsBudDtlBean.getTxtFood()))
      fFood = Float.valueOf(oHrmTrngCrsBudDtlBean.getTxtFood()).floatValue(); 
    if (EnrgiseUtil.checkString(oHrmTrngCrsBudDtlBean.getTxtFees()))
      fFees = Float.valueOf(oHrmTrngCrsBudDtlBean.getTxtFees()).floatValue(); 
    if (EnrgiseUtil.checkString(oHrmTrngCrsBudDtlBean.getTxtMisc()))
      fMisc = Float.valueOf(oHrmTrngCrsBudDtlBean.getTxtMisc()).floatValue(); 
    float fTotal = fMisc + fFood + fDailyAllowances + fAccomodation + fTravel + fFees;
    return String.valueOf(fTotal);
  }
}
