package wenrgise.hrms.ejb.business;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
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.HrmAprslStartOrderHdrBean;
import wenrgise.hrms.vo.HrmAprslStartOrderQVO;

public class HrmAprslStartOrderBO extends HrmBaseBO {
  public HrmAprslStartOrderBO() {}
  
  public HrmAprslStartOrderBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmAprslStOrderMetaInfo(HrmAprslStartOrderQVO oHrmAprslStartOrderQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int iCount = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, oHrmAprslStartOrderQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(2, 1, 12, oHrmAprslStartOrderQVO.getStOrderNo()));
    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, "HRMAPRSLSTORDER.procAprslStOrderCount(?,?,?,?,?,?,?)");
    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 getHrmAprslStOrderInfo(HrmAprslStartOrderQVO oHrmAprslStartOrderQVO, long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int iCount = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    ArrayList arylstHeaderList = null;
    if (oHrmAprslStartOrderQVO == null)
      oHrmAprslStartOrderQVO = new HrmAprslStartOrderQVO(); 
    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, oHrmAprslStartOrderQVO.getHeaderPrimaryKey()));
    arylstParameters.add(new DBObject(4, 1, 12, oHrmAprslStartOrderQVO.getStOrderNo()));
    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, "HRMAPRSLSTORDER.procAprslStOrderInfo(?,?,?,?,?,?,?,?)");
    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 oIt = arylstList.iterator();
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    while (oIt.hasNext()) {
      if (iCount == 0)
        arylstHeaderList = new ArrayList(); 
      iCount++;
      oRow = oIt.next();
      HrmAprslStartOrderHdrBean oAprslStartOrderHdr = new HrmAprslStartOrderHdrBean();
      oAprslStartOrderHdr.setStOrderId(oRow.get("id").getString());
      oAprslStartOrderHdr.setStOrderNo(oRow.get("StOrderNo").getString());
      oAprslStartOrderHdr.setPeriodStDate(EnrgiseUtil.convertToString(oRow.get("PStDate").getDate()));
      oAprslStartOrderHdr.setPeriodEndDate(EnrgiseUtil.convertToString(oRow.get("PEndDate").getDate()));
      oAprslStartOrderHdr.setAprslStDate(EnrgiseUtil.convertToString(oRow.get("ApStDate").getDate()));
      oAprslStartOrderHdr.setAprslEndDate(EnrgiseUtil.convertToString(oRow.get("ApEndDate").getDate()));
      arylstHeaderList.add(oAprslStartOrderHdr);
    } 
    return arylstHeaderList;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_APRSL_START_ORDER";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {}
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("Update ille!");
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl ille!";
  }
  
  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();
    HrmAprslStartOrderHdrBean oHrmAprslStartOrderHdrBean = (HrmAprslStartOrderHdrBean)oBaseHeaderBean;
    if (sScreenMode.equalsIgnoreCase("D"))
      ArrayList arrayList = new ArrayList(); 
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        checkDatesConstraint(oHrmAprslStartOrderHdrBean, oErrorList);  
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    HrmAprslStartOrderHdrBean oHrmAprslStartOrderHdrBean = (HrmAprslStartOrderHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      arylstParameters = new ArrayList();
      arylstParameters.add(new DBObject(1, 1, 12, "N"));
      arylstParameters.add(new DBObject(2, 1, 12, oHrmAprslStartOrderHdrBean.getStOrderId()));
      arylstParameters.add(new DBObject(3, 1, 12, oHrmAprslStartOrderHdrBean.getStOrderNo()));
      arylstParameters.add(new DBObject(4, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAprslStartOrderHdrBean.getPeriodStDate())));
      arylstParameters.add(new DBObject(5, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAprslStartOrderHdrBean.getPeriodEndDate())));
      arylstParameters.add(new DBObject(6, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAprslStartOrderHdrBean.getAprslStDate())));
      arylstParameters.add(new DBObject(7, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAprslStartOrderHdrBean.getAprslEndDate())));
      arylstParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getUserId()));
      arylstParameters.add(new DBObject(9, 1, 12, this.oUserInfo.getSiteId()));
      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, "HRMAPRSLSTORDER.procUpsertAprslStOrder(?,?,?,?,?,?,?,?,?,?,?,?,?)");
      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, oHrmAprslStartOrderHdrBean.getStOrderId()));
      arylstParameters.add(new DBObject(3, 1, 12, oHrmAprslStartOrderHdrBean.getStOrderNo()));
      arylstParameters.add(new DBObject(4, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAprslStartOrderHdrBean.getPeriodStDate())));
      arylstParameters.add(new DBObject(5, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAprslStartOrderHdrBean.getPeriodEndDate())));
      arylstParameters.add(new DBObject(6, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAprslStartOrderHdrBean.getAprslStDate())));
      arylstParameters.add(new DBObject(7, 1, 93, EnrgiseUtil.convertToSqlDate(oHrmAprslStartOrderHdrBean.getAprslEndDate())));
      arylstParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getUserId()));
      arylstParameters.add(new DBObject(9, 1, 12, this.oUserInfo.getSiteId()));
      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, "HRMAPRSLSTORDER.procUpsertAprslStOrder(?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = arylstOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } 
    return returnString;
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oErrorList = new ArrayList();
    HrmAprslStartOrderHdrBean oHrmDeputApplnHdrBean = (HrmAprslStartOrderHdrBean)oBaseHeaderBean;
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        checkMandatoryHeader((HrmAprslStartOrderHdrBean)oBaseHeaderBean);  
    reportError(oErrorList);
  }
  
  private void checkMandatoryHeader(HrmAprslStartOrderHdrBean oHrmAprslStartOrderHdrBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oErrorList = new ArrayList();
    ArrayList oParams = new ArrayList();
    if (!EnrgiseUtil.checkString(oHrmAprslStartOrderHdrBean.getStOrderNo())) {
      MessageKey oMessageKey = new MessageKey("hrm.AprslStartOrder.stOrderNo");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmAprslStartOrderHdrBean.getPeriodStDate())) {
      MessageKey oMessageKey = new MessageKey("hrm.AprslStartOrder.periodStDate");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmAprslStartOrderHdrBean.getPeriodEndDate())) {
      MessageKey oMessageKey = new MessageKey("hrm.AprslStartOrder.periodEndDate");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmAprslStartOrderHdrBean.getAprslStDate())) {
      MessageKey oMessageKey = new MessageKey("hrm.AprslStartOrder.aprslStDate");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmAprslStartOrderHdrBean.getAprslEndDate())) {
      MessageKey oMessageKey = new MessageKey("hrm.AprslStartOrder.aprslEndDate");
      oParams = new ArrayList();
      oParams.add(oMessageKey);
      oErrorList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParams, "E"));
    } 
    reportError(oErrorList);
  }
  
  public LovVO getHrmAprslStOrderNoLOVQrydata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("ID");
    arylstHeaderList.add("hrm.AprslStartOrder.stOrderNo");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int iCount = 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, "HRMAPRSLSTORDER.proc_GetHrmAprslStOrderLOVdata(?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstList.iterator();
    while (iter.hasNext()) {
      if (iCount == 0)
        arylstList = new ArrayList(); 
      iCount++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("ID").getString());
      oLOVBean.setDetailField2(oRow.get("StOrderNo").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  private void checkDatesConstraint(HrmAprslStartOrderHdrBean oHrmAprslStartOrderHdrBean, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    if (EnrgiseUtil.checkString(oHrmAprslStartOrderHdrBean.getPeriodStDate())) {
      int dateFlag = EnrgiseUtil.compareDates(dateFormat, oHrmAprslStartOrderHdrBean.getPeriodEndDate(), oHrmAprslStartOrderHdrBean.getPeriodStDate());
      if (dateFlag == -1 && dateFlag != -2)
        oErrorList.add(new EnrgiseApplicationException("wenrgise.hrm.startOrder.pDateError")); 
    } 
    if (EnrgiseUtil.checkString(oHrmAprslStartOrderHdrBean.getAprslStDate())) {
      int dateFlag1 = EnrgiseUtil.compareDates(dateFormat, oHrmAprslStartOrderHdrBean.getAprslEndDate(), oHrmAprslStartOrderHdrBean.getAprslStDate());
      if (dateFlag1 == -1 && dateFlag1 != -2)
        oErrorList.add(new EnrgiseApplicationException("wenrgise.hrm.startOrder.apDateError")); 
    } 
    reportError(oErrorList);
  }
}
