已办查看接口封装

各个模块只需要添加一个业务类,继承IBusiEntityViewerProvider接口即可

image.png

具体实现方式

1.根据流程id获取业务对象的实体类(SWF_PROC_BUSI_ENTITY),SWF_PROC_BUSI_ENTITY表中entityId为实现类主键id,entityName为路径,procId为流程id

@RequestMapping({"/getViewerProvider"})
  @ResponseBody
  public OperResult getViewerProvider(String procId)
  {
    String webPageUrl = "";
    List list = this.wfProcBusiEntityService.getByProcId(procId);
    if ((list != null) && (list.size() > 0)) {
      WfProcBusiEntity entity = (WfProcBusiEntity)list.get(0);
      if (entity != null) {
        webPageUrl = CommonUtil.trim(EIPService.getBusiEntityService().getViewerUrl(entity.getDbYear(), entity.getEntityName(), entity.getEntityId()));//根据entityName获取具体url
      }
    }

    return OperResult.succeed("200", "ok", webPageUrl);
  }

根据具体的实体类名与所有实现类中获取的实体类名称是否一致,来确定具体执行哪个实现类

获取具体url

public String getViewerUrl(int budgetYear, String busiEntityClassName, Object busiEntityId) {
        String getViewerUrl =  busiEntityViewerService.getViewerUrl(budgetYear, busiEntityClassName, busiEntityId);
        return getViewerUrl;
    }

this.getProvider(busiEntityClassName)是具体的实现类

public String getViewerUrl(int budgetYear, String busiEntityClassName, Object busiEntityId) {
        busiEntityClassName = CommonUtil.trim(busiEntityClassName);
        if (busiEntityClassName.length() == 0) {
            EIPService.getLogService().error("busiEntityClassName is empty.");
            return null;
        }
        IBusiEntityViewerProvider provider = this.getProvider(busiEntityClassName);
        if (provider == null) {
            EIPService.getLogService().error("no IBusiEntityViewerProvider instance found for: " + busiEntityClassName);
            return null;
        }
        return provider.getViewerUrl(budgetYear, busiEntityId);
    }

viewerProviderBeans为缓存

private Map < String , IBusiEntityViewerProvider > viewerProviderBeans = null;
     
    public IBusiEntityViewerProvider getProvider(String providerId) {
        providerId = CommonUtil.trim(providerId);
        if (providerId.length() == 0) {
            EIPService.getLogService().error("providerId is null.");
            return null;
        } 
        
        if (viewerProviderBeans == null) getProviderBeans();
        
        if (!viewerProviderBeans.containsKey(providerId)) {
            //传过来的可能是实体类,也可能是domainObjectId,判断一下,试试看另一个是否存在?
            boolean isClassName = false;
            if (providerId.indexOf(".") > 0) {
                try {
                    Class < ? > clazz = Class.forName(providerId);
                    if (clazz != null) {
                        isClassName = true;
                    } else {
                        isClassName = false;
                    }
                } catch (ClassNotFoundException e) {
                    isClassName = false;
                }
            }
            
            if (!isClassName) {
                //providerId不是类名,那么它是domainObjectId吗?
                IDomainObject domainObject = EIPService.getModuleService().getDomainObject(providerId);
                if (domainObject != null) {
                    //说明providerId是一个domainObjectId,既然viewerProviderBeans中没有按domainObjectId标识的viewerProvider,那么试试看对应的主表类名
                    List < IDomainObjectTable > tables = domainObject.getTables();
                    if (tables != null) {
                        for (IDomainObjectTable table : tables) {
                            if (table.getRelationType() != IDomainObjectTable.RelationType.MASTER) continue;
                            
                            String masterClassName = CommonUtil.trim(table.getClassName());
                            if (viewerProviderBeans.containsKey(masterClassName)) {
                                //找到了根据主表类名标识的viewerProvider,那么加入map中
                                //这样viewerProviderBeans中就存在providerId标识的provider,下次就不用再提取一次了
                                IBusiEntityViewerProvider viewerProvider = viewerProviderBeans.get(masterClassName);
                                if (viewerProvider != null) viewerProviderBeans.put(providerId, viewerProvider); 
                            }
                        }
                    }
                }
            }
        }
        
        if (!viewerProviderBeans.containsKey(providerId)) {
            //throw new RuntimeException("no viewerProvider found for entityClass:" + entityName);
            EIPService.getLogService().error("no viewerProvider found for entityClass:" + providerId);
            return null;
        }
        
        IBusiEntityViewerProvider viewerProviderBean = viewerProviderBeans.get(providerId);
        return viewerProviderBean;
    }

获取所有实现类的bean,通过调用每个实现类中的getId()方法获取实体类的路径,用该路径与传过来的entityName比较来锁定具体的实现类

Map < String, IBusiEntityViewerProvider > beans = SpringContextHolder.getApplicationContext().getBeansOfType(IBusiEntityViewerProvider.class);//同个spring获取接口所有实现类的bean

private void getProviderBeans() {
        viewerProviderBeans = new ConcurrentHashMap();
        
        Map < String, IBusiEntityViewerProvider > beans = SpringContextHolder.getApplicationContext().getBeansOfType(IBusiEntityViewerProvider.class);
        
        Set < String > keys = beans.keySet();
        for (String key : keys) {
            IBusiEntityViewerProvider viewerProvider = beans.get(key);
            if (viewerProvider == null) continue;
            
            String providerId = CommonUtil.trim(viewerProvider.getId());
            //Class < ? > entityClass = viewerProvider.getEntityClass();
            if (providerId.length() == 0) {
                EIPService.getLogService().error("no entityClass is defined for bean:" + key + ":" + viewerProvider.getClass().getName());
                continue;
            }
            
            viewerProviderBeans.put(providerId, viewerProvider);
        }
    }

最后接口初始化为具体的实现类,调用获取url的方法,便可获得实际url

IBusiEntityViewerProvider provider = this.getProvider(busiEntityClassName);
        if (provider == null) {
            EIPService.getLogService().error("no IBusiEntityViewerProvider instance found for: " + busiEntityClassName);
            return null;
        }
        return provider.getViewerUrl(budgetYear, busiEntityId);

你可能感兴趣的:(已办查看接口封装)