package wenrgise.hrms.ejb.business;

import java.rmi.RemoteException;
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 javax.ejb.CreateException;
import javax.ejb.RemoveException;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.bean.LOVBean;
import wenrgise.common.ejb.business.HrmCommonBO;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseMessageKeyException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.DateUtility;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.MessageKey;
import wenrgise.common.utility.RecordMetaInfo;
import wenrgise.common.utility.UserInfo;
import wenrgise.common.utility.WorkFlowServiceLocator;
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.utility.DBUtilitiesBean;
import wenrgise.hrms.bean.HrmATDDeputChrgRptHdrBean;
import wenrgise.hrms.vo.HrmATDDptnChrgRptQVO;
import wenrgise.workflow.bean.WflAuthorizationBean;
import wenrgise.workflow.core.WflDocumentInfo;
import wenrgise.workflow.core.WflResource;
import wenrgise.workflow.core.WflSite;
import wenrgise.workflow.core.WflStatus;
import wenrgise.workflow.core.impl.WflDocumentInfoImpl;
import wenrgise.workflow.core.impl.WflResourceImpl;
import wenrgise.workflow.core.impl.WflSiteImpl;
import wenrgise.workflow.ejb.facade.WorkFlowFacade;
import wenrgise.workflow.ejb.facade.WorkFlowFacadeHome;

public class HrmATDDptnChrgRptBO extends HrmBaseBO {
  public HrmATDDptnChrgRptBO() {}
  
  public HrmATDDptnChrgRptBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmDeputChargeRepHdrMetaInfo(HrmATDDptnChrgRptQVO oHrmATDDptnChrgRptQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int iCount = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, oHrmATDDptnChrgRptQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(2, 1, 12, oHrmATDDptnChrgRptQVO.getDptOrderNo()));
    arylstParameters.add(new DBObject(3, 1, 12, oHrmATDDptnChrgRptQVO.getEmpId()));
    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, "HRMDEPTNCHARGEREPORT.proc_HrmDeputChargeRepHdrCount(?,?,?,?,?,?,?,?)");
    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 getHrmDeputChargeRepHeaderInfo(HrmATDDptnChrgRptQVO oHrmATDDptnChrgRptQVO, long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int iCount = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    ArrayList arylstHeaderList = null;
    if (oHrmATDDptnChrgRptQVO == null)
      oHrmATDDptnChrgRptQVO = new HrmATDDptnChrgRptQVO(); 
    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, oHrmATDDptnChrgRptQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(4, 1, 12, oHrmATDDptnChrgRptQVO.getDptOrderNo()));
    arylstParameters.add(new DBObject(5, 1, 12, oHrmATDDptnChrgRptQVO.getEmpId()));
    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, "HRMDEPTNCHARGEREPORT.proc_HrmDeputChargeRepHdr(?,?,?,?,?,?,?,?,?)");
    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 oIt = arylstList.iterator();
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    while (oIt.hasNext()) {
      if (iCount == 0)
        arylstHeaderList = new ArrayList(); 
      iCount++;
      oRow = oIt.next();
      HrmATDDeputChrgRptHdrBean oDeputChargeRepHeader = new HrmATDDeputChrgRptHdrBean();
      oDeputChargeRepHeader.setHeaderPrimaryKey(oRow.get("ID").getString());
      oDeputChargeRepHeader.setDptOrderNoId(oRow.get("DEPUT_HDR_ID").getString());
      oDeputChargeRepHeader.setChargeType(oRow.get("CHARGE_TYPE").getString());
      oDeputChargeRepHeader.setChargeCode(oRow.get("CHARGE_CODE").getString());
      oDeputChargeRepHeader.setNoonFlag(oRow.get("FNOON_ANOON_FLAG").getString());
      oDeputChargeRepHeader.setRemarks(oRow.get("HANDOVER_REMARKS").getString());
      oDeputChargeRepHeader.setChargeStatus(oRow.get("APPROVAL_FLAG").getString());
      oDeputChargeRepHeader.setHandOverDate(EnrgiseUtil.convertToString(oRow.get("HANDOVER_DATE").getDate()));
      oDeputChargeRepHeader.setDeputationDate(EnrgiseUtil.convertToString(oRow.get("DEPUTN_ORDER_DATE").getDate()));
      oDeputChargeRepHeader.setEmpName(oRow.get("EMPNAME").getString());
      oDeputChargeRepHeader.setEmpId(oRow.get("E_PER_DTL_ID").getString());
      oDeputChargeRepHeader.setDptOrderNo(oRow.get("DEPUTN_ORDER_NO").getString());
      oDeputChargeRepHeader.setFromSiteCode(oRow.get("FROM_SITE_NAME").getString());
      oDeputChargeRepHeader.setFromWorkGroupCode(oRow.get("FROM_WRKGRP_NAME").getString());
      oDeputChargeRepHeader.setToSiteCode(oRow.get("TO_SITE_NAME").getString());
      oDeputChargeRepHeader.setToWorkGroupCode(oRow.get("TO_WRKGRP_NAME").getString());
      oDeputChargeRepHeader.setFromSiteId(oRow.get("FROM_SITE_ID").getString());
      oDeputChargeRepHeader.setFromWrkGrpId(oRow.get("FROM_WRKGRP_ID").getString());
      oDeputChargeRepHeader.setToSiteId(oRow.get("TO_SITE_ID").getString());
      oDeputChargeRepHeader.setToWrkGrpId(oRow.get("TO_WRKGRP_ID").getString());
      oDeputChargeRepHeader.setDepOrdrDtlId(oRow.get("DEPUT_ORDER_DTL_ID").getString());
      arylstHeaderList.add(oDeputChargeRepHeader);
    } 
    return arylstHeaderList;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_EMP_CHARGE_REPORT";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {}
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {}
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return new String();
  }
  
  public LovVO getHrmDepChrgOrderNoLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("ID");
    arylstHeaderList.add("hrm.HrmDeputChargeReport.dptOrderNo");
    arylstHeaderList.add("hrm.HrmDeputChargeReport.empName");
    arylstHeaderList.add("hrm.HrmDeputChargeReport.chargeType");
    arylstHeaderList.add("hrm.HrmChargeReport.fromWorkGroupCode");
    arylstHeaderList.add("hrm.HrmChargeReport.toWorkGroupCode");
    arylstHeaderList.add("hrm.HrmChargeReport.fromSiteCode");
    arylstHeaderList.add("hrm.HrmChargeReport.toSiteCode");
    arylstHeaderList.add("hrm.HrmDeputChargeReport.deputationDate");
    arylstHeaderList.add("hrm.HrmDeputChargeReport.depOrdrDtlId");
    oLovVO.setHeaderList(arylstHeaderList);
    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");
    arylstVisibility.add("V");
    arylstVisibility.add("H");
    oLovVO.setVisibilityList(arylstVisibility);
    int iCount = 0;
    String empId = null;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    if (oLovQueryVO.getProperty("empId") != null) {
      empId = oLovQueryVO.getProperty("empId");
      System.out.println(empId);
    } 
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParameters.add(new DBObject(2, 1, 12, empId));
    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, "HRMDEPTNCHARGELOV.proc_GetHrmChrgDeputNoLOVdata(?,?,?,?,?,?)");
    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("DEPUTN_ORDER_NO").getString());
      oLOVBean.setDetailField3(oRow.get("EMPNAME").getString());
      if (String.valueOf(oRow.get("CHARGE_TYPE").getString()).equalsIgnoreCase("H")) {
        oLOVBean.setDetailField4("T");
      } else if (String.valueOf(oRow.get("CHARGE_TYPE").getString()).equalsIgnoreCase("null")) {
        oLOVBean.setDetailField4("H");
      } else if (String.valueOf(oRow.get("CHARGE_TYPE").getString()).equalsIgnoreCase("T")) {
        oLOVBean.setDetailField4("H");
      } 
      oLOVBean.setDetailField5(oRow.get("FROM_WRKGRP_NAME").getString());
      oLOVBean.setDetailField6(oRow.get("TO_WRKGRP_NAME").getString());
      oLOVBean.setDetailField7(oRow.get("FROM_SITE_NAME").getString());
      oLOVBean.setDetailField8(oRow.get("TO_SITE_NAME").getString());
      oLOVBean.setDetailField9(EnrgiseUtil.convertToString(oRow.get("DEPUTN_ORDER_DATE").getDate()));
      oLOVBean.setDetailField10(oRow.get("DEPUT_DTL_ID").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public LovVO getHrmDepChrgOrderNoQLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("Id");
    arylstHeaderList.add("hrm.HrmDeputChargeReport.dptOrderNo");
    arylstHeaderList.add("hrm.HrmDeputChargeReport.empName");
    arylstHeaderList.add("chargeType");
    arylstHeaderList.add("hrm.HrmChargeReport.fromWorkGroupCode");
    arylstHeaderList.add("hrm.HrmChargeReport.toWorkGroupCode");
    arylstHeaderList.add("hrm.HrmChargeReport.fromSiteCode");
    arylstHeaderList.add("hrm.HrmChargeReport.toSiteCode");
    arylstHeaderList.add("hrm.HrmDeputChargeReport.deputationDate");
    arylstHeaderList.add("hrm.HrmDeputChargeReport.depOrdrDtlId");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("H");
    oLovVO.setVisibilityList(arylstVisibility);
    int iCount = 0;
    String empId = null;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    if (oLovQueryVO.getProperty("empId") != null) {
      empId = oLovQueryVO.getProperty("empId");
      System.out.println(empId);
    } 
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParameters.add(new DBObject(2, 1, 12, empId));
    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, "HRMDEPTNCHARGELOV.proc_GetHrmChrgDeputNoQLOVdata(?,?,?,?,?,?)");
    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("DEPUTN_ORDER_NO").getString());
      oLOVBean.setDetailField3(oRow.get("EMPNAME").getString());
      oLOVBean.setDetailField4(oRow.get("CHARGE_TYPE").getString());
      oLOVBean.setDetailField5(oRow.get("FROM_WRKGRP_NAME").getString());
      oLOVBean.setDetailField6(oRow.get("TO_WRKGRP_NAME").getString());
      oLOVBean.setDetailField7(oRow.get("FROM_SITE_NAME").getString());
      oLOVBean.setDetailField8(oRow.get("TO_SITE_NAME").getString());
      oLOVBean.setDetailField9(EnrgiseUtil.convertToString(oRow.get("DEPUTN_ORDER_DATE").getDate()));
      oLOVBean.setDetailField10(oRow.get("DEPUT_DTL_ID").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public LovVO getHrmDepChrgCodeLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("ID");
    arylstHeaderList.add("hrm.HrmDeputChargeReport.chargeCode");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int iCount = 0;
    String dptOrderNoId = null;
    String chargeType = null;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParameters.add(new DBObject(2, 2, -10));
    arylstParameters.add(new DBObject(3, 2, 12));
    arylstParameters.add(new DBObject(4, 2, 12));
    arylstParameters.add(new DBObject(5, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMDEPTNCHARGELOV.proc_GetHrmDeputChrgCdLOVdata(?,?,?,?,?)");
    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("CHARGE_CODE").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    String sSiteId = new String();
    String sWorkGroupId = new String();
    HrmATDDeputChrgRptHdrBean oHrmATDDeputChrgRptHdrBean = (HrmATDDeputChrgRptHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      arylstParameters = new ArrayList();
      arylstParameters.add(new DBObject(1, 1, 12, "N"));
      arylstParameters.add(new DBObject(2, 1, 12, oHrmATDDeputChrgRptHdrBean.getDepOrdrDtlId()));
      arylstParameters.add(new DBObject(3, 1, 12, oHrmATDDeputChrgRptHdrBean.getChargeType()));
      System.out.println(oHrmATDDeputChrgRptHdrBean.getChargeType());
      arylstParameters.add(new DBObject(4, 1, 12, oHrmATDDeputChrgRptHdrBean.getChargeCode()));
      arylstParameters.add(new DBObject(5, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmATDDeputChrgRptHdrBean.getHandOverDate())));
      arylstParameters.add(new DBObject(6, 1, 12, oHrmATDDeputChrgRptHdrBean.getNoonFlag()));
      arylstParameters.add(new DBObject(7, 1, 12, oHrmATDDeputChrgRptHdrBean.getRemarks()));
      if (oHrmATDDeputChrgRptHdrBean.getChargeType().equalsIgnoreCase("H")) {
        sSiteId = oHrmATDDeputChrgRptHdrBean.getFromSiteId();
        sWorkGroupId = oHrmATDDeputChrgRptHdrBean.getFromWrkGrpId();
      } else if (oHrmATDDeputChrgRptHdrBean.getChargeType().equalsIgnoreCase("T")) {
        sSiteId = oHrmATDDeputChrgRptHdrBean.getToSiteId();
        sWorkGroupId = oHrmATDDeputChrgRptHdrBean.getToWrkGrpId();
      } 
      arylstParameters.add(new DBObject(8, 1, 12, sWorkGroupId));
      arylstParameters.add(new DBObject(9, 1, 12, sSiteId));
      arylstParameters.add(new DBObject(10, 1, 12, "A"));
      arylstParameters.add(new DBObject(11, 1, 12, this.oUserInfo.getUserTypeId()));
      arylstParameters.add(new DBObject(12, 1, 12, this.oUserInfo.getSiteId()));
      arylstParameters.add(new DBObject(13, 1, 12, null));
      arylstParameters.add(new DBObject(14, 2, 12));
      arylstParameters.add(new DBObject(15, 2, 12));
      arylstParameters.add(new DBObject(16, 2, 12));
      arylstParameters.add(new DBObject(17, 2, 4));
      ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMDEPTNCHARGEREPORT.proc_UpsertHrDeputChrgRptHdr(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      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, oHrmATDDeputChrgRptHdrBean.getDepOrdrDtlId()));
      arylstParameters.add(new DBObject(3, 1, 12, oHrmATDDeputChrgRptHdrBean.getChargeType()));
      arylstParameters.add(new DBObject(4, 1, 12, oHrmATDDeputChrgRptHdrBean.getChargeCode()));
      arylstParameters.add(new DBObject(5, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmATDDeputChrgRptHdrBean.getHandOverDate())));
      arylstParameters.add(new DBObject(6, 1, 12, oHrmATDDeputChrgRptHdrBean.getNoonFlag()));
      arylstParameters.add(new DBObject(7, 1, 12, oHrmATDDeputChrgRptHdrBean.getRemarks()));
      if (oHrmATDDeputChrgRptHdrBean.getChargeType().equalsIgnoreCase("H")) {
        sSiteId = oHrmATDDeputChrgRptHdrBean.getFromSiteId();
        sWorkGroupId = oHrmATDDeputChrgRptHdrBean.getFromWrkGrpId();
      } else if (oHrmATDDeputChrgRptHdrBean.getChargeType().equalsIgnoreCase("T")) {
        sSiteId = oHrmATDDeputChrgRptHdrBean.getToSiteId();
        sWorkGroupId = oHrmATDDeputChrgRptHdrBean.getToWrkGrpId();
      } 
      arylstParameters.add(new DBObject(8, 1, 12, sWorkGroupId));
      arylstParameters.add(new DBObject(9, 1, 12, sSiteId));
      arylstParameters.add(new DBObject(10, 1, 12, oHrmATDDeputChrgRptHdrBean.getChargeStatus()));
      arylstParameters.add(new DBObject(11, 1, 12, this.oUserInfo.getUserTypeId()));
      arylstParameters.add(new DBObject(12, 1, 12, this.oUserInfo.getSiteId()));
      arylstParameters.add(new DBObject(13, 1, 12, oHrmATDDeputChrgRptHdrBean.getHeaderPrimaryKey()));
      arylstParameters.add(new DBObject(14, 2, 12));
      arylstParameters.add(new DBObject(15, 2, 12));
      arylstParameters.add(new DBObject(16, 2, 12));
      arylstParameters.add(new DBObject(17, 2, 4));
      ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMDEPTNCHARGEREPORT.proc_UpsertHrDeputChrgRptHdr(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = arylstOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } else if (ScreenMode.equalsIgnoreCase("D")) {
    
    } 
    updateTables((HrmATDDeputChrgRptHdrBean)oBaseHeaderBean);
    return returnString;
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oErrorList = new ArrayList();
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        checkMandatoryHeader((HrmATDDeputChrgRptHdrBean)oBaseHeaderBean);  
    reportError(oErrorList);
  }
  
  private void checkMandatoryHeader(HrmATDDeputChrgRptHdrBean oHrmATDDeputChrgRptHdrBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oErrorList = new ArrayList();
    if (!EnrgiseUtil.checkString(oHrmATDDeputChrgRptHdrBean.getDptOrderNo())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmDeputChargeReport.dptOrderNo");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmATDDeputChrgRptHdrBean.getChargeCode())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmDeputChargeReport.chargeCode");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(oErrorList);
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  private void checkDatesConstraint(HrmATDDeputChrgRptHdrBean oHrmATDDeputChrgRptHdrBean, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    if (EnrgiseUtil.checkString(oHrmATDDeputChrgRptHdrBean.getHandOverDate())) {
      int iDateFlag = EnrgiseUtil.compareDates(dateFormat, oHrmATDDeputChrgRptHdrBean.getHandOverDate(), oHrmATDDeputChrgRptHdrBean.getDeputationDate());
      if (iDateFlag == -1 && iDateFlag != -2)
        oErrorList.add(new EnrgiseApplicationException("wenrgise.common.dateConstraintViolated")); 
    } 
  }
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oErrorList = new ArrayList();
    HrmATDDeputChrgRptHdrBean oHrmATDDeputChrgRptHdrBean = (HrmATDDeputChrgRptHdrBean)oBaseHeaderBean;
    if (bHeaderDataChanged) {
      if (!sScreenMode.equalsIgnoreCase("D"))
        checkDatesConstraint((HrmATDDeputChrgRptHdrBean)oBaseHeaderBean, oErrorList); 
      if (sScreenMode.equalsIgnoreCase("D")) {
        HrmCommonBO oCommonBO = new HrmCommonBO();
        if (!oHrmATDDeputChrgRptHdrBean.getChargeStatus().equalsIgnoreCase("N"))
          if (oErrorList.isEmpty())
            oErrorList.add(new EnrgiseApplicationException("wenrgise.common.deletion.rejected"));  
      } 
    } 
    reportError(oErrorList);
  }
  
  public String approveDeputChrgRpt(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    DBUtilitiesBean oBean = null;
    HrmATDDeputChrgRptHdrBean oHrmATDDeputChrgRptHdrBean = (HrmATDDeputChrgRptHdrBean)oBaseHeaderBean;
    updateTables((HrmATDDeputChrgRptHdrBean)oBaseHeaderBean);
    String sHeaderPrimaryKey = (null != oBaseHeaderBean) ? oBaseHeaderBean.getHeaderPrimaryKey() : "";
    WflStatus status = null;
    if (!EnrgiseUtil.checkString(oBaseHeaderBean.getWorkListId())) {
      status = callWorkFlow(oBaseHeaderBean, arylstDetailBeanArray, "Approve", "Initiated");
    } else {
      status = callWorkFlow(oBaseHeaderBean, arylstDetailBeanArray, "Approve", (String)null);
    } 
    if (status.getStatus().equalsIgnoreCase("FinallyApproved")) {
      oBean = new DBUtilitiesBean();
      ArrayList arylstParameters = new ArrayList();
      arylstParameters = new ArrayList();
      arylstParameters.add(new DBObject(1, 1, 12, sHeaderPrimaryKey));
      arylstParameters.add(new DBObject(2, 2, 12));
      arylstParameters.add(new DBObject(3, 2, 12));
      arylstParameters.add(new DBObject(4, 2, 4));
      ArrayList arrayList = oBean.callProc(arylstParameters, "HRMDEPTNCHARGEREPORT.procApproveDeputChrgRpt(?,?,?,?)");
    } 
    return sHeaderPrimaryKey;
  }
  
  public String submitDeputChrgRpt(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    DBUtilitiesBean oBean = null;
    HrmATDDeputChrgRptHdrBean oHrmATDDeputChrgRptHdrBean = (HrmATDDeputChrgRptHdrBean)oBaseHeaderBean;
    String sHeaderPrimaryKey = (null != oBaseHeaderBean) ? oBaseHeaderBean.getHeaderPrimaryKey() : "";
    WflStatus status = null;
    if (!EnrgiseUtil.checkString(oBaseHeaderBean.getWorkListId())) {
      status = callWorkFlow(oBaseHeaderBean, arylstDetailBeanArray, "Approve", "Initiated");
    } else {
      status = callWorkFlow(oBaseHeaderBean, arylstDetailBeanArray, "Approve", (String)null);
    } 
    if (status.getStatus().equalsIgnoreCase("Pending")) {
      oBean = new DBUtilitiesBean();
      ArrayList arylstParameters = new ArrayList();
      arylstParameters = new ArrayList();
      arylstParameters.add(new DBObject(1, 1, 12, sHeaderPrimaryKey));
      arylstParameters.add(new DBObject(2, 2, 12));
      arylstParameters.add(new DBObject(3, 2, 12));
      arylstParameters.add(new DBObject(4, 2, 4));
      ArrayList arrayList = oBean.callProc(arylstParameters, "HRMDEPTNCHARGEREPORT.procSubmitDeputChrgRpt(?,?,?,?)");
    } 
    return sHeaderPrimaryKey;
  }
  
  public String rejectDeputChrgRpt(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    DBUtilitiesBean oBean = null;
    HrmATDDeputChrgRptHdrBean oHrmATDDeputChrgRptHdrBean = (HrmATDDeputChrgRptHdrBean)oBaseHeaderBean;
    String sHeaderPrimaryKey = (null != oBaseHeaderBean) ? oBaseHeaderBean.getHeaderPrimaryKey() : "";
    WflStatus status = null;
    if (!EnrgiseUtil.checkString(oBaseHeaderBean.getWorkListId())) {
      status = callWorkFlow(oBaseHeaderBean, arylstDetailBeanArray, "Reject", "Initiated");
    } else {
      status = callWorkFlow(oBaseHeaderBean, arylstDetailBeanArray, "Reject", (String)null);
    } 
    if (status.getStatus().equalsIgnoreCase("Rejected")) {
      oBean = new DBUtilitiesBean();
      ArrayList arylstParameters = new ArrayList();
      arylstParameters = new ArrayList();
      arylstParameters.add(new DBObject(1, 1, 12, sHeaderPrimaryKey));
      arylstParameters.add(new DBObject(2, 2, 12));
      arylstParameters.add(new DBObject(3, 2, 12));
      arylstParameters.add(new DBObject(4, 2, 4));
      ArrayList arrayList = oBean.callProc(arylstParameters, "HRMDEPTNCHARGEREPORT.procRejectDeputChrgRpt(?,?,?,?)");
    } 
    return sHeaderPrimaryKey;
  }
  
  private void updateTables(HrmATDDeputChrgRptHdrBean oHrmATDDeputChrgRptHdrBean) throws EnrgiseSystemException {
    String sEmpId = oHrmATDDeputChrgRptHdrBean.getEmpId();
    String sFromSiteId = oHrmATDDeputChrgRptHdrBean.getFromSiteId();
    String sBaseSiteId = oHrmATDDeputChrgRptHdrBean.getToSiteId();
    String sTransferDate = oHrmATDDeputChrgRptHdrBean.getDeputationDate();
    String sHandOverDate = oHrmATDDeputChrgRptHdrBean.getHandOverDate();
    String sFromWrkGrpId = oHrmATDDeputChrgRptHdrBean.getFromWrkGrpId();
    String sToWrkGrpId = oHrmATDDeputChrgRptHdrBean.getToWrkGrpId();
    if (oHrmATDDeputChrgRptHdrBean.getDeputType().equalsIgnoreCase("W"))
      if (oHrmATDDeputChrgRptHdrBean.getChargeType().equalsIgnoreCase("H")) {
        DBUtilitiesBean oBean1 = null;
        oBean1 = new DBUtilitiesBean();
        ArrayList arylstParameters1 = new ArrayList();
        arylstParameters1 = new ArrayList();
        arylstParameters1.add(new DBObject(1, 1, 12, sEmpId));
        arylstParameters1.add(new DBObject(2, 1, 12, sBaseSiteId));
        arylstParameters1.add(new DBObject(3, 2, 12));
        arylstParameters1.add(new DBObject(4, 2, 12));
        arylstParameters1.add(new DBObject(5, 2, 4));
        ArrayList arylstOutArray1 = oBean1.callProc(arylstParameters1, "HRMCHARGEREPORT.procUpdateHrmEmpPers(?,?,?,?,?)");
        DBUtilitiesBean oBean2 = null;
        oBean2 = new DBUtilitiesBean();
        String sWrkGrpId = new String();
        if (sToWrkGrpId != null) {
          sWrkGrpId = sToWrkGrpId;
        } else {
          sWrkGrpId = sFromWrkGrpId;
        } 
        ArrayList arylstParameters2 = new ArrayList();
        arylstParameters2 = new ArrayList();
        arylstParameters2.add(new DBObject(1, 1, 12, sEmpId));
        arylstParameters2.add(new DBObject(2, 1, 12, sBaseSiteId));
        arylstParameters2.add(new DBObject(3, 1, 12, sHandOverDate));
        arylstParameters2.add(new DBObject(4, 1, 12, sWrkGrpId));
        arylstParameters2.add(new DBObject(5, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParameters2.add(new DBObject(6, 1, 12, this.oUserInfo.getSiteId()));
        arylstParameters2.add(new DBObject(7, 2, 12));
        arylstParameters2.add(new DBObject(8, 2, 12));
        arylstParameters2.add(new DBObject(9, 2, 4));
        ArrayList arylstOutArray2 = oBean2.callProc(arylstParameters2, "HRMCHARGEREPORT.procUpsertHrmWrkGrpDtl(?,?,?,?,?,?,?,?,?)");
        DBUtilitiesBean oBean3 = null;
        oBean3 = new DBUtilitiesBean();
        ArrayList arylstParameters3 = new ArrayList();
        arylstParameters3.add(new DBObject(1, 1, 12, sEmpId));
        arylstParameters3.add(new DBObject(2, 1, 12, sFromSiteId));
        arylstParameters3.add(new DBObject(3, 1, 12, sBaseSiteId));
        arylstParameters3.add(new DBObject(4, 1, 12, sTransferDate));
        arylstParameters3.add(new DBObject(5, 1, 12, sHandOverDate));
        arylstParameters3.add(new DBObject(6, 1, 12, sFromWrkGrpId));
        arylstParameters3.add(new DBObject(7, 1, 12, sToWrkGrpId));
        arylstParameters3.add(new DBObject(8, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParameters3.add(new DBObject(9, 1, 12, this.oUserInfo.getSiteId()));
        arylstParameters3.add(new DBObject(10, 2, 12));
        arylstParameters3.add(new DBObject(11, 2, 12));
        arylstParameters3.add(new DBObject(12, 2, 4));
        ArrayList arrayList1 = oBean3.callProc(arylstParameters3, "HRMCHARGEREPORT.procUpsertHrmEmpIncr(?,?,?,?,?,?,?,?,?,?,?,?)");
      }  
    if (!oHrmATDDeputChrgRptHdrBean.getDeputType().equalsIgnoreCase("W")) {
      DBUtilitiesBean oBean5 = null;
      oBean5 = new DBUtilitiesBean();
      String sWrkGrpId = new String();
      if (sToWrkGrpId != null) {
        sWrkGrpId = sToWrkGrpId;
      } else {
        sWrkGrpId = sFromWrkGrpId;
      } 
      ArrayList arylstParameters5 = new ArrayList();
      arylstParameters5 = new ArrayList();
      arylstParameters5.add(new DBObject(1, 1, 12, sEmpId));
      arylstParameters5.add(new DBObject(2, 1, 12, sBaseSiteId));
      arylstParameters5.add(new DBObject(3, 1, 12, sHandOverDate));
      arylstParameters5.add(new DBObject(4, 1, 12, sWrkGrpId));
      arylstParameters5.add(new DBObject(5, 1, 12, this.oUserInfo.getUserTypeId()));
      arylstParameters5.add(new DBObject(6, 1, 12, this.oUserInfo.getSiteId()));
      arylstParameters5.add(new DBObject(7, 2, 12));
      arylstParameters5.add(new DBObject(8, 2, 12));
      arylstParameters5.add(new DBObject(9, 2, 4));
      ArrayList arylstOutArray5 = oBean5.callProc(arylstParameters5, "HRMDEPTNCHARGEREPORT.procUpsertHrmWrkGrpDtl(?,?,?,?,?,?,?,?,?)");
      DBUtilitiesBean oBean7 = null;
      oBean7 = new DBUtilitiesBean();
      ArrayList arylstParameters7 = new ArrayList();
      arylstParameters7 = new ArrayList();
      arylstParameters7.add(new DBObject(1, 1, 12, sEmpId));
      arylstParameters7.add(new DBObject(2, 1, 12, sBaseSiteId));
      arylstParameters7.add(new DBObject(3, 2, 12));
      arylstParameters7.add(new DBObject(4, 2, 12));
      arylstParameters7.add(new DBObject(5, 2, 4));
      ArrayList arrayList1 = oBean7.callProc(arylstParameters7, "HRMDEPTNCHARGEREPORT.procUpdateEmpTypeHrmEmpPers(?,?,?,?,?)");
    } 
  }
  
  public WflStatus callWorkFlow(BaseHeaderBean oBaseHeaderBean, ArrayList arylstDetailBeanArray, String activity, String initiatedFlag) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      WorkFlowFacadeHome oHome = (WorkFlowFacadeHome)WorkFlowServiceLocator.getLocator().getService("WorkFlowFacade");
      WorkFlowFacade workFlowFacade = oHome.create();
      HrmATDDeputChrgRptHdrBean oHrmATDDeputChrgRptHdrBean = (HrmATDDeputChrgRptHdrBean)oBaseHeaderBean;
      WflResourceImpl wflResourceImpl = new WflResourceImpl();
      wflResourceImpl.setEmployeeId(this.oUserInfo.getUserTypeId());
      WflSiteImpl wflSiteImpl = new WflSiteImpl();
      wflSiteImpl.setSiteId(this.oUserInfo.getSiteId());
      wflResourceImpl.setLoginSite((WflSite)wflSiteImpl);
      WflDocumentInfoImpl wflDocumentInfoImpl = new WflDocumentInfoImpl();
      wflDocumentInfoImpl.setRequester((WflResource)wflResourceImpl);
      wflDocumentInfoImpl.setDocumentId(oHrmATDDeputChrgRptHdrBean.getHeaderPrimaryKey());
      wflDocumentInfoImpl.setDocumentType("Deputation");
      wflDocumentInfoImpl.setInitiatedFlag(initiatedFlag);
      String documentTypeId = workFlowFacade.getDocumentTypeId("DeputChrgRpt");
      if (documentTypeId == null)
        throw new EnrgiseSystemException(); 
      if (!EnrgiseUtil.checkString(initiatedFlag))
        wflDocumentInfoImpl.setWorkListId(oBaseHeaderBean.getWorkListId()); 
      String activityId = workFlowFacade.getActivityId(activity);
      if (activityId == null)
        throw new EnrgiseSystemException(); 
      wflDocumentInfoImpl.setDocumentTypeId(documentTypeId);
      if (!activity.equalsIgnoreCase("Reject")) {
        wflDocumentInfoImpl.setDocumentNumber(String.valueOf(String.valueOf("Deputation Charge Report No:").concat(String.valueOf(oHrmATDDeputChrgRptHdrBean.getHeaderPrimaryKey()))).concat(String.valueOf("is pending for approval")));
        wflDocumentInfoImpl.setDocDesc(String.valueOf("The following document has been pending for approval:Training Requirement:").concat(String.valueOf(oHrmATDDeputChrgRptHdrBean.getHeaderPrimaryKey())));
      } else if (!activity.equalsIgnoreCase("Approve")) {
        wflDocumentInfoImpl.setDocumentNumber(String.valueOf(String.valueOf("Deputation Charge Report No:").concat(String.valueOf(oHrmATDDeputChrgRptHdrBean.getHeaderPrimaryKey()))).concat(String.valueOf("is pending for rejection")));
        wflDocumentInfoImpl.setDocDesc(String.valueOf("The following document has been pending for rejection:Deputation Application No:").concat(String.valueOf(oHrmATDDeputChrgRptHdrBean.getHeaderPrimaryKey())));
      } 
      wflDocumentInfoImpl.setModuleId("8");
      wflDocumentInfoImpl.setCreator((WflResource)wflResourceImpl);
      wflDocumentInfoImpl.setRequesterAction(activityId);
      HashMap oProp = new HashMap();
      oProp.put("1", "1");
      wflDocumentInfoImpl.setDocumentAttributes(oProp);
      WflAuthorizationBean wflAuthBean = workFlowFacade.canDo((WflDocumentInfo)wflDocumentInfoImpl);
      if (EnrgiseUtil.checkString(wflAuthBean.getErrorCode()))
        throw new EnrgiseApplicationException(wflAuthBean.getErrorCode()); 
      if (!wflAuthBean.isAuthStatus())
        throw new EnrgiseApplicationException("wenrgise.workflow.submit.notauthorized"); 
      WflStatus wflStatus = workFlowFacade.process((WflDocumentInfo)wflDocumentInfoImpl);
      if (wflStatus.getStatus().equals("Error"))
        throw new EnrgiseSystemException(); 
      workFlowFacade.remove();
      return wflStatus;
    } catch (RemoteException oRtEx) {
      throw new EnrgiseSystemException("wenrgise.workflow.connect", oRtEx);
    } catch (CreateException oCx) {
      throw new EnrgiseSystemException("wenrgise.workflow.connect", oCx);
    } catch (RemoveException oRx) {
      throw new EnrgiseSystemException("wenrgise.workflow.connect", oRx);
    } 
  }
}
