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.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.HrmOrgHolTypeMstHdrBean;
import wenrgise.hrms.vo.HrmOrgHolTypeMstQVO;

public class HrmOrgHolTypeMstBO extends HrmBaseBO {
  public HrmOrgHolTypeMstBO() {}
  
  public HrmOrgHolTypeMstBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmHolTypeHdrMetaInfo(HrmOrgHolTypeMstQVO oHrmOrgHolTypeMstQVO) 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, oHrmOrgHolTypeMstQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(2, 1, 12, oHrmOrgHolTypeMstQVO.getHolidayType()));
    arylstParameters.add(new DBObject(3, 2, -5));
    arylstParameters.add(new DBObject(4, 2, 93));
    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, "HRMORGHOLTYPEMST.proc_HrHolTypCount(?,?,?,?,?,?,?)");
    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 getHrmHolTypeHeaderInfo(HrmOrgHolTypeMstQVO oHrmOrgHolTypeMstQVO, 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 (oHrmOrgHolTypeMstQVO == null)
      oHrmOrgHolTypeMstQVO = new HrmOrgHolTypeMstQVO(); 
    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, oHrmOrgHolTypeMstQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(4, 1, 12, oHrmOrgHolTypeMstQVO.getHolidayType()));
    arylstParameters.add(new DBObject(5, 2, -10));
    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, "HRMORGHOLTYPEMST.proc_GetHrHolTypInfo(?,?,?,?,?,?,?,?)");
    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();
      HrmOrgHolTypeMstHdrBean oHolTypeHeader = new HrmOrgHolTypeMstHdrBean();
      oHolTypeHeader.setHeaderPrimaryKey(oRow.get("ID").getString());
      oHolTypeHeader.setHolidayType(oRow.get("DESCRIPTION").getString());
      oHolTypeHeader.setMandatory(oRow.get("LEAVE_MANDATORY_FLG").getString());
      oHolTypeHeader.setYearlyLimit(oRow.get("Yearly_LIMIT").getString());
      oHolTypeHeader.setCompensatoryLeave(oRow.get("COMPENSATORY_LEAVE").getString());
      oHolTypeHeader.setRewardPercentageOfBasic(oRow.get("REWARD_PERC").getString());
      arylstHeaderList.add(oHolTypeHeader);
    } 
    return arylstHeaderList;
  }
  
  public ArrayList getHrmHolTypeHeaderInfoByFlag(String sleaveManFlag) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    ArrayList arylstHeaderList = null;
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sleaveManFlag));
    arylstParameters.add(new DBObject(2, 2, -10));
    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, "HRMORGHOLTYPEMST.proc_GetHrHolTypInfoByFlag(?,?,?,?,?)");
    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();
      HrmOrgHolTypeMstHdrBean oHolTypeHeader = new HrmOrgHolTypeMstHdrBean();
      oHolTypeHeader.setHeaderPrimaryKey(oRow.get("ID").getString());
      oHolTypeHeader.setHolidayType(oRow.get("DESCRIPTION").getString());
      oHolTypeHeader.setMandatory(oRow.get("LEAVE_MANDATORY_FLAG").getString());
      oHolTypeHeader.setYearlyLimit(oRow.get("YEARLY_LIMIT").getString());
      oHolTypeHeader.setCompensatoryLeave(oRow.get("COMPENSATORY_LEAVE").getString());
      oHolTypeHeader.setRewardPercentageOfBasic(oRow.get("REWARD_PERC").getString());
      arylstHeaderList.add(oHolTypeHeader);
    } 
    return arylstHeaderList;
  }
  
  public LovVO getHrmHolidayTypeLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("Code ID");
    arylstHeaderList.add("hrm.HrmCoreCompetency.desc");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParameters.add(new DBObject(2, 2, -10));
    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, "HRMORGSETUPLOV.proc_GetHolidayTypeLOV(?,?,?,?,?)");
    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("DESCRIPTION").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_HOL_TYPE_MST";
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    HrmOrgHolTypeMstHdrBean oHrmOrgHolTypeMstHdrBean = (HrmOrgHolTypeMstHdrBean)oBaseHeaderBean;
    System.out.println(ScreenMode);
    if (ScreenMode.equalsIgnoreCase("N")) {
      arylstParameters = new ArrayList();
      arylstParameters.add(new DBObject(1, 1, 12, "i"));
      arylstParameters.add(new DBObject(2, 1, 12, oHrmOrgHolTypeMstHdrBean.getHolidayType()));
      arylstParameters.add(new DBObject(3, 1, 12, oHrmOrgHolTypeMstHdrBean.getYearlyLimit()));
      arylstParameters.add(new DBObject(4, 1, 12, oHrmOrgHolTypeMstHdrBean.getMandatory()));
      arylstParameters.add(new DBObject(5, 1, 12, oHrmOrgHolTypeMstHdrBean.getCompensatoryLeave()));
      arylstParameters.add(new DBObject(6, 1, 12, oHrmOrgHolTypeMstHdrBean.getRewardPercentageOfBasic()));
      arylstParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserTypeId()));
      arylstParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
      arylstParameters.add(new DBObject(9, 1, 12, null));
      arylstParameters.add(new DBObject(10, 2, 12));
      arylstParameters.add(new DBObject(11, 2, 12));
      arylstParameters.add(new DBObject(12, 2, 12));
      arylstParameters.add(new DBObject(13, 2, 4));
      ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGHOLTYPEMST.proc_UpsertHrHolType(?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = arylstOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } else if (ScreenMode.equalsIgnoreCase("U")) {
      arylstParameters = new ArrayList();
      arylstParameters.add(new DBObject(1, 1, 12, "u"));
      arylstParameters.add(new DBObject(2, 1, 12, oHrmOrgHolTypeMstHdrBean.getHolidayType()));
      arylstParameters.add(new DBObject(3, 1, 12, oHrmOrgHolTypeMstHdrBean.getYearlyLimit()));
      arylstParameters.add(new DBObject(4, 1, 12, oHrmOrgHolTypeMstHdrBean.getMandatory()));
      arylstParameters.add(new DBObject(5, 1, 12, oHrmOrgHolTypeMstHdrBean.getCompensatoryLeave()));
      arylstParameters.add(new DBObject(6, 1, 12, oHrmOrgHolTypeMstHdrBean.getRewardPercentageOfBasic()));
      arylstParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserTypeId()));
      arylstParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
      arylstParameters.add(new DBObject(9, 1, 12, oHrmOrgHolTypeMstHdrBean.getHeaderPrimaryKey()));
      arylstParameters.add(new DBObject(10, 2, 12));
      arylstParameters.add(new DBObject(11, 2, 12));
      arylstParameters.add(new DBObject(12, 2, 12));
      arylstParameters.add(new DBObject(13, 2, 4));
      ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMORGHOLTYPEMST.proc_UpsertHrHolType(?,?,?,?,?,?,?,?,?,?,?,?,?)");
      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 {
    System.out.println("No Detail");
  }
  
  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 (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        checkMandatoryHeader((HrmOrgHolTypeMstHdrBean)oBaseHeaderBean);  
    reportError(arylstErrorList);
  }
  
  private void checkMandatoryHeader(HrmOrgHolTypeMstHdrBean oHrmOrgHolTypeMstHdrBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oErrorList = new ArrayList();
    ArrayList oParams = new ArrayList();
    if (!EnrgiseUtil.checkString(oHrmOrgHolTypeMstHdrBean.getHolidayType())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmHolidaytypeMst.holidayType");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmOrgHolTypeMstHdrBean.getMandatory())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmHolidaytypeMst.mandatory");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(oErrorList);
  }
  
  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();
    HrmOrgHolTypeMstHdrBean oHrmOrgHolTypeMstHdrBean = (HrmOrgHolTypeMstHdrBean)oBaseHeaderBean;
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D")) {
        checkUniqueHeader((HrmOrgHolTypeMstHdrBean)oBaseHeaderBean);
        checkYearlyLimit((HrmOrgHolTypeMstHdrBean)oBaseHeaderBean);
      }  
  }
  
  private void checkYearlyLimit(HrmOrgHolTypeMstHdrBean oHrmOrgHolTypeMstHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    ArrayList arylstErrorList = new ArrayList();
    String sYearlyLimit = new String();
    if (EnrgiseUtil.checkString(oHrmOrgHolTypeMstHdrBean.getYearlyLimit())) {
      sYearlyLimit = oHrmOrgHolTypeMstHdrBean.getYearlyLimit();
      if (Integer.parseInt(sYearlyLimit) == 0) {
        arylstParam = new ArrayList();
        arylstParam.add("Holiday Tyme Master : Yearly limit");
        arylstParam.add(new Integer(0));
        arylstErrorList.add(new EnrgiseMessageKeyException("hrm.HrmHolidaytypeMst.checkYearlyLimit", arylstParam));
      } 
    } 
    reportError(arylstErrorList);
  }
  
  private void checkUniqueHeader(HrmOrgHolTypeMstHdrBean oHrmOrgHolTypeMstHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = String.valueOf(String.valueOf(" Select ID as ID from HRM_HOL_TYPE_MST where DESCRIPTION='").concat(String.valueOf(oHrmOrgHolTypeMstHdrBean.getHolidayType()))).concat(String.valueOf("'"));
    System.out.println(sQuery);
    ArrayList arylstList1 = new ArrayList();
    ArrayList arylstParam = new ArrayList();
    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(oHrmOrgHolTypeMstHdrBean.getHeaderPrimaryKey())) {
        arylstParam = new ArrayList();
        arylstParam.add("");
        arylstParam.add(new Integer(0));
        arylstList1.add(new EnrgiseMessageKeyException("wenrgise.common.uniqueConstraintViolated", arylstParam));
      } 
    } 
    reportError(arylstList1);
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
}
