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.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.HrmRecScrutAppMtnDtlBean;
import wenrgise.hrms.bean.HrmRecScrutAppMtnHdrBean;
import wenrgise.hrms.vo.HrmRecScrutAppMntQVO;

public class HrmRecScrutAppMntBO extends HrmBaseBO {
  String sStageFlag;
  
  public HrmRecScrutAppMntBO() {}
  
  public HrmRecScrutAppMntBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public LovVO getRefNoLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("");
    arylstHeaderList.add("hrm.HrmScrutinizingApplicant.referenceNo");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    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(); 
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sQuerySearch1));
    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, "HRMRECSCRUTAPPMNT.proc_GetReferenceNoLOV(?,?,?,?,?)");
    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("REF_NO").getString());
      arylstParam.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstParam);
    return oLovVO;
  }
  
  public LovVO getDesigNoLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    String sReferenceId = oLovQueryVO.getProperty("RefId");
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("");
    arylstHeaderList.add("hrm.HrmScrutinizingApplicant.adForDesignation");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    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(); 
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sReferenceId));
    arylstParam.add(new DBObject(2, 1, 12, sQuerySearch1));
    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, "HRMRECSCRUTAPPMNT.proc_GetAdvForDesigLOV(?,?,?,?,?,?)");
    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("DESIGNATION_ID").getString());
      oLOVBean.setDetailField2(oRow.get("DESIG_CODE").getString());
      arylstParam.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstParam);
    return oLovVO;
  }
  
  public RecordMetaInfo getHrmRecScrutAppDtlMetaInfo(HrmRecScrutAppMntQVO oHrmRecScrutAppMntQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String stage = oHrmRecScrutAppMntQVO.getStage();
    this.sStageFlag = stage;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, oHrmRecScrutAppMntQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(2, 1, 12, stage));
    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));
    System.out.println(String.valueOf(String.valueOf(String.valueOf("Before calling proc_GetSAMDetailCount --> ").concat(String.valueOf(oHrmRecScrutAppMntQVO.getHeaderPrimaryKey()))).concat(String.valueOf(" "))).concat(String.valueOf(stage)));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMRECSCRUTAPPMNT.proc_GetSAMDetailCount(?,?,?,?,?,?,?)");
    System.out.println("proc_GetSAMDetailCount executed successfully");
    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 getHrmRecScrutAppDetailInfo(HrmRecScrutAppMntQVO oHrmRecScrutAppMntQVO, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    String stage = oHrmRecScrutAppMntQVO.getStage();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList arylstHrmRecScrutAppMtnDetail = 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, oHrmRecScrutAppMntQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(4, 1, 12, stage));
    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));
    System.out.println(String.valueOf(String.valueOf(String.valueOf("Before calling proc_GetSAMDetailInfo --> ").concat(String.valueOf(oHrmRecScrutAppMntQVO.getHeaderPrimaryKey()))).concat(String.valueOf(" "))).concat(String.valueOf(stage)));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMRECSCRUTAPPMNT.proc_GetSAMDetailInfo(?,?,?,?,?,?,?,?)");
    System.out.println("proc_GetSAMDetailInfo executed successfully");
    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)
        arylstHrmRecScrutAppMtnDetail = new ArrayList(); 
      count++;
      oRow = oIt.next();
      HrmRecScrutAppMtnDtlBean oHrmRecScrutAppMtnDtlBean = new HrmRecScrutAppMtnDtlBean();
      oHrmRecScrutAppMtnDtlBean.setDetailId(oRow.get("DTL_ID").getString());
      oHrmRecScrutAppMtnDtlBean.setTxtApplicantName(oRow.get("NAME").getString());
      oHrmRecScrutAppMtnDtlBean.setTxtApplicantNo(oRow.get("APPL_NO").getString());
      if (oHrmRecScrutAppMntQVO.getStage().equalsIgnoreCase("I")) {
        oHrmRecScrutAppMtnDtlBean.setTxtEligibility("N");
      } else if (oHrmRecScrutAppMntQVO.getStage().equalsIgnoreCase("W")) {
        oHrmRecScrutAppMtnDtlBean.setTxtEligibility("N");
      } else if (oHrmRecScrutAppMntQVO.getStage().equalsIgnoreCase("G")) {
        oHrmRecScrutAppMtnDtlBean.setTxtEligibility("N");
      } else {
        oHrmRecScrutAppMtnDtlBean.setTxtEligibility("N");
      } 
      oHrmRecScrutAppMtnDtlBean.setTxtWrittemMarksA(oRow.get("WRTN_TEST_SCORE").getString());
      oHrmRecScrutAppMtnDtlBean.setTxtWrittemMarksB(oRow.get("gd_score").getString());
      oHrmRecScrutAppMtnDtlBean.setTxtInterviewMarks(oRow.get("INTERVIEW_SCORE").getString());
      oHrmRecScrutAppMtnDtlBean.setTxtExpYrs(oRow.get("work_experience").getString());
      oHrmRecScrutAppMtnDtlBean.setTxtTestCenter(oRow.get("TEST_CODE").getString());
      arylstHrmRecScrutAppMtnDetail.add(oHrmRecScrutAppMtnDtlBean);
    } 
    return arylstHrmRecScrutAppMtnDetail;
  }
  
  private void saveScrutAppMtnDetails(ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bUpdate = false;
    boolean bDelete = false;
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = null;
    DBUtilitiesBean oBean1 = null;
    HrmRecScrutAppMtnHdrBean oHrmRecScrutAppMtnHdrBean = new HrmRecScrutAppMtnHdrBean();
    String stageFlag = oHrmRecScrutAppMtnHdrBean.getStage();
    String desigId = oHrmRecScrutAppMtnHdrBean.getAdForDesignationId();
    Iterator itrBean = arylstDetailBeanArray.iterator();
    while (itrBean.hasNext()) {
      HrmRecScrutAppMtnDtlBean oHrmRecScrutAppMtnDtlBean = itrBean.next();
      if (oHrmRecScrutAppMtnDtlBean.getStatus().equals("N")) {
        if (!bInsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMRECSCRUTAPPMNT.proc_UpsertScrutAppMtnDtl(?,?,?,?,?,?,?)");
          bInsert = true;
        } 
        arylstParam = new ArrayList();
        arylstParam.add(new DBObject(1, 1, 12, "I"));
        arylstParam.add(new DBObject(2, 1, 12, "1"));
        arylstParam.add(new DBObject(3, 1, 12, desigId));
        arylstParam.add(new DBObject(4, 1, 12, this.sStageFlag));
        arylstParam.add(new DBObject(5, 1, 12, oHrmRecScrutAppMtnDtlBean.getTxtEligibility()));
        arylstParam.add(new DBObject(6, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParam.add(new DBObject(7, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParam);
        continue;
      } 
      if (oHrmRecScrutAppMtnDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMRECSCRUTAPPMNT.proc_UpsertScrutAppMtnDtl(?,?,?,?,?,?)");
          bUpdate = true;
        } 
        System.out.println(String.valueOf("Stage Flag is set to : ").concat(String.valueOf(this.sStageFlag)));
        arylstParam = new ArrayList();
        arylstParam.add(new DBObject(1, 1, 12, "U"));
        arylstParam.add(new DBObject(2, 1, 12, oHrmRecScrutAppMtnDtlBean.getDetailId()));
        arylstParam.add(new DBObject(3, 1, 12, this.sStageFlag));
        arylstParam.add(new DBObject(4, 1, 12, oHrmRecScrutAppMtnDtlBean.getTxtEligibility()));
        arylstParam.add(new DBObject(5, 1, 12, "200"));
        arylstParam.add(new DBObject(6, 1, 12, "200"));
        oBean.addToBatch(arylstParam);
      } 
    } 
    if (bInsert)
      oBean.executeBatch(); 
    if (bUpdate)
      oBean.executeBatch(); 
  }
  
  public void initializeBOImpl() {
    this.headerTable = "hrm_advt_dtl";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {}
  
  public void saveDetailImpl(BaseHeaderBean oBean, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    saveScrutAppMtnDetails(oDetailBeanArray);
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("Update ille!");
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl ille!";
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String param1) throws EnrgiseSystemException {
    return "saveHeaderImpl toBeDone";
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public void additionalBusinessValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
}
