package wenrgise.workflow.ejb.business;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Logger;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.UserInfo;
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.workflow.bean.WflDtlsHdrBean;
import wenrgise.workflow.bean.WflWorkListBean;
import wenrgise.workflow.core.WflDocumentInfo;
import wenrgise.workflow.core.WflResource;

public class WorkFlowCommonBO extends WorkFlowBaseBO {
  public static final Logger log = Logger.getLogger("wenrgise.workflow.ejb.business.WorkFlowCommonBO");
  
  public String getMailAddress(String sEmpId) {
    String sMail = "kolkata.confroom@tcs.com";
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    try {
      if (EnrgiseUtil.checkString(sEmpId) == false) {
        log.severe("Employee Id is null");
        return sMail;
      } 
      ArrayList oList = oBean.executeQuery(String.valueOf("SELECT email FROM HRM_EMP_PERS WHERE id = ").concat(String.valueOf(sEmpId)));
      if (null != oList && oList.size() > 0) {
        QueryRow oRow = oList.get(0);
        sMail = oRow.get("EMAIL").getString();
        log.severe(String.valueOf("E mail id is ").concat(String.valueOf(sMail)));
      } 
      return sMail;
    } catch (EnrgiseSystemException oSx) {
      log.severe(String.valueOf(String.valueOf(oSx.getClass().getName()).concat(String.valueOf(" "))).concat(String.valueOf(oSx.getMessage())));
      return sMail;
    } 
  }
  
