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.HrmSepCatMstHdrBean;
import wenrgise.hrms.vo.HrmSepCatMstQVO;

public class HrmSepCatMstBO extends HrmBaseBO {
  public HrmSepCatMstBO() {}
  
  public HrmSepCatMstBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmSepCatHdrMetaInfo(HrmSepCatMstQVO oHrmSepCatMstQVO) 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, oHrmSepCatMstQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(2, 1, 12, oHrmSepCatMstQVO.getSeparationCode()));
    oParameters.add(new DBObject(3, 2, -5));
    oParameters.add(new DBObject(4, 2, 93));
    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, "HRMSEPCATEGORY.proc_HrSepCatCount(?,?,?,?,?,?,?)");
    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 getHrmSepCatHeaderInfo(HrmSepCatMstQVO oHrmSepCatMstQVO, 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 (oHrmSepCatMstQVO == null)
      oHrmSepCatMstQVO = new HrmSepCatMstQVO(); 
    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, oHrmSepCatMstQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(4, 1, 12, oHrmSepCatMstQVO.getSeparationCode()));
    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, "HRMSEPCATEGORY.proc_GetHrSepCatInfo(?,?,?,?,?,?,?,?)");
    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();
      HrmSepCatMstHdrBean oHrmSepCatHdrBean = new HrmSepCatMstHdrBean();
      oHrmSepCatHdrBean.setHeaderPrimaryKey(oRow.get("ID").getString());
      oHrmSepCatHdrBean.setSeparationCode(oRow.get("SEPARATION_CODE").getString());
      oHrmSepCatHdrBean.setSeparationType(oRow.get("SEPARATION_TYPE").getString());
      oHrmSepCatHdrBean.setNoticePeriod(oRow.get("NOTICE_PERIOD").getString());
      oHrmSepCatHdrBean.setRemarks(oRow.get("REMARKS").getString());
      oHrmSepCatHdrBean.setDescription(oRow.get("DESCRIPTION").getString());
      oHeaderList.add(oHrmSepCatHdrBean);
    } 
    return oHeaderList;
  }
  
  public LovVO getHrmSeparationCodeLOV(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderNames = new ArrayList();
    arylstHeaderNames.add("Separation Id");
    arylstHeaderNames.add("hrm.HrmSepCatMstrForm.sepCode");
    arylstHeaderNames.add("hrm.HrmSepCatMstrForm.sepDesc");
    oLovVO.setHeaderList(arylstHeaderNames);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = new ArrayList();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null) {
      sQuerySearch1 = oLovQueryVO.getSearchField1();
      System.out.println(sQuerySearch1);
    } 
    if (oLovQueryVO.getSearchField2() != null) {
      sQuerySearch2 = oLovQueryVO.getSearchField2();
      System.out.println(sQuerySearch2);
    } 
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    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, "HRMSEPCATEGORY.proc_GetSeaparationCodeLOV(?,?,?,?,?,?)");
    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("SEPARATION_CODE").getString());
      oLOVBean.setDetailField3(oRow.get("DESCRIPTION").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_SEP_CAT_MST";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    System.out.println("Save ");
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("Update ");
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl ille!";
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    HrmSepCatMstHdrBean oHrmSepCatMstHdrBean = (HrmSepCatMstHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "N"));
      oParameters.add(new DBObject(2, 1, 12, oHrmSepCatMstHdrBean.getSeparationCode()));
      oParameters.add(new DBObject(3, 1, 12, oHrmSepCatMstHdrBean.getDescription()));
      oParameters.add(new DBObject(4, 1, 12, oHrmSepCatMstHdrBean.getNoticePeriod()));
      oParameters.add(new DBObject(5, 1, 12, oHrmSepCatMstHdrBean.getRemarks()));
      oParameters.add(new DBObject(6, 1, 12, oHrmSepCatMstHdrBean.getSeparationType()));
      oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserTypeId()));
      oParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(9, 1, 12, null));
      oParameters.add(new DBObject(10, 2, 12));
      oParameters.add(new DBObject(11, 2, 12));
      oParameters.add(new DBObject(12, 2, 12));
      oParameters.add(new DBObject(13, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMSEPCATEGORY.proc_UpsertHrSepCatHdr(?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } else if (ScreenMode.equalsIgnoreCase("U")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "U"));
      oParameters.add(new DBObject(2, 1, 12, oHrmSepCatMstHdrBean.getSeparationCode()));
      oParameters.add(new DBObject(3, 1, 12, oHrmSepCatMstHdrBean.getDescription()));
      oParameters.add(new DBObject(4, 1, 12, oHrmSepCatMstHdrBean.getNoticePeriod()));
      oParameters.add(new DBObject(5, 1, 12, oHrmSepCatMstHdrBean.getRemarks()));
      oParameters.add(new DBObject(6, 1, 12, oHrmSepCatMstHdrBean.getSeparationType()));
      oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserTypeId()));
      oParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(9, 1, 12, oHrmSepCatMstHdrBean.getHeaderPrimaryKey()));
      oParameters.add(new DBObject(10, 2, 12));
      oParameters.add(new DBObject(11, 2, 12));
      oParameters.add(new DBObject(12, 2, 12));
      oParameters.add(new DBObject(13, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMSEPCATEGORY.proc_UpsertHrSepCatHdr(?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } else if (ScreenMode.equalsIgnoreCase("D")) {
      oParameters.add(new DBObject(1, 1, 12, oHrmSepCatMstHdrBean.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, "HRMSEPCATEGORY.proc_DeleteHrSepCatHdr(?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = null;
    } 
    return returnString;
  }
  
  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((HrmSepCatMstHdrBean)oBaseHeaderBean);  
    reportError(oErrorList);
  }
  
  private void checkMandatoryHeader(HrmSepCatMstHdrBean oHrmSepCatMstHdrBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oErrorList = new ArrayList();
    ArrayList oParams = new ArrayList();
    if (!EnrgiseUtil.checkString(oHrmSepCatMstHdrBean.getSeparationCode())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmSeparationCategoryMaster.separationCode");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmSepCatMstHdrBean.getSeparationType())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmSeparationCategoryMaster.separationType");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(oErrorList);
  }
  
  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 {
    HrmSepCatMstHdrBean oHrmSepCatMstHdrBean = (HrmSepCatMstHdrBean)oBaseHeaderBean;
    ArrayList oList = new ArrayList();
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D")) {
        checkUniqueHeader((HrmSepCatMstHdrBean)oBaseHeaderBean);
      } else if (sScreenMode.equalsIgnoreCase("D")) {
        HrmCommonBO oCommonBO = new HrmCommonBO();
        String sRefId = oCommonBO.getRefList("HRM_EMP_PERS", "TYPE_OF_SEPERATION ", oHrmSepCatMstHdrBean.getHeaderPrimaryKey(), "ID");
        if (EnrgiseUtil.checkString(sRefId))
          if (oList.isEmpty())
            oList.add(new EnrgiseApplicationException("wenrgise.common.deletion"));  
      }  
    reportError(oList);
  }
  
  private void checkUniqueHeader(HrmSepCatMstHdrBean oHrmSepCatMstHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = String.valueOf(String.valueOf(" Select ID as ID from HRM_SEP_CAT_MST where SEPARATION_CODE='").concat(String.valueOf(oHrmSepCatMstHdrBean.getSeparationCode()))).concat(String.valueOf("'"));
    System.out.println(sQuery);
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList arylstList = oBean.executeQuery(sQuery);
    Iterator itrBean = arylstList.iterator();
    ArrayList oList = new ArrayList();
    if (itrBean.hasNext()) {
      QueryRow oRow = itrBean.next();
      if (!oRow.get("ID").getString().equalsIgnoreCase(oHrmSepCatMstHdrBean.getHeaderPrimaryKey()))
        oList.add(new EnrgiseApplicationException("wenrgise.common.uniqueConstraintViolated")); 
    } 
    reportError(oList);
  }
}
