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.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.business.BaseBO;
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.HrmAttShiftMstHdrBean;
import wenrgise.hrms.vo.HrmAttShiftMstQVO;

public class HrmAttShiftMstBO extends BaseBO {
  public HrmAttShiftMstBO() {}
  
  public HrmAttShiftMstBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public void initializeBOImpl() {
    this.headerTable = "hrm_shift_mst";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {}
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String sScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oParameters = new ArrayList();
    HrmAttShiftMstHdrBean oHrmAttShiftMstHdrBean = (HrmAttShiftMstHdrBean)oBaseHeaderBean;
    if (sScreenMode.equalsIgnoreCase("N")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "N"));
      oParameters.add(new DBObject(2, 1, 12, null));
      oParameters.add(new DBObject(3, 1, 12, oHrmAttShiftMstHdrBean.getShiftCode()));
      oParameters.add(new DBObject(4, 1, 12, oHrmAttShiftMstHdrBean.getShiftDescription()));
      oParameters.add(new DBObject(5, 1, 12, oHrmAttShiftMstHdrBean.getStartTime()));
      oParameters.add(new DBObject(6, 1, 12, oHrmAttShiftMstHdrBean.getEndTime()));
      oParameters.add(new DBObject(7, 1, 12, oHrmAttShiftMstHdrBean.getRelaxationTime()));
      oParameters.add(new DBObject(9, 1, 12, oHrmAttShiftMstHdrBean.getStartTolerenceTime()));
      oParameters.add(new DBObject(10, 1, 12, oHrmAttShiftMstHdrBean.getEndTolerenceTime()));
      oParameters.add(new DBObject(8, 1, 12, oHrmAttShiftMstHdrBean.getOverlapTime()));
      oParameters.add(new DBObject(11, 1, 12, oHrmAttShiftMstHdrBean.getTxtDayOverlapFlag()));
      oParameters.add(new DBObject(12, 1, 12, this.oUserInfo.getUserId()));
      oParameters.add(new DBObject(13, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(14, 2, 12));
      oParameters.add(new DBObject(15, 2, 12));
      oParameters.add(new DBObject(16, 2, 12));
      oParameters.add(new DBObject(17, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMATTENDENCE.PROC_UPSERTHRMATTSFTMST(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } else if (sScreenMode.equalsIgnoreCase("U")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "U"));
      oParameters.add(new DBObject(2, 1, 12, oHrmAttShiftMstHdrBean.getHeaderPrimaryKey()));
      oParameters.add(new DBObject(3, 1, 12, oHrmAttShiftMstHdrBean.getShiftCode()));
      oParameters.add(new DBObject(4, 1, 12, oHrmAttShiftMstHdrBean.getShiftDescription()));
      oParameters.add(new DBObject(5, 1, 12, oHrmAttShiftMstHdrBean.getStartTime()));
      oParameters.add(new DBObject(6, 1, 12, oHrmAttShiftMstHdrBean.getEndTime()));
      oParameters.add(new DBObject(7, 1, 12, oHrmAttShiftMstHdrBean.getRelaxationTime()));
      oParameters.add(new DBObject(8, 1, 12, oHrmAttShiftMstHdrBean.getStartTolerenceTime()));
      oParameters.add(new DBObject(9, 1, 12, oHrmAttShiftMstHdrBean.getEndTolerenceTime()));
      oParameters.add(new DBObject(10, 1, 12, oHrmAttShiftMstHdrBean.getOverlapTime()));
      oParameters.add(new DBObject(11, 1, 12, oHrmAttShiftMstHdrBean.getTxtDayOverlapFlag()));
      oParameters.add(new DBObject(12, 1, 12, this.oUserInfo.getUserId()));
      oParameters.add(new DBObject(13, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(14, 2, 12));
      oParameters.add(new DBObject(15, 2, 12));
      oParameters.add(new DBObject(16, 2, 12));
      oParameters.add(new DBObject(17, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMATTENDENCE.PROC_UPSERTHRMATTSFTMST(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
    } else if (sScreenMode.equalsIgnoreCase("D")) {
    
    } 
    return returnString;
  }
  
  public RecordMetaInfo getAttShiftMstHeaderMetaInfo(HrmAttShiftMstQVO oHrmAttShiftMstQVO) 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, oHrmAttShiftMstQVO.getShiftCode()));
    oParameters.add(new DBObject(2, 1, 12, oHrmAttShiftMstQVO.getHeaderPrimaryKey()));
    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, "HRMATTENDENCE.PROC_GETATTSFTMSTHDRCOUNT(?,?,?,?,?,?,?)");
    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 getAttShiftMstHeader(HrmAttShiftMstQVO oHrmAttShiftMstQVO, long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    if (oHrmAttShiftMstQVO == null)
      oHrmAttShiftMstQVO = new HrmAttShiftMstQVO(); 
    ArrayList oHeaderList = null;
    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, oHrmAttShiftMstQVO.getShiftCode()));
    oParameters.add(new DBObject(4, 1, 12, oHrmAttShiftMstQVO.getHeaderPrimaryKey()));
    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, "HRMATTENDENCE.PROC_ATTSFTMSTHEADER(?,?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.common.norecordfound", "M"); 
    count = 0;
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      if (count == 0)
        oHeaderList = new ArrayList(); 
      count++;
      oRow = oIt.next();
      HrmAttShiftMstHdrBean oHrmAttShiftMstHdrBean = new HrmAttShiftMstHdrBean();
      oHrmAttShiftMstHdrBean.setHeaderPrimaryKey(oRow.get("id").getString());
      oHrmAttShiftMstHdrBean.setShiftCode(oRow.get("CODE").getString());
      oHrmAttShiftMstHdrBean.setShiftDescription(oRow.get("DESCRIPTION").getString());
      oHrmAttShiftMstHdrBean.setStartTime(oRow.get("START_TIME").getString());
      oHrmAttShiftMstHdrBean.setEndTime(oRow.get("END_TIME").getString());
      oHrmAttShiftMstHdrBean.setRelaxationTime(oRow.get("RELAXATION_TIME").getString());
      oHrmAttShiftMstHdrBean.setStartTolerenceTime(oRow.get("START_TOLERANCE_TIME").getString());
      oHrmAttShiftMstHdrBean.setEndTolerenceTime(oRow.get("END_TOLERANCE_TIME").getString());
      oHrmAttShiftMstHdrBean.setOverlapTime(oRow.get("OVERLAP_TIME").getString());
      oHrmAttShiftMstHdrBean.setTxtDayOverlapFlag(oRow.get("DAY_OVERLAP_FLAG").getString());
      oHeaderList.add(oHrmAttShiftMstHdrBean);
    } 
    return oHeaderList;
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, BaseHeaderBean oBaseHeaderBean, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {}
  
  public LovVO getShiftCodeLOVData(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderNames = new ArrayList();
    arylstHeaderNames.add("headerPrimaryKey");
    arylstHeaderNames.add("hrm.HrmAttShiftMaster.shiftCode");
    arylstHeaderNames.add("hrm.HrmAttShiftMaster.desc");
    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();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, oLovQueryVO.getSearchField1()));
    oParameters.add(new DBObject(2, 2, -10));
    oParameters.add(new DBObject(3, 2, 12));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMATTENDENCE.proc_ShiftCodeLov(?,?,?,?,?)");
    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("code").getString());
      oLOVBean.setDetailField3(oRow.get("DESCRIPTION").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  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();
    HrmAttShiftMstHdrBean oHrmAttShiftMstHdrBean = (HrmAttShiftMstHdrBean)oBaseHeaderBean;
    checkMandatoryHdr(oHrmAttShiftMstHdrBean, oErrorList);
    reportError(oErrorList);
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {}
  
  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();
    HrmAttShiftMstHdrBean oHrmAttShiftMstHdrBean = (HrmAttShiftMstHdrBean)oBaseHeaderBean;
    if (bHeaderDataChanged) {
      checkShiftUnique(oHrmAttShiftMstHdrBean.getShiftCode(), sScreenMode, oList);
      String sStHour = convertHour(oHrmAttShiftMstHdrBean.getStartTime());
      String sStMin = convertTime(oHrmAttShiftMstHdrBean.getStartTime());
      String sEndHour = convertHour(oHrmAttShiftMstHdrBean.getEndTime());
      String sEndMin = convertTime(oHrmAttShiftMstHdrBean.getEndTime());
      int stHour = Integer.parseInt(sStHour);
      int edHour = Integer.parseInt(sEndHour);
      int stMin = Integer.parseInt(sStMin);
      int edMin = Integer.parseInt(sEndMin);
      checkField(oList, (BaseHeaderBean)oHrmAttShiftMstHdrBean);
      if (stHour >= 24) {
        MessageKey oMessageKey = new MessageKey("hrm.HrmAttShiftMaster.startTime");
        ArrayList oParams = new ArrayList();
        oParams.add(oMessageKey);
        oList.add(new EnrgiseMessageKeyException("wenrgise.Hrm.EmpShiftMaster.StartHour", oParams, "E"));
      } 
      if (edHour >= 24) {
        MessageKey oMessageKey = new MessageKey("hrm.HrmAttShiftMaster.endTime");
        ArrayList oParams = new ArrayList();
        oParams.add(oMessageKey);
        oList.add(new EnrgiseMessageKeyException("wenrgise.Hrm.EmpShiftMaster.EndHour", oParams, "E"));
      } 
      if (stMin >= 60) {
        MessageKey oMessageKey = new MessageKey("hrm.HrmAttShiftMaster.startTime");
        ArrayList oParams = new ArrayList();
        oParams.add(oMessageKey);
        oList.add(new EnrgiseMessageKeyException("wenrgise.Hrm.EmpShiftMaster.StartMin", oParams, "E"));
      } 
      if (edMin >= 60) {
        MessageKey oMessageKey = new MessageKey("hrm.HrmAttShiftMaster.endTime");
        ArrayList oParams = new ArrayList();
        oParams.add(oMessageKey);
        oList.add(new EnrgiseMessageKeyException("wenrgise.Hrm.EmpShiftMaster.EndMin", oParams, "E"));
      } 
      reportError(oList);
    } 
  }
  
  public void checkField(ArrayList oList, BaseHeaderBean oBaseHeaderBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    HrmAttShiftMstHdrBean oHrmAttShiftMstHdrBean = (HrmAttShiftMstHdrBean)oBaseHeaderBean;
    String StHour = convertHour(oHrmAttShiftMstHdrBean.getStartTime());
    String EndHour = convertHour(oHrmAttShiftMstHdrBean.getEndTime());
    int stField = Integer.parseInt(StHour);
    int edField = Integer.parseInt(EndHour);
    if (oHrmAttShiftMstHdrBean.getTxtDayOverlapFlag().equalsIgnoreCase("N"))
      if (stField > edField) {
        ArrayList oParams = new ArrayList();
        oList.add(new EnrgiseMessageKeyException("hrm.HrmAttShiftMaster.RelationCheck", oParams, "E"));
      }  
    if (oHrmAttShiftMstHdrBean.getTxtDayOverlapFlag().equalsIgnoreCase("Y"))
      if (edField > stField) {
        ArrayList oParams = new ArrayList();
        oList.add(new EnrgiseMessageKeyException("hrm.HrmAttShiftMaster.RelationCheck1", oParams, "E"));
      }  
  }
  
  private void checkMandatoryHdr(HrmAttShiftMstHdrBean oHrmAttShiftMstHdrBean, ArrayList oErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {
    if (!EnrgiseUtil.checkString(oHrmAttShiftMstHdrBean.getStartTime())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmAttShiftMaster.startTime");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmAttShiftMstHdrBean.getEndTime())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmAttShiftMaster.endTime");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmAttShiftMstHdrBean.getShiftCode())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmAttShiftMaster.shiftCode");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmAttShiftMstHdrBean.getShiftDescription())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmAttShiftMaster.desc");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmAttShiftMstHdrBean.getTxtDayOverlapFlag())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmAttShiftMaster.txtDayOverlapFlag");
      ArrayList oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(oErrorList);
  }
  
  public String convertTime(String sTime) {
    String sMin = sTime.substring(3, 5);
    return sMin;
  }
  
  public String convertHour(String sTHour) {
    String sHour = sTHour.substring(0, 2);
    return sHour;
  }
  
  private void checkShiftUnique(String PrimKey, String sScreenMode, ArrayList oList) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList oRetList = null;
    if (sScreenMode.equals("N")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, PrimKey));
      oParameters.add(new DBObject(2, 2, -10));
      oParameters.add(new DBObject(3, 2, 12));
      oParameters.add(new DBObject(4, 2, 12));
      oParameters.add(new DBObject(5, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMATTENDENCE.proc_ShiftCodeCheck(?,?,?,?,?)");
      oRetList = (ArrayList)((DBObject)oOutArray.get(0)).getObject();
      if (oRetList.size() > 0) {
        ArrayList oArguments = new ArrayList();
        oArguments.add(new Integer(1));
        oArguments.add(new Integer(count));
        oList.add(new EnrgiseApplicationException("Hrm.AttShiftMast.Code"));
      } 
    } 
  }
}
