package wenrgise.hrms.ejb.business;

import java.sql.Timestamp;
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.EnrgiseSystemException;
import wenrgise.common.utility.RecordMetaInfo;
import wenrgise.common.utility.UserInfo;
import wenrgise.common.vo.BaseDetailVO;
import wenrgise.common.vo.BaseHeaderVO;
import wenrgise.common.vo.BaseQueryVO;
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.HrmMyWorkflowDtlBean;
import wenrgise.hrms.bean.HrmMyWorkflowHdrBean;
import wenrgise.hrms.vo.HrmMyWorkflowQVO;

public class HrmMyWorkflowBO extends HrmBaseBO {
  public HrmMyWorkflowBO() {}
  
  public HrmMyWorkflowBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_CLAIM_MST";
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    return null;
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {}
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {}
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return new String();
  }
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {}
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public void additionalFieldValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {}
  
  public RecordMetaInfo getHrmMyWorkflowHdrMetaInfo(HrmMyWorkflowQVO oHrmMyWorkflowQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    String empId = oHrmMyWorkflowQVO.getEmpId();
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, oHrmMyWorkflowQVO.getId()));
    arylstParam.add(new DBObject(2, 1, 12, oHrmMyWorkflowQVO.getEmpId()));
    arylstParam.add(new DBObject(3, 1, 12, oHrmMyWorkflowQVO.getTxtStatus()));
    arylstParam.add(new DBObject(4, 1, 12, oHrmMyWorkflowQVO.getWorkflowType()));
    arylstParam.add(new DBObject(5, 2, -5));
    arylstParam.add(new DBObject(6, 2, 93));
    arylstParam.add(new DBObject(7, 2, 12));
    arylstParam.add(new DBObject(8, 2, 12));
    arylstParam.add(new DBObject(9, 2, 4));
    ArrayList oOutArray = oBean.callProc(arylstParam, "WORKFLOW.proc_myWorkListcount(?,?,?,?,?,?,?,?,?)");
    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 getHrmMyWorkflowHdrInfo(HrmMyWorkflowQVO oHrmMyWorkflowQVO, long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String empId = oHrmMyWorkflowQVO.getEmpId();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    ArrayList oHeaderList = null;
    if (oHrmMyWorkflowQVO == null)
      oHrmMyWorkflowQVO = new HrmMyWorkflowQVO(); 
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, -5, new Long(lStartPosition)));
    arylstParam.add(new DBObject(2, 1, -5, new Long(lLastPosition)));
    arylstParam.add(new DBObject(3, 1, 12, oHrmMyWorkflowQVO.getId()));
    arylstParam.add(new DBObject(4, 1, 12, oHrmMyWorkflowQVO.getEmpId()));
    arylstParam.add(new DBObject(5, 1, 12, oHrmMyWorkflowQVO.getTxtStatus()));
    arylstParam.add(new DBObject(6, 1, 12, oHrmMyWorkflowQVO.getWorkflowType()));
    arylstParam.add(new DBObject(7, 2, -10));
    arylstParam.add(new DBObject(8, 2, 12));
    arylstParam.add(new DBObject(9, 2, 12));
    arylstParam.add(new DBObject(10, 2, 4));
    ArrayList oOutArray = oBean.callProc(arylstParam, "WORKFLOW.proc_myWorkListinfo(?,?,?,?,?,?,?,?,?,?)");
    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();
      HrmMyWorkflowHdrBean oHrmMyWorkflowHeader = new HrmMyWorkflowHdrBean();
      oHrmMyWorkflowHeader.setId(oRow.get("id").getString());
      oHrmMyWorkflowHeader.setRequestType(oRow.get("requestType").getString());
      oHrmMyWorkflowHeader.setTxtStatus(oRow.get("status").getString());
      oHrmMyWorkflowHeader.setWorkflowType(oRow.get("workFlowType").getString());
      oHeaderList.add(oHrmMyWorkflowHeader);
    } 
    return oHeaderList;
  }
  
  public LovVO getWorkIdLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("ID");
    arylstHeaderList.add("hrm.HrmMyWorkflow.id");
    arylstHeaderList.add("hrm.HrmMyWorkflow.requestType");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList arylstParam = 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(); 
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParam.add(new DBObject(2, 1, 12, sQuerySearch2));
    arylstParam.add(new DBObject(3, 2, -10));
    arylstParam.add(new DBObject(4, 2, 12));
    arylstParam.add(new DBObject(5, 2, 12));
    arylstParam.add(new DBObject(6, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "WORKFLOW.procGetHrmMyWorkflowIdLOVdata(?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    arylstParam = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstParam.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        arylstParam = new ArrayList(); 
      count++;
      oRow = (QueryRow)iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("ID").getString());
      oLOVBean.setDetailField2(oRow.get("ID").getString());
      oLOVBean.setDetailField3(oRow.get("doc_type_desc").getString());
      arylstParam.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstParam);
    return oLovVO;
  }
  
  public RecordMetaInfo getHrmMyWorkflowDetailMetaInfo(BaseQueryVO oBaseQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    HrmMyWorkflowQVO oHrmMyWorkflowQVO = (HrmMyWorkflowQVO)oBaseQueryVO;
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, oHrmMyWorkflowQVO.getEmpId()));
    arylstParameters.add(new DBObject(2, 1, 12, oHrmMyWorkflowQVO.getId()));
    arylstParameters.add(new DBObject(3, 1, 12, oHrmMyWorkflowQVO.getTxtStatus()));
    arylstParameters.add(new DBObject(4, 1, 12, oHrmMyWorkflowQVO.getWorkflowType()));
    arylstParameters.add(new DBObject(5, 2, -5));
    arylstParameters.add(new DBObject(6, 2, 93));
    arylstParameters.add(new DBObject(7, 2, 12));
    arylstParameters.add(new DBObject(8, 2, 12));
    arylstParameters.add(new DBObject(9, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "WORKFLOW.proc_HrmWorkFlowDtlCount(?,?,?,?,?,?,?,?,?)");
    RecordMetaInfo oRecordMetaInfo = new RecordMetaInfo();
    DBObject oTotalRecord = arylstOutArray.get(0);
    oRecordMetaInfo.setRecordCount(((Long)oTotalRecord.getObject()).longValue());
    DBObject oTimeObject = arylstOutArray.get(1);
    oRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    return oRecordMetaInfo;
  }
  
  public ArrayList getHrmMyWorkflowDetailInfo(BaseQueryVO oBaseQueryVO, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    HrmMyWorkflowQVO oHrmMyWorkflowQVO = (HrmMyWorkflowQVO)oBaseQueryVO;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList arrHrmMyWorkflowDtl = null;
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, -5, new Long(lDetailFirstPosition)));
    arylstParameters.add(new DBObject(2, 1, -5, new Long(lDetailLastPosition)));
    arylstParameters.add(new DBObject(3, 1, 12, oHrmMyWorkflowQVO.getEmpId()));
    arylstParameters.add(new DBObject(4, 1, 12, oHrmMyWorkflowQVO.getId()));
    arylstParameters.add(new DBObject(5, 1, 12, oHrmMyWorkflowQVO.getTxtStatus()));
    arylstParameters.add(new DBObject(6, 1, 12, oHrmMyWorkflowQVO.getWorkflowType()));
    arylstParameters.add(new DBObject(7, 2, -10));
    arylstParameters.add(new DBObject(8, 2, 12));
    arylstParameters.add(new DBObject(9, 2, 12));
    arylstParameters.add(new DBObject(10, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "WORKFLOW.proc_HrmWorkFlowDtlInfo(?,?,?,?,?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    if (arylstList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    QueryRow oRow = null;
    QueryValue oValue = null;
    HashMap oColumns = null;
    Iterator itrBean = arylstList.iterator();
    while (itrBean.hasNext()) {
      if (count == 0)
        arrHrmMyWorkflowDtl = new ArrayList(); 
      count++;
      oRow = itrBean.next();
      HrmMyWorkflowDtlBean oWorkflowBean = new HrmMyWorkflowDtlBean();
      oWorkflowBean.setDetailId(oRow.get("ID").getString());
      oWorkflowBean.setDocDtlId(oRow.get("doc_dtl_id").getString());
      oWorkflowBean.setRequestId(oRow.get("id").getString());
      oWorkflowBean.setRequestor(oRow.get("delegator_emp_id").getString());
      oWorkflowBean.setRequestorName(oRow.get("delegator_emp_name").getString());
      oWorkflowBean.setApprover(oRow.get("delegated_emp_id").getString());
      oWorkflowBean.setApproverName(oRow.get("delegated_emp_name").getString());
      oWorkflowBean.setLevel(oRow.get("level_no").getString());
      arrHrmMyWorkflowDtl.add(oWorkflowBean);
    } 
    return arrHrmMyWorkflowDtl;
  }
}
