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.ejb.business.HrmCommonBO;
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.BaseDetailVO;
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.HrmSepClrLstDtlBean;

public class HrmSepClrLstBO extends HrmBaseBO {
  public HrmSepClrLstBO() {}
  
  public HrmSepClrLstBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmSepClrLstDtlMetaInfo() throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 2, -5));
    arylstParam.add(new DBObject(2, 2, 93));
    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, "HRMSEPCLRLIST.proc_HrmSepClrLstDtlCount(?,?,?,?,?)");
    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 getHrmSepClrLstDetailInfo(long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList arylstHrmSepClrLstDetail = null;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, -5, new Long(lDetailFirstPosition)));
    arylstParam.add(new DBObject(2, 1, -5, new Long(lDetailLastPosition)));
    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, "HRMSEPCLRLIST.proc_GetSepClrLstDtl(?,?,?,?,?,?)");
    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)
        arylstHrmSepClrLstDetail = new ArrayList(); 
      count++;
      oRow = itrBean.next();
      HrmSepClrLstDtlBean oHrmSepClrLstDtlBean = new HrmSepClrLstDtlBean();
      oHrmSepClrLstDtlBean.setDetailId(oRow.get("ID").getString());
      oHrmSepClrLstDtlBean.setDescription(oRow.get("CLRNC_DESCRIPTION").getString());
      oHrmSepClrLstDtlBean.setType(oRow.get("EXT_INT_FLAG").getString());
      arylstHrmSepClrLstDetail.add(oHrmSepClrLstDtlBean);
    } 
    return arylstHrmSepClrLstDetail;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_EMP_PERS";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    if (sScreenName.equalsIgnoreCase("HrmSepClrLst"))
      saveHrmSepClrLstDetail(arylstDetailBeanArray); 
  }
  
  private void saveHrmSepClrLstDetail(ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bUpdate = false;
    boolean bDelete = false;
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = null;
    DBUtilitiesBean oBean1 = null;
    Iterator itrBean = arylstDetailBeanArray.iterator();
    while (itrBean.hasNext()) {
      HrmSepClrLstDtlBean oHrmSepClrLstDtlBean = itrBean.next();
      if (oHrmSepClrLstDtlBean.getStatus().equals("N")) {
        if (!bInsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMSEPCLRLIST.proc_UpsertHrmSepClrLstDetail(?,?,?,?,?,?)");
          bInsert = true;
        } 
        arylstParam = new ArrayList();
        arylstParam.add(new DBObject(1, 1, 12, "I"));
        arylstParam.add(new DBObject(2, 1, 12, null));
        arylstParam.add(new DBObject(3, 1, 12, oHrmSepClrLstDtlBean.getDescription()));
        arylstParam.add(new DBObject(4, 1, 12, oHrmSepClrLstDtlBean.getType()));
        arylstParam.add(new DBObject(5, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParam.add(new DBObject(6, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParam);
        continue;
      } 
      if (oHrmSepClrLstDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMSEPCLRLIST.proc_UpsertHrmSepClrLstDetail(?,?,?,?,?,?)");
          bUpdate = true;
        } 
        arylstParam = new ArrayList();
        arylstParam.add(new DBObject(1, 1, 12, "U"));
        arylstParam.add(new DBObject(2, 1, 12, oHrmSepClrLstDtlBean.getDetailId()));
        arylstParam.add(new DBObject(3, 1, 12, oHrmSepClrLstDtlBean.getDescription()));
        arylstParam.add(new DBObject(4, 1, 12, oHrmSepClrLstDtlBean.getType()));
        arylstParam.add(new DBObject(5, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParam.add(new DBObject(6, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParam);
        continue;
      } 
      if (oHrmSepClrLstDtlBean.getStatus().equals("D")) {
        if (!bDelete) {
          oBean1 = new DBUtilitiesBean();
          oBean1.createBatch("HRMSEPCLRLIST.proc_DeleteHrmSepClrLstDetail(?)");
          bDelete = true;
        } 
        arylstParam = new ArrayList();
        arylstParam.add(new DBObject(1, 1, 12, oHrmSepClrLstDtlBean.getDetailId()));
        oBean1.addToBatch(arylstParam);
      } 
    } 
    if (bInsert)
      oBean.executeBatch(); 
    if (bUpdate)
      oBean.executeBatch(); 
    if (bDelete)
      oBean1.executeBatch(); 
  }
  
  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 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 (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmSepClrLst")) {
        Iterator itrBean1 = oDetailBeanArray.iterator();
        int iCount = 1;
        while (itrBean1.hasNext()) {
          HrmSepClrLstDtlBean oHrmSepClrLstDtlBean = itrBean1.next();
          if (!oHrmSepClrLstDtlBean.getStatus().equalsIgnoreCase("D")) {
            checkMandatory(oHrmSepClrLstDtlBean, iCount, oErrorList);
            EnrgiseUtil.checkDuplicate(oDetailBeanArray, "description", "hrm.HrmSepClrLst.description", oErrorList, true);
          } 
          iCount++;
        } 
      }  
    reportError(oErrorList);
  }
  
  private void checkMandatory(HrmSepClrLstDtlBean oHrmSepClrLstDtlBean, int iCount, ArrayList oErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {
    if (!EnrgiseUtil.checkString(oHrmSepClrLstDtlBean.getDescription())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmSepClrLst.description");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(new Integer(iCount));
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmSepClrLstDtlBean.getType())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmSepClrLst.externalInternalFlag");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(new Integer(iCount));
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(oErrorList);
  }
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oList = new ArrayList();
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmSepClrLst")) {
        Iterator oIt1 = oDetailBeanArray.iterator();
        while (oIt1.hasNext()) {
          HrmSepClrLstDtlBean oHrmSepClrLstDtlBean = oIt1.next();
          if (!oHrmSepClrLstDtlBean.getStatus().equalsIgnoreCase("D"))
            continue; 
          if (oHrmSepClrLstDtlBean.getStatus().equalsIgnoreCase("D")) {
            HrmCommonBO oCommonBO = new HrmCommonBO();
            String sQuery = String.valueOf(String.valueOf("Select i.ID as ID from hrm_sep_tmpl_dtl i,hrm_sep_clrnc_list m where m.id=i.CLRNC_LIST_ID and m.CLRNC_DESCRIPTION='").concat(String.valueOf(oHrmSepClrLstDtlBean.getDescription()))).concat(String.valueOf("'"));
            System.out.println(sQuery);
            DBUtilitiesBean oBean = new DBUtilitiesBean();
            ArrayList oList1 = oBean.executeQuery(sQuery);
            if (oList1.size() > 0)
              oList.add(new EnrgiseApplicationException("wenrgise.common.deletion")); 
          } 
        } 
      }  
    reportError(oList);
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
}
