Activiti对接业务表实现自定义用户和用户组

声明:Activiti版本5.22.0。
Activiti在初始化时会生成act_id_*系列与用户、用户组相关的表,但在实际业务中,我们系统可能初期并没有接入工作流,后期接入时就涉及到如何将现有业务表中的用户与Activiti进行对接,本文即简单介绍下思路。

1、自定义实现Activiti用户管理接口

自定义类CustomUserEntityManagerFactory实现SessionFactory接口,重写getSessionType和openSession方法,核心代码如下:

import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.interceptor.SessionFactory;
import org.activiti.engine.impl.persistence.entity.UserIdentityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomUserEntityManagerFactory implements SessionFactory{
    @Autowired 
    private CustomUserEntityManager customUserEntityManager;

    @Override
    public Class<?> getSessionType() {
        return UserIdentityManager.class;
    }

    @Override
    public Session openSession() {
        return customUserEntityManager;
    }
    
}

其中,类CustomUserEntityManager继承UserEntityManager类:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.UserQueryImpl;
import org.activiti.engine.impl.persistence.entity.IdentityInfoEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import com.fengunion.scf.data.usermanage.role.service.RoleService;
import com.fengunion.scf.data.usermanage.user.entity.User;
import com.fengunion.scf.data.usermanage.user.service.UserService;
import com.fengunion.scf.data.workflow.common.constant.WorkflowConstants;
import com.fengunion.scf.data.workflow.common.util.ActivitiUserUtil;

@Component
public class CustomUserEntityManager extends UserEntityManager{

    @Autowired
    @Lazy
    private UserService userService;
    @Autowired
    @Lazy
    private RoleService roleService;
    @Override
    public UserEntity findUserById(String userId) {
        User user = getUser(userId);
        return ActivitiUserUtil.toActivitiUser(user);
    }

    @Override
    public List<Group> findGroupsByUser(String userId) {
        if (userId == null) {
            return null;
        }
        User uparam = new User();
        uparam.setPkid(Long.valueOf(userId));
        User user = userService.selectOne(uparam);
        List<String> list = roleService.fingRoleCodeByUserId(Long.valueOf(userId));
        List<Group> gs = ActivitiUserUtil.toActivitiGroups(user.getFkEnterpriseBasicId(),list);
        return gs;
    }

    @Override
    public List<org.activiti.engine.identity.User> findUserByQueryCriteria(UserQueryImpl query, Page page) {
        User user = getUser(query.getId());
        List<org.activiti.engine.identity.User> list = new ArrayList<>();
        list.add(ActivitiUserUtil.toActivitiUser(user));
        return list;
    }
    
    private User getUser(String userId) {
        User user = new User();
        if(WorkflowConstants.INTERFACE_SYSTEM_ID.equals(userId)) {
            user.setPkid(Long.valueOf(WorkflowConstants.INTERFACE_SYSTEM_ID));
            user.setNickName(WorkflowConstants.INTERFACE_SYSTEM_NAME);
            user.setRealName(WorkflowConstants.INTERFACE_SYSTEM_NAME); 
            user.setPswd("");
            user.setEmail("");
        }else {
            User prarmUser = new User();
            prarmUser.setPkid(Long.valueOf(userId));
            user = userService.selectOne(prarmUser);
        }
        return user;
    }

    @Override
    public long findUserCountByQueryCriteria(UserQueryImpl query) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public IdentityInfoEntity findUserInfoByUserIdAndKey(String userId, String key) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public List<String> findUserInfoKeysByUserIdAndType(String userId, String type) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public List<org.activiti.engine.identity.User> findPotentialStarterUsers(String proceDefId) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public List<org.activiti.engine.identity.User> findUsersByNativeQuery(Map<String, Object> parameterMap,
            int firstResult, int maxResults) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public long findUserCountByNativeQuery(Map<String, Object> parameterMap) {
        throw new RuntimeException("not implement method.");
    }
    
}

2、自定义实现Activiti用户管理接口

自定义类CustomGroupEntityManagerFactory 实现SessionFactory接口,重写getSessionType和openSession方法,核心代码如下:

import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.interceptor.SessionFactory;
import org.activiti.engine.impl.persistence.entity.GroupIdentityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomGroupEntityManagerFactory implements SessionFactory{
    @Autowired
    private CustomGroupEntityManager customGroupEntityManager;    

    @Override
    public Class<?> getSessionType() {
        return GroupIdentityManager.class;
    }

    @Override
    public Session openSession() {
        return customGroupEntityManager;
    }

}

其中,CustomGroupEntityManager类如下:

import java.util.List;
import java.util.Map;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.GroupQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Component;
import com.fengunion.scf.data.usermanage.role.service.RoleService;
import com.fengunion.scf.data.usermanage.user.entity.User;
import com.fengunion.scf.data.usermanage.user.service.UserService;
import com.fengunion.scf.data.workflow.common.util.ActivitiUserUtil;

@Component
public class CustomGroupEntityManager extends GroupEntityManager{
    @Lazy
    @Autowired
    private RoleService roleService;
    
    @Lazy
    @Autowired
    private UserService userService;
    
