package wenrgise.workflow.utility;

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.UserInfo;
import wenrgise.common.vo.BaseHeaderVO;
import wenrgise.ejb.common.helper.DBObject;
import wenrgise.ejb.common.helper.QueryRow;
import wenrgise.ejb.common.utility.DBUtilitiesBean;
import wenrgise.workflow.bean.WflDocMstAttrDtlBean;
import wenrgise.workflow.core.WflDepartment;
import wenrgise.workflow.core.WflDocumentInfo;
import wenrgise.workflow.core.impl.WflDepartmentImpl;
import wenrgise.workflow.core.impl.WflResourceImpl;
import wenrgise.workflow.ejb.business.WflDocMstBO;

public class RouteHandler {
  public static final Logger log = Logger.getLogger("wenrgise.workflow.utility.RouteHandler");
  
  private Node nodeInfo;
  
  private String routeDtlId;
  
  private String activityId;
  
  private String dtlDocId;
  
  private String notifFlag;
  
  public Node getNodeInfo() {
    return this.nodeInfo;
  }
  
  public void setNodeInfo(Node newNodeInfo) {
    this.nodeInfo = newNodeInfo;
  }
  
  public String getRouteDtlId() {
    return this.routeDtlId;
  }
  
  public void setRouteDtlId(String newRouteDtlId) {
    this.routeDtlId = newRouteDtlId;
  }
  
  public String getActivityId() {
    return this.activityId;
  }
  
  public void setActivityId(String newActivityId) {
    this.activityId = newActivityId;
  }
  
  public String getDtlDocId() {
    return this.dtlDocId;
  }
  
  public void setDtlDocId(String newDtlDocId) {
    this.dtlDocId = newDtlDocId;
  }
  
  public String getNotifFlag() {
    return this.notifFlag;
  }
  
  public void setNotifFlag(String newNotifFlag) {
    this.notifFlag = newNotifFlag;
  }
  
