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.EnrgiseSystemException;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.RecordMetaInfo;
import wenrgise.common.utility.UserInfo;
import wenrgise.common.vo.BaseDetailVO;
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.vo.HrmTrngCalCloseQVO;

public class HrmTrngCalCloseBO extends HrmBaseBO {
  public HrmTrngCalCloseBO() {}
  
  public HrmTrngCalCloseBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public void initializeBOImpl() {
    this.headerTable = "PUM_PO_HDR";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    System.out.println("Save Ille!");
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("Update ille!");
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl ille!";
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String param1) throws EnrgiseSystemException {
    return "saveHeaderImpl toBeDone";
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public void additionalBusinessValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public RecordMetaInfo getHrmTrngCalCloseHdrMetaInfo(HrmTrngCalCloseQVO oHrmTrngCalCloseQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    return null;
  }
  
  public ArrayList getHrmTrngCalCloseHdrInfo(HrmTrngCalCloseQVO oHrmTrngCalCloseQVO, long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    return null;
  }
  
  public LovVO getHrmCalCloseCrsNameLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("hrm.HrmTrngCalClose.calendarCode");
    arylstHeaderList.add("hrm.HrmTrngCalClose.courseId");
    arylstHeaderList.add("hrm.HrmTrngCalClose.calendarName");
    arylstHeaderList.add("hrm.HrmTrngCalClose.courseName");
    arylstHeaderList.add("hrm.HrmTrngCalClose.institutionName");
    arylstHeaderList.add("hrm.HrmTrngCalClose.startDate");
    arylstHeaderList.add("hrm.HrmTrngCalClose.endDate");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 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, "HRMTRNGCALCLOSE.proc_GetHrmTrngCalCloseLOVdata(?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstList.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        arylstList = new ArrayList(); 
      count++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("CAL_HDR_ID").getString());
      oLOVBean.setDetailField2(oRow.get("CAL_DTL_ID").getString());
      oLOVBean.setDetailField3(oRow.get("CAL_NAME").getString());
      oLOVBean.setDetailField4(oRow.get("CRS_NAME").getString());
      oLOVBean.setDetailField5(oRow.get("INST_NAME").getString());
      oLOVBean.setDetailField6(EnrgiseUtil.convertToString(oRow.get("START_DATE").getDate()));
      oLOVBean.setDetailField7(EnrgiseUtil.convertToString(oRow.get("END_DATE").getDate()));
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public void calendarClose(String sCalId) throws EnrgiseSystemException, EnrgiseApplicationException {
    processEmployeeRating(sCalId);
    processFacultyRating(sCalId);
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    QueryRow oRow = null;
    ArrayList arylstParameters1 = new ArrayList();
    arylstParameters1.add(new DBObject(1, 1, 12, "U"));
    arylstParameters1.add(new DBObject(2, 1, 12, sCalId));
    arylstParameters1.add(new DBObject(3, 1, 12, "100"));
    arylstParameters1.add(new DBObject(4, 1, 12, "100"));
    arylstParameters1.add(new DBObject(5, 2, 12));
    arylstParameters1.add(new DBObject(6, 2, 12));
    arylstParameters1.add(new DBObject(7, 2, 4));
    oBean.callProc(arylstParameters1, "HRMTRNGCALCLOSE.proc_UpsertTrngCalClose(?,?,?,?,?,?,?)");
  }
  
