package wenrgise.hrms.ejb.business;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.bean.LOVBean;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseMessageKeyException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.MessageKey;
import wenrgise.common.utility.RecordMetaInfo;
import wenrgise.common.utility.UserInfo;
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.HrmPrmQualCriteriaDtlBean;
import wenrgise.hrms.bean.HrmPrmQualCriteriaHdrBean;
import wenrgise.hrms.vo.HrmPrmQualCriteriaQVO;

public class HrmPrmQualCriteriaBO extends HrmBaseBO {
  public HrmPrmQualCriteriaBO() {}
  
  public HrmPrmQualCriteriaBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getPrmQualCriteriaDetailMetaInfo(HrmPrmQualCriteriaQVO oHrmPrmQualCriteriaQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, oHrmPrmQualCriteriaQVO.getHeaderPrimaryKey()));
    arylstParam.add(new DBObject(2, 1, 12, oHrmPrmQualCriteriaQVO.getDesignationId()));
    arylstParam.add(new DBObject(3, 2, -5));
    arylstParam.add(new DBObject(4, 2, 93));
    arylstParam.add(new DBObject(5, 2, 12));
    arylstParam.add(new DBObject(6, 2, 12));
    arylstParam.add(new DBObject(7, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMPRMQUALCRITERIA.proc_QualCriteriaDtlCount(?,?,?,?,?,?,?)");
    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 getPrmQualCriteriaDetail(HrmPrmQualCriteriaQVO oHrmPrmQualCriteriaQVO, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    QueryRow oRow = null;
    ArrayList oList = null;
    Iterator oIt = null;
    ArrayList oLocVavancyDetail = new ArrayList();
    ArrayList oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, new Long(lDetailFirstPosition)));
    oParameters.add(new DBObject(2, 1, -5, new Long(lDetailLastPosition)));
    oParameters.add(new DBObject(3, 1, 12, oHrmPrmQualCriteriaQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(4, 1, 12, oHrmPrmQualCriteriaQVO.getDesignationId()));
    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, "HRMPRMQUALCRITERIA.proc_QualCriteriaDetail(?,?,?,?,?,?,?,?)");
    oList = (ArrayList)((DBObject)oOutArray.get(0)).getObject();
    oIt = oList.iterator();
    while (oIt.hasNext()) {
      oRow = oIt.next();
      HrmPrmQualCriteriaDtlBean oHrmPrmQualCriteriaDtlBean = new HrmPrmQualCriteriaDtlBean();
      oHrmPrmQualCriteriaDtlBean.setDetailId(oRow.get("ID").getString());
      oHrmPrmQualCriteriaDtlBean.setHdnQualificationId(oRow.get("QUAL_MST_ID").getString());
      oHrmPrmQualCriteriaDtlBean.setTxtQualCode(oRow.get("code").getString());
      oHrmPrmQualCriteriaDtlBean.setTxtQualDesc(oRow.get("name").getString());
      oHrmPrmQualCriteriaDtlBean.setTxtMinPercentage(oRow.get("MIN_PERC").getString());
      oHrmPrmQualCriteriaDtlBean.setStatus("Q");
      oLocVavancyDetail.add(oHrmPrmQualCriteriaDtlBean);
    } 
    return oLocVavancyDetail;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_PROM_QUAL_CRIT";
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("Update ille!");
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl ille!";
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String param1) throws EnrgiseSystemException {
    return "saveHeaderImpl toBeDone";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {}
  
  public void saveDetailImpl(BaseHeaderBean oBaseHeaderBean, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseApplicationException, EnrgiseSystemException {
    if (sScreenName.equalsIgnoreCase("HrmPrmQualCriteria"))
      saveQualCriteriaDetails(oBaseHeaderBean, oDetailBeanArray); 
  }
  
  private void saveQualCriteriaDetails(BaseHeaderBean oBaseHeaderBean, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    boolean bUpsert = false;
    boolean bDelete = false;
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = null;
    DBUtilitiesBean oBean1 = null;
    HrmPrmQualCriteriaHdrBean oHrmPrmQualCriteriaHdrBean = (HrmPrmQualCriteriaHdrBean)oBaseHeaderBean;
    Iterator itrBean = arylstDetailBeanArray.iterator();
    while (itrBean.hasNext()) {
      HrmPrmQualCriteriaDtlBean oHrmPrmQualCriteriaDtlBean = itrBean.next();
      if (oHrmPrmQualCriteriaDtlBean.getStatus().equals("N")) {
        if (!bUpsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMPRMQUALCRITERIA.proc_UpsertQualCriteriaDtl(?,?,?,?,?,?,?,?)");
          bUpsert = true;
        } 
        arylstParam = new ArrayList();
        arylstParam.add(new DBObject(1, 1, 12, "N"));
        arylstParam.add(new DBObject(2, 1, 12, null));
        arylstParam.add(new DBObject(3, 1, 12, oHrmPrmQualCriteriaDtlBean.getHdnQualificationId()));
        arylstParam.add(new DBObject(4, 1, 12, oHrmPrmQualCriteriaHdrBean.getHeaderPrimaryKey()));
        arylstParam.add(new DBObject(5, 1, 12, oHrmPrmQualCriteriaHdrBean.getDesignationId()));
        arylstParam.add(new DBObject(6, 1, 12, oHrmPrmQualCriteriaDtlBean.getTxtMinPercentage()));
        arylstParam.add(new DBObject(7, 1, 12, this.oUserInfo.getUserId()));
        arylstParam.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParam);
        continue;
      } 
      if (oHrmPrmQualCriteriaDtlBean.getStatus().equals("U")) {
        if (!bUpsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMPRMQUALCRITERIA.proc_UpsertQualCriteriaDtl(?,?,?,?,?,?,?,?)");
          bUpsert = true;
        } 
        arylstParam = new ArrayList();
        arylstParam.add(new DBObject(1, 1, 12, "U"));
        arylstParam.add(new DBObject(2, 1, 12, oHrmPrmQualCriteriaDtlBean.getDetailId()));
        arylstParam.add(new DBObject(3, 1, 12, oHrmPrmQualCriteriaDtlBean.getHdnQualificationId()));
        arylstParam.add(new DBObject(4, 1, 12, oHrmPrmQualCriteriaHdrBean.getHeaderPrimaryKey()));
        arylstParam.add(new DBObject(5, 1, 12, oHrmPrmQualCriteriaHdrBean.getDesignationId()));
        arylstParam.add(new DBObject(6, 1, 12, oHrmPrmQualCriteriaDtlBean.getTxtMinPercentage()));
        arylstParam.add(new DBObject(7, 1, 12, this.oUserInfo.getUserId()));
        arylstParam.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParam);
        continue;
      } 
      if (oHrmPrmQualCriteriaDtlBean.getStatus().equals("D")) {
        if (!bDelete) {
          oBean1 = new DBUtilitiesBean();
          oBean1.createBatch("HRMPRMQUALCRITERIA.proc_DeletePrmQualCriteriaDtl(?)");
          bDelete = true;
        } 
        arylstParam = new ArrayList();
        arylstParam.add(new DBObject(1, 1, 12, oHrmPrmQualCriteriaDtlBean.getDetailId()));
        oBean1.addToBatch(arylstParam);
      } 
    } 
    if (bUpsert)
      oBean.executeBatch(); 
    if (bDelete)
      oBean1.executeBatch(); 
  }
  
  public LovVO getHrmPrmQualPositionLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("hrm.HrmAdvertisementAttribute.id");
    arylstHeaderList.add("hrm.HrmQualification.positionCode");
    arylstHeaderList.add("hrm.HrmQualification.desc");
    arylstHeaderList.add("hrm.HrmQualification.designation");
    arylstHeaderList.add("hrm.HrmQualification.scale");
    oLovVO.setHeaderList(arylstHeaderList);
    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 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, "HRMPRMQUALCRITERIA.proc_GetPositionLOVdata(?,?,?,?,?,?)");
    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("CODE").getString());
      oLOVBean.setDetailField3(oRow.get("Description").getString());
      oLOVBean.setDetailField4(oRow.get("Designation").getString());
      oLOVBean.setDetailField5(oRow.get("SCALE").getString());
      arylstParam.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstParam);
    return oLovVO;
  }
  
  public LovVO getHrmPrmQualificationLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("hrm.HrmAdvertisementAttribute.id");
    arylstHeaderList.add("hrm.HrmQualification.txtQualificationCode");
    arylstHeaderList.add("hrm.HrmQualification.txtQualificationDesc");
    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, "HRMPRMQUALCRITERIA.proc_GetAQualificationLOVdata(?,?,?,?,?,?)");
    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("CODE").getString());
      oLOVBean.setDetailField3(oRow.get("NAME").getString());
      arylstParam.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstParam);
    return oLovVO;
  }
  
  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 (bDetailDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D")) {
        Iterator oIt1 = arylstDetailBeanArray.iterator();
        int rowCount = 1;
        while (oIt1.hasNext()) {
          HrmPrmQualCriteriaDtlBean oHrmPrmQualCriteriaDtlBean = oIt1.next();
          if (!oHrmPrmQualCriteriaDtlBean.getStatus().equalsIgnoreCase("D"))
            checkMandatoryQualificationDtls(oHrmPrmQualCriteriaDtlBean, rowCount, oErrorList); 
          rowCount++;
        } 
      }  
    reportError(oErrorList);
  }
  
  private void checkMandatoryQualificationDtls(HrmPrmQualCriteriaDtlBean oHrmPrmQualCriteriaDtlBean, int rowCount, ArrayList oErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {
    if (!EnrgiseUtil.checkString(oHrmPrmQualCriteriaDtlBean.getTxtQualCode())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmQualification.txtQualificationCode");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(new Integer(rowCount));
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmPrmQualCriteriaDtlBean.getTxtMinPercentage())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmQualification.txtMinPercentage");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(new Integer(rowCount));
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
  }
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oErrorList = new ArrayList();
    if (bDetailDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D")) {
        Iterator oIt1 = oDetailBeanArray.iterator();
        EnrgiseUtil.checkDuplicate(oDetailBeanArray, "txtQualCode", "hrm.HrmQualification.txtQualificationCode", oErrorList, true);
        checkUniqueQualificationDtl(oDetailBeanArray, oErrorList);
        checkNumericQualificationDtl(oDetailBeanArray, oErrorList);
      }  
    reportError(oErrorList);
  }
  
  private void checkUniqueQualificationDtl(ArrayList oDetailBeanArray, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {}
  
  private void checkNumericQualificationDtl(ArrayList oDetailBeanArray, ArrayList oErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {
    int count = 1;
    Iterator oIt1 = oDetailBeanArray.iterator();
    while (oIt1.hasNext()) {
      HrmPrmQualCriteriaDtlBean oHrmPrmQualCriteriaDtlBean = oIt1.next();
      if (!oHrmPrmQualCriteriaDtlBean.getTxtMinPercentage().equalsIgnoreCase("")) {
        MessageKey oMessageKey = new MessageKey("hrm.HrmQualification.txtMinPercentage");
        ArrayList oParam = new ArrayList();
        oParam.add(oMessageKey);
        oParam.add(new Integer(count));
        if (!EnrgiseUtil.checkNumber(oHrmPrmQualCriteriaDtlBean.getTxtMinPercentage(), 2, 2, "P"))
          oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.invalidNumber", oParam)); 
      } 
      count++;
    } 
    reportError(oErrorList);
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
}
