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.EnrgiseUtil;
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.HrmTrngEmpAttDtlBean;
import wenrgise.hrms.bean.HrmTrngEmpAttHdrBean;
import wenrgise.hrms.vo.HrmTrngEmpAttQVO;

public class HrmTrngEmpAttBO extends HrmBaseBO {
  public HrmTrngEmpAttBO() {}
  
  public HrmTrngEmpAttBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmTrngEmpAttHdrMetaInfo(HrmTrngEmpAttQVO oHrmTrngEmpAttQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    String sId = oHrmTrngEmpAttQueryVO.getCourseNameId();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sId));
    oParameters.add(new DBObject(2, 2, -5));
    oParameters.add(new DBObject(3, 2, 93));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMEMPTRNGATT.proc_HrEmpTrngAttHdrCount(?,?,?,?,?,?)");
    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 getHrmTrngEmpAttHeaderInfo(HrmTrngEmpAttQVO oHrmTrngEmpAttQueryVO, long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    ArrayList oHeaderList = null;
    if (oHrmTrngEmpAttQueryVO == null)
      oHrmTrngEmpAttQueryVO = new HrmTrngEmpAttQVO(); 
    String sId = oHrmTrngEmpAttQueryVO.getCourseNameId();
    String sPK = oHrmTrngEmpAttQueryVO.getHeaderPrimaryKey();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, -5, new Long(lStartPosition)));
    oParameters.add(new DBObject(2, 1, -5, new Long(lLastPosition)));
    oParameters.add(new DBObject(3, 1, 12, sId));
    oParameters.add(new DBObject(4, 1, 12, sPK));
    oParameters.add(new DBObject(5, 2, -10));
    oParameters.add(new DBObject(6, 2, 12));
    oParameters.add(new DBObject(7, 2, 12));
    oParameters.add(new DBObject(8, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMEMPTRNGATT.proc_HrEmpTrngAttHdr(?,?,?,?,?,?,?,?)");
    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();
      HrmTrngEmpAttHdrBean oHrmTrngEmpAttHdrBean = new HrmTrngEmpAttHdrBean();
      oHrmTrngEmpAttHdrBean.setHeaderPrimaryKey(oRow.get("ID").getString());
      oHrmTrngEmpAttHdrBean.setCourseName(oRow.get("CRS_NAME").getString());
      oHrmTrngEmpAttHdrBean.setInstitutionName(oRow.get("INST_NAME").getString());
      oHrmTrngEmpAttHdrBean.setStartDate(EnrgiseUtil.convertToString(oRow.get("START_DATE").getDate()));
      oHrmTrngEmpAttHdrBean.setEndDate(EnrgiseUtil.convertToString(oRow.get("END_DATE").getDate()));
      oHeaderList.add(oHrmTrngEmpAttHdrBean);
    } 
    return oHeaderList;
  }
  
  public RecordMetaInfo getHrmTrngEmpAttDetailMetaInfo(String lPrimaryKey) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, lPrimaryKey));
    oParameters.add(new DBObject(2, 2, -5));
    oParameters.add(new DBObject(3, 2, 93));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMEMPTRNGATT.proc_HrEmpTrngAttDtlCount(?,?,?,?,?,?)");
    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 getHrmTRngEmpAttDetail(String lPrimaryKey, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList oHrmTrngEmpAttDetail = null;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, -5, new Long(lDetailFirstPosition)));
    oParameters.add(new DBObject(2, 1, -5, new Long(lDetailLastPosition)));
    oParameters.add(new DBObject(3, 1, 12, lPrimaryKey));
    oParameters.add(new DBObject(4, 2, -10));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 12));
    oParameters.add(new DBObject(7, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMEMPTRNGATT.proc_HrEmpTrngAttDtlInfo(?,?,?,?,?,?,?)");
    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)
        oHrmTrngEmpAttDetail = new ArrayList(); 
      count++;
      oRow = oIt.next();
      HrmTrngEmpAttDtlBean oHrmTrngEmpAttDtlBean = new HrmTrngEmpAttDtlBean();
      if (oRow.get("status_flag").getString().equalsIgnoreCase("C")) {
        oHrmTrngEmpAttDtlBean.setDetailId(oRow.get("ID").getString());
        oHrmTrngEmpAttDtlBean.setTxtTrainingDate(EnrgiseUtil.convertToString(oRow.get("TRAIN_DATE").getDate()));
        oHrmTrngEmpAttDtlBean.setDisabtxtTrainingDate("true");
        oHrmTrngEmpAttDtlBean.setTxtEmployeeNo(oRow.get("EMP_NO").getString());
        oHrmTrngEmpAttDtlBean.setDisabtxtEmployeeNo("true");
        oHrmTrngEmpAttDtlBean.setTxtEmployeeName(oRow.get("EMPNAME").getString());
        oHrmTrngEmpAttDtlBean.setDisabtxtEmployeeName("true");
        oHrmTrngEmpAttDtlBean.setDisabtxtAttendance("true");
        oHrmTrngEmpAttDtlBean.setTxtAttendance(oRow.get("PRESENT_FLAG").getString());
        oHrmTrngEmpAttDtlBean.setDisabtxtReasonForAbsence("true");
        oHrmTrngEmpAttDtlBean.setTxtReasonForAbsence(oRow.get("NONPART_REASON").getString());
        oHrmTrngEmpAttDetail.add(oHrmTrngEmpAttDtlBean);
        continue;
      } 
      oHrmTrngEmpAttDtlBean.setDetailId(oRow.get("ID").getString());
      oHrmTrngEmpAttDtlBean.setTxtTrainingDate(EnrgiseUtil.convertToString(oRow.get("TRAIN_DATE").getDate()));
      oHrmTrngEmpAttDtlBean.setDisabtxtTrainingDate("true");
      oHrmTrngEmpAttDtlBean.setTxtEmployeeNo(oRow.get("EMP_NO").getString());
      oHrmTrngEmpAttDtlBean.setDisabtxtEmployeeNo("true");
      oHrmTrngEmpAttDtlBean.setTxtEmployeeName(oRow.get("EMPNAME").getString());
      oHrmTrngEmpAttDtlBean.setDisabtxtEmployeeName("true");
      oHrmTrngEmpAttDtlBean.setTxtAttendance(oRow.get("PRESENT_FLAG").getString());
      if (oHrmTrngEmpAttDtlBean.getTxtAttendance().equals("Y")) {
        oHrmTrngEmpAttDtlBean.setDisabtxtReasonForAbsence("true");
      } else {
        oHrmTrngEmpAttDtlBean.setDisabtxtReasonForAbsence("false");
      } 
      oHrmTrngEmpAttDtlBean.setTxtReasonForAbsence(oRow.get("NONPART_REASON").getString());
      oHrmTrngEmpAttDetail.add(oHrmTrngEmpAttDtlBean);
    } 
    return oHrmTrngEmpAttDetail;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_EMP_TRNG_HDR";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bUpdate = false;
    boolean bDelete = false;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = null;
    DBUtilitiesBean oBean1 = null;
    Iterator oIt = oDetailBeanArray.iterator();
    while (oIt.hasNext()) {
      HrmTrngEmpAttDtlBean oDtlBean = oIt.next();
      if (oDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMEMPTRNGATT.proc_UpsertTrngAttDtl(?,?,?,?,?)");
          bUpdate = true;
        } 
        oParameters = new ArrayList();
        oParameters.add(new DBObject(1, 1, 12, oDtlBean.getDetailId()));
        oParameters.add(new DBObject(2, 1, 12, oDtlBean.getTxtAttendance()));
        oParameters.add(new DBObject(3, 1, 12, oDtlBean.getTxtReasonForAbsence()));
        oParameters.add(new DBObject(4, 1, 12, "100"));
        oParameters.add(new DBObject(5, 1, 12, "100"));
        oBean.addToBatch(oParameters);
      } 
    } 
    if (bUpdate)
      oBean.executeBatch(); 
  }
  
  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 void additionalTimestampValidationImpl(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 additionalBusinessValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public LovVO getHrmTrngCourseLOVData(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("Course Id");
    arylstHeaderList.add("hrm.HrmTrnInCoDtl.course");
    arylstHeaderList.add("hrm.HrmTrnInCoDtl.courseName");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    String countryId = null;
    String calId = null;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null) {
      sQuerySearch1 = oLovQueryVO.getSearchField1();
      System.out.println(sQuerySearch1);
    } 
    if (oLovQueryVO.getSearchField2() != null) {
      sQuerySearch2 = oLovQueryVO.getSearchField2();
      System.out.println(sQuerySearch2);
    } 
    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, "HRMTRNGLOV.proc_GetTrgAttCourseLOV(?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstList.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        arylstList = new ArrayList(); 
      count++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("ID").getString());
      oLOVBean.setDetailField2(oRow.get("COURSE").getString());
      oLOVBean.setDetailField3(oRow.get("NAME").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
}
