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.HrmAprslEmpDtlsBean;
import wenrgise.hrms.bean.HrmPrmOrderHdrBean;
import wenrgise.hrms.vo.HrmPrmOrderQVO;

public class HrmPrmOrderBO extends HrmBaseBO {
  public HrmPrmOrderBO() {}
  
  public HrmPrmOrderBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public LovVO getPromOrderEmpNoNLOVData(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("id");
    oHeaderList.add("hrm.HrmSepApplication.employeeNo");
    oHeaderList.add("hrm.HrmSepApplication.firstName");
    oHeaderList.add("hrm.HrmSepApplication.middleName");
    oHeaderList.add("hrm.HrmSepApplication.lastName");
    oHeaderList.add("gradeSeq");
    oHeaderList.add("hrm.HrmSepApplication.grade");
    oHeaderList.add("designationSeq");
    oHeaderList.add("hrm.HrmSepApplication.designation");
    oHeaderList.add("hrm.HrmSepApplication.location");
    oHeaderList.add("hrm.HrmSepApplication.pastExp");
    oHeaderList.add("hrm.HrmSepApplication.presExp");
    oLovVO.setHeaderList(oHeaderList);
    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");
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int iCount = 0;
    ArrayList arylstParameters = 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(); 
    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, "HRMPROMORDER.procGetPrmOrderEmpNoLOV(?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstList.iterator();
    while (iter.hasNext()) {
      if (iCount == 0)
        arylstList = new ArrayList(); 
      iCount++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("id").getString());
      oLOVBean.setDetailField2(oRow.get("emp_no").getString());
      oLOVBean.setDetailField3(oRow.get("first_name").getString());
      oLOVBean.setDetailField4(oRow.get("middle_name").getString());
      oLOVBean.setDetailField5(oRow.get("last_name").getString());
      oLOVBean.setDetailField6(oRow.get("gradeSeq").getString());
      oLOVBean.setDetailField7(oRow.get("grade").getString());
      oLOVBean.setDetailField8(oRow.get("designationSeq").getString());
      oLOVBean.setDetailField9(oRow.get("designation").getString());
      oLOVBean.setDetailField10(oRow.get("location").getString());
      oLOVBean.setDetailField11(oRow.get("past_exp").getString());
      oLOVBean.setDetailField12(oRow.get("pres_exp").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public LovVO getPromOrderEmpNoQLOVData(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("id");
    oHeaderList.add("hrm.HrmSepApplication.employeeNo");
    oHeaderList.add("hrm.HrmSepApplication.firstName");
    oHeaderList.add("hrm.HrmSepApplication.middleName");
    oHeaderList.add("hrm.HrmSepApplication.lastName");
    oHeaderList.add("hrm.HrmSepApplication.grade");
    oHeaderList.add("hrm.HrmSepApplication.designation");
    oHeaderList.add("hrm.HrmSepApplication.location");
    oLovVO.setHeaderList(oHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int iCount = 0;
    ArrayList arylstParameters = 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(); 
    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, "HRMPROMORDER.procGetPrmOrderEmpNoQLOV(?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstList.iterator();
    while (iter.hasNext()) {
      if (iCount == 0)
        arylstList = new ArrayList(); 
      iCount++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("id").getString());
      oLOVBean.setDetailField2(oRow.get("emp_no").getString());
      oLOVBean.setDetailField3(oRow.get("first_name").getString());
      oLOVBean.setDetailField4(oRow.get("middle_name").getString());
      oLOVBean.setDetailField5(oRow.get("last_name").getString());
      oLOVBean.setDetailField6(oRow.get("grade").getString());
      oLOVBean.setDetailField7(oRow.get("designation").getString());
      oLOVBean.setDetailField8(oRow.get("location").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public LovVO getPromOrderNoQLOVData(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = "select distinct hpo.order_no,hpo.EFFECTIVE_DATE from HRM_PROM_ORDER hpo  ";
    sQuery = String.valueOf(sQuery).concat(String.valueOf(" where  1=1 "));
    if (EnrgiseUtil.checkString(oLovQueryVO.getSearchField1()))
      sQuery = String.valueOf(String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and upper(hpo.order_no) LIKE upper('%"))).concat(String.valueOf(oLovQueryVO.getSearchField1()))).concat(String.valueOf("%') ")); 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oResultList = oBean.executeQuery(sQuery);
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    ArrayList oList = new ArrayList();
    oHeaderList.add("hrm.HrmPrmOrder.orderNo");
    oHeaderList.add("hrm.HrmPrmOrder.effecDate");
    oLovVO.setHeaderList(oHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    QueryRow oRow = null;
    Iterator oIt = oResultList.iterator();
    while (oIt.hasNext()) {
      oRow = oIt.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("order_no").getString());
      oLOVBean.setDetailField2(EnrgiseUtil.convertToString(oRow.get("EFFECTIVE_DATE").getDate()));
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public LovVO getPromToGradeNLOVData(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = "select distinct hcd.id,hcd.code,hcd.name  from hrm_class_mst hcm,hrm_class_dtl hcd  ";
    sQuery = String.valueOf(String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" where hcd.CLS_MST_ID=hcm.id and hcm.code='20' and hcd.HIERARCHY_LEVEL < '"))).concat(String.valueOf(oLovQueryVO.getProperty("gradeSeq")))).concat(String.valueOf("'  "));
    if (EnrgiseUtil.checkString(oLovQueryVO.getSearchField1()))
      sQuery = String.valueOf(String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and upper(hcd.code) LIKE upper('%"))).concat(String.valueOf(oLovQueryVO.getSearchField1()))).concat(String.valueOf("%') ")); 
    if (EnrgiseUtil.checkString(oLovQueryVO.getSearchField2()))
      sQuery = String.valueOf(String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and upper(hcd.name) LIKE upper('%"))).concat(String.valueOf(oLovQueryVO.getSearchField2()))).concat(String.valueOf("%') ")); 
    sQuery = String.valueOf(sQuery).concat(String.valueOf(" order by hcd.HIERARCHY_LEVEL desc"));
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oResultList = oBean.executeQuery(sQuery);
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    ArrayList oList = new ArrayList();
    oHeaderList.add("gradeId");
    oHeaderList.add("hrm.HrmPrmOrder.gradeName");
    oHeaderList.add("hrm.HrmPrmOrder.gradeCode");
    oLovVO.setHeaderList(oHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    QueryRow oRow = null;
    Iterator oIt = oResultList.iterator();
    while (oIt.hasNext()) {
      oRow = oIt.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("id").getString());
      oLOVBean.setDetailField2(oRow.get("name").getString());
      oLOVBean.setDetailField3(oRow.get("code").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public LovVO getPromToDesignationNLOVData(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = "select distinct hcd.id,hcd.code,hcd.name  from hrm_class_mst hcm,hrm_class_dtl hcd  ";
    sQuery = String.valueOf(sQuery).concat(String.valueOf(" where hcd.CLS_MST_ID=hcm.id and hcm.code='10'  "));
    if (EnrgiseUtil.checkString(oLovQueryVO.getSearchField1()))
      sQuery = String.valueOf(String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and upper(hcd.code) LIKE upper('%"))).concat(String.valueOf(oLovQueryVO.getSearchField1()))).concat(String.valueOf("%') ")); 
    if (EnrgiseUtil.checkString(oLovQueryVO.getSearchField2()))
      sQuery = String.valueOf(String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and upper(hcd.name) LIKE upper('%"))).concat(String.valueOf(oLovQueryVO.getSearchField2()))).concat(String.valueOf("%') ")); 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oResultList = oBean.executeQuery(sQuery);
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    ArrayList oList = new ArrayList();
    oHeaderList.add("gradeId");
    oHeaderList.add("hrm.HrmPrmOrder.desigName");
    oHeaderList.add("hrm.HrmPrmOrder.desigCode");
    oLovVO.setHeaderList(oHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    QueryRow oRow = null;
    Iterator oIt = oResultList.iterator();
    while (oIt.hasNext()) {
      oRow = oIt.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("id").getString());
      oLOVBean.setDetailField2(oRow.get("name").getString());
      oLOVBean.setDetailField3(oRow.get("code").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public RecordMetaInfo getHrmPromOrderHdrMetaInfo(HrmPrmOrderQVO oHrmPrmOrderQVO) 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, oHrmPrmOrderQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(2, 1, 12, oHrmPrmOrderQVO.getEmpId()));
    arylstParameters.add(new DBObject(3, 1, 12, oHrmPrmOrderQVO.getOrderNo()));
    arylstParameters.add(new DBObject(4, 2, -5));
    arylstParameters.add(new DBObject(5, 2, 93));
    arylstParameters.add(new DBObject(6, 2, 12));
    arylstParameters.add(new DBObject(7, 2, 12));
    arylstParameters.add(new DBObject(8, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMPROMORDER.procHrPromHdrCount(?,?,?,?,?,?,?,?)");
    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 String getEmpPastExp(String sEmpId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    String sEmpPastTotal = null;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sEmpId));
    arylstParam.add(new DBObject(2, 2, -10));
    arylstParam.add(new DBObject(3, 2, 12));
    arylstParam.add(new DBObject(4, 2, 12));
    arylstParam.add(new DBObject(5, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMPROMORDER.procGetEmpTotalPastExp(?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    arylstParam = (ArrayList)oOutObject.getObject();
    if (arylstParam.size() == 0);
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator itrBean = arylstParam.iterator();
    while (itrBean.hasNext()) {
      oRow = (QueryRow)itrBean.next();
      sEmpPastTotal = oRow.get("pastTotal").getString();
    } 
    return sEmpPastTotal;
  }
  
  public String getEmpPresentExp(String sEmpId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    String sEmpPresentTotal = null;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sEmpId));
    arylstParam.add(new DBObject(2, 2, -10));
    arylstParam.add(new DBObject(3, 2, 12));
    arylstParam.add(new DBObject(4, 2, 12));
    arylstParam.add(new DBObject(5, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMPROMORDER.procGetEmpTotalPresentExp(?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    arylstParam = (ArrayList)oOutObject.getObject();
    if (arylstParam.size() == 0);
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator itrBean = arylstParam.iterator();
    while (itrBean.hasNext()) {
      oRow = (QueryRow)itrBean.next();
      sEmpPresentTotal = oRow.get("total").getString();
    } 
    return sEmpPresentTotal;
  }
  
  public ArrayList getHrmPromOrderHdr(HrmPrmOrderQVO oHrmPrmOrderQVO, 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 (oHrmPrmOrderQVO == null)
      oHrmPrmOrderQVO = new HrmPrmOrderQVO(); 
    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, oHrmPrmOrderQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(4, 1, 12, oHrmPrmOrderQVO.getEmpId()));
    arylstParameters.add(new DBObject(5, 1, 12, oHrmPrmOrderQVO.getOrderNo()));
    arylstParameters.add(new DBObject(6, 2, -10));
    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, "HRMPROMORDER.procHrPromHdrInfo(?,?,?,?,?,?,?,?,?)");
    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();
      HrmPrmOrderHdrBean oHrmPrmOrderHdrBean = new HrmPrmOrderHdrBean();
      oHrmPrmOrderHdrBean.setHeaderPrimaryKey(oRow.get("ID").getString());
      oHrmPrmOrderHdrBean.setEmpId(oRow.get("emp_id").getString());
      oHrmPrmOrderHdrBean.setEmployeeNo(oRow.get("emp_no").getString());
      oHrmPrmOrderHdrBean.setFirstName(oRow.get("first_name").getString());
      oHrmPrmOrderHdrBean.setMiddleName(oRow.get("middle_name").getString());
      oHrmPrmOrderHdrBean.setLastName(oRow.get("last_name").getString());
      oHrmPrmOrderHdrBean.setGrade(oRow.get("grade").getString());
      oHrmPrmOrderHdrBean.setDesignation(oRow.get("designation").getString());
      oHrmPrmOrderHdrBean.setLocation(oRow.get("location").getString());
      oHrmPrmOrderHdrBean.setOrderNo(oRow.get("ORDER_NO").getString());
      oHrmPrmOrderHdrBean.setEffecDate(EnrgiseUtil.convertToString(oRow.get("EFFECTIVE_DATE").getDate()));
      oHrmPrmOrderHdrBean.setRemarks(oRow.get("appr_remark").getString());
      oHrmPrmOrderHdrBean.setApproverId(oRow.get("appr_id").getString());
      oHrmPrmOrderHdrBean.setApprovedBy(oRow.get("appr_name").getString());
      oHrmPrmOrderHdrBean.setToGrade(oRow.get("to_grade").getString());
      oHrmPrmOrderHdrBean.setToGradeId(oRow.get("to_grade_id").getString());
      oHrmPrmOrderHdrBean.setToDesig(oRow.get("to_designation").getString());
      oHrmPrmOrderHdrBean.setToDesigId(oRow.get("to_designation_id").getString());
      String sPastTotal = getEmpPastExp(oHrmPrmOrderHdrBean.getEmpId());
      oHrmPrmOrderHdrBean.setPastExp(sPastTotal);
      String sPresentTotal = getEmpPresentExp(oHrmPrmOrderHdrBean.getEmpId());
      oHrmPrmOrderHdrBean.setPresExp(sPresentTotal);
      arylstHeaderList.add(oHrmPrmOrderHdrBean);
    } 
    return arylstHeaderList;
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    HrmPrmOrderHdrBean oHrmPrmOrderHdrBean = (HrmPrmOrderHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "N"));
      oParameters.add(new DBObject(2, 1, 12, oHrmPrmOrderHdrBean.getOrderNo()));
      oParameters.add(new DBObject(3, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmPrmOrderHdrBean.getEffecDate())));
      oParameters.add(new DBObject(4, 1, 12, oHrmPrmOrderHdrBean.getEmpId()));
      oParameters.add(new DBObject(5, 1, 12, oHrmPrmOrderHdrBean.getToGradeId()));
      oParameters.add(new DBObject(6, 1, 12, oHrmPrmOrderHdrBean.getToDesigId()));
      oParameters.add(new DBObject(7, 1, 12, oHrmPrmOrderHdrBean.getApproverId()));
      oParameters.add(new DBObject(8, 1, 12, oHrmPrmOrderHdrBean.getRemarks()));
      oParameters.add(new DBObject(9, 1, 12, this.oUserInfo.getUserTypeId()));
      oParameters.add(new DBObject(10, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(11, 1, 12, null));
      oParameters.add(new DBObject(12, 2, 12));
      oParameters.add(new DBObject(13, 2, 12));
      oParameters.add(new DBObject(14, 2, 12));
      oParameters.add(new DBObject(15, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMPROMORDER.procUpsertHrPromOrderHdr(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      updateEmpHistory(oHrmPrmOrderHdrBean.getEmpId(), oHrmPrmOrderHdrBean.getToDesigId(), oHrmPrmOrderHdrBean.getToGradeId(), oHrmPrmOrderHdrBean.getEffecDate());
      updateEmpPersonalDetail(oHrmPrmOrderHdrBean.getEmpId(), oHrmPrmOrderHdrBean.getToDesigId(), oHrmPrmOrderHdrBean.getToGradeId(), oHrmPrmOrderHdrBean.getEffecDate());
    } else if (ScreenMode.equalsIgnoreCase("U")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "u"));
      oParameters.add(new DBObject(2, 1, 12, oHrmPrmOrderHdrBean.getOrderNo()));
      oParameters.add(new DBObject(3, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmPrmOrderHdrBean.getEffecDate())));
      oParameters.add(new DBObject(4, 1, 12, oHrmPrmOrderHdrBean.getEmpId()));
      oParameters.add(new DBObject(5, 1, 12, oHrmPrmOrderHdrBean.getToGradeId()));
      oParameters.add(new DBObject(6, 1, 12, oHrmPrmOrderHdrBean.getToDesigId()));
      oParameters.add(new DBObject(7, 1, 12, oHrmPrmOrderHdrBean.getApproverId()));
      oParameters.add(new DBObject(8, 1, 12, oHrmPrmOrderHdrBean.getRemarks()));
      oParameters.add(new DBObject(9, 1, 12, this.oUserInfo.getUserTypeId()));
      oParameters.add(new DBObject(10, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(11, 1, 12, oHrmPrmOrderHdrBean.getHeaderPrimaryKey()));
      oParameters.add(new DBObject(12, 2, 12));
      oParameters.add(new DBObject(13, 2, 12));
      oParameters.add(new DBObject(14, 2, 12));
      oParameters.add(new DBObject(15, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMPROMORDER.procUpsertHrPromOrderHdr(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
    } 
    return returnString;
  }
  
  public void updateEmpHistory(String sEmpId, String sToDesigId, String sToGradeId, String sEffectDate) throws EnrgiseSystemException {
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Iterator oIt = null;
    ArrayList arylstEmpPresDtls = getEmpPresentDetail(sEmpId);
    oIt = arylstEmpPresDtls.iterator();
    while (oIt.hasNext()) {
      HrmAprslEmpDtlsBean oHrmAprslEmpDtlsBean = oIt.next();
      ArrayList arylstParam = new ArrayList();
      arylstParam.add(new DBObject(1, 1, 12, sEmpId));
      arylstParam.add(new DBObject(2, 1, 12, oHrmAprslEmpDtlsBean.getPresLoc()));
      arylstParam.add(new DBObject(3, 1, 12, oHrmAprslEmpDtlsBean.getDesignation()));
      arylstParam.add(new DBObject(4, 1, 12, oHrmAprslEmpDtlsBean.getGrade()));
      arylstParam.add(new DBObject(5, 1, 12, sToDesigId));
      arylstParam.add(new DBObject(6, 1, 12, sToGradeId));
      arylstParam.add(new DBObject(7, 1, 93, EnrgiseUtil.convertToSqlDate(sEffectDate)));
      arylstParam.add(new DBObject(8, 1, 12, this.oUserInfo.getUserTypeId()));
      arylstParam.add(new DBObject(9, 1, 12, this.oUserInfo.getSiteId()));
      arylstParam.add(new DBObject(10, 2, 12));
      arylstParam.add(new DBObject(11, 2, 12));
      arylstParam.add(new DBObject(12, 2, 4));
      ArrayList arrayList = oBean.callProc(arylstParam, "HRMPROMORDER.procUpsertHrmEmpIncr(?,?,?,?,?,?,?,?,?,?,?,?)");
    } 
  }
  
  public ArrayList getEmpPresentDetail(String sEmpId) throws EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList arylstList = null;
    Iterator oIt = null;
    ArrayList arylstEmpPresentDetail = null;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sEmpId));
    arylstParam.add(new DBObject(2, 2, -10));
    arylstParam.add(new DBObject(3, 2, 12));
    arylstParam.add(new DBObject(4, 2, 12));
    arylstParam.add(new DBObject(5, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMSEPORDER.procEmpPresDtls(?,?,?,?,?)");
    arylstList = (ArrayList)((DBObject)arylstOutArray.get(0)).getObject();
    if (arylstList.size() == 0);
    count = 0;
    oIt = arylstList.iterator();
    while (oIt.hasNext()) {
      if (count == 0)
        arylstEmpPresentDetail = new ArrayList(); 
      count++;
      oRow = oIt.next();
      HrmAprslEmpDtlsBean oHrmAprslEmpDtlsBean = new HrmAprslEmpDtlsBean();
      oHrmAprslEmpDtlsBean.setGrade(oRow.get("present_grade_id").getString());
      oHrmAprslEmpDtlsBean.setDesignation(oRow.get("present_designation_id").getString());
      oHrmAprslEmpDtlsBean.setPresLoc(oRow.get("present_site_id").getString());
      arylstEmpPresentDetail.add(oHrmAprslEmpDtlsBean);
    } 
    return arylstEmpPresentDetail;
  }
  
  public void updateEmpPersonalDetail(String sEmpId, String sToDesigId, String sToGradeId, String sEffectDate) throws EnrgiseSystemException {
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sEmpId));
    arylstParam.add(new DBObject(2, 1, 12, sToDesigId));
    arylstParam.add(new DBObject(3, 1, 12, sToGradeId));
    arylstParam.add(new DBObject(4, 1, 93, EnrgiseUtil.convertToSqlDate(sEffectDate)));
    arylstParam.add(new DBObject(5, 2, 12));
    arylstParam.add(new DBObject(6, 2, 12));
    arylstParam.add(new DBObject(7, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMPROMORDER.procUpdateHrmEmpPers(?,?,?,?,?,?,?)");
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_PROM_ORDER";
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {}
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return new String();
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {}
  
  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((HrmPrmOrderHdrBean)oBaseHeaderBean, oErrorList);  
    reportError(oErrorList);
  }
  
  private void checkMandatoryHeader(HrmPrmOrderHdrBean oHrmPrmOrderHdrBean, ArrayList oErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {
    if (!EnrgiseUtil.checkString(oHrmPrmOrderHdrBean.getEmpId())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmPrmOrder.employeeNo");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmPrmOrderHdrBean.getOrderNo())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmPrmOrder.orderNo");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmPrmOrderHdrBean.getToGrade())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmPrmOrder.toGrade");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmPrmOrderHdrBean.getEffecDate())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmPrmOrder.effecDate");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmPrmOrderHdrBean.getApproverId())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmPrmOrder.approvedBy");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
  }
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstErrorList = new ArrayList();
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D")) {
        checkUniqueHeader((HrmPrmOrderHdrBean)oBaseHeaderBean, arylstErrorList);
        checkEffectiveDate((HrmPrmOrderHdrBean)oBaseHeaderBean, arylstErrorList);
      }  
    reportError(arylstErrorList);
  }
  
  private void checkEffectiveDate(HrmPrmOrderHdrBean oHrmPrmOrderHdrBean, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = String.valueOf(String.valueOf(" Select INCR_EFFECT_DATE,COMP_JOIN_DATE from HRM_EMP_PERS where ID='").concat(String.valueOf(oHrmPrmOrderHdrBean.getEmpId()))).concat(String.valueOf("'"));
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList arylstList = oBean.executeQuery(sQuery);
    Iterator itrBean = arylstList.iterator();
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    DateFormat dateFormat1 = new SimpleDateFormat("dd-MMM-yyyy");
    if (itrBean.hasNext()) {
      QueryRow oRow = itrBean.next();
      String lastPromDate = EnrgiseUtil.convertToString(oRow.get("INCR_EFFECT_DATE").getDate());
      String compJoinDate = EnrgiseUtil.convertToString(oRow.get("COMP_JOIN_DATE").getDate());
      if (EnrgiseUtil.checkString(lastPromDate)) {
        int dateFlag = EnrgiseUtil.compareDates(dateFormat, oHrmPrmOrderHdrBean.getEffecDate(), lastPromDate);
        if (dateFlag == -1 && dateFlag != -2) {
          MessageKey oMessageKey = new MessageKey("hrm.HrmPrmOrder.orderNo");
          ArrayList oParams = new ArrayList();
          oParams.add(oMessageKey);
          oErrorList.add(new EnrgiseMessageKeyException("hrm.HrmPrmOrder.dateValidate", oParams, "E"));
        } 
      } 
      int dateFlag1 = EnrgiseUtil.compareDates(dateFormat1, oHrmPrmOrderHdrBean.getEffecDate(), compJoinDate);
      if (dateFlag1 == -1 && dateFlag1 != -2) {
        MessageKey oMessageKey = new MessageKey("hrm.HrmPrmOrder.orderNo");
        ArrayList oParams = new ArrayList();
        oParams.add(oMessageKey);
        oErrorList.add(new EnrgiseMessageKeyException("hrm.HrmPrmOrder.dateValidate1", oParams, "E"));
      } 
    } 
  }
  
  private void checkUniqueHeader(HrmPrmOrderHdrBean oHrmPrmOrderHdrBean, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = String.valueOf(String.valueOf(" Select ID as ID from HRM_PROM_ORDER where ORDER_NO='").concat(String.valueOf(oHrmPrmOrderHdrBean.getOrderNo()))).concat(String.valueOf("'"));
    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(oHrmPrmOrderHdrBean.getHeaderPrimaryKey())) {
        MessageKey oMessageKey = new MessageKey("hrm.HrmPrmOrder.orderNo");
        ArrayList oParams = new ArrayList();
        oParams.add(oMessageKey);
        oErrorList.add(new EnrgiseMessageKeyException("wenrgise.sepappln.uniqueConstraintViolated", oParams, "E"));
      } 
    } 
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
}
