您现在的位置是:首页 > 文章详情

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

日期:2018-10-22点击:306

声明: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配置方式为例):

... @Autowired private CustomUserEntityManagerFactory customUserEntityManagerFactory; @Autowired private CustomGroupEntityManagerFactory customGroupEntityManagerFactory; ... @Override public void configure(SpringProcessEngineConfiguration processEngineConfiguration) { ... //自定义用户和组 List<SessionFactory> customSessionFactories = new ArrayList<>(); customSessionFactories.add(customUserEntityManagerFactory); customSessionFactories.add(customGroupEntityManagerFactory); processEngineConfiguration.setCustomSessionFactories(customSessionFactories); ... } 

其中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; } } 

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

原文链接:https://my.oschina.net/u/180480/blog/2250441
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章