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.HrmAdvanceMstDtlBean;
import wenrgise.hrms.bean.HrmAdvanceMstHdrBean;
import wenrgise.hrms.vo.HrmAdvMstQueryVO;

public class HrmAdvMstBO extends HrmBaseBO {
  public HrmAdvMstBO() {}
  
  public HrmAdvMstBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmAdvanceMstHdrMetaInfo(HrmAdvMstQueryVO oHrmAdvMstQueryVO) 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, oHrmAdvMstQueryVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(2, 1, 12, oHrmAdvMstQueryVO.getAdvanceCode()));
    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, "HRMADVMST.proc_HrAdvMstHdrCount(?,?,?,?,?,?,?)");
    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 getHrmAdvanceMstHdrInfo(HrmAdvMstQueryVO oHrmAdvMstQueryVO, 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 (oHrmAdvMstQueryVO == null)
      oHrmAdvMstQueryVO = new HrmAdvMstQueryVO(); 
    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, oHrmAdvMstQueryVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(4, 1, 12, oHrmAdvMstQueryVO.getAdvanceCode()));
    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, "HRMADVMST.proc_GetHrAdvMstHdr(?,?,?,?,?,?,?,?)");
    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();
      HrmAdvanceMstHdrBean oHrmAdvanceMstHdrBean = new HrmAdvanceMstHdrBean();
      oHrmAdvanceMstHdrBean.setHeaderPrimaryKey(oRow.get("ID").getString());
      oHrmAdvanceMstHdrBean.setAdvanceCode(oRow.get("CODE").getString());
      oHrmAdvanceMstHdrBean.setDescription(oRow.get("NAME").getString());
      oHrmAdvanceMstHdrBean.setCalendar(oRow.get("CALENDAR_TYPE").getString());
      oHeaderList.add(oHrmAdvanceMstHdrBean);
    } 
    return oHeaderList;
  }
  
  public RecordMetaInfo getHrmAdvanceMstDetailMetaInfo(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, "HRMADVMST.proc_HrAdvMstDtlCount(?,?,?,?,?,?)");
    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 getHrmAdvanceMstDetailInfo(String lPrimaryKey, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList oHrmAdvanceMstDetail = 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, "HRMADVMST.proc_GetHrAdvMstDtl(?,?,?,?,?,?,?)");
    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)
        oHrmAdvanceMstDetail = new ArrayList(); 
      count++;
      oRow = oIt.next();
      HrmAdvanceMstDtlBean oHrmAdvanceMstDtlBean = new HrmAdvanceMstDtlBean();
      oHrmAdvanceMstDtlBean.setDetailId(oRow.get("ID").getString());
      oHrmAdvanceMstDtlBean.setTxtAdvanceDescription(oRow.get("NAME").getString());
      oHrmAdvanceMstDtlBean.setTxtEffectiveFromDate(EnrgiseUtil.convertToString(oRow.get("EFFECTIVE_FROM").getDate()));
      oHrmAdvanceMstDtlBean.setTxtEffectiveToDate(EnrgiseUtil.convertToString(oRow.get("EFFECTIVE_TO").getDate()));
      oHrmAdvanceMstDtlBean.setTxtMaxCalLimit(oRow.get("MAX_YEARLY_LIMIT").getString());
      oHrmAdvanceMstDtlBean.setTxtNoOfTXN(oRow.get("TXN_PER_PERIOD").getString());
      oHrmAdvanceMstDtlBean.setTxtMaxLimitPerTXN(oRow.get("MAX_LIMIT_PER_TXN").getString());
      oHrmAdvanceMstDtlBean.setTxtEligibilityFactor(oRow.get("GREATER_LESSER_FLAG").getString());
      oHrmAdvanceMstDtlBean.setTxtFlatAmount(oRow.get("FLAT_AMOUNT").getString());
      oHrmAdvanceMstDtlBean.setTxtNoOfMonSal(oRow.get("NO_OF_MONTHS_SAL").getString());
      oHrmAdvanceMstDtlBean.setTxtSettlementUnit(oRow.get("SETTLEMENT_UNIT").getString());
      oHrmAdvanceMstDtlBean.setTxtSettlementPeriod(oRow.get("SETTLEMENT_PERIOD").getString());
      oHrmAdvanceMstDtlBean.setTxtMoratoriumInMonths(oRow.get("MORATORIUM_MONTHS").getString());
      oHrmAdvanceMstDtlBean.setTxtSettleThroughPayroll(oRow.get("PAYROLL_SETTLE_FLAG").getString());
      oHrmAdvanceMstDtlBean.setTxtApplicableToAll(oRow.get("ALL_FLAG").getString());
      oHrmAdvanceMstDetail.add(oHrmAdvanceMstDtlBean);
    } 
    return oHrmAdvanceMstDetail;
  }
  
  public ArrayList getHrmCurAdvMstDetailInfo() throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList oHrmCurAdvMstDetail = null;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 2, -10));
    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, "HRMADVMST.proc_GetHrCurAdvInfo(?,?,?,?)");
    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)
        oHrmCurAdvMstDetail = new ArrayList(); 
      count++;
      oRow = oIt.next();
      HrmAdvanceMstDtlBean oHrmAdvanceMstDtlBean = new HrmAdvanceMstDtlBean();
      oHrmAdvanceMstDtlBean.setDetailId(oRow.get("ID").getString());
      oHrmAdvanceMstDtlBean.setTxtAdvanceDescription(oRow.get("NAME").getString());
      oHrmCurAdvMstDetail.add(oHrmAdvanceMstDtlBean);
    } 
    return oHrmCurAdvMstDetail;
  }
  
  public void updateAdvanceMst(String sSql, String sId, String sUserId, String sUserSiteId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sSql));
    oParameters.add(new DBObject(2, 1, 12, sId));
    oParameters.add(new DBObject(3, 1, 12, sUserId));
    oParameters.add(new DBObject(4, 1, 12, sUserSiteId));
    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, "HRMADVMST.proc_UpdateHrAdvDtl(?,?,?,?,?,?,?)");
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_ADV_MST";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    saveHrmAdvDtl(sHeaderPrimaryKey, arylstDetailBeanArray);
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("!");
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl!";
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public LovVO getHrmAdvMstCodeLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("ID");
    oHeaderList.add("hrm.HrmAdvMst.advanceCode");
    oHeaderList.add("hrm.HrmAdvMst.description");
    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, "HRMADVMST.proc_GetAdvMstCodeLOV(?,?,?,?,?,?)");
    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 getHrmAdvMstCodeInsLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("GlobalCodeType");
    oHeaderList.add("hrm.HrmAdvMst.advanceCode");
    oHeaderList.add("hrm.HrmAdvMst.description");
    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();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    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, "HRMADVMST.proc_GetAdvMstCodeInsLOV(?,?,?,?,?)");
    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("GLOBAL_CODE_TYPE").getString());
      oLOVBean.setDetailField2(oRow.get("GLOBAL_CODE").getString());
      oLOVBean.setDetailField3(oRow.get("GLOBAL_CODE_DESC").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public LovVO getHrmAdvMstCalendarCodeLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("ID");
    oHeaderList.add("hrm.HrmAdvMst.calendarType");
    oHeaderList.add("hrm.HrmAdvMst.beginMonth");
    oHeaderList.add("hrm.HrmAdvMst.durationMonth");
    oLovVO.setHeaderList(oHeaderList);
    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();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    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, "HRMADVMST.proc_GetAdvMstCalendarCodeLOV(?,?,?,?,?)");
    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("CALENDAR_TYPE").getString());
      oLOVBean.setDetailField3(oRow.get("BEGIN_MONTH").getString());
      oLOVBean.setDetailField4(oRow.get("DURATION_MONTH").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();
    HrmAdvanceMstHdrBean oHrmAdvanceMstHdrBean = (HrmAdvanceMstHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "N"));
      oParameters.add(new DBObject(2, 1, 12, oHrmAdvanceMstHdrBean.getAdvanceCode()));
      oParameters.add(new DBObject(3, 1, 12, oHrmAdvanceMstHdrBean.getDescription()));
      oParameters.add(new DBObject(4, 1, 12, oHrmAdvanceMstHdrBean.getCalendarId()));
      oParameters.add(new DBObject(5, 1, 12, this.oUserInfo.getUserTypeId()));
      oParameters.add(new DBObject(6, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(7, 1, 12, null));
      oParameters.add(new DBObject(8, 2, 12));
      oParameters.add(new DBObject(9, 2, 12));
      oParameters.add(new DBObject(10, 2, 12));
      oParameters.add(new DBObject(11, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMADVMST.proc_UpsertHrAdvMstHdr(?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } else if (ScreenMode.equalsIgnoreCase("U")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "U"));
      oParameters.add(new DBObject(2, 1, 12, oHrmAdvanceMstHdrBean.getAdvanceCode()));
      oParameters.add(new DBObject(3, 1, 12, oHrmAdvanceMstHdrBean.getDescription()));
      oParameters.add(new DBObject(4, 1, 12, oHrmAdvanceMstHdrBean.getCalendarId()));
      oParameters.add(new DBObject(5, 1, 12, this.oUserInfo.getUserTypeId()));
      oParameters.add(new DBObject(6, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(7, 1, 12, oHrmAdvanceMstHdrBean.getHeaderPrimaryKey()));
      oParameters.add(new DBObject(8, 2, 12));
      oParameters.add(new DBObject(9, 2, 12));
      oParameters.add(new DBObject(10, 2, 12));
      oParameters.add(new DBObject(11, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMADVMST.proc_UpsertHrAdvMstHdr(?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } 
    return returnString;
  }
  
  private void saveHrmAdvDtl(String sHeaderPrimaryKey, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bUpdate = false;
    boolean bDelete = false;
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = null;
    DBUtilitiesBean oBean1 = null;
    Iterator itrBean = arylstDetailBeanArray.iterator();
    while (itrBean.hasNext()) {
      HrmAdvanceMstDtlBean oHrmAdvanceMstDtlBean = itrBean.next();
      if (oHrmAdvanceMstDtlBean.getStatus().equals("N")) {
        if (!bInsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMADVMST.proc_UpsertHrAdvMstDtl(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
          bInsert = true;
        } 
        arylstParam = new ArrayList();
        arylstParam.add(new DBObject(1, 1, 12, "N"));
        arylstParam.add(new DBObject(2, 1, 12, null));
        arylstParam.add(new DBObject(3, 1, 12, sHeaderPrimaryKey));
        arylstParam.add(new DBObject(4, 1, 12, oHrmAdvanceMstDtlBean.getTxtAdvanceDescription()));
        arylstParam.add(new DBObject(5, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAdvanceMstDtlBean.getTxtEffectiveFromDate())));
        arylstParam.add(new DBObject(6, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAdvanceMstDtlBean.getTxtEffectiveToDate())));
        arylstParam.add(new DBObject(7, 1, 12, oHrmAdvanceMstDtlBean.getTxtMaxCalLimit()));
        arylstParam.add(new DBObject(8, 1, 12, oHrmAdvanceMstDtlBean.getTxtNoOfTXN()));
        arylstParam.add(new DBObject(9, 1, 12, oHrmAdvanceMstDtlBean.getTxtMaxLimitPerTXN()));
        arylstParam.add(new DBObject(10, 1, 12, oHrmAdvanceMstDtlBean.getTxtEligibilityFactor()));
        arylstParam.add(new DBObject(11, 1, 12, oHrmAdvanceMstDtlBean.getTxtFlatAmount()));
        arylstParam.add(new DBObject(12, 1, 12, oHrmAdvanceMstDtlBean.getTxtNoOfMonSal()));
        arylstParam.add(new DBObject(13, 1, 12, oHrmAdvanceMstDtlBean.getTxtSettlementUnit()));
        arylstParam.add(new DBObject(14, 1, 12, oHrmAdvanceMstDtlBean.getTxtSettlementPeriod()));
        arylstParam.add(new DBObject(15, 1, 12, oHrmAdvanceMstDtlBean.getTxtMoratoriumInMonths()));
        arylstParam.add(new DBObject(16, 1, 12, oHrmAdvanceMstDtlBean.getTxtSettleThroughPayroll()));
        arylstParam.add(new DBObject(17, 1, 12, oHrmAdvanceMstDtlBean.getTxtApplicableToAll()));
        arylstParam.add(new DBObject(18, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParam.add(new DBObject(19, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParam);
        continue;
      } 
      if (oHrmAdvanceMstDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMADVMST.proc_UpsertHrAdvMstDtl(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
          bUpdate = true;
        } 
        arylstParam = new ArrayList();
        arylstParam.add(new DBObject(1, 1, 12, "U"));
        arylstParam.add(new DBObject(2, 1, 12, oHrmAdvanceMstDtlBean.getDetailId()));
        arylstParam.add(new DBObject(3, 1, 12, sHeaderPrimaryKey));
        arylstParam.add(new DBObject(4, 1, 12, oHrmAdvanceMstDtlBean.getTxtAdvanceDescription()));
        arylstParam.add(new DBObject(5, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAdvanceMstDtlBean.getTxtEffectiveFromDate())));
        arylstParam.add(new DBObject(6, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAdvanceMstDtlBean.getTxtEffectiveToDate())));
        arylstParam.add(new DBObject(7, 1, 12, oHrmAdvanceMstDtlBean.getTxtMaxCalLimit()));
        arylstParam.add(new DBObject(8, 1, 12, oHrmAdvanceMstDtlBean.getTxtNoOfTXN()));
        arylstParam.add(new DBObject(9, 1, 12, oHrmAdvanceMstDtlBean.getTxtMaxLimitPerTXN()));
        arylstParam.add(new DBObject(10, 1, 12, oHrmAdvanceMstDtlBean.getTxtEligibilityFactor()));
        arylstParam.add(new DBObject(11, 1, 12, oHrmAdvanceMstDtlBean.getTxtFlatAmount()));
        arylstParam.add(new DBObject(12, 1, 12, oHrmAdvanceMstDtlBean.getTxtNoOfMonSal()));
        arylstParam.add(new DBObject(13, 1, 12, oHrmAdvanceMstDtlBean.getTxtSettlementUnit()));
        arylstParam.add(new DBObject(14, 1, 12, oHrmAdvanceMstDtlBean.getTxtSettlementPeriod()));
        arylstParam.add(new DBObject(15, 1, 12, oHrmAdvanceMstDtlBean.getTxtMoratoriumInMonths()));
        arylstParam.add(new DBObject(16, 1, 12, oHrmAdvanceMstDtlBean.getTxtSettleThroughPayroll()));
        arylstParam.add(new DBObject(17, 1, 12, oHrmAdvanceMstDtlBean.getTxtApplicableToAll()));
        arylstParam.add(new DBObject(18, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParam.add(new DBObject(19, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParam);
        continue;
      } 
      if (oHrmAdvanceMstDtlBean.getStatus().equals("D")) {
        if (!bDelete) {
          oBean1 = new DBUtilitiesBean();
          oBean1.createBatch("HRMADVMST.proc_DeleteHrAdvMstDtl(?)");
          bDelete = true;
        } 
        arylstParam = new ArrayList();
        arylstParam.add(new DBObject(1, 1, 12, oHrmAdvanceMstDtlBean.getDetailId()));
        oBean1.addToBatch(arylstParam);
      } 
    } 
    if (bInsert)
      oBean.executeBatch(); 
    if (bUpdate)
      oBean.executeBatch(); 
    if (bDelete)
      oBean1.executeBatch(); 
  }
  
  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((HrmAdvanceMstHdrBean)oBaseHeaderBean);  
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmAdvanceMaster")) {
        Iterator itrBean1 = arylstDetailBeanArray.iterator();
        int iCount = 1;
        while (itrBean1.hasNext()) {
          HrmAdvanceMstDtlBean oHrmAdvanceMstDtlBean = itrBean1.next();
          if (!oHrmAdvanceMstDtlBean.getStatus().equalsIgnoreCase("D")) {
            checkMandatory(oHrmAdvanceMstDtlBean, iCount, arylstErrorList);
            EnrgiseUtil.checkDuplicate(arylstDetailBeanArray, "txtAdvanceDescription", "hrm.HrmAdvMst.advanceDescription", arylstErrorList, true);
          } 
          iCount++;
        } 
      }  
    reportError(arylstErrorList);
  }
  
  private void checkMandatoryHeader(HrmAdvanceMstHdrBean oHrmAdvanceMstHdrBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oErrorList = new ArrayList();
    ArrayList oParams = new ArrayList();
    if (!EnrgiseUtil.checkString(oHrmAdvanceMstHdrBean.getAdvanceCode())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmAdvMst.advanceCode");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(oErrorList);
  }
  
  private void checkMandatory(HrmAdvanceMstDtlBean oHrmAdvanceMstDtlBean, int iCount, ArrayList arylstErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {
    if (!EnrgiseUtil.checkString(oHrmAdvanceMstDtlBean.getTxtAdvanceDescription())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmAdvMst.advanceDescription");
      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(oHrmAdvanceMstDtlBean.getTxtEffectiveFromDate())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmAdvMst.effectiveFromDate");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(String.valueOf(iCount));
      arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmAdvanceMstDtlBean.getTxtApplicableToAll())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmAdvMst.applicableToAll");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(String.valueOf(iCount));
      arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmAdvanceMstDtlBean.getTxtSettlementPeriod())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmAdvMst.settlementPeriod");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(String.valueOf(iCount));
      arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmAdvanceMstDtlBean.getTxtEligibilityFactor())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmAdvMst.eligibility");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(String.valueOf(iCount));
      arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    if (EnrgiseUtil.checkString(oHrmAdvanceMstDtlBean.getTxtEligibilityFactor())) {
      if (oHrmAdvanceMstDtlBean.getTxtEligibilityFactor().equalsIgnoreCase("F"))
        if (!EnrgiseUtil.checkString(oHrmAdvanceMstDtlBean.getTxtFlatAmount())) {
          MessageKey oMessageKey = new MessageKey("hrm.HrmAdvMst.flatAmount");
          ArrayList oParams = new ArrayList();
          oParams.add(oMessageKey);
          oParams.add(new Integer(iCount));
          arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
        }  
      if (oHrmAdvanceMstDtlBean.getTxtEligibilityFactor().equalsIgnoreCase("S"))
        if (!EnrgiseUtil.checkString(oHrmAdvanceMstDtlBean.getTxtNoOfMonSal())) {
          MessageKey oMessageKey = new MessageKey("hrm.HrmAdvMst.salary");
          ArrayList oParams = new ArrayList();
          oParams.add(oMessageKey);
          oParams.add(new Integer(iCount));
          arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
        }  
    } 
    reportError(arylstErrorList);
  }
  
  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();
    HrmAdvanceMstHdrBean oHrmAdvanceMstHdrBean = (HrmAdvanceMstHdrBean)oBaseHeaderBean;
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        checkUniqueHeader((HrmAdvanceMstHdrBean)oBaseHeaderBean);  
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmAdvanceMaster")) {
        Iterator oIt1 = arylstDetailBeanArray.iterator();
        while (oIt1.hasNext()) {
          HrmAdvanceMstDtlBean oHrmAdvanceMstDtlBean = oIt1.next();
          if (!oHrmAdvanceMstDtlBean.getStatus().equalsIgnoreCase("D"))
            checkDatesConstraint(oHrmAdvanceMstDtlBean, arylstErrorList); 
        } 
      }  
    reportError(arylstErrorList);
  }
  
  private void checkUniqueHeader(HrmAdvanceMstHdrBean oHrmAdvanceMstHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = String.valueOf(String.valueOf(" Select ID as ID from HRM_ADV_MST where CODE='").concat(String.valueOf(oHrmAdvanceMstHdrBean.getAdvanceCode()))).concat(String.valueOf("'"));
    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(oHrmAdvanceMstHdrBean.getHeaderPrimaryKey()))
        throw new EnrgiseApplicationException("wenrgise.common.uniqueConstraintViolated", "M"); 
    } 
  }
  
  private void checkUniqueDtl(String sHeaderPrimaryKey, ArrayList arylstDetailBeanArray) throws EnrgiseApplicationException, EnrgiseSystemException {
    int count = 0;
    Iterator itrBean1 = arylstDetailBeanArray.iterator();
    while (itrBean1.hasNext()) {
      HrmAdvanceMstDtlBean oHrmAdvanceMstDtlBean = itrBean1.next();
      String sQuery = String.valueOf(String.valueOf(String.valueOf(" Select ID as ID from HRM_ADV_DTL where NAME= ").concat(String.valueOf(oHrmAdvanceMstDtlBean.getTxtAdvanceDescription()))).concat(String.valueOf(" and ADV_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(oHrmAdvanceMstDtlBean.getDetailId()))
          throw new EnrgiseApplicationException("wenrgise.common.uniqueConstraintViolated", "M"); 
      } 
    } 
  }
  
  public LovVO getHrmSettleHeadsLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("ID");
    oHeaderList.add("hrm.HrmAdvMst.description");
    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();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    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, "HRMADVMST.proc_GetSetHeadsLOV(?,?,?,?,?)");
    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;
  }
  
  private void checkDatesConstraint(HrmAdvanceMstDtlBean oHrmAdvanceMstDtlBean, ArrayList arylstErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    if (EnrgiseUtil.checkString(oHrmAdvanceMstDtlBean.getTxtEffectiveToDate())) {
      int dateFlag = EnrgiseUtil.compareDates(dateFormat, oHrmAdvanceMstDtlBean.getTxtEffectiveToDate(), oHrmAdvanceMstDtlBean.getTxtEffectiveFromDate());
      if (dateFlag == -1 && dateFlag != -2)
        arylstErrorList.add(new EnrgiseApplicationException("wenrgise.common.FromTodateConstraintViolated")); 
    } 
    reportError(arylstErrorList);
  }
}