  public void processFacultyRating(String sCalId) throws EnrgiseSystemException, EnrgiseApplicationException {
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList arylstParameters = new ArrayList();
    QueryRow oRow = null;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList arylstHrmTrngCalDetail = null;
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sCalId));
    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, "HRMTRNGCALCLOSE.proc_GetTrngCalCloseFacFdbk(?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    if (arylstList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    HashMap mapFacRating = new HashMap();
    HashMap mapFacCount = new HashMap();
    String sRating = null;
    String sCount = null;
    Iterator itrBean = arylstList.iterator();
    while (itrBean.hasNext()) {
      oRow = itrBean.next();
      String sEmp = oRow.get("EID").getString();
      sRating = oRow.get("RATING").getString();
      if (sRating == null)
        sRating = "0"; 
      if (mapFacRating.containsKey(sEmp)) {
        String sOldRating = (String)mapFacRating.get(sEmp);
        String sOldCount = (String)mapFacCount.get(sEmp);
        double dRating = Double.parseDouble(sOldRating) + Double.parseDouble(sRating);
        int i = Integer.parseInt(sOldCount) + 1;
        mapFacRating.put(sEmp, String.valueOf(dRating));
        mapFacCount.put(sEmp, String.valueOf(i));
        continue;
      } 
      mapFacRating.put(sEmp, sRating);
      mapFacCount.put(sEmp, String.valueOf(1));
    } 
    DBUtilitiesBean oBean1 = new DBUtilitiesBean();
    oBean1.createBatch("HRMTRNGCALCLOSE.proc_UpsertTrngCalFacFdbk(?,?,?,?,?)");
    Object[] sKey = mapFacRating.keySet().toArray();
    int iCount = 0;
    while (iCount < mapFacRating.size()) {
      ArrayList arylstParameters1 = new ArrayList();
      arylstParameters1.add(new DBObject(1, 1, 12, "U"));
      String sId = sKey[iCount].toString();
      arylstParameters1.add(new DBObject(2, 1, 12, sId));
      String sFinalRating = (String)mapFacRating.get(sKey[iCount]);
      String sFinalCount = (String)mapFacCount.get(sKey[iCount]);
      double dFinalRating = Double.parseDouble(sFinalRating) / Integer.parseInt(sFinalCount);
      sFinalRating = String.valueOf(dFinalRating);
      System.out.println(String.valueOf(String.valueOf(String.valueOf("set ").concat(String.valueOf(sId))).concat(String.valueOf(" with  rating"))).concat(String.valueOf(sFinalRating)));
      arylstParameters1.add(new DBObject(3, 1, 8, sFinalRating));
      arylstParameters1.add(new DBObject(4, 1, 12, "100"));
      arylstParameters1.add(new DBObject(5, 1, 12, "100"));
      System.out.println(arylstParameters1);
      oBean1.addToBatch(arylstParameters1);
      iCount++;
    } 
    oBean1.executeBatch();
  }
  
  public void processEmployeeRating(String sCalId) throws EnrgiseSystemException, EnrgiseApplicationException {
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList arylstParameters = new ArrayList();
    QueryRow oRow = null;
    BaseDetailVO oBaseDetailVO = null;
    ArrayList arylstHrmTrngCalDetail = null;
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sCalId));
    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, "HRMTRNGCALCLOSE.proc_GetTrngCalCloseEmpFdbk(?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    if (arylstList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    HashMap mapFacRating = new HashMap();
    HashMap mapFacCount = new HashMap();
    String sRating = null;
    String sCount = null;
    Iterator itrBean = arylstList.iterator();
    while (itrBean.hasNext()) {
      oRow = itrBean.next();
      String sFac = oRow.get("FID").getString();
      sRating = oRow.get("RATING").getString();
      if (sRating == null)
        sRating = "0"; 
      if (mapFacRating.containsKey(sFac)) {
        String sOldRating = (String)mapFacRating.get(sFac);
        String sOldCount = (String)mapFacCount.get(sFac);
        double dRating = Double.parseDouble(sOldRating) + Double.parseDouble(sRating);
        int i = Integer.parseInt(sOldCount) + 1;
        mapFacRating.put(sFac, String.valueOf(dRating));
        mapFacCount.put(sFac, String.valueOf(i));
        continue;
      } 
      mapFacRating.put(sFac, sRating);
      mapFacCount.put(sFac, String.valueOf(1));
    } 
    DBUtilitiesBean oBean1 = new DBUtilitiesBean();
    oBean1.createBatch("HRMTRNGCALCLOSE.proc_UpsertTrngCalEmpFdbk(?,?,?,?,?)");
    Object[] sKey = mapFacRating.keySet().toArray();
    int iCount = 0;
    while (iCount < mapFacRating.size()) {
      ArrayList arylstParameters1 = new ArrayList();
      arylstParameters1.add(new DBObject(1, 1, 12, "U"));
      String sId = sKey[iCount].toString();
      arylstParameters1.add(new DBObject(2, 1, 12, sId));
      String sFinalRating = (String)mapFacRating.get(sKey[iCount]);
      String sFinalCount = (String)mapFacCount.get(sKey[iCount]);
      double dFinalRating = Double.parseDouble(sFinalRating) / Integer.parseInt(sFinalCount);
      sFinalRating = String.valueOf(dFinalRating);
      System.out.println(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf("set ").concat(String.valueOf(sId))).concat(String.valueOf(" cal= "))).concat(String.valueOf(sCalId))).concat(String.valueOf(" with  rating"))).concat(String.valueOf(sFinalRating)));
      arylstParameters1.add(new DBObject(3, 1, 12, sFinalRating));
      arylstParameters1.add(new DBObject(4, 1, 12, "100"));
      arylstParameters1.add(new DBObject(5, 1, 12, "100"));
      oBean1.addToBatch(arylstParameters1);
      iCount++;
    } 
    oBean1.executeBatch();
  }
}
