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.HrmOrgHolCalDtlBean;
import wenrgise.hrms.bean.HrmOrgHolCalHdrBean;
import wenrgise.hrms.vo.HrmOrgHolCalQVO;

public class HrmOrgHolCalBO extends HrmBaseBO {
  public HrmOrgHolCalBO() {}
  
  public HrmOrgHolCalBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmHolCalHdrMetaInfo(HrmOrgHolCalQVO oHrmOrgHolCalQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, oHrmOrgHolCalQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(2, 1, 12, oHrmOrgHolCalQVO.getCalendarName()));
    arylstParameters.add(new DBObject(3, 1, 12, oHrmOrgHolCalQVO.getSiteId()));
    arylstParameters.add(new DBObject(4, 1, 12, oHrmOrgHolCalQVO.getStateCode()));
    arylstParameters.add(new DBObject(5, 2, -5));
    arylstParameters.add(new DBObject(6, 2, 93));
    arylstParameters.add(new DBObject(7, 2, 12));
    arylstParameters.add(new DBObject(8, 2, 12));
    arylstParameters.add(new DBObject(9, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGHOLCALMST.proc_HrHolCalCount(?,?,?,?,?,?,?,?,?)");
    RecordMetaInfo oRecordMetaInfo = new RecordMetaInfo();
    DBObject oTimeObject = arylstOutArray.get(1);
    oRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    DBObject oTotalRecord = arylstOutArray.get(0);
    oRecordMetaInfo.setRecordCount(((Long)oTotalRecord.getObject()).longValue());
    return oRecordMetaInfo;
  }
  
  public ArrayList getHrmHolCalHeaderInfo(HrmOrgHolCalQVO oHrmOrgHolCalQVO, long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    ArrayList arylstHeaderList = null;
    if (oHrmOrgHolCalQVO == null)
      oHrmOrgHolCalQVO = new HrmOrgHolCalQVO(); 
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, -5, new Long(lStartPosition)));
    arylstParameters.add(new DBObject(2, 1, -5, new Long(lLastPosition)));
    arylstParameters.add(new DBObject(3, 1, 12, oHrmOrgHolCalQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(4, 1, 12, oHrmOrgHolCalQVO.getCalendarName()));
    arylstParameters.add(new DBObject(5, 1, 12, oHrmOrgHolCalQVO.getSiteId()));
    arylstParameters.add(new DBObject(6, 1, 12, oHrmOrgHolCalQVO.getStateCode()));
    arylstParameters.add(new DBObject(7, 2, -10));
    arylstParameters.add(new DBObject(8, 2, 12));
    arylstParameters.add(new DBObject(9, 2, 12));
    arylstParameters.add(new DBObject(10, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGHOLCALMST.proc_GetHrHolCal(?,?,?,?,?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    if (arylstList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator itrBean = arylstList.iterator();
    while (itrBean.hasNext()) {
      if (count == 0)
        arylstHeaderList = new ArrayList(); 
      count++;
      oRow = itrBean.next();
      HrmOrgHolCalHdrBean oHolCalHeader = new HrmOrgHolCalHdrBean();
      oHolCalHeader.setHeaderPrimaryKey(oRow.get("ID").getString());
      oHolCalHeader.setCalendarName(oRow.get("CAL_CODE").getString());
      oHolCalHeader.setDescription(oRow.get("DESCRIPTION").getString());
      oHolCalHeader.setFromDate(EnrgiseUtil.convertToString(oRow.get("FROM_DATE").getDate()));
      oHolCalHeader.setToDate(EnrgiseUtil.convertToString(oRow.get("TO_DATE").getDate()));
      oHolCalHeader.setSiteCode(oRow.get("NAME").getString());
      oHolCalHeader.setStateCode(oRow.get("state_code").getString());
      oHolCalHeader.setState(oRow.get("GLOBAL_CODE_DESC").getString());
      oHolCalHeader.setSiteId(oRow.get("SITE_ID").getString());
      arylstHeaderList.add(oHolCalHeader);
    } 
    return arylstHeaderList;
  }
  
  public RecordMetaInfo getHrmHolCalDetailMetaInfo(String lPrimaryKey) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, lPrimaryKey));
    arylstParameters.add(new DBObject(2, 2, -5));
    arylstParameters.add(new DBObject(3, 2, 93));
    arylstParameters.add(new DBObject(4, 2, 12));
    arylstParameters.add(new DBObject(5, 2, 12));
    arylstParameters.add(new DBObject(6, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGHOLCALMST.proc_HrHolCalDtlCount(?,?,?,?,?,?)");
    RecordMetaInfo oRecordMetaInfo = new RecordMetaInfo();
    DBObject oTotalRecord = arylstOutArray.get(0);
    oRecordMetaInfo.setRecordCount(((Long)oTotalRecord.getObject()).longValue());
    DBObject oTimeObject = arylstOutArray.get(1);
    oRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    return oRecordMetaInfo;
  }
  
  public ArrayList getHrmHolCalDetail(String lPrimaryKey, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList arylstHrmHolCalDetail = null;
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, -5, new Long(lDetailFirstPosition)));
    arylstParameters.add(new DBObject(2, 1, -5, new Long(lDetailLastPosition)));
    arylstParameters.add(new DBObject(3, 1, 12, lPrimaryKey));
    arylstParameters.add(new DBObject(4, 2, -10));
    arylstParameters.add(new DBObject(5, 2, 12));
    arylstParameters.add(new DBObject(6, 2, 12));
    arylstParameters.add(new DBObject(7, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGHOLCALMST.proc_GetHrHolCalDtlInfo(?,?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    if (arylstList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    QueryRow oRow = null;
    QueryValue oValue = null;
    HashMap oColumns = null;
    Iterator itrBean = arylstList.iterator();
    while (itrBean.hasNext()) {
      if (count == 0)
        arylstHrmHolCalDetail = new ArrayList(); 
      count++;
      oRow = itrBean.next();
      HrmOrgHolCalDtlBean oHrHolCalDtlBean = new HrmOrgHolCalDtlBean();
      oHrHolCalDtlBean.setDetailId(oRow.get("ID").getString());
      oHrHolCalDtlBean.setTxtHolidayDate(EnrgiseUtil.convertToString(oRow.get("HOLIDAY_DATE").getDate()));
      oHrHolCalDtlBean.setTxtHolidayName(oRow.get("NAME").getString());
      oHrHolCalDtlBean.setTxtHolTypeId(oRow.get("HOL_TYPMST_ID").getString());
      oHrHolCalDtlBean.setTxtHolidayType(oRow.get("description").getString());
      oHrHolCalDtlBean.setTxtDay(oRow.get("day").getString());
      System.out.println(oRow.get("day").getString());
      arylstHrmHolCalDetail.add(oHrHolCalDtlBean);
    } 
    return arylstHrmHolCalDetail;
  }
  
  public LovVO getHrmCalNameLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderNames = new ArrayList();
    arylstHeaderNames.add("Calendar Id");
    arylstHeaderNames.add("hrm.HrmHolidayCalendar.calendarName");
    arylstHeaderNames.add("hrm.HrmHolidayCalendar.description");
    arylstHeaderNames.add("hrm.HrmHolidayCalendar.fromDate");
    arylstHeaderNames.add("hrm.HrmHolidayCalendar.toDate");
    arylstHeaderNames.add("siteId");
    arylstHeaderNames.add("hrm.HrmHolidayCalendar.siteCode");
    String frmDate = null;
    String toDate = null;
    oLovVO.setHeaderList(arylstHeaderNames);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    ArrayList arylstParameters = new ArrayList();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null) {
      sQuerySearch1 = oLovQueryVO.getSearchField1();
      System.out.println(sQuerySearch1);
    } 
    if (oLovQueryVO.getSearchField2() != null) {
      sQuerySearch2 = oLovQueryVO.getSearchField2();
      System.out.println(sQuerySearch2);
    } 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParameters.add(new DBObject(2, 1, 12, sQuerySearch2));
    arylstParameters.add(new DBObject(3, 2, -10));
    arylstParameters.add(new DBObject(4, 2, 12));
    arylstParameters.add(new DBObject(5, 2, 12));
    arylstParameters.add(new DBObject(6, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGSETUPLOV.proc_GetHolCalNameLOV(?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstList.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        arylstList = new ArrayList(); 
      count++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("ID").getString());
      oLOVBean.setDetailField2(oRow.get("CAL_CODE").getString());
      oLOVBean.setDetailField3(oRow.get("DESCRIPTION").getString());
      frmDate = formatter.format(oRow.get("FROM_DATE").getDate());
      toDate = formatter.format(oRow.get("TO_DATE").getDate());
      oLOVBean.setDetailField4(frmDate);
      oLOVBean.setDetailField5(toDate);
      oLOVBean.setDetailField6(oRow.get("gen_site_id").getString());
      oLOVBean.setDetailField7(oRow.get("code").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public LovVO getHrmSiteCodeLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderNames = new ArrayList();
    arylstHeaderNames.add("Site Id");
    arylstHeaderNames.add("hrm.HrmHolidayCalendar.siteCode");
    arylstHeaderNames.add("hrm.HrmHolidayCalendar.siteDesc");
    oLovVO.setHeaderList(arylstHeaderNames);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    String stateCode = null;
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    ArrayList arylstParameters = new ArrayList();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null) {
      sQuerySearch1 = oLovQueryVO.getSearchField1();
      System.out.println(sQuerySearch1);
    } 
    if (oLovQueryVO.getSearchField2() != null) {
      sQuerySearch2 = oLovQueryVO.getSearchField2();
      System.out.println(sQuerySearch2);
    } 
    if (oLovQueryVO.getProperty("stateCode") != null) {
      stateCode = oLovQueryVO.getProperty("stateCode");
      System.out.println(stateCode);
    } 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParameters.add(new DBObject(2, 1, 12, sQuerySearch2));
    arylstParameters.add(new DBObject(3, 1, 12, stateCode));
    arylstParameters.add(new DBObject(4, 2, -10));
    arylstParameters.add(new DBObject(5, 2, 12));
    arylstParameters.add(new DBObject(6, 2, 12));
    arylstParameters.add(new DBObject(7, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGSETUPLOV.proc_GetHolSiteCodeLOV(?,?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstList.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        arylstList = 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());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public LovVO getHrmStateCodeInsLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderNames = new ArrayList();
    arylstHeaderNames.add("Site Id");
    arylstHeaderNames.add("hrm.HrmHolidayCalendar.stateCode");
    arylstHeaderNames.add("hrm.HrmHolidayCalendar.stateDesc");
    oLovVO.setHeaderList(arylstHeaderNames);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    ArrayList arylstParameters = new ArrayList();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null) {
      sQuerySearch1 = oLovQueryVO.getSearchField1();
      System.out.println(sQuerySearch1);
    } 
    if (oLovQueryVO.getSearchField2() != null) {
      sQuerySearch2 = oLovQueryVO.getSearchField2();
      System.out.println(sQuerySearch2);
    } 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParameters.add(new DBObject(2, 1, 12, sQuerySearch2));
    arylstParameters.add(new DBObject(3, 2, -10));
    arylstParameters.add(new DBObject(4, 2, 12));
    arylstParameters.add(new DBObject(5, 2, 12));
    arylstParameters.add(new DBObject(6, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGSETUPLOV.proc_GetHolCalStateLOVdata(?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstList.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        arylstList = new ArrayList(); 
      count++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("GLOBAL_CODE_TYPE").getString());
      oLOVBean.setDetailField2(oRow.get("GLOBAL_CODE").getString());
      oLOVBean.setDetailField3(oRow.get("GLOBAL_CODE_DESC").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public LovVO getHrmStateCodeQueryLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderNames = new ArrayList();
    arylstHeaderNames.add("Site Id");
    arylstHeaderNames.add("hrm.HrmHolidayCalendar.stateCode");
    arylstHeaderNames.add("hrm.HrmHolidayCalendar.stateDesc");
    oLovVO.setHeaderList(arylstHeaderNames);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    ArrayList arylstParameters = new ArrayList();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null) {
      sQuerySearch1 = oLovQueryVO.getSearchField1();
      System.out.println(sQuerySearch1);
    } 
    if (oLovQueryVO.getSearchField2() != null) {
      sQuerySearch2 = oLovQueryVO.getSearchField2();
      System.out.println(sQuerySearch2);
    } 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParameters.add(new DBObject(2, 1, 12, sQuerySearch2));
    arylstParameters.add(new DBObject(3, 2, -10));
    arylstParameters.add(new DBObject(4, 2, 12));
    arylstParameters.add(new DBObject(5, 2, 12));
    arylstParameters.add(new DBObject(6, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGSETUPLOV.proc_GetHolCalStateQLOVdata(?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstList.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        arylstList = new ArrayList(); 
      count++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("GLOBAL_CODE_TYPE").getString());
      oLOVBean.setDetailField2(oRow.get("GLOBAL_CODE").getString());
      oLOVBean.setDetailField3(oRow.get("GLOBAL_CODE_DESC").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "GEN_HOL_CAL_MST";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    if (sScreenName.equalsIgnoreCase("HrmOrgHolCal"))
      saveHrmHolCalDetail(sHeaderPrimaryKey, arylstDetailBeanArray); 
  }
  
  private void saveHrmHolCalDetail(String sHeaderPrimaryKey, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bUpdate = false;
    boolean bDelete = false;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = null;
    DBUtilitiesBean oBean1 = null;
    Iterator itrBean = arylstDetailBeanArray.iterator();
    while (itrBean.hasNext()) {
      HrmOrgHolCalDtlBean oHrmOrgHolCalDtlBean = itrBean.next();
      if (oHrmOrgHolCalDtlBean.getStatus().equals("N")) {
        if (!bInsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMORGHOLCALMST.proc_UpsertHrHolCalDtl(?,?,?,?,?,?,?,?)");
          bInsert = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, "I"));
        arylstParameters.add(new DBObject(2, 1, 12, null));
        arylstParameters.add(new DBObject(3, 1, 12, sHeaderPrimaryKey));
        arylstParameters.add(new DBObject(4, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmOrgHolCalDtlBean.getTxtHolidayDate())));
        arylstParameters.add(new DBObject(5, 1, 12, oHrmOrgHolCalDtlBean.getTxtHolidayName()));
        arylstParameters.add(new DBObject(6, 1, 12, oHrmOrgHolCalDtlBean.getTxtHolTypeId()));
        arylstParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParameters);
        continue;
      } 
      if (oHrmOrgHolCalDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMORGHOLCALMST.proc_UpsertHrHolCalDtl(?,?,?,?,?,?,?,?)");
          bUpdate = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, "U"));
        arylstParameters.add(new DBObject(2, 1, 12, oHrmOrgHolCalDtlBean.getDetailId()));
        arylstParameters.add(new DBObject(3, 1, 12, sHeaderPrimaryKey));
        arylstParameters.add(new DBObject(4, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmOrgHolCalDtlBean.getTxtHolidayDate())));
        arylstParameters.add(new DBObject(5, 1, 12, oHrmOrgHolCalDtlBean.getTxtHolidayName()));
        arylstParameters.add(new DBObject(6, 1, 12, oHrmOrgHolCalDtlBean.getTxtHolTypeId()));
        arylstParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParameters);
        continue;
      } 
      if (oHrmOrgHolCalDtlBean.getStatus().equals("D")) {
        if (!bDelete) {
          oBean1 = new DBUtilitiesBean();
          oBean1.createBatch("HRMORGHOLCALMST.proc_DeleteHolCalDtl(?)");
          bDelete = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, oHrmOrgHolCalDtlBean.getDetailId()));
        oBean1.addToBatch(arylstParameters);
      } 
    } 
    if (bInsert)
      oBean.executeBatch(); 
    if (bUpdate)
      oBean.executeBatch(); 
    if (bDelete)
      oBean1.executeBatch(); 
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("Update ille!");
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl ille!";
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    HrmOrgHolCalHdrBean oHrmOrgHolCalHdrBean = (HrmOrgHolCalHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      arylstParameters = new ArrayList();
      arylstParameters.add(new DBObject(1, 1, 12, "i"));
      arylstParameters.add(new DBObject(2, 1, 12, oHrmOrgHolCalHdrBean.getCalendarName()));
      arylstParameters.add(new DBObject(3, 1, 12, oHrmOrgHolCalHdrBean.getDescription()));
      arylstParameters.add(new DBObject(4, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmOrgHolCalHdrBean.getFromDate())));
      arylstParameters.add(new DBObject(5, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmOrgHolCalHdrBean.getToDate())));
      arylstParameters.add(new DBObject(6, 1, 12, oHrmOrgHolCalHdrBean.getSiteId()));
      arylstParameters.add(new DBObject(7, 1, 12, oHrmOrgHolCalHdrBean.getStateCode()));
      arylstParameters.add(new DBObject(8, 1, 12, oHrmOrgHolCalHdrBean.getCalendarYear()));
      arylstParameters.add(new DBObject(9, 1, 12, this.oUserInfo.getUserTypeId()));
      arylstParameters.add(new DBObject(10, 1, 12, this.oUserInfo.getSiteId()));
      arylstParameters.add(new DBObject(11, 1, 12, null));
      arylstParameters.add(new DBObject(12, 2, 12));
      arylstParameters.add(new DBObject(13, 2, 12));
      arylstParameters.add(new DBObject(14, 2, 4));
      arylstParameters.add(new DBObject(15, 2, 4));
      ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGHOLCALMST.proc_UpsertHrHolCalHdr(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = arylstOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } else if (ScreenMode.equalsIgnoreCase("U")) {
      arylstParameters = new ArrayList();
      arylstParameters.add(new DBObject(1, 1, 12, "u"));
      arylstParameters.add(new DBObject(2, 1, 12, oHrmOrgHolCalHdrBean.getCalendarName()));
      arylstParameters.add(new DBObject(3, 1, 12, oHrmOrgHolCalHdrBean.getDescription()));
      arylstParameters.add(new DBObject(4, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmOrgHolCalHdrBean.getFromDate())));
      arylstParameters.add(new DBObject(5, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmOrgHolCalHdrBean.getToDate())));
      arylstParameters.add(new DBObject(6, 1, 12, oHrmOrgHolCalHdrBean.getSiteId()));
      arylstParameters.add(new DBObject(7, 1, 12, oHrmOrgHolCalHdrBean.getStateCode()));
      arylstParameters.add(new DBObject(8, 1, 12, oHrmOrgHolCalHdrBean.getCalendarYear()));
      arylstParameters.add(new DBObject(9, 1, 12, this.oUserInfo.getUserTypeId()));
      arylstParameters.add(new DBObject(10, 1, 12, this.oUserInfo.getSiteId()));
      arylstParameters.add(new DBObject(11, 1, 12, oHrmOrgHolCalHdrBean.getHeaderPrimaryKey()));
      arylstParameters.add(new DBObject(12, 2, 12));
      arylstParameters.add(new DBObject(13, 2, 12));
      arylstParameters.add(new DBObject(14, 2, 4));
      arylstParameters.add(new DBObject(15, 2, 4));
      ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGHOLCALMST.proc_UpsertHrHolCalHdr(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = arylstOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } 
    return returnString;
  }
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstErrorList = new ArrayList();
    HrmOrgHolCalHdrBean oHrmOrgHolCalHdrBean = (HrmOrgHolCalHdrBean)oBaseHeaderBean;
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D")) {
        checkUniqueHeader((HrmOrgHolCalHdrBean)oBaseHeaderBean);
        checkHeaderDates(oHrmOrgHolCalHdrBean, arylstErrorList);
      }  
    if (bDetailDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        if (sScreenName.equalsIgnoreCase("HrmOrgHolCal")) {
          Iterator itrBean1 = arylstDetailBeanArray.iterator();
          EnrgiseUtil.checkDuplicate(arylstDetailBeanArray, "txtHolidayName", "hrm.HrmHolidayCalendar.txtHolidayName", arylstErrorList, true);
          checkDatesConstraint(oHrmOrgHolCalHdrBean, arylstDetailBeanArray, arylstErrorList);
        }   
    reportError(arylstErrorList);
  }
  
