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.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.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.HrmTrngFacBean;
import wenrgise.hrms.vo.HrmTrngFacQVO;

public class HrmTrngFacBO extends HrmBaseBO {
  public HrmTrngFacBO() {}
  
  public HrmTrngFacBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmFacHdrMetaInfo(HrmTrngFacQVO oHrmTrngFacQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, oHrmTrngFacQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(2, 1, 12, oHrmTrngFacQVO.getFacultyType()));
    oParameters.add(new DBObject(3, 1, 12, oHrmTrngFacQVO.getEmployeeNo()));
    oParameters.add(new DBObject(4, 1, 12, oHrmTrngFacQVO.getBaseInstitution()));
    oParameters.add(new DBObject(5, 2, -5));
    oParameters.add(new DBObject(6, 2, 93));
    oParameters.add(new DBObject(7, 2, 12));
    oParameters.add(new DBObject(8, 2, 12));
    oParameters.add(new DBObject(9, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGFACMST.proc_HrFacCount(?,?,?,?,?,?,?,?,?)");
    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 getHrmFacHeaderInfo(HrmTrngFacQVO oHrmTrngFacQVO, 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 (oHrmTrngFacQVO == null)
      oHrmTrngFacQVO = new HrmTrngFacQVO(); 
    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, oHrmTrngFacQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(4, 1, 12, oHrmTrngFacQVO.getFacultyType()));
    oParameters.add(new DBObject(5, 1, 12, oHrmTrngFacQVO.getEmployeeNo()));
    oParameters.add(new DBObject(6, 1, 12, oHrmTrngFacQVO.getBaseInstitution()));
    oParameters.add(new DBObject(7, 2, -10));
    oParameters.add(new DBObject(8, 2, 12));
    oParameters.add(new DBObject(9, 2, 12));
    oParameters.add(new DBObject(10, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGFACMST.proc_GetHrFac(?,?,?,?,?,?,?,?,?,?)");
    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();
      HrmTrngFacBean oFacultyHeader = new HrmTrngFacBean();
      oFacultyHeader.setHeaderPrimaryKey(oRow.get("ID").getString());
      oFacultyHeader.setFacultyType(oRow.get("FACULTY_TYPE").getString());
      oFacultyHeader.setEmployeeNo(oRow.get("EMP_NO").getString());
      String empId = oRow.get("EMPID").getString();
      if (empId != null && !empId.equals("")) {
        oFacultyHeader.setTrgFacMstEmpId(empId);
      } else {
        oFacultyHeader.setTrgFacMstEmpId("");
      } 
      oFacultyHeader.setBaseInstitution(oRow.get("INST_CODE").getString());
      oFacultyHeader.setInstitutionName(oRow.get("INST_NAME").getString());
      oFacultyHeader.setTrgFacMstInstCode(oRow.get("INSTID").getString());
      oFacultyHeader.setFirstName(oRow.get("FACULTY_FIRST_NAME").getString());
      oFacultyHeader.setMiddleName(oRow.get("FACULTY_MIDDLE_NAME").getString());
      oFacultyHeader.setLastName(oRow.get("FACULTY_LAST_NAME").getString());
      oFacultyHeader.setExperience(oRow.get("EXPERIENCE").getString());
      oHeaderList.add(oFacultyHeader);
    } 
    return oHeaderList;
  }
  
  public LovVO getHrmTrgFacMstEmpNoLOVNmode(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("Employee Id");
    oHeaderList.add("hrm.HrmFacMst.employeeNo");
    oHeaderList.add("hrm.HrmFacMst.firstName");
    oHeaderList.add("hrm.HrmFacMst.middleName");
    oHeaderList.add("hrm.HrmFacMst.lastName");
    oLovVO.setHeaderList(oHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = 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(); 
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    oParameters.add(new DBObject(2, 1, 12, sQuerySearch2));
    oParameters.add(new DBObject(3, 2, -10));
    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, "HRMTRNGFACMST.proc_GetTrgFacMstEmpLOVNmode(?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = oList.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        oList = new ArrayList(); 
      count++;
      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());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public LovVO getHrmTrgFacMstBaseInstLOVQmode(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("Institute Id");
    oHeaderList.add("hrm.HrmFacMst.baseInstitution");
    oHeaderList.add("hrm.HrmFacMst.instituteName");
    oLovVO.setHeaderList(oHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    String facType = null;
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    if (oLovQueryVO.getProperty("facultyType") != null)
      facType = oLovQueryVO.getProperty("facultyType"); 
    if (oLovQueryVO.getSearchField2() != null)
      sQuerySearch2 = oLovQueryVO.getSearchField2(); 
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    oParameters.add(new DBObject(2, 1, 12, sQuerySearch2));
    oParameters.add(new DBObject(3, 1, 12, facType));
    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, "HRMTRNGFACMST.proc_GetTrgBaseInstLOVQmode(?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = oList.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        oList = new ArrayList(); 
      count++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("ID").getString());
      oLOVBean.setDetailField2(oRow.get("INST_CODE").getString());
      oLOVBean.setDetailField3(oRow.get("INST_NAME").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  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 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((HrmTrngFacBean)oBaseHeaderBean);  
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    HrmTrngFacBean oHrmTrngFacBean = (HrmTrngFacBean)oBaseHeaderBean;
    ArrayList oList = new ArrayList();
    if (bHeaderDataChanged)
      if (sScreenMode.equalsIgnoreCase("D"))
        if (sScreenMode.equalsIgnoreCase("D")) {
          HrmCommonBO oCommonBO = new HrmCommonBO();
          String sRefId = oCommonBO.getRefList("HRM_TRNG_FAC_DTL", "HRM_FACM_ID ", oHrmTrngFacBean.getHeaderPrimaryKey(), "ID");
          if (EnrgiseUtil.checkString(sRefId))
            if (oList.isEmpty())
              oList.add(new EnrgiseApplicationException("wenrgise.common.deletion"));  
          oList.add(new EnrgiseApplicationException("wenrgise.common.deletion"));
        }   
    reportError(oList);
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_TRNG_FAC_MST";
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    System.out.println(this.oUserInfo);
    HrmTrngFacBean oHrmTrngFacBean = (HrmTrngFacBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "i"));
      oParameters.add(new DBObject(2, 1, 12, oHrmTrngFacBean.getFacultyType()));
      oParameters.add(new DBObject(3, 1, 12, oHrmTrngFacBean.getTrgFacMstEmpId()));
      oParameters.add(new DBObject(4, 1, 12, oHrmTrngFacBean.getTrgFacMstInstCode()));
      oParameters.add(new DBObject(5, 1, 12, oHrmTrngFacBean.getFirstName()));
      oParameters.add(new DBObject(6, 1, 12, oHrmTrngFacBean.getMiddleName()));
      oParameters.add(new DBObject(7, 1, 12, oHrmTrngFacBean.getLastName()));
      oParameters.add(new DBObject(8, 1, 12, oHrmTrngFacBean.getExperience()));
      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, "HRMTRNGFACMST.proc_UpsertHrFacMstHdr(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
    } else if (ScreenMode.equalsIgnoreCase("U")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "u"));
      oParameters.add(new DBObject(2, 1, 12, oHrmTrngFacBean.getFacultyType()));
      oParameters.add(new DBObject(3, 1, 12, oHrmTrngFacBean.getTrgFacMstEmpId()));
      oParameters.add(new DBObject(4, 1, 12, oHrmTrngFacBean.getTrgFacMstInstCode()));
      oParameters.add(new DBObject(5, 1, 12, oHrmTrngFacBean.getFirstName()));
      oParameters.add(new DBObject(6, 1, 12, oHrmTrngFacBean.getMiddleName()));
      oParameters.add(new DBObject(7, 1, 12, oHrmTrngFacBean.getLastName()));
      oParameters.add(new DBObject(8, 1, 12, oHrmTrngFacBean.getExperience()));
      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, oHrmTrngFacBean.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, "HRMTRNGFACMST.proc_UpsertHrFacMstHdr(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
    } else if (ScreenMode.equalsIgnoreCase("D")) {
      oParameters.add(new DBObject(1, 1, 12, oHrmTrngFacBean.getHeaderPrimaryKey()));
      oParameters.add(new DBObject(2, 2, 12));
      oParameters.add(new DBObject(3, 2, 12));
      oParameters.add(new DBObject(4, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMTRNGFACMST.proc_DeleteHrFacMstHdr(?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = null;
    } 
    return returnString;
  }
  
  private void checkMandatoryHeader(HrmTrngFacBean oHrmTrngFacBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oErrorList = new ArrayList();
    ArrayList oParams = new ArrayList();
    if (!EnrgiseUtil.checkString(oHrmTrngFacBean.getFacultyType())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmFacMst.facultyType");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmTrngFacBean.getEmployeeNo()))
      if (oHrmTrngFacBean.getFacultyType().equals("I")) {
        MessageKey oMessageKey = new MessageKey("hrm.HrmFacMst.employeeNo");
        oParams = new ArrayList();
        oParams.add(oMessageKey);
        oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
      }  
    if (!EnrgiseUtil.checkString(oHrmTrngFacBean.getBaseInstitution()))
      if (oHrmTrngFacBean.getFacultyType().equals("E")) {
        MessageKey oMessageKey = new MessageKey("hrm.HrmFacMst.baseInstitution");
        oParams = new ArrayList();
        oParams.add(oMessageKey);
        oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
      }  
    reportError(oErrorList);
  }
}
