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.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.ejb.common.helper.DBObject;
import wenrgise.ejb.common.helper.QueryRow;
import wenrgise.ejb.common.utility.DBUtilitiesBean;
import wenrgise.hrms.bean.HrmAttEmpAttOutHdrBean;
import wenrgise.hrms.vo.HrmAttEmpAttOutQVO;

public class HrmAttEmpAttOutBO extends HrmBaseBO {
  public HrmAttEmpAttOutBO() {}
  
  public HrmAttEmpAttOutBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmAttOutHdrMetaInfo(HrmAttEmpAttOutQVO oHrmAttEmpAttOutQVO) 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, oHrmAttEmpAttOutQVO.getEmpId()));
    oParameters.add(new DBObject(2, 2, -5));
    oParameters.add(new DBObject(3, 2, 93));
    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, "HRMATTEMPATTQRY.proc_getEmpAttOutCount(?,?,?,?,?,?)");
    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 getHrmAttOutHeaderInfo(HrmAttEmpAttOutQVO oHrmAttEmpAttOutQVO, 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 (oHrmAttEmpAttOutQVO == null)
      oHrmAttEmpAttOutQVO = new HrmAttEmpAttOutQVO(); 
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, oHrmAttEmpAttOutQVO.getEmpId()));
    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_getEmpAttOutInfo(?,?,?,?,?)");
    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();
      HrmAttEmpAttOutHdrBean oHrmAttEmpAttOutHdrBean = new HrmAttEmpAttOutHdrBean();
      oHrmAttEmpAttOutHdrBean.setHeaderPrimaryKey(oRow.get("ID").getString());
      oHrmAttEmpAttOutHdrBean.setEmpId(oRow.get("E_PER_DTL_ID").getString());
      oHrmAttEmpAttOutHdrBean.setEmployeeNo(oRow.get("EMP_NO").getString());
      oHrmAttEmpAttOutHdrBean.setFirstName(oRow.get("FIRST_NAME").getString());
      oHrmAttEmpAttOutHdrBean.setMiddleName(oRow.get("MIDDLE_NAME").getString());
      oHrmAttEmpAttOutHdrBean.setLastName(oRow.get("LAST_NAME").getString());
      oHrmAttEmpAttOutHdrBean.setDate(EnrgiseUtil.convertToString(oRow.get("INDATE").getDate()));
      oHrmAttEmpAttOutHdrBean.setTimeIn(oRow.get("INTIME").getString());
      oHrmAttEmpAttOutHdrBean.setShiftCode(oRow.get("SHIFTCODE").getString());
      oHrmAttEmpAttOutHdrBean.setShiftId(oRow.get("SHFT_MST_ID").getString());
      oHrmAttEmpAttOutHdrBean.setAttndStatus(oRow.get("ATTND_STATUS").getString());
      oHrmAttEmpAttOutHdrBean.setOutDate(EnrgiseUtil.convertToString(oRow.get("OUTDATE").getDate()));
      oHrmAttEmpAttOutHdrBean.setTimeOut(oRow.get("OUTTIME").getString());
      oHeaderList.add(oHrmAttEmpAttOutHdrBean);
    } 
    return oHeaderList;
  }
  
  public HrmAttEmpAttOutHdrBean getOnLoadSysDtTimeOut(HrmAttEmpAttOutHdrBean oHrmAttEmpAttOutHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    return null;
  }
  
  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();
    HrmAttEmpAttOutHdrBean oHrmAttEmpAttOutHdrBean = (HrmAttEmpAttOutHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("U")) {
      arylstParam = new ArrayList();
      arylstParam.add(new DBObject(1, 1, 12, "u"));
      arylstParam.add(new DBObject(2, 1, 12, oHrmAttEmpAttOutHdrBean.getHeaderPrimaryKey()));
      arylstParam.add(new DBObject(3, 2, 12));
      arylstParam.add(new DBObject(4, 2, 12));
      arylstParam.add(new DBObject(5, 2, 12));
      arylstParam.add(new DBObject(6, 2, 4));
      ArrayList oOutArray = oBean.callProc(arylstParam, "HRMATTEMPATTQRY.proc_UpdHrmEmpAttOut(?,?,?,?,?,?)");
      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();
    HrmAttEmpAttOutHdrBean oHrmAttEmpAttOutHdrBean = (HrmAttEmpAttOutHdrBean)oBaseHeaderBean;
    if (bHeaderDataChanged)
      if (sScreenMode.equalsIgnoreCase("U"))
        multipleAttendance((HrmAttEmpAttOutHdrBean)oBaseHeaderBean, oErrorList);  
    reportError(oErrorList);
  }
  
  private void multipleAttendance(HrmAttEmpAttOutHdrBean oHrmAttEmpAttOutHdrBean, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sEmpId = oHrmAttEmpAttOutHdrBean.getEmpId();
    String sAttInDt = oHrmAttEmpAttOutHdrBean.getDate();
    String sAttOutDt = oHrmAttEmpAttOutHdrBean.getOutDate();
    String sAttId = String.valueOf(String.valueOf(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(sAttInDt))).concat(String.valueOf("' AND to_char(HEAD.ATND_OUT_DATE, 'dd-Mon-yyyy') in '"))).concat(String.valueOf(sAttOutDt))).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.HrmAttEmpAttOut.attndStatus");
        oParam.add(oMessageKey);
        oErrorList.add(new EnrgiseMessageKeyException("wenrgise.hrms.header1.multipleAttendance", oParam));
      } 
    } 
    reportError(oErrorList);
  }
}
