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.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.HrmRecTestCentreMstHdrBean;
import wenrgise.hrms.vo.HrmRecTestCentreMstQVO;

public class HrmRecTestCentreMstBO extends HrmBaseBO {
  public HrmRecTestCentreMstBO() {}
  
  public HrmRecTestCentreMstBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmTCMstHdrMetaInfo(HrmRecTestCentreMstQVO oHrmTCMstQueryVO) 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, oHrmTCMstQueryVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(2, 1, 12, oHrmTCMstQueryVO.getTestCentreCodeId()));
    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, "HRMTCMST.proc_HrTCMstCount(?,?,?,?,?,?,?)");
    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 getHrmTCMstHeaderInfo(HrmRecTestCentreMstQVO oHrmTCMstQueryVO, 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 (oHrmTCMstQueryVO == null)
      oHrmTCMstQueryVO = new HrmRecTestCentreMstQVO(); 
    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, oHrmTCMstQueryVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(4, 1, 12, oHrmTCMstQueryVO.getTestCentreCodeId()));
    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, "HRMTCMST.PROC_GETHRTCMSTINFO(?,?,?,?,?,?,?,?)");
    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();
      HrmRecTestCentreMstHdrBean oTCMstHeader = new HrmRecTestCentreMstHdrBean();
      oTCMstHeader.setHeaderPrimaryKey(oRow.get("ID").getString());
      oTCMstHeader.setTestCentreCode(oRow.get("CODE").getString());
      oTCMstHeader.setTestCentreName(oRow.get("NAME").getString());
      oTCMstHeader.setAddress(oRow.get("ADDRESS").getString());
      oTCMstHeader.setPhone(oRow.get("PHONE").getString());
      oTCMstHeader.setFax(oRow.get("FAX").getString());
      oHeaderList.add(oTCMstHeader);
    } 
    return oHeaderList;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_TST_CNTR_MST";
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    HrmRecTestCentreMstHdrBean oHrmTestCenterMasterHdrBean = (HrmRecTestCentreMstHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "N"));
      oParameters.add(new DBObject(2, 1, 12, oHrmTestCenterMasterHdrBean.getTestCentreCode()));
      oParameters.add(new DBObject(3, 1, 12, oHrmTestCenterMasterHdrBean.getTestCentreName()));
      oParameters.add(new DBObject(4, 1, 12, oHrmTestCenterMasterHdrBean.getAddress()));
      oParameters.add(new DBObject(5, 1, 12, oHrmTestCenterMasterHdrBean.getPhone()));
      oParameters.add(new DBObject(6, 1, 12, oHrmTestCenterMasterHdrBean.getFax()));
      oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserTypeId()));
      oParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(9, 1, 12, null));
      oParameters.add(new DBObject(10, 2, 12));
      oParameters.add(new DBObject(11, 2, 12));
      oParameters.add(new DBObject(12, 2, 12));
      oParameters.add(new DBObject(13, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMTCMST.proc_UpsertHrTCMstHdr(?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } else if (ScreenMode.equalsIgnoreCase("U")) {
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, "U"));
      oParameters.add(new DBObject(2, 1, 12, oHrmTestCenterMasterHdrBean.getTestCentreCode()));
      oParameters.add(new DBObject(3, 1, 12, oHrmTestCenterMasterHdrBean.getTestCentreName()));
      oParameters.add(new DBObject(4, 1, 12, oHrmTestCenterMasterHdrBean.getAddress()));
      oParameters.add(new DBObject(5, 1, 12, oHrmTestCenterMasterHdrBean.getPhone()));
      oParameters.add(new DBObject(6, 1, 12, oHrmTestCenterMasterHdrBean.getFax()));
      oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserTypeId()));
      oParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(9, 1, 12, oHrmTestCenterMasterHdrBean.getHeaderPrimaryKey()));
      oParameters.add(new DBObject(10, 2, 12));
      oParameters.add(new DBObject(11, 2, 12));
      oParameters.add(new DBObject(12, 2, 12));
      oParameters.add(new DBObject(13, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HRMTCMST.proc_UpsertHrTCMstHdr(?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } 
    return returnString;
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    System.out.println("No Detail");
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("Update Nischt!");
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl Nischt!";
  }
  
  public LovVO getHrmTCMstCodeLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuery = "SELECT htcm.ID, htcm.CODE, htcm.NAME from HRM_TST_CNTR_MST htcm where 1 = 1";
    if (oLovQueryVO.getSearchField1() != null)
      sQuery = String.valueOf(String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and htcm.CODE LIKE '%"))).concat(String.valueOf(oLovQueryVO.getSearchField1()))).concat(String.valueOf("%' ")); 
    if (oLovQueryVO.getSearchField2() != null)
      sQuery = String.valueOf(String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and htcm.NAME LIKE '%"))).concat(String.valueOf(oLovQueryVO.getSearchField2()))).concat(String.valueOf("%' ")); 
    System.out.println(sQuery);
    DBUtilitiesBean oBean1 = new DBUtilitiesBean();
    ArrayList oList1 = oBean1.executeQuery(sQuery);
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("");
    oHeaderList.add("hrm.HrmTestCentreMaster.testCentreCode");
    oHeaderList.add("hrm.HrmTestCentreMaster.testCentreName");
    oLovVO.setHeaderList(oHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    QueryRow oRow = null;
    Iterator oIt1 = oList1.iterator();
    while (oIt1.hasNext()) {
      if (count == 0)
        oList1 = new ArrayList(); 
      count++;
      oRow = oIt1.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("ID").getString());
      oLOVBean.setDetailField2(oRow.get("CODE").getString());
      oLOVBean.setDetailField3(oRow.get("NAME").getString());
      oList1.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList1);
    return oLovVO;
  }
  
  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();
    HrmRecTestCentreMstHdrBean oHrmRecTestCentreMstHdrBean = (HrmRecTestCentreMstHdrBean)oBaseHeaderBean;
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        checkUniqueHeader((HrmRecTestCentreMstHdrBean)oBaseHeaderBean);  
  }
  
  private void checkUniqueHeader(HrmRecTestCentreMstHdrBean oHrmRecTestCentreMstHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = String.valueOf(String.valueOf(" Select id as ID from hrm_tst_cntr_mst where CODE='").concat(String.valueOf(oHrmRecTestCentreMstHdrBean.getTestCentreCode()))).concat(String.valueOf("'"));
    System.out.println(sQuery);
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oList = oBean.executeQuery(sQuery);
    Iterator oIt = oList.iterator();
    if (oIt.hasNext()) {
      QueryRow oRow = oIt.next();
      if (!oRow.get("ID").getString().equalsIgnoreCase(oHrmRecTestCentreMstHdrBean.getHeaderPrimaryKey()))
        throw new EnrgiseApplicationException("hrm.advt.header.uniqueConstraintViolated", "M"); 
    } 
  }
  
  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();
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        checkMandatoryHeader((HrmRecTestCentreMstHdrBean)oBaseHeaderBean);  
  }
  
  private void checkMandatoryHeader(HrmRecTestCentreMstHdrBean oHrmRecTestCentreMstHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oList = new ArrayList();
    if (!EnrgiseUtil.checkString(oHrmRecTestCentreMstHdrBean.getTestCentreCode())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmTestCentreMaster.testCentreCode");
      ArrayList oParam = new ArrayList();
      oParam.add(oMessageKey);
      oList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParam, "E"));
    } 
    if (!EnrgiseUtil.checkString(oHrmRecTestCentreMstHdrBean.getAddress())) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmTestCentreMaster.address");
      ArrayList oParam = new ArrayList();
      oParam.add(oMessageKey);
      oList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParam, "E"));
    } 
    reportError(oList);
  }
}
