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.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.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.HrmOrgScaleMstDtlBean;
import wenrgise.hrms.bean.HrmOrgScaleMstHdrBean;
import wenrgise.hrms.vo.HrmOrgScaleMstQVO;

public class HrmOrgScaleMstBO extends HrmBaseBO {
  public HrmOrgScaleMstBO() {}
  
  public HrmOrgScaleMstBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmScaleHdrMetaInfo(HrmOrgScaleMstQVO oHrmOrgScaleMstQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, oHrmOrgScaleMstQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(2, 1, 12, oHrmOrgScaleMstQVO.getScaleCode()));
    arylstParameters.add(new DBObject(3, 1, 12, oHrmOrgScaleMstQVO.getRevision()));
    arylstParameters.add(new DBObject(4, 2, -5));
    arylstParameters.add(new DBObject(5, 2, 93));
    arylstParameters.add(new DBObject(6, 2, 12));
    arylstParameters.add(new DBObject(7, 2, 12));
    arylstParameters.add(new DBObject(8, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGSCALEMST.proc_HrScaleCount(?,?,?,?,?,?,?,?)");
    RecordMetaInfo oRecordMetaInfo = new RecordMetaInfo();
    DBObject oTimeObject = arylstOutArray.get(1);
    oRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    DBObject oTotalRecord = arylstOutArray.get(0);
    oRecordMetaInfo.setRecordCount(((Long)oTotalRecord.getObject()).longValue());
    return oRecordMetaInfo;
  }
  
  public ArrayList getHrmScaleHeaderInfo(HrmOrgScaleMstQVO oHrmOrgScaleMstQVO, long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    ArrayList arylstHeaderList = null;
    if (oHrmOrgScaleMstQVO == null)
      oHrmOrgScaleMstQVO = new HrmOrgScaleMstQVO(); 
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, -5, new Long(lStartPosition)));
    arylstParameters.add(new DBObject(2, 1, -5, new Long(lLastPosition)));
    arylstParameters.add(new DBObject(3, 1, 12, oHrmOrgScaleMstQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(4, 1, 12, oHrmOrgScaleMstQVO.getScaleCode()));
    arylstParameters.add(new DBObject(5, 1, 12, oHrmOrgScaleMstQVO.getRevision()));
    arylstParameters.add(new DBObject(6, 2, -10));
    arylstParameters.add(new DBObject(7, 2, 12));
    arylstParameters.add(new DBObject(8, 2, 12));
    arylstParameters.add(new DBObject(9, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGSCALEMST.proc_GetScaleHdr(?,?,?,?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    if (arylstList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator itrBean = arylstList.iterator();
    while (itrBean.hasNext()) {
      if (count == 0)
        arylstHeaderList = new ArrayList(); 
      count++;
      oRow = itrBean.next();
      HrmOrgScaleMstHdrBean oScaleHeader = new HrmOrgScaleMstHdrBean();
      oScaleHeader.setHeaderPrimaryKey(oRow.get("ID").getString());
      oScaleHeader.setScaleCode(oRow.get("CODE").getString());
      oScaleHeader.setDesc(oRow.get("NAME").getString());
      oScaleHeader.setScaleId(oRow.get("SCALE_ID").getString());
      oScaleHeader.setHierarchyLevel(oRow.get("HIERARCHY_LEVEL").getString());
      oScaleHeader.setParentId(oRow.get("PARENT_ID").getString());
      oScaleHeader.setRevision(oRow.get("REV_YYYYMM").getString());
      arylstHeaderList.add(oScaleHeader);
    } 
    return arylstHeaderList;
  }
  
  public RecordMetaInfo getHrmScaleDtlMetaInfo(String lPrimaryKey) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, lPrimaryKey));
    arylstParameters.add(new DBObject(2, 2, -5));
    arylstParameters.add(new DBObject(3, 2, 93));
    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, "HRMORGSCALEMST.proc_HrScaleDtlCount(?,?,?,?,?,?)");
    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 getHrmScaleDetailInfo(String lPrimaryKey, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList arylstHrmScaleDetail = 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, 1, 12, lPrimaryKey));
    arylstParameters.add(new DBObject(4, 2, -10));
    arylstParameters.add(new DBObject(5, 2, 12));
    arylstParameters.add(new DBObject(6, 2, 12));
    arylstParameters.add(new DBObject(7, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGSCALEMST.proc_GetScaleDtl(?,?,?,?,?,?,?)");
    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)
        arylstHrmScaleDetail = new ArrayList(); 
      count++;
      oRow = itrBean.next();
      HrmOrgScaleMstDtlBean oScaleDetailBean = new HrmOrgScaleMstDtlBean();
      oScaleDetailBean.setDetailId(oRow.get("ID").getString());
      oScaleDetailBean.setTxtStartBasic(oRow.get("START_BASIC").getString());
      oScaleDetailBean.setTxtIncrement(oRow.get("INCREMENT_INCREMENT").getString());
      oScaleDetailBean.setTxtEndBasic(oRow.get("FINAL_BASIC").getString());
      arylstHrmScaleDetail.add(oScaleDetailBean);
    } 
    return arylstHrmScaleDetail;
  }
  
  public LovVO getHrmScaleMstScaleCodeLOV(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("Scale ID");
    arylstHeaderList.add("hrm.HrmPositionMaster.scaleCode");
    arylstHeaderList.add("hrm.HrmPositionMaster.scale");
    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();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParam.add(new DBObject(2, 2, -10));
    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, "HRMORGSETUPLOV.procScaleMstScaleCodeLOV(?,?,?,?,?)");
    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("CODE").getString());
      oLOVBean.setDetailField3(oRow.get("NAME").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public LovVO getHrmOrgScaleGradeLOVData(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("Scale ID");
    arylstHeaderList.add("hrm.HrmScaleMaster.grade");
    arylstHeaderList.add("hrm.HrmScaleMaster.desc");
    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, "HRMORGSETUPLOV.procscalemstgradecodelov(?,?,?,?,?,?)");
    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("CODE").getString());
      oLOVBean.setDetailField3(oRow.get("NAME").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_SCALE_HDR";
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    HrmOrgScaleMstHdrBean oHrmOrgScaleMstHdrBean = (HrmOrgScaleMstHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      arylstParam = new ArrayList();
      arylstParam.add(new DBObject(1, 1, 12, "i"));
      arylstParam.add(new DBObject(2, 1, 12, oHrmOrgScaleMstHdrBean.getScaleId()));
      arylstParam.add(new DBObject(3, 1, 12, oHrmOrgScaleMstHdrBean.getRevision()));
      arylstParam.add(new DBObject(4, 1, 12, this.oUserInfo.getUserTypeId()));
      arylstParam.add(new DBObject(5, 1, 12, this.oUserInfo.getSiteId()));
      arylstParam.add(new DBObject(6, 1, 12, null));
      arylstParam.add(new DBObject(7, 2, 12));
      arylstParam.add(new DBObject(8, 2, 12));
      arylstParam.add(new DBObject(9, 2, 12));
      arylstParam.add(new DBObject(10, 2, 4));
      ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMORGSCALEMST.proc_UpsertHrScaleHdr(?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = arylstOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } else if (ScreenMode.equalsIgnoreCase("U")) {
      arylstParam = new ArrayList();
      arylstParam.add(new DBObject(1, 1, 12, "u"));
      arylstParam.add(new DBObject(2, 1, 12, oHrmOrgScaleMstHdrBean.getScaleId()));
      arylstParam.add(new DBObject(3, 1, 12, oHrmOrgScaleMstHdrBean.getRevision()));
      arylstParam.add(new DBObject(4, 1, 12, this.oUserInfo.getUserTypeId()));
      arylstParam.add(new DBObject(5, 1, 12, this.oUserInfo.getSiteId()));
      arylstParam.add(new DBObject(6, 1, 12, oHrmOrgScaleMstHdrBean.getHeaderPrimaryKey()));
      arylstParam.add(new DBObject(7, 2, 12));
      arylstParam.add(new DBObject(8, 2, 12));
      arylstParam.add(new DBObject(9, 2, 12));
      arylstParam.add(new DBObject(10, 2, 4));
      ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMORGSCALEMST.proc_UpsertHrScaleHdr(?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = arylstOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } 
    return returnString;
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    if (sScreenName.equalsIgnoreCase("HrmOrgScaleMst"))
      saveHrmScaleMstDetail(sHeaderPrimaryKey, arylstDetailBeanArray); 
  }
  
  private void saveHrmScaleMstDetail(String sHeaderPrimaryKey, 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()) {
      HrmOrgScaleMstDtlBean oHrmOrgScaleMstDtlBean = itrBean.next();
      if (oHrmOrgScaleMstDtlBean.getStatus().equals("N")) {
        if (!bInsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMORGSCALEMST.proc_UpsertHrScaleMstDtl(?,?,?,?,?,?,?,?)");
          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, sHeaderPrimaryKey));
        arylstParameters.add(new DBObject(4, 1, 12, oHrmOrgScaleMstDtlBean.getTxtStartBasic()));
        arylstParameters.add(new DBObject(5, 1, 12, oHrmOrgScaleMstDtlBean.getTxtIncrement()));
        arylstParameters.add(new DBObject(6, 1, 12, oHrmOrgScaleMstDtlBean.getTxtEndBasic()));
        arylstParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParameters);
        continue;
      } 
      if (oHrmOrgScaleMstDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("HRMORGSCALEMST.proc_UpsertHrScaleMstDtl(?,?,?,?,?,?,?,?)");
          bUpdate = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, "U"));
        arylstParameters.add(new DBObject(2, 1, 12, oHrmOrgScaleMstDtlBean.getDetailId()));
        arylstParameters.add(new DBObject(3, 1, 12, sHeaderPrimaryKey));
        arylstParameters.add(new DBObject(4, 1, 12, oHrmOrgScaleMstDtlBean.getTxtStartBasic()));
        arylstParameters.add(new DBObject(5, 1, 12, oHrmOrgScaleMstDtlBean.getTxtIncrement()));
        arylstParameters.add(new DBObject(6, 1, 12, oHrmOrgScaleMstDtlBean.getTxtEndBasic()));
        arylstParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(arylstParameters);
        continue;
      } 
      if (oHrmOrgScaleMstDtlBean.getStatus().equals("D")) {
        if (!bDelete) {
          oBean1 = new DBUtilitiesBean();
          oBean1.createBatch("HRMORGSCALEMST.proc_DeleteScaleMstDtl(?)");
          bDelete = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, oHrmOrgScaleMstDtlBean.getDetailId()));
        oBean1.addToBatch(arylstParameters);
      } 
    } 
    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 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("HrmOrgScaleMst")) {
        Iterator itrBean1 = arylstDetailBeanArray.iterator();
        int iCount = 1;
        while (itrBean1.hasNext()) {
          HrmOrgScaleMstDtlBean oHrmOrgScaleMstDtlBean = itrBean1.next();
          if (!oHrmOrgScaleMstDtlBean.getStatus().equalsIgnoreCase("D"))
            checkMandatory(oHrmOrgScaleMstDtlBean, iCount, arylstErrorList); 
          iCount++;
        } 
      }  
    reportError(arylstErrorList);
  }
  
  private void checkMandatory(HrmOrgScaleMstDtlBean oHrmOrgScaleMstDtlBean, int iCount, ArrayList arylstErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {
    if (!EnrgiseUtil.checkString(oHrmOrgScaleMstDtlBean.getTxtStartBasic())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmScaleMaster.txtStartBasic");
      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(oHrmOrgScaleMstDtlBean.getTxtEndBasic())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmScaleMaster.txtEndBasic");
      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(oHrmOrgScaleMstDtlBean.getTxtIncrement())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmScaleMaster.txtIncrement");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oParams.add(new Integer(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();
    HrmOrgScaleMstHdrBean oHrmOrgScaleMstHdrBean = (HrmOrgScaleMstHdrBean)oBaseHeaderBean;
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D")) {
        checkUniqueHeader((HrmOrgScaleMstHdrBean)oBaseHeaderBean);
        checkYearMonth((HrmOrgScaleMstHdrBean)oBaseHeaderBean);
      }  
    if (bDetailDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        if (sScreenName.equalsIgnoreCase("HrmOrgScaleMst")) {
          Iterator itrBean1 = arylstDetailBeanArray.iterator();
          while (itrBean1.hasNext()) {
            HrmOrgScaleMstDtlBean oHrmOrgScaleMstDtlBean = itrBean1.next();
            if (!oHrmOrgScaleMstDtlBean.getStatus().equalsIgnoreCase("D")) {
              checkZeroIncrement(arylstDetailBeanArray);
              checkDtlFields(arylstDetailBeanArray);
            } 
            if (!oHrmOrgScaleMstDtlBean.getStatus().equalsIgnoreCase("N"))
              checkStartEndBasic(arylstDetailBeanArray); 
          } 
        }   
    reportError(arylstErrorList);
  }
  
  private void checkUniqueHeader(HrmOrgScaleMstHdrBean oHrmOrgScaleMstHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstList1 = new ArrayList();
    ArrayList arylstParam = new ArrayList();
    String sQuery = String.valueOf(String.valueOf(String.valueOf(" Select ID as ID from HRM_SCALE_HDR where SCALE_ID=").concat(String.valueOf(oHrmOrgScaleMstHdrBean.getScaleId()))).concat(String.valueOf(" and REV_YYYYMM="))).concat(String.valueOf(oHrmOrgScaleMstHdrBean.getRevision()));
    System.out.println(sQuery);
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList arylstList = oBean.executeQuery(sQuery);
    Iterator itrBean = arylstList.iterator();
    if (itrBean.hasNext()) {
      QueryRow oRow = itrBean.next();
      if (!oRow.get("ID").getString().equalsIgnoreCase(oHrmOrgScaleMstHdrBean.getHeaderPrimaryKey())) {
        arylstParam = new ArrayList();
        arylstParam.add("");
        arylstParam.add(new Integer(0));
        arylstList1.add(new EnrgiseMessageKeyException("hrms.scale.uniqueConstraintViolated", arylstParam));
      } 
    } 
    reportError(arylstList1);
  }
  
  public void checkYearMonth(HrmOrgScaleMstHdrBean oHrmOrgScaleMstHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    String yymm = oHrmOrgScaleMstHdrBean.getRevision();
    ArrayList oErrorList = new ArrayList();
    if (yymm.length() != 6) {
      oErrorList.add(new EnrgiseApplicationException("hrms.common.yyyymm"));
      return;
    } 
    if (yymm.substring(0, 1).equals("0")) {
      oErrorList.add(new EnrgiseApplicationException("hrms.common.yyyymm"));
      return;
    } 
    int mon = Integer.parseInt(yymm.substring(4, 6));
    if (mon > 12 || mon < 1) {
      oErrorList.add(new EnrgiseApplicationException("hrms.common.yyyymm"));
      return;
    } 
    reportError(oErrorList);
  }
  
  private void checkZeroIncrement(ArrayList oDetailBeanArray) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstList = new ArrayList();
    ArrayList arylstParam = new ArrayList();
    int count = 0;
    Iterator oIt = oDetailBeanArray.iterator();
    int n = oDetailBeanArray.size();
    while (oIt.hasNext()) {
      oIt.next();
      HrmOrgScaleMstDtlBean oHrmOrgScaleMstDtlBean = oDetailBeanArray.get(count);
      int iIncrement = Integer.parseInt(oHrmOrgScaleMstDtlBean.getTxtIncrement());
      if (count < n) {
        count++;
        if (iIncrement == 0) {
          arylstParam = new ArrayList();
          arylstParam.add("");
          arylstParam.add(new Integer(0));
          arylstList.add(new EnrgiseMessageKeyException("hrm.HrmScaleMaster.detail.zeroIncrement", arylstParam));
          continue;
        } 
        checkIncrement(oHrmOrgScaleMstDtlBean);
      } 
    } 
    reportError(arylstList);
  }
  
  private void checkStartEndBasic(ArrayList oDetailBeanArray) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstList = new ArrayList();
    ArrayList arylstParam = new ArrayList();
    int count = 0;
    Iterator oIt = oDetailBeanArray.iterator();
    int n = oDetailBeanArray.size();
    while (oIt.hasNext()) {
      oIt.next();
      HrmOrgScaleMstDtlBean oHrmOrgScaleMstDtlBean = oDetailBeanArray.get(count);
      int iEndBasic = Integer.parseInt(oHrmOrgScaleMstDtlBean.getTxtEndBasic());
      System.out.println(iEndBasic);
      if (count < n - 1) {
        count++;
        HrmOrgScaleMstDtlBean oHrmOrgScaleMstDtlBean1 = oDetailBeanArray.get(count);
        int iNextStartBasic = Integer.parseInt(oHrmOrgScaleMstDtlBean1.getTxtStartBasic());
        System.out.println(iNextStartBasic);
        if (iNextStartBasic != iEndBasic) {
          arylstParam = new ArrayList();
          arylstParam.add("");
          arylstParam.add(new Integer(0));
          arylstList.add(new EnrgiseMessageKeyException("hrm.HrmScaleMaster.detail.checkBasic", arylstParam));
        } 
      } 
    } 
    reportError(arylstList);
  }
  
  private void checkIncrement(HrmOrgScaleMstDtlBean oHrmOrgScaleMstDtlBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstList = new ArrayList();
    ArrayList arylstParam = new ArrayList();
    int iEndBasic = Integer.parseInt(oHrmOrgScaleMstDtlBean.getTxtEndBasic());
    int iStartBasic = Integer.parseInt(oHrmOrgScaleMstDtlBean.getTxtStartBasic());
    int iIncrement = Integer.parseInt(oHrmOrgScaleMstDtlBean.getTxtIncrement());
    int iDiff = iEndBasic - iStartBasic;
    int iReminder = iDiff % iIncrement;
    if (iReminder != 0) {
      arylstParam = new ArrayList();
      arylstParam.add("");
      arylstParam.add(new Integer(0));
      arylstList.add(new EnrgiseMessageKeyException("hrm.HrmScaleMaster.detail.increment", arylstParam));
    } 
    reportError(arylstList);
  }
  
  private void checkDtlFields(ArrayList oDetailBeanArray) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstList = new ArrayList();
    ArrayList arylstParam = new ArrayList();
    int count = 0;
    Iterator oIt = oDetailBeanArray.iterator();
    int n = oDetailBeanArray.size();
    while (oIt.hasNext()) {
      oIt.next();
      HrmOrgScaleMstDtlBean oHrmOrgScaleMstDtlBean = oDetailBeanArray.get(count);
      int iEndBasic = Integer.parseInt(oHrmOrgScaleMstDtlBean.getTxtEndBasic());
      int iStartBasic = Integer.parseInt(oHrmOrgScaleMstDtlBean.getTxtStartBasic());
      int iIncrement = Integer.parseInt(oHrmOrgScaleMstDtlBean.getTxtIncrement());
      System.out.println(iEndBasic);
      System.out.println(iStartBasic);
      System.out.println(iIncrement);
      if (count < n) {
        count++;
        int iDiff = iEndBasic - iStartBasic;
        int iDiff1 = iEndBasic - iIncrement;
        int iDiff2 = iStartBasic - iIncrement;
        if (iDiff < 0) {
          arylstParam = new ArrayList();
          arylstParam.add("");
          arylstParam.add(new Integer(0));
          arylstList.add(new EnrgiseMessageKeyException("hrm.HrmScaleMaster.detail.checkEndBasic", arylstParam));
        } 
        if (iDiff1 < 0) {
          arylstParam = new ArrayList();
          arylstParam.add("");
          arylstParam.add(new Integer(0));
          arylstList.add(new EnrgiseMessageKeyException("hrm.HrmScaleMaster.detail.checkIncrement", arylstParam));
        } 
        if (iDiff2 < 0) {
          arylstParam = new ArrayList();
          arylstParam.add("");
          arylstParam.add(new Integer(0));
          arylstList.add(new EnrgiseMessageKeyException("hrm.HrmScaleMaster.detail.checkIncrement", arylstParam));
        } 
      } 
    } 
    reportError(arylstList);
  }
}
