package wenrgise.ejb.common.business;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import wenrgise.common.bean.AccessBean;
import wenrgise.common.bean.DynamicMenuBean;
import wenrgise.common.bean.EmpInfoBean;
import wenrgise.common.bean.MenuBean;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.vo.BaseHeaderVO;
import wenrgise.ejb.common.helper.DBObject;
import wenrgise.ejb.common.helper.QueryRow;
import wenrgise.ejb.common.utility.DBUtilitiesBean;

public class SecurityBO {
  public HashMap getDisabledFields(String sScreenName, String sHdrDtlFlag, String sScreenMode, String sPageStatus) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    HashMap oMap = new HashMap();
    if (sHdrDtlFlag.equalsIgnoreCase("Header")) {
      oParameters.add(new DBObject(1, 1, 12, sScreenName));
      oParameters.add(new DBObject(2, 1, 12, sHdrDtlFlag));
      oParameters.add(new DBObject(3, 1, 12, sScreenMode));
      oParameters.add(new DBObject(4, 1, 12, sPageStatus));
      oParameters.add(new DBObject(5, 1, 12, "D"));
      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, "commonprocedures.proc_getfielddisableinfo(?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      ArrayList oList = (ArrayList)oOutObject.getObject();
      if (oList.size() != 0) {
        QueryRow oRow = null;
        Iterator oIt = oList.iterator();
        ArrayList arylstFields = new ArrayList();
        while (oIt.hasNext()) {
          oRow = oIt.next();
          arylstFields.add(oRow.get("disabled_field").getString());
        } 
        oMap.put("D", arylstFields);
      } 
    } else if (sHdrDtlFlag.equalsIgnoreCase("Detail")) {
      oParameters.add(new DBObject(1, 1, 12, sScreenName));
      oParameters.add(new DBObject(2, 1, 12, sHdrDtlFlag));
      oParameters.add(new DBObject(3, 1, 12, sScreenMode));
      oParameters.add(new DBObject(4, 1, 12, sPageStatus));
      oParameters.add(new DBObject(5, 1, 12, "D"));
      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, "commonprocedures.proc_getfielddisableinfo(?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      ArrayList oList = (ArrayList)oOutObject.getObject();
      if (oList.size() != 0) {
        QueryRow oRow = null;
        Iterator oIt = oList.iterator();
        ArrayList disableList = new ArrayList();
        while (oIt.hasNext()) {
          oRow = oIt.next();
          disableList.add(oRow.get("disabled_field").getString());
        } 
        oMap.put("D", disableList);
      } 
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, sScreenName));
      oParameters.add(new DBObject(2, 1, 12, sHdrDtlFlag));
      oParameters.add(new DBObject(3, 1, 12, sScreenMode));
      oParameters.add(new DBObject(4, 1, 12, sPageStatus));
      oParameters.add(new DBObject(5, 1, 12, "E"));
      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));
      oOutArray = oBean.callProc(oParameters, "commonprocedures.proc_getfielddisableinfo(?,?,?,?,?,?,?,?,?)");
      oOutObject = oOutArray.get(0);
      oList = (ArrayList)oOutObject.getObject();
      if (oList.size() != 0) {
        QueryRow oRow = null;
        Iterator oIt = oList.iterator();
        ArrayList enableList = new ArrayList();
        while (oIt.hasNext()) {
          oRow = oIt.next();
          enableList.add(oRow.get("disabled_field").getString());
        } 
        oMap.put("E", enableList);
      } 
    } else if (sHdrDtlFlag.equalsIgnoreCase("all")) {
      oParameters.add(new DBObject(1, 1, 12, sScreenName));
      oParameters.add(new DBObject(2, 1, 12, sHdrDtlFlag));
      oParameters.add(new DBObject(3, 1, 12, sScreenMode));
      oParameters.add(new DBObject(4, 1, 12, sPageStatus));
      oParameters.add(new DBObject(5, 1, 12, "D"));
      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, "commonprocedures.proc_getfielddisableinfo(?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      ArrayList oList = (ArrayList)oOutObject.getObject();
      if (oList.size() != 0) {
        QueryRow oRow = null;
        Iterator oIt = oList.iterator();
        ArrayList arylstFields = new ArrayList();
        while (oIt.hasNext()) {
          oRow = oIt.next();
          arylstFields.add(oRow.get("disabled_field").getString());
        } 
        oMap.put("D", arylstFields);
      } 
    } 
    return oMap;
  }
  
  private String getGroupIdForUser(String sUserId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    ArrayList oList = null;
    String sGroupId = null;
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    boolean returnFlag = true;
    boolean tempFlag = true;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sUserId));
    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, "DYNAMIC_MENU.PROC_GetGroupId(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    sGroupId = (String)oOutObject.getObject();
    return sGroupId;
  }
  
  public ArrayList addMenuList(EmpInfoBean oEmpInfoBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList menuList = new ArrayList();
    ArrayList finalList = new ArrayList();
    MenuBean oMenuBean = new MenuBean();
    oEmpInfoBean = getGroupId(oEmpInfoBean);
    oMenuBean = getComponentIds(oEmpInfoBean, menuList);
    Iterator oIt = menuList.iterator();
    while (oIt.hasNext()) {
      DynamicMenuBean dynamicMenuBean = oIt.next();
      finalList.add(new DynamicMenuBean(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf("MyMenu.makeMenu('").concat(String.valueOf(dynamicMenuBean.getCompId()))).concat(String.valueOf("','"))).concat(String.valueOf(dynamicMenuBean.getModuleId()))).concat(String.valueOf("','"))).concat(String.valueOf(dynamicMenuBean.getCompDesc()))).concat(String.valueOf("','"))).concat(String.valueOf(dynamicMenuBean.getActionName()))).concat(String.valueOf("?screenName="))).concat(String.valueOf(dynamicMenuBean.getScreenName()))).concat(String.valueOf("')"))));
    } 
    if (oMenuBean.getModuleId() == null)
      return null; 
    DynamicMenuBean oBean = new DynamicMenuBean();
    do {
      oMenuBean = getModuleIds(oMenuBean, oBean, menuList);
      Iterator oIt1 = menuList.iterator();
      while (oIt1.hasNext()) {
        oBean = oIt1.next();
        if (oBean.getModuleId() == oEmpInfoBean.getModuleId())
          continue; 
        finalList.add(new DynamicMenuBean(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf("MyMenu.makeMenu('").concat(String.valueOf(oBean.getModuleId()))).concat(String.valueOf("','"))).concat(String.valueOf(oBean.getParentModuleId()))).concat(String.valueOf("','"))).concat(String.valueOf(oBean.getModuleDesc()))).concat(String.valueOf("')"))));
      } 
    } while (oMenuBean != null);
    return finalList;
  }
  
  public EmpInfoBean getGroupId(EmpInfoBean oEmpInfoBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, oEmpInfoBean.getEmpId()));
    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, "DYNAMIC_MENU.PROC_GetGroupId(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    int count = 0;
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    StringBuffer concatList = new StringBuffer();
    int c = 0;
    while (oIt.hasNext()) {
      if (count == 0)
        oList = new ArrayList(); 
      count++;
      oRow = oIt.next();
      oEmpInfoBean.setGrpId(oRow.get("GROUP_ID").getString());
      if (c == 0) {
        concatList = concatList.append(oEmpInfoBean.getGrpId());
        c++;
        continue;
      } 
      concatList = concatList.append(String.valueOf(",").concat(String.valueOf(oEmpInfoBean.getGrpId())));
    } 
    String sGrpIds = null;
    if (c != 0)
      sGrpIds = concatList.toString(); 
    oEmpInfoBean.setGrpId(sGrpIds);
    return oEmpInfoBean;
  }
  
  public MenuBean getModuleIds(MenuBean oBean, DynamicMenuBean oMenuBean, ArrayList oMenuList) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oDBBean = new DBUtilitiesBean();
    oMenuList.clear();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, oBean.getModuleId()));
    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 = oDBBean.callProc(oParameters, "DYNAMIC_MENU.PROC_ModuleInfo(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() == 0)
      return null; 
    int count = 0;
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    StringBuffer concatList = new StringBuffer();
    int c = 0;
    while (oIt.hasNext()) {
      oMenuBean = new DynamicMenuBean();
      if (count == 0)
        oList = new ArrayList(); 
      count++;
      oRow = oIt.next();
      oMenuBean.setModuleId(oRow.get("module_id").getString());
      oMenuBean.setParentModuleId(oRow.get("parent_module_id").getString());
      oMenuBean.setModuleCode(oRow.get("module_code").getString());
      oMenuBean.setModuleDesc(oRow.get("module_description").getString());
      oMenuBean.setHierchyLevel(oRow.get("hierarchy_lvl").getString());
      oMenuList.add(oMenuBean);
      if (c == 0) {
        concatList = concatList.append(oMenuBean.getModuleId());
        c++;
        continue;
      } 
      concatList = concatList.append(String.valueOf(",").concat(String.valueOf(oMenuBean.getModuleId())));
    } 
    String sModuleId = null;
    if (c != 0)
      sModuleId = concatList.toString(); 
    oBean.setModuleId(sModuleId);
    return oBean;
  }
  
  public MenuBean getComponentIds(EmpInfoBean oEmpInfoBean, ArrayList oMenuList) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, oEmpInfoBean.getEmpId()));
    oParameters.add(new DBObject(2, 1, 12, oEmpInfoBean.getGrpId()));
    oParameters.add(new DBObject(3, 1, 12, oEmpInfoBean.getModuleId()));
    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));
    ArrayList oOutArray = oBean.callProc(oParameters, "DYNAMIC_MENU.PROC_GetCompInfo(?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    int count = 0;
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    StringBuffer concatList = new StringBuffer();
    int c = 0;
    while (oIt.hasNext()) {
      DynamicMenuBean oMenuBean = new DynamicMenuBean();
      if (count == 0)
        oList = new ArrayList(); 
      count++;
      oRow = oIt.next();
      oMenuBean.setCompId(oRow.get("component_id").getString());
      oMenuBean.setCompCode(oRow.get("component_code").getString());
      oMenuBean.setCompDesc(oRow.get("description").getString());
      oMenuBean.setActionName(oRow.get("action_name").getString());
      oMenuBean.setScreenName(oRow.get("screen_name").getString());
      oMenuBean.setModuleId(oRow.get("module_id").getString());
      oMenuList.add(oMenuBean);
      if (c == 0) {
        concatList = concatList.append(oMenuBean.getCompId());
        c++;
        continue;
      } 
      concatList = concatList.append(String.valueOf(",").concat(String.valueOf(oMenuBean.getCompId())));
    } 
    String sCompId = null;
    if (c != 0)
      sCompId = concatList.toString(); 
    MenuBean oBean1 = new MenuBean();
    oBean1.setModuleId(sCompId);
    return oBean1;
  }
  
  public HashMap getAccessInfo(EmpInfoBean oEmpInfoBean) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    HashMap mapInfo = new HashMap();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, oEmpInfoBean.getEmpId()));
    oParameters.add(new DBObject(2, 1, 12, "null"));
    oParameters.add(new DBObject(3, 1, 12, oEmpInfoBean.getModuleId()));
    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));
    ArrayList oOutArray = oBean.callProc(oParameters, "DYNAMIC_MENU.PROC_GetCompInfo(?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      oRow = oIt.next();
      String sComponentName = oRow.get("Component_Code").getString();
      AccessBean oAccessBean = new AccessBean();
      oAccessBean.setApproveFlag(oRow.get("APPROVFLAG").getString());
      oAccessBean.setDeleteFlag(oRow.get("DELFLAG").getString());
      oAccessBean.setInsertFlag(oRow.get("INSERTFLAG").getString());
      oAccessBean.setQueryFlag(oRow.get("QRYFLAG").getString());
      oAccessBean.setUpdateFlag(oRow.get("UPDATEFLAG").getString());
      mapInfo.put(sComponentName.trim(), oAccessBean);
    } 
    oEmpInfoBean = getGroupId(oEmpInfoBean);
    HashMap oHshMap = getAccessInfoFromGroup(oEmpInfoBean, mapInfo);
    return oHshMap;
  }
  
  private HashMap getAccessInfoFromGroup(EmpInfoBean oEmpInfoBean, HashMap oMap) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, null));
    oParameters.add(new DBObject(2, 1, 12, oEmpInfoBean.getGrpId()));
    oParameters.add(new DBObject(3, 1, 12, oEmpInfoBean.getModuleId()));
    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));
    ArrayList oOutArray = oBean.callProc(oParameters, "DYNAMIC_MENU.PROC_GetCompInfo(?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      oRow = oIt.next();
      String sComponentName = oRow.get("Component_Code").getString();
      AccessBean oAccessBean = new AccessBean();
      oAccessBean.setApproveFlag(oRow.get("APPROVFLAG").getString());
      oAccessBean.setDeleteFlag(oRow.get("DELFLAG").getString());
      oAccessBean.setInsertFlag(oRow.get("INSERTFLAG").getString());
      oAccessBean.setQueryFlag(oRow.get("QRYFLAG").getString());
      oAccessBean.setUpdateFlag(oRow.get("UPDATEFLAG").getString());
      oMap.put(sComponentName.trim(), oAccessBean);
    } 
    return oMap;
  }
}