    @Override
    public List<Group> findGroupsByUser(String userId) {
        if (userId == null){  
            return null;
        }
        try {
            User uparam = new User();
            uparam.setPkid(Long.valueOf(userId));
            User user = userService.selectOne(uparam);
            List<String> list = roleService.fingRoleCodeByUserId(Long.valueOf(userId));
            List<Group> gs = ActivitiUserUtil.toActivitiGroups(user.getFkEnterpriseBasicId(),list);
            return gs;
        } catch (EmptyResultDataAccessException e) {  
            return null;  
        }
    }

    @Override
    public List<Group> findGroupByQueryCriteria(GroupQueryImpl query, Page page) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public long findGroupCountByQueryCriteria(GroupQueryImpl query) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public List<Group> findGroupsByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public long findGroupCountByNativeQuery(Map<String, Object> parameterMap) {
        throw new RuntimeException("not implement method.");
    }
    
    

}

3、在Activiti配置中注册自己定义的用户、用户组管理类,核心代码如下(我这儿以springboot配置方式为例):

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.impl.interceptor.SessionFactory;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.fengunion.scf.data.workflow.manager.CustomGroupEntityManagerFactory;
import com.fengunion.scf.data.workflow.manager.CustomProcessDiagramGeneratorI;
import com.fengunion.scf.data.workflow.manager.CustomUserEntityManagerFactory;
import com.fengunion.scf.data.workflow.manager.ProcessHistoryManagerSessionFactory;


@Configuration
public class ActivitiConfiguration implements ProcessEngineConfigurationConfigurer{
    
    @Autowired
    private CustomUserEntityManagerFactory customUserEntityManagerFactory;
    
    @Autowired
    private CustomGroupEntityManagerFactory customGroupEntityManagerFactory;
    
    @Autowired
    private ProcessHistoryManagerSessionFactory processHistoryManagerSessionFactory;
    
    @Autowired
    private CustomProcessDiagramGeneratorI customProcessDiagramGeneratorI;
    
    @Override
    public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {
        // TODO Auto-generated method stub
        //processEngineConfiguration.setDataSource(dataSource);
        processEngineConfiguration.setDatabaseSchemaUpdate("none");// none true
        processEngineConfiguration.setDatabaseType("mysql");

        //processEngineConfiguration.setTransactionManager(transactionManager);

        // 流程图字体
        processEngineConfiguration.setActivityFontName("宋体");
        processEngineConfiguration.setAnnotationFontName("宋体");
        processEngineConfiguration.setLabelFontName("宋体");

        processEngineConfiguration.setJpaHandleTransaction(false);
        processEngineConfiguration.setJpaCloseEntityManager(false);
        //
//      processEngineConfiguration.setMailServerHost(mailProperty.getMailServerHost());
//      processEngineConfiguration.setMailServerUsername(mailProperty.getMailServerUsername());
//      processEngineConfiguration.setMailServerPassword(mailProperty.getMailServerPassword());
//      processEngineConfiguration.setMailServerPort(mailProperty.getMailServerPort());
        //
        processEngineConfiguration.setJobExecutorActivate(false);
        processEngineConfiguration.setAsyncExecutorEnabled(false);
        //processEngineConfiguration.setAsyncExecutorActivate(false);
        //自定义用户和组
        List<SessionFactory> customSessionFactories = new ArrayList<>();
        customSessionFactories.add(customUserEntityManagerFactory);
        customSessionFactories.add(customGroupEntityManagerFactory);
        customSessionFactories.add(processHistoryManagerSessionFactory);
        processEngineConfiguration.setCustomSessionFactories(customSessionFactories);
        
    //自定义流程图样式
    processEngineConfiguration.setProcessDiagramGenerator(customProcessDiagramGeneratorI);
    }
}

其中ActivitiUserUtil工具类如下:

import java.util.ArrayList;
import java.util.List;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import com.fengunion.scf.data.usermanage.user.entity.User;


public class ActivitiUserUtil {
    public static UserEntity toActivitiUser(User bUser){  
        UserEntity userEntity = new UserEntity();  
        userEntity.setId(bUser.getPkid().toString());  
        userEntity.setFirstName(bUser.getNickName());  
        userEntity.setLastName(bUser.getRealName());  
        userEntity.setPassword(bUser.getPswd());  
        userEntity.setEmail(bUser.getEmail());  
        userEntity.setRevision(1);  
        return userEntity;  
    } 
    
    public static GroupEntity toActivitiGroup(Long enterpriseBasicId,String code){  
        GroupEntity groupEntity = new GroupEntity();  
        groupEntity.setRevision(1);  
        groupEntity.setType("assignment");  
        groupEntity.setId(enterpriseBasicId+"_"+code);  
        return groupEntity;  
    }  
  
    public static List<Group> toActivitiGroups(Long enterpriseBasicId,List<String> roleCodeList){  
        List<Group> groups = new ArrayList<>();  
        for (String code : roleCodeList) {  
            GroupEntity groupEntity = toActivitiGroup(enterpriseBasicId,code);  
            groups.add(groupEntity);  
        }  
        return groups;  
    }  
}

至此,实现自定义用户和用户组就基本实现了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,126评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,254评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,445评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,185评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,178评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,970评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,276评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,927评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,400评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,883评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,997评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,646评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,213评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,204评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,423评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,423评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,722评论 2 345

推荐阅读更多精彩内容