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.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.HrmOrgRelationMstDtlBean;

public class HrmOrgRelationMstBO extends HrmBaseBO {
  public HrmOrgRelationMstBO() {}
  
  public HrmOrgRelationMstBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmRelationDtlMetaInfo(String lPrimaryKey) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 2, -5));
    arylstParameters.add(new DBObject(2, 2, 93));
    arylstParameters.add(new DBObject(3, 2, 12));
    arylstParameters.add(new DBObject(4, 2, 12));
    arylstParameters.add(new DBObject(5, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGRELATIONMST.proc_HrRelationDtlCount(?,?,?,?,?)");
    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 getHrmRelationDetailInfo(String lPrimaryKey, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList arylstHrmRelationDetail = null;
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, -5, new Long(lDetailFirstPosition)));
    arylstParameters.add(new DBObject(2, 1, -5, new Long(lDetailLastPosition)));
    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, "HRMORGRELATIONMST.proc_GetRelationDtl(?,?,?,?,?,?)");
    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)
        arylstHrmRelationDetail = new ArrayList(); 
      count++;
      oRow = itrBean.next();
      HrmOrgRelationMstDtlBean oRelationBean = new HrmOrgRelationMstDtlBean();
      oRelationBean.setDetailId(oRow.get("ID").getString());
      oRelationBean.setTxtRelationCode(oRow.get("CODE").getString());
      oRelationBean.setTxtRelationName(oRow.get("NAME").getString());
      oRelationBean.setTxtOccurence(oRow.get("OCCURANCE").getString());
      arylstHrmRelationDetail.add(oRelationBean);
    } 
    return arylstHrmRelationDetail;
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("Update ille!");
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String param1) throws EnrgiseSystemException {
    return "true";
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl ille!";
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_RELA_MST";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    if (sScreenName.equalsIgnoreCase("HrmOrgRelationMst"))
      saveHrmRelationDetail(arylstDetailBeanArray); 
  }
  
  private void saveHrmRelationDetail(ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bUpdate = false;
    boolean bDelete = false;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = null;
    DBUtilitiesBean oBean1 = null;
    Iterator itrBean = arylstDetailBeanArray.iterator();
    while (itrBean.hasNext()) {
      HrmOrgRelationMstDtlBean oHrmOrgRelationMstDtlBean = itrBean.next();
      if (oHrmOrgRelationMstDtlBean.getStatus().equals("N")) {
        if (!bInsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMORGRELATIONMST.proc_UpsertHrRelationMst(?,?,?,?,?,?,?)");
          bInsert = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, "I"));
        arylstParameters.add(new DBObject(2, 1, 12, null));
        arylstParameters.add(new DBObject(3, 1, 12, oHrmOrgRelationMstDtlBean.getTxtRelationCode()));
        arylstParameters.add(new DBObject(4, 1, 12, oHrmOrgRelationMstDtlBean.getTxtRelationName()));
        arylstParameters.add(new DBObject(5, 1, 12, oHrmOrgRelationMstDtlBean.getTxtOccurence()));
        arylstParameters.add(new DBObject(6, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParameters);
        continue;
      } 
      if (oHrmOrgRelationMstDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMORGRELATIONMST.proc_UpsertHrRelationMst(?,?,?,?,?,?,?)");
          bUpdate = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, "U"));
        arylstParameters.add(new DBObject(2, 1, 12, oHrmOrgRelationMstDtlBean.getDetailId()));
        arylstParameters.add(new DBObject(3, 1, 12, oHrmOrgRelationMstDtlBean.getTxtRelationCode()));
        arylstParameters.add(new DBObject(4, 1, 12, oHrmOrgRelationMstDtlBean.getTxtRelationName()));
        arylstParameters.add(new DBObject(5, 1, 12, oHrmOrgRelationMstDtlBean.getTxtOccurence()));
        arylstParameters.add(new DBObject(6, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParameters);
        continue;
      } 
      if (oHrmOrgRelationMstDtlBean.getStatus().equals("D")) {
        if (!bDelete) {
          oBean1 = new DBUtilitiesBean();
          oBean1.createBatch("HRMORGRELATIONMST.proc_DeleteRelaMst(?)");
          bDelete = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, oHrmOrgRelationMstDtlBean.getDetailId()));
        oBean1.addToBatch(arylstParameters);
      } 
    } 
    if (bInsert)
      oBean.executeBatch(); 
    if (bUpdate)
      oBean.executeBatch(); 
    if (bDelete)
      oBean1.executeBatch(); 
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstErrorList = new ArrayList();
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmOrgRelationMst")) {
        Iterator itrBean1 = arylstDetailBeanArray.iterator();
        int iCount = 1;
        while (itrBean1.hasNext()) {
          HrmOrgRelationMstDtlBean oHrmOrgRelationMstDtlBean = itrBean1.next();
          if (!oHrmOrgRelationMstDtlBean.getStatus().equalsIgnoreCase("D")) {
            checkMandatory(oHrmOrgRelationMstDtlBean, iCount, arylstErrorList);
            EnrgiseUtil.checkDuplicate(arylstDetailBeanArray, "txtRelationCode", "hrm.HrmRelationMaster.txtRelationCode", arylstErrorList, true);
          } 
          iCount++;
        } 
      }  
    reportError(arylstErrorList);
  }
  
  private void checkMandatory(HrmOrgRelationMstDtlBean oHrmOrgRelationMstDtlBean, int iCount, ArrayList arylstErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {
    if (!EnrgiseUtil.checkString(oHrmOrgRelationMstDtlBean.getTxtRelationCode())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmRelationMaster.txtRelationCode");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(new Integer(iCount));
      arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmOrgRelationMstDtlBean.getTxtRelationName())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmRelationMaster.txtRelationName");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(String.valueOf(iCount));
      arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmOrgRelationMstDtlBean.getTxtOccurence())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmRelationMaster.txtOccurence");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(String.valueOf(iCount));
      arylstErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(arylstErrorList);
  }
  
  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 arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstErrorList = new ArrayList();
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmOrgRelationMst")) {
        Iterator itrBean1 = arylstDetailBeanArray.iterator();
        while (itrBean1.hasNext()) {
          HrmOrgRelationMstDtlBean oHrmOrgRelationMstDtlBean = itrBean1.next();
          if (!oHrmOrgRelationMstDtlBean.getStatus().equalsIgnoreCase("D")) {
            EnrgiseUtil.checkDuplicate(arylstDetailBeanArray, "txtRelationCode", "hrm.HrmRelationMaster.txtRelationCode", arylstErrorList, true);
            checkUniqueDtl(oBaseHeaderBean.getHeaderPrimaryKey(), arylstDetailBeanArray);
          } 
        } 
      }  
    reportError(arylstErrorList);
  }
  
  private void checkUniqueDtl(String sHeaderPrimaryKey, ArrayList oDetailBeanArray) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParams = new ArrayList();
    ArrayList oErrorList = new ArrayList();
    int count = 0;
    Iterator oIt1 = oDetailBeanArray.iterator();
    while (oIt1.hasNext()) {
      HrmOrgRelationMstDtlBean oHrmOrgRelationMstDtlBean = oIt1.next();
      String sQuery = String.valueOf(String.valueOf(" Select ID from HRM_RELA_MST where CODE='").concat(String.valueOf(oHrmOrgRelationMstDtlBean.getTxtRelationCode()))).concat(String.valueOf("'"));
      System.out.println(sQuery);
      DBUtilitiesBean oBean = new DBUtilitiesBean();
      ArrayList oList = oBean.executeQuery(sQuery);
      Iterator oIt = oList.iterator();
      if (oIt.hasNext()) {
        QueryRow oRow = oIt.next();
        if (!oRow.get("ID").getString().equalsIgnoreCase(oHrmOrgRelationMstDtlBean.getDetailId())) {
          MessageKey oMessageKey = new MessageKey("hrm.HrmRelationMaster.txtRelationCode");
          oParams = new ArrayList();
          oParams.add(oMessageKey);
          oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.uniqueConstraintViolated", oParams, "E"));
        } 
      } 
    } 
    reportError(oErrorList);
  }
}
