package wenrgise.hrms.ejb.business;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.bean.LOVBean;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.RecordMetaInfo;
import wenrgise.common.utility.UserInfo;
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.HrmMisReportDtlBean;
import wenrgise.hrms.bean.HrmMisReportHdrBean;
import wenrgise.hrms.vo.HrmMisreportQVO;

public class HrmMisReportBO extends HrmBaseBO {
  public HrmMisReportBO() {}
  
  public HrmMisReportBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getMisDataCount(HrmMisreportQVO oHrmMisreportQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, oHrmMisreportQVO.getLocationCode()));
    arylstParam.add(new DBObject(2, 2, -5));
    arylstParam.add(new DBObject(3, 2, 93));
    arylstParam.add(new DBObject(4, 2, 12));
    arylstParam.add(new DBObject(5, 2, 12));
    arylstParam.add(new DBObject(6, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMMISREP.procGetMisDataCount(?,?,?,?,?,?)");
    RecordMetaInfo oRecordMetaInfo = new RecordMetaInfo();
    DBObject oTotalRecord = arylstOutArray.get(0);
    oRecordMetaInfo.setRecordCount(((Long)oTotalRecord.getObject()).longValue());
    DBObject oTimeObject = arylstOutArray.get(1);
    oRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    return oRecordMetaInfo;
  }
  
  public ArrayList getMisDataInfo(HrmMisreportQVO oHrmMisreportQVO, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList arylstList = null;
    Iterator itrBean = null;
    ArrayList arylstMisReport = null;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, -5, new Long(lDetailFirstPosition)));
    arylstParam.add(new DBObject(2, 1, -5, new Long(lDetailLastPosition)));
    arylstParam.add(new DBObject(3, 1, 12, oHrmMisreportQVO.getLocationCode()));
    arylstParam.add(new DBObject(4, 2, -10));
    arylstParam.add(new DBObject(5, 2, 12));
    arylstParam.add(new DBObject(6, 2, 12));
    arylstParam.add(new DBObject(7, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMMISREP.procGetMisDataInfo(?,?,?,?,?,?,?)");
    arylstList = (ArrayList)((DBObject)arylstOutArray.get(0)).getObject();
    if (arylstList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    count = 0;
    itrBean = arylstList.iterator();
    while (itrBean.hasNext()) {
      if (count == 0)
        arylstMisReport = new ArrayList(); 
      count++;
      oRow = itrBean.next();
      HrmMisReportDtlBean oHrmMisReportDtlBean = new HrmMisReportDtlBean();
      oHrmMisReportDtlBean.setTxtPositionId(oRow.get("desigId").getString());
      oHrmMisReportDtlBean.setTxtPosition(oRow.get("Desig").getString());
      oHrmMisReportDtlBean.setTxtDesiredStrength(oRow.get("Strength").getString());
      arylstMisReport.add(oHrmMisReportDtlBean);
    } 
    return arylstMisReport;
  }
  
  public void saveDetailImpl(BaseHeaderBean oBaseHeaderBean, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseApplicationException, EnrgiseSystemException {
    if (sScreenName.equalsIgnoreCase("HrmMisReport"))
      saveHrmMisDetail(oBaseHeaderBean, oDetailBeanArray); 
  }
  
  public String saveHrmMisDetail(BaseHeaderBean oBaseHeaderBean, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bUpdate = false;
    boolean bDelete = false;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean1 = null;
    DBUtilitiesBean oBean2 = null;
    DBUtilitiesBean oBean3 = null;
    HrmMisReportHdrBean oHrmMisReportHdrBean = (HrmMisReportHdrBean)oBaseHeaderBean;
    Iterator itrBean = arylstDetailBeanArray.iterator();
    while (itrBean.hasNext()) {
      HrmMisReportDtlBean oHrmMisReportDtlBean = itrBean.next();
      if (oHrmMisReportDtlBean.getStatus().equals("N")) {
        if (!bInsert) {
          oBean1 = new DBUtilitiesBean();
          oBean1.createBatch("HRMMISREP.proc_insertdata(?,?,?,?)");
          bInsert = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, "N"));
        arylstParameters.add(new DBObject(2, 1, 12, oHrmMisReportHdrBean.getLocationCode()));
        arylstParameters.add(new DBObject(3, 1, 12, oHrmMisReportDtlBean.getTxtPositionId()));
        arylstParameters.add(new DBObject(4, 1, 12, oHrmMisReportDtlBean.getTxtDesiredStrength()));
        oBean1.addToBatch(arylstParameters);
        continue;
      } 
      if (oHrmMisReportDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean2 = new DBUtilitiesBean();
          oBean2.createBatch("HRMMISREP.proc_insertdata(?,?,?,?)");
          bUpdate = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, "U"));
        arylstParameters.add(new DBObject(2, 1, 12, oHrmMisReportHdrBean.getLocationCode()));
        arylstParameters.add(new DBObject(3, 1, 12, oHrmMisReportDtlBean.getTxtPositionId()));
        arylstParameters.add(new DBObject(4, 1, 12, oHrmMisReportDtlBean.getTxtDesiredStrength()));
        oBean2.addToBatch(arylstParameters);
        continue;
      } 
      if (oHrmMisReportDtlBean.getStatus().equals("D")) {
        if (!bDelete) {
          oBean3 = new DBUtilitiesBean();
          oBean3.createBatch("HRMMISREP.procDelHrmMisData(?,?)");
          bDelete = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, oHrmMisReportHdrBean.getLocationCode()));
        arylstParameters.add(new DBObject(2, 1, 12, oHrmMisReportDtlBean.getTxtPositionId()));
        oBean3.addToBatch(arylstParameters);
      } 
    } 
    if (bInsert)
      oBean1.executeBatch(); 
    if (bUpdate)
      oBean2.executeBatch(); 
    if (bDelete)
      oBean3.executeBatch(); 
    return oHrmMisReportHdrBean.getLocationCode();
  }
  
  public void initializeBOImpl() {
    this.headerTable = "hrm_temp_mis_tab";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {}
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {}
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl";
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  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();
    reportError(arylstErrorList);
  }
  
  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();
    reportError(arylstErrorList);
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String param1) throws EnrgiseSystemException {
    return new String();
  }
  
  public LovVO getMisPositionLOVData(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("ID");
    arylstHeaderList.add("hrm.HrmMisReport.Position");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = new ArrayList();
    ArrayList oList = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    String sLocation = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    sLocation = oLovQueryVO.getProperty("location");
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sQuerySearch1));
    oParameters.add(new DBObject(2, 1, 12, sLocation));
    oParameters.add(new DBObject(3, 2, -10));
    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, "HRMMISREP.procGetPosition(?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    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("POSITION").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public String InsertData(BaseHeaderBean oBaseHeaderBean, ArrayList arylstDetailBeanArray) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    HrmMisReportHdrBean oHrmMisReportHdrBean = (HrmMisReportHdrBean)oBaseHeaderBean;
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList arylstList = null;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, oHrmMisReportHdrBean.getLocationCode()));
    arylstParam.add(new DBObject(2, 1, -10, arylstDetailBeanArray));
    arylstParam.add(new DBObject(3, 2, 12));
    arylstParam.add(new DBObject(4, 2, 12));
    arylstParam.add(new DBObject(5, 2, 4));
    oBean.callProc(arylstParam, "HRMMISREP.proc_insertdata(?,?,?,?,?)");
    return oHrmMisReportHdrBean.getLocationCode();
  }
  
  public LovVO getMisYearMonthLOVData(LovQueryVO oLovQueryVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("hrm.HrmMisReport.yearmonth");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList oParameters = new ArrayList();
    ArrayList oList = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    String sLocation = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    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, "HRMMISREP.procGetYearMonth(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    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("YEARMONTH").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  public String getYearMonth() throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    String yearmonth = null;
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList arylstList = null;
    Iterator itrBean = null;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 2, 12));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMMISREP.procGetCurrentYearMonth(?)");
    yearmonth = (String)((DBObject)arylstOutArray.get(0)).getObject();
    return yearmonth;
  }
}
