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.HrmAttEmpAttInHdrBean;
import wenrgise.hrms.vo.HrmAttEmpAttInQVO;

public class HrmAttEmpAttInBO extends HrmBaseBO {
  public HrmAttEmpAttInBO() {}
  
  public HrmAttEmpAttInBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmAttHdrMetaInfo(HrmAttEmpAttInQVO oHrmAttEmpAttInQVO) 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, oHrmAttEmpAttInQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(2, 1, 12, oHrmAttEmpAttInQVO.getEmpId()));
    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, "HRMATTEMPATTQRY.proc_getAttCount(?,?,?,?,?,?,?)");
    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 getHrmAttHeaderInfo(HrmAttEmpAttInQVO oHrmAttEmpAttInQVO, 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 (oHrmAttEmpAttInQVO == null)
      oHrmAttEmpAttInQVO = new HrmAttEmpAttInQVO(); 
    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, oHrmAttEmpAttInQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(4, 1, 12, oHrmAttEmpAttInQVO.getEmpId()));
    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, "HRMATTEMPATTQRY.proc_getEmpAttInfo(?,?,?,?,?,?,?,?)");
    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();
      HrmAttEmpAttInHdrBean oHrmAttEmpAttInHdrBean = new HrmAttEmpAttInHdrBean();
      oHrmAttEmpAttInHdrBean.setHeaderPrimaryKey(oRow.get("ID").getString());
      oHrmAttEmpAttInHdrBean.setEmployeeNo(oRow.get("EMPNO").getString());
      oHrmAttEmpAttInHdrBean.setFirstName(oRow.get("FIRSTNAME").getString());
      oHrmAttEmpAttInHdrBean.setMiddleName(oRow.get("MIDDLENAME").getString());
      oHrmAttEmpAttInHdrBean.setLastName(oRow.get("LASTNAME").getString());
      oHrmAttEmpAttInHdrBean.setDate(oRow.get("CHKINDATE").getString());
      oHrmAttEmpAttInHdrBean.setTimeIn(oRow.get("CHKINTIME").getString());
      oHrmAttEmpAttInHdrBean.setShiftCode(oRow.get("SHIFTCODE").getString());
      oHrmAttEmpAttInHdrBean.setShiftId(oRow.get("SHIFTID").getString());
      oHrmAttEmpAttInHdrBean.setAttndStatus(oRow.get("ATTSTATUS").getString());
      oHrmAttEmpAttInHdrBean.setReasons(oRow.get("REASONS").getString());
      oHeaderList.add(oHrmAttEmpAttInHdrBean);
    } 
    return oHeaderList;
  }
  
  public HrmAttEmpAttInHdrBean getOnLoadSysDtTime(HrmAttEmpAttInHdrBean objHrmAttEmpAttInHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList arylstAttndDateTime = null;
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 2, -10));
    arylstParameters.add(new DBObject(2, 2, 12));
    arylstParameters.add(new DBObject(3, 2, 12));
    arylstParameters.add(new DBObject(4, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParameters, "HRMATTEMPATTQRY.proc_getOnLoadDateTime(?,?,?,?)");
    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)
        arylstAttndDateTime = new ArrayList(); 
      count++;
      oRow = itrBean.next();
      objHrmAttEmpAttInHdrBean.setDate(oRow.get("ATNDDT").getString());
      objHrmAttEmpAttInHdrBean.setTimeIn(oRow.get("ATNDTM").getString());
    } 
    return objHrmAttEmpAttInHdrBean;
  }
  
  public LovVO getHrmAttEmpShftCodeLOV(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("Code ID");
    oHeaderList.add("hrm.HrmAttEmpAttQuery.shiftCode");
    oHeaderList.add("Code ID");
    oLovVO.setHeaderList(oHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("H");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = 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(); 
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    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, "HRMATTEMPATTQRY.proc_GetShiftCodeLOV(?,?,?,?,?)");
    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());
      String sShiftTime = oRow.get("NEWTIME").getString();
      String sSysTime = null;
      if (oLovQueryVO.getProperty("timeIn") != null)
        sSysTime = oLovQueryVO.getProperty("timeIn"); 
      if (doTimeComparision(sSysTime, sShiftTime) == true) {
        oLOVBean.setDetailField3("T");
      } else {
        oLOVBean.setDetailField3("L");
      } 
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  private boolean doTimeComparision(String sSysTime, String sShiftTime) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sSysHr = sSysTime.substring(0, 2);
    String sSysMin = sSysTime.substring(3, 5);
    int iConSysTime = Integer.parseInt(String.valueOf(sSysHr).concat(String.valueOf(sSysMin)));
    String sShftHr = sShiftTime.substring(0, 2);
    String sShftMin = sShiftTime.substring(3, 5);
    int iConShftTime = Integer.parseInt(String.valueOf(sShftHr).concat(String.valueOf(sShftMin)));
    if (iConSysTime <= iConShftTime)
      return true; 
    return false;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "Hrm_Emp_Attnd_Dtl";
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    HrmAttEmpAttInHdrBean oHrmAttEmpAttInHdrBean = (HrmAttEmpAttInHdrBean)oBaseHeaderBean;
    String sEmpId = oHrmAttEmpAttInHdrBean.getEmpId();
    String sAttDt = oHrmAttEmpAttInHdrBean.getDate();
    String sAttTime = oHrmAttEmpAttInHdrBean.getTimeIn();
    if (ScreenMode.equalsIgnoreCase("N")) {
      arylstParam = new ArrayList();
      arylstParam.add(new DBObject(1, 1, 12, "i"));
      arylstParam.add(new DBObject(2, 1, 93, EnrgiseUtil.convertToSqlDate(sAttDt)));
      arylstParam.add(new DBObject(3, 1, 12, sAttTime));
      arylstParam.add(new DBObject(4, 1, 12, oHrmAttEmpAttInHdrBean.getEmpId()));
      arylstParam.add(new DBObject(5, 1, 12, oHrmAttEmpAttInHdrBean.getShiftId()));
      arylstParam.add(new DBObject(6, 1, 12, oHrmAttEmpAttInHdrBean.getAttndStatus()));
      arylstParam.add(new DBObject(7, 1, 12, oHrmAttEmpAttInHdrBean.getReasons()));
      arylstParam.add(new DBObject(8, 1, 12, "420"));
      arylstParam.add(new DBObject(9, 1, 12, "100"));
      arylstParam.add(new DBObject(10, 2, 12));
      arylstParam.add(new DBObject(11, 2, 12));
      arylstParam.add(new DBObject(12, 2, 12));
      arylstParam.add(new DBObject(13, 2, 4));
      ArrayList oOutArray = oBean.callProc(arylstParam, "HRMATTEMPATTQRY.proc_UpsertHrmEmpAtt(?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
    } 
    return returnString;
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {}
  
  public void updateHeaderImpl(BaseHeaderBean objBaseHeaderBean) throws EnrgiseSystemException {}
  
  public String saveNewHeaderImpl(BaseHeaderBean objBaseHeaderBean) throws EnrgiseSystemException {
    return new String();
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  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 {
    ArrayList oErrorList = new ArrayList();
    HrmAttEmpAttInHdrBean oHrmAttEmpAttInHdrBean = (HrmAttEmpAttInHdrBean)oBaseHeaderBean;
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D")) {
        checkMandatoryHeader((HrmAttEmpAttInHdrBean)oBaseHeaderBean);
        multipleAttendance((HrmAttEmpAttInHdrBean)oBaseHeaderBean, oErrorList);
      }  
    reportError(oErrorList);
  }
  
  private void multipleAttendance(HrmAttEmpAttInHdrBean oHrmAttEmpAttInHdrBean, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sEmpId = oHrmAttEmpAttInHdrBean.getEmpId();
    String sAttDt = oHrmAttEmpAttInHdrBean.getDate();
    String sAttId = String.valueOf(String.valueOf(String.valueOf(String.valueOf("SELECT distinct ID from Hrm_Emp_Attnd_Dtl HEAD WHERE HEAD.E_PER_DTL_ID in ").concat(String.valueOf(sEmpId))).concat(String.valueOf(" AND to_char(HEAD.ATND_IN_DATE, 'dd-Mon-yyyy') in '"))).concat(String.valueOf(sAttDt))).concat(String.valueOf("' "));
    DBUtilitiesBean oBean1 = new DBUtilitiesBean();
    ArrayList oList1 = oBean1.executeQuery(sAttId);
    Iterator oIt = oList1.iterator();
    if (oIt.hasNext()) {
      QueryRow oRow = oIt.next();
      if (oRow.get("ID").getString() != null) {
        ArrayList oParam = new ArrayList();
        MessageKey oMessageKey = new MessageKey("hrm.HrmAttEmpAttIn.attndStatus");
        oParam.add(oMessageKey);
        oErrorList.add(new EnrgiseMessageKeyException("wenrgise.hrms.header1.multipleAttendance", oParam));
      } 
    } 
    reportError(oErrorList);
  }
  
  private void checkMandatoryHeader(HrmAttEmpAttInHdrBean oHrmAttEmpAttInHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oList = new ArrayList();
    String sAttStatus = oHrmAttEmpAttInHdrBean.getAttndStatus();
    if (sAttStatus.equalsIgnoreCase("L"))
      if (!EnrgiseUtil.checkString(oHrmAttEmpAttInHdrBean.getReasons())) {
        ArrayList oParam = new ArrayList();
        MessageKey oMessageKey = new MessageKey("hrm.HrmAttEmpAttIn.reasons");
        oParam.add(oMessageKey);
        oList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParam, "E"));
      }  
    if (!EnrgiseUtil.checkString(oHrmAttEmpAttInHdrBean.getShiftCode())) {
      ArrayList oParam = new ArrayList();
      MessageKey oMessageKey = new MessageKey("hrm.HrmAttEmpAttIn.shiftCode");
      oParam.add(oMessageKey);
      oList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParam, "E"));
    } 
    reportError(oList);
  }
}