  public ArrayList findNextApprover(WflDocumentInfo wflDocInfo) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    oParameters = new ArrayList();
    log.severe(String.valueOf("Route Id ").concat(String.valueOf(getRouteDtlId())));
    oParameters.add(new DBObject(1, 1, 12, getRouteDtlId()));
    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_GetNextLevelConditions(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    String condtitionExpr = null;
    String conditionValue = null;
    String condtitionId = null;
    QueryRow oRow = null;
    Iterator oIt = oList.iterator();
    if (oIt.hasNext()) {
      WflDocMstBO oWflDocMstBO = new WflDocMstBO();
      UserInfo oUserInfo = new UserInfo();
      oUserInfo.setUserId(wflDocInfo.getRequester().getUserId());
      oUserInfo.setSiteId(wflDocInfo.getRequester().getLoginSite().getSiteId());
      oUserInfo.setUserSystemId(wflDocInfo.getRequester().getEmployeeId());
      oUserInfo.setSiteName(wflDocInfo.getRequester().getLoginSite().getSiteName());
      oWflDocMstBO.setOUserInfo(oUserInfo);
      long oDocAttrCount = oWflDocMstBO.getWflDocTypeAttrDtlMetaInfo(wflDocInfo.getDocumentId()).getRecordCount();
      ArrayList arylstDocType = oWflDocMstBO.getWflDocTypeAttrDtlInfo(wflDocInfo.getDocumentId(), 1L, oDocAttrCount);
      Iterator oIt1 = arylstDocType.iterator();
      HashMap oDocAttrTypeMap = new HashMap();
      while (oIt1.hasNext()) {
        WflDocMstAttrDtlBean oWflDocMstAttrDtlBean = oIt1.next();
        oDocAttrTypeMap.put(oWflDocMstAttrDtlBean.getTxtAttrName(), oWflDocMstAttrDtlBean.getTxtType());
      } 
      oRow = oIt.next();
      condtitionId = oRow.get("conditionId").getString();
      condtitionExpr = oRow.get("conditionExpr").getString();
      ConditionChecker cc = new ConditionChecker();
      conditionValue = String.valueOf(cc.checkCondition(condtitionExpr, wflDocInfo.getDocumentAttributes(), oDocAttrTypeMap));
    } 
    ArrayList oResourceList = new ArrayList();
    String activityCode = WorkFlowUtil.getActivityCode(wflDocInfo.getRequesterAction());
    if (!activityCode.equalsIgnoreCase("Reject")) {
      log.severe(String.valueOf("Approve ").concat(String.valueOf(getRouteDtlId())));
      log.severe(String.valueOf("Approve Cond ").concat(String.valueOf(condtitionId)));
      log.severe(String.valueOf("Approve Cond value ").concat(String.valueOf(conditionValue)));
      oOutArray = new ArrayList();
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, getRouteDtlId()));
      oParameters.add(new DBObject(2, 1, 12, condtitionId));
      oParameters.add(new DBObject(3, 1, 12, conditionValue));
      oParameters.add(new DBObject(4, 2, -10));
      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_GetNextLevelRouteDtls(?,?,?,?,?,?,?)");
      oList = new ArrayList();
      oOutObject = new DBObject();
      oOutObject = oOutArray.get(0);
      oList = (ArrayList)oOutObject.getObject();
      oRow = null;
      Iterator oIt1 = oList.iterator();
      while (oIt1.hasNext()) {
        oRow = oIt1.next();
        String sToRouteDtlId = oRow.get("routeDtlId").getString();
        String splitJoinFlag = oRow.get("splitJoinFlag").getString();
        getNextLevelApprovers(wflDocInfo, splitJoinFlag, sToRouteDtlId, oResourceList);
      } 
    } else {
      oOutArray = new ArrayList();
      oParameters = new ArrayList();
      log.severe(String.valueOf("Reject ").concat(String.valueOf(getRouteDtlId())));
      log.severe(String.valueOf("Reject Cond ").concat(String.valueOf(condtitionId)));
      log.severe(String.valueOf("Reject Cond value ").concat(String.valueOf(conditionValue)));
      oParameters.add(new DBObject(1, 1, 12, getRouteDtlId()));
      oParameters.add(new DBObject(2, 1, 12, condtitionId));
      oParameters.add(new DBObject(3, 1, 12, conditionValue));
      oParameters.add(new DBObject(4, 2, -10));
      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_NextLevelRouteDtlsReject(?,?,?,?,?,?,?)");
      oList = new ArrayList();
      oOutObject = new DBObject();
      oOutObject = oOutArray.get(0);
      oList = (ArrayList)oOutObject.getObject();
      oRow = null;
      Iterator oIt1 = oList.iterator();
      while (oIt1.hasNext()) {
        oRow = oIt1.next();
        String sToRouteDtlId = oRow.get("routeDtlId").getString();
        String splitJoinFlag = oRow.get("splitJoinFlag").getString();
        getNextLevelApproversForReject(wflDocInfo, splitJoinFlag, sToRouteDtlId, oResourceList);
      } 
    } 
    return oResourceList;
  }
  
  public ArrayList getNextLevelApproversForReject(WflDocumentInfo wflDocInfo, String splitJoinFlag, String sToRouteDtlId, ArrayList oResourceList) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oOutArray = new ArrayList();
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters.add(new DBObject(1, 1, 12, sToRouteDtlId));
    oParameters.add(new DBObject(2, 1, 12, splitJoinFlag));
    oParameters.add(new DBObject(3, 1, 12, wflDocInfo.getWorkListId()));
    oParameters.add(new DBObject(4, 2, -10));
    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_NextLevelApproversReject(?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList != null) {
      QueryRow oRow = null;
      Iterator oIt = oList.iterator();
      while (oIt.hasNext()) {
        oRow = oIt.next();
        WflResourceImpl wflResourceImpl = new WflResourceImpl();
        WflDepartmentImpl wflDepartmentImpl = new WflDepartmentImpl();
        wflDepartmentImpl.setDepartmentId(oRow.get("departmentId").getString());
        wflDepartmentImpl.setDepartmentName(oRow.get("departmentName").getString());
        wflResourceImpl.setDepartment((WflDepartment)wflDepartmentImpl);
        wflResourceImpl.setEmployeeId(oRow.get("empId").getString());
        wflResourceImpl.setEmpNo(oRow.get("empNo").getString());
        wflResourceImpl.setRouteDtlId(sToRouteDtlId);
        oResourceList.add(wflResourceImpl);
      } 
    } 
    return oResourceList;
  }
  
  public ArrayList getNextLevelApprovers(WflDocumentInfo wflDocInfo, String splitJoinFlag, String sToRouteDtlId, ArrayList oResourceList) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oOutArray = new ArrayList();
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    log.severe(String.valueOf("Route Id ").concat(String.valueOf(sToRouteDtlId)));
    log.severe(String.valueOf("Split Join Flag ").concat(String.valueOf(splitJoinFlag)));
    log.severe(String.valueOf("Requester Emp Id ").concat(String.valueOf(wflDocInfo.getRequester().getEmployeeId())));
    oParameters.add(new DBObject(1, 1, 12, sToRouteDtlId));
    oParameters.add(new DBObject(2, 1, 12, splitJoinFlag));
    oParameters.add(new DBObject(3, 1, 12, wflDocInfo.getRequester().getEmployeeId()));
    oParameters.add(new DBObject(4, 2, -10));
    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_GetNextLevelApprovers(?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList != null) {
      QueryRow oRow = null;
      Iterator oIt = oList.iterator();
      while (oIt.hasNext()) {
        oRow = oIt.next();
        WflResourceImpl wflResourceImpl = new WflResourceImpl();
        WflDepartmentImpl wflDepartmentImpl = new WflDepartmentImpl();
        wflDepartmentImpl.setDepartmentId(oRow.get("departmentId").getString());
        wflDepartmentImpl.setDepartmentName(oRow.get("departmentName").getString());
        wflResourceImpl.setDepartment((WflDepartment)wflDepartmentImpl);
        wflResourceImpl.setEmployeeId(oRow.get("empId").getString());
        wflResourceImpl.setEmpNo(oRow.get("empNo").getString());
        wflResourceImpl.setDelegatedBy(oRow.get("delegatedBy").getString());
        wflResourceImpl.setRouteDtlId(sToRouteDtlId);
        oResourceList.add(wflResourceImpl);
      } 
    } 
    return oResourceList;
  }
  
  public void populateData() throws EnrgiseSystemException, EnrgiseApplicationException {
    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, getRouteDtlId()));
    oParameters.add(new DBObject(2, 1, 12, getDtlDocId()));
    oParameters.add(new DBObject(3, 1, 12, getActivityId()));
    Node nodeInfo = getNodeInfo();
    if (nodeInfo != null) {
      oParameters.add(new DBObject(4, 1, 12, nodeInfo.getLane()));
      oParameters.add(new DBObject(5, 1, 12, nodeInfo.getLevel()));
    } else {
      oParameters.add(new DBObject(4, 1, 12, null));
      oParameters.add(new DBObject(5, 1, 12, null));
    } 
    oParameters.add(new DBObject(6, 2, -10));
    oParameters.add(new DBObject(7, 2, 12));
    oParameters.add(new DBObject(8, 2, 12));
    oParameters.add(new DBObject(9, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "WFLWORKLIST.proc_GetRouteDtlInfo(?,?,?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() <= 0)
      throw new EnrgiseApplicationException("wenrgise.workflow.norecordfound", "M"); 
    QueryRow oRow = null;
    Iterator oIt = oList.iterator();
    if (oIt.hasNext()) {
      oRow = oIt.next();
      setRouteDtlId(oRow.get("routeDtlId").getString());
      setDtlDocId(oRow.get("dtlDocId").getString());
      setActivityId(oRow.get("activityId").getString());
      setNodeInfo(new Node(oRow.get("lane").getString(), oRow.get("level_number").getString()));
      setNotifFlag(oRow.get("notification_flag").getString());
    } 
  }
}