  public UserInfo getLoginUserInfo(String sEmpNo, String sSiteId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    ArrayList arylstOutArray = new ArrayList();
    UserInfo oUserInfo = null;
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList oList = null;
    oBean = new DBUtilitiesBean();
    arylstParameters = new ArrayList();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, 12, sEmpNo));
    arylstParameters.add(new DBObject(2, 1, 12, sSiteId));
    arylstParameters.add(new DBObject(3, 2, -10));
    arylstParameters.add(new DBObject(4, 2, 12));
    arylstParameters.add(new DBObject(5, 2, 12));
    arylstParameters.add(new DBObject(6, 2, 4));
    arylstOutArray = oBean.callProc(arylstParameters, "HRMUSERINFO.procGetUserInfo(?,?,?,?,?,?)");
    oList = (ArrayList)((DBObject)arylstOutArray.get(0)).getObject();
    if (oList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      oRow = oIt.next();
      oUserInfo = new UserInfo();
      System.out.println(oRow.get("CURRENT_YEAR").getString());
      oUserInfo.setCurrentYear(oRow.get("CURRENT_YEAR").getString());
      System.out.println(oRow.get("SITE_ID").getString());
      oUserInfo.setSiteId(oRow.get("SITE_ID").getString());
      oUserInfo.setSiteName(oRow.get("SITE_NAME").getString());
      oUserInfo.setUserActiveFlag(oRow.get("USER_ACTIVE_FLAG").getString());
      oUserInfo.setUserId(oRow.get("USER_ID").getString());
      oUserInfo.setUserLocked(oRow.get("USER_LOCKED").getString());
      oUserInfo.setUserName(oRow.get("USER_NAME").getString());
      oUserInfo.setUserSystemId(oRow.get("USER_SYSTEM_ID").getString());
      oUserInfo.setUserType(oRow.get("USER_TYPE").getString());
      oUserInfo.setUserTypeId(oRow.get("USER_TYPE_ID").getString());
    } 
    return oUserInfo;
  }
  
  public String updateWorkList(String sUpsertFlag, WflResource wflResource, WflDocumentInfo docInfo) throws EnrgiseApplicationException, EnrgiseSystemException {
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oParameters = null;
    ArrayList oOutArray = null;
    String returnString = null;
    if (wflResource.getEmployeeId() == wflResource.getDelegatedBy())
      wflResource.setDelegatedBy(""); 
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sUpsertFlag));
    oParameters.add(new DBObject(2, 1, 12, docInfo.getWorkListId()));
    oParameters.add(new DBObject(3, 1, 12, wflResource.getRouteDtlId()));
    oParameters.add(new DBObject(4, 1, 12, docInfo.getDocumentId()));
    oParameters.add(new DBObject(5, 1, 12, docInfo.getDocumentTypeId()));
    oParameters.add(new DBObject(6, 1, 12, docInfo.getDocumentNumber()));
    oParameters.add(new DBObject(7, 1, 12, docInfo.getDocDesc()));
    oParameters.add(new DBObject(8, 1, 12, wflResource.getEmployeeId()));
    oParameters.add(new DBObject(9, 1, 12, docInfo.getRequester().getEmployeeId()));
    oParameters.add(new DBObject(10, 1, 12, wflResource.getDelegatedBy()));
    oParameters.add(new DBObject(11, 1, 12, null));
    oParameters.add(new DBObject(12, 1, 12, docInfo.getCreator().getEmployeeId()));
    oParameters.add(new DBObject(13, 1, 12, docInfo.getModuleId()));
    oParameters.add(new DBObject(14, 1, 12, docInfo.getRequesterAction()));
    oParameters.add(new DBObject(15, 1, 12, docInfo.getCreator().getEmployeeId()));
    oParameters.add(new DBObject(16, 1, 12, docInfo.getCreator().getLoginSite().getSiteId()));
    oParameters.add(new DBObject(17, 2, 12));
    oParameters.add(new DBObject(18, 2, 12));
    oParameters.add(new DBObject(19, 2, 12));
    oParameters.add(new DBObject(20, 2, 4));
    oOutArray = oBean.callProc(oParameters, "WFLWORKLIST.proc_UpsertWorkItem(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    returnString = (String)oOutObject.getObject();
    return returnString;
  }
  
  public ArrayList getWorkListInfo(String sEmpId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    ArrayList oHeaderList = new ArrayList();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sEmpId));
    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, "WFLWORKLIST.proc_GetWorkFlowList(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() == 0)
      return oHeaderList; 
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      count++;
      oRow = oIt.next();
      WflWorkListBean oWrkFlowCommonBean = new WflWorkListBean();
      oWrkFlowCommonBean.setTxtAssignedOn(oRow.get("LAST_ASSIGNED_DATE").getString());
      oWrkFlowCommonBean.setRouteDtlId(oRow.get("route_dtl_id").getString());
      oWrkFlowCommonBean.setTxtDocNo(oRow.get("doc_number").getString());
      oWrkFlowCommonBean.setTxtDocTypeId(oRow.get("doc_type_id").getString());
      oWrkFlowCommonBean.setTxtDocId(oRow.get("DOC_ID").getString());
      oWrkFlowCommonBean.setTxtHiddenId(oRow.get("ID").getString());
      oWrkFlowCommonBean.setTxtEmpNo(oRow.get("pending_for_emp_id").getString());
      oWrkFlowCommonBean.setTxtModuleId(oRow.get("MODULE_ID").getString());
      oWrkFlowCommonBean.setTxtModuleCode(oRow.get("MODULE_CODE").getString());
      oWrkFlowCommonBean.setTxtDocTypeCode(oRow.get("doc_type_code").getString());
      oHeaderList.add(oWrkFlowCommonBean);
    } 
    return oHeaderList;
  }
  
  public String getDocumentTypeId(String documentType) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, documentType));
    oParameters.add(new DBObject(2, 2, 12));
    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, "WFLWORKLIST.proc_GetDocumentTypeId(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    if (null != oOutObject)
      return (String)oOutObject.getObject(); 
    return null;
  }
  
  public String getActivityId(String activity) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, activity));
    oParameters.add(new DBObject(2, 2, 12));
    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, "WFLWORKLIST.proc_GetActivityId(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    if (null != oOutObject)
      return (String)oOutObject.getObject(); 
    return null;
  }
  
  public String getActivityCode(String activityId) throws EnrgiseApplicationException, EnrgiseSystemException {
    try {
      log.severe(activityId);
      ArrayList oParameters = new ArrayList();
      DBUtilitiesBean oBean = new DBUtilitiesBean();
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, activityId));
      oParameters.add(new DBObject(2, 2, 12));
      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, "WFLWORKLIST.proc_GetActivityCode(?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      if (null != oOutObject)
        return (String)oOutObject.getObject(); 
      return null;
    } catch (Exception oEx) {
      log.severe(String.valueOf(String.valueOf(oEx.getClass().getName()).concat(String.valueOf(" "))).concat(String.valueOf(oEx.getMessage())));
      return null;
    } 
  }
  
  public String getRouteDtlId(WflDocumentInfo docInfo) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, docInfo.getWorkListId()));
    oParameters.add(new DBObject(2, 2, 12));
    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, "WFLWORKLIST.proc_GetRouteDtl(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    if (null != oOutObject)
      return (String)oOutObject.getObject(); 
    return null;
  }
  
  public WflDtlsHdrBean getWorkFlowDtlsInfo(String sWorkListId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    WflDtlsHdrBean oWflDtlsHdrBean = new WflDtlsHdrBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    ArrayList oHeaderList = new ArrayList();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sWorkListId));
    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, "WFLWORKLIST.proc_GetWorkFlowListDesc(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() == 0)
      new EnrgiseApplicationException("wenrgise.common.norecordfound", "M"); 
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      count++;
      oRow = oIt.next();
      oWflDtlsHdrBean = new WflDtlsHdrBean();
      oWflDtlsHdrBean.setToDtls(oRow.get("EmpName").getString());
      oWflDtlsHdrBean.setFromDtls(oRow.get("From_EmpName").getString());
      oWflDtlsHdrBean.setSubjectDtls(oRow.get("subject").getString());
      oWflDtlsHdrBean.setDescDtls(oRow.get("docDesc").getString());
      oWflDtlsHdrBean.setNotifFlag(oRow.get("notificationFlag").getString());
      oWflDtlsHdrBean.setModuleId(oRow.get("moduleId").getString());
      oWflDtlsHdrBean.setModuleName(oRow.get("ModuleName").getString());
      oHeaderList.add(oWflDtlsHdrBean);
    } 
    return oWflDtlsHdrBean;
  }
  
  public String closeNotification(WflDtlsHdrBean oWflDtlsHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oParameters = null;
    ArrayList oOutArray = null;
    String returnString = null;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, "U"));
    oParameters.add(new DBObject(2, 1, 12, oWflDtlsHdrBean.getWorkListId()));
    oParameters.add(new DBObject(3, 1, 12, "Close"));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 12));
    oParameters.add(new DBObject(7, 2, 4));
    oOutArray = oBean.callProc(oParameters, "WFLWORKLIST.proc_closeWorkList(?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    returnString = (String)oOutObject.getObject();
    return returnString;
  }
}