  private void checkUniqueHeader(HrmOrgHolCalHdrBean oHrmOrgHolCalHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = String.valueOf(String.valueOf(" Select ID as ID from GEN_HOL_CAL_MST where CAL_CODE='").concat(String.valueOf(oHrmOrgHolCalHdrBean.getCalendarName()))).concat(String.valueOf("'"));
    System.out.println(sQuery);
    ArrayList arylstList1 = new ArrayList();
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList arylstList = oBean.executeQuery(sQuery);
    Iterator itrBean = arylstList.iterator();
    if (itrBean.hasNext()) {
      QueryRow oRow = itrBean.next();
      if (!oRow.get("ID").getString().equalsIgnoreCase(oHrmOrgHolCalHdrBean.getHeaderPrimaryKey())) {
        arylstParam = new ArrayList();
        arylstParam.add("");
        arylstParam.add(new Integer(0));
        arylstList1.add(new EnrgiseMessageKeyException("wenrgise.common.uniqueConstraintViolated", arylstParam));
      } 
    } 
    reportError(arylstList1);
  }
  
  private void checkUniqueDtl(String sHeaderPrimaryKey, ArrayList arylstDetailBeanArray) throws EnrgiseApplicationException, EnrgiseSystemException {
    int count = 0;
    Iterator itrBean1 = arylstDetailBeanArray.iterator();
    while (itrBean1.hasNext()) {
      HrmOrgHolCalDtlBean oHrmOrgHolCalDtlBean = itrBean1.next();
      String sQuery = String.valueOf(String.valueOf(String.valueOf(" Select ID as ID from GEN_HOL_CAL_DTL where NAME= '").concat(String.valueOf(oHrmOrgHolCalDtlBean.getTxtHolidayName()))).concat(String.valueOf("' and GEN_HOLC_MST_ID="))).concat(String.valueOf(sHeaderPrimaryKey));
      System.out.println(sQuery);
      DBUtilitiesBean oBean = new DBUtilitiesBean();
      ArrayList arylstList = oBean.executeQuery(sQuery);
      Iterator itrBean = arylstList.iterator();
      if (itrBean.hasNext()) {
        QueryRow oRow = itrBean.next();
        if (!oRow.get("ID").getString().equalsIgnoreCase(oHrmOrgHolCalDtlBean.getDetailId()))
          throw new EnrgiseApplicationException("wenrgise.common.uniqueConstraintViolated", "M"); 
      } 
    } 
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstErrorList = new ArrayList();
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        checkMandatoryHeader((HrmOrgHolCalHdrBean)oBaseHeaderBean);  
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmOrgHolCal")) {
        Iterator itrBean1 = arylstDetailBeanArray.iterator();
        int iCount = 1;
        while (itrBean1.hasNext()) {
          HrmOrgHolCalDtlBean oHrmOrgHolCalDtlBean = itrBean1.next();
          if (!oHrmOrgHolCalDtlBean.getStatus().equalsIgnoreCase("D"))
            checkMandatory(oHrmOrgHolCalDtlBean, iCount, arylstErrorList); 
          iCount++;
        } 
      }  
    reportError(arylstErrorList);
  }
  
  private void checkMandatory(HrmOrgHolCalDtlBean oHrmOrgHolCalDtlBean, int iCount, ArrayList arylstErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {
    if (!EnrgiseUtil.checkString(oHrmOrgHolCalDtlBean.getTxtHolidayName())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmHolidayCalendar.txtHolidayName");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(new Integer(iCount));
      arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmOrgHolCalDtlBean.getTxtHolidayDate())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmHolidayCalendar.txtHolidayDate");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(new Integer(iCount));
      arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmOrgHolCalDtlBean.getTxtHolidayType())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmHolidayCalendar.txtHolidayType");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(new Integer(iCount));
      arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(arylstErrorList);
  }
  
  private void checkMandatoryHeader(HrmOrgHolCalHdrBean oHrmOrgHolCalHdrBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oErrorList = new ArrayList();
    ArrayList oParams = new ArrayList();
    if (!EnrgiseUtil.checkString(oHrmOrgHolCalHdrBean.getCalendarName())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmHolidayCalendar.calendarName");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmOrgHolCalHdrBean.getFromDate())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmHolidayCalendar.fromDate");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmOrgHolCalHdrBean.getToDate())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmHolidayCalendar.toDate");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmOrgHolCalHdrBean.getState())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmHolidayCalendar.state");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(oErrorList);
  }
  
  private void checkDatesConstraint(HrmOrgHolCalHdrBean oHrmOrgHolCalHdrBean, ArrayList arylstDetailBeanArray, ArrayList arylstErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    int n = arylstDetailBeanArray.size();
    HrmOrgHolCalDtlBean oHrmOrgHolCalDtlBean = arylstDetailBeanArray.get(n - 1);
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    if (EnrgiseUtil.checkString(oHrmOrgHolCalDtlBean.getTxtHolidayDate())) {
      int dateFlag = EnrgiseUtil.compareDates(dateFormat, oHrmOrgHolCalHdrBean.getToDate(), oHrmOrgHolCalDtlBean.getTxtHolidayDate());
      if (dateFlag == -1 && dateFlag != -2)
        if (arylstErrorList.isEmpty()) {
          arylstParam = new ArrayList();
          arylstParam.add("");
          arylstParam.add(new Integer(0));
          arylstErrorList.add(new EnrgiseMessageKeyException("hrm.HrmHolidayCalendar.holidayDateValidation", arylstParam));
        }  
    } 
    if (EnrgiseUtil.checkString(oHrmOrgHolCalDtlBean.getTxtHolidayDate())) {
      int dateFlag = EnrgiseUtil.compareDates(dateFormat, oHrmOrgHolCalHdrBean.getFromDate(), oHrmOrgHolCalDtlBean.getTxtHolidayDate());
      if (dateFlag == 1 && dateFlag != -2)
        if (arylstErrorList.isEmpty()) {
          arylstParam = new ArrayList();
          arylstParam.add("");
          arylstParam.add(new Integer(0));
          arylstErrorList.add(new EnrgiseMessageKeyException("hrm.HrmHolidayCalendar.holidayDateValidation", arylstParam));
        }  
    } 
  }
  
  private void checkHeaderDates(HrmOrgHolCalHdrBean oHrmOrgHolCalHdrBean, ArrayList arylstErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    System.out.println(oHrmOrgHolCalHdrBean.getFromDate());
    System.out.println(oHrmOrgHolCalHdrBean.getToDate());
    if (EnrgiseUtil.checkString(oHrmOrgHolCalHdrBean.getFromDate()) || EnrgiseUtil.checkString(oHrmOrgHolCalHdrBean.getToDate())) {
      int dateFlag = EnrgiseUtil.compareDates(dateFormat, oHrmOrgHolCalHdrBean.getToDate(), oHrmOrgHolCalHdrBean.getFromDate());
      if ((dateFlag == -1 && dateFlag != -2) || (dateFlag == 0 && dateFlag != -2))
        if (arylstErrorList.isEmpty()) {
          arylstParam = new ArrayList();
          arylstParam.add("");
          arylstParam.add(new Integer(0));
          arylstErrorList.add(new EnrgiseMessageKeyException("hrm.HrmHolidayCalendar.headerDateValidation", arylstParam));
        }  
    } 
  }
}
