首页 > 代码库 > 基于Activiti5.15.1 自定义用户、组(User,Group)实现

基于Activiti5.15.1 自定义用户、组(User,Group)实现

基于Activiti5.15.1 自定义用户、组(User,Group)实现

     本人刚接触Activiti,最近工作中需要将Activiti中原有的用户,组(ACT_ID_USER,ACT_ID_GROUP,ACT_ID_MEMBERSHIP)表替换为公司已有的相关表。查看了咖啡兔及论坛相关文章。今天有空整理一下。以帮助后续有此需要的初学者。

 

自定义Group,User工厂类,实现SessionFactory接口

1)CustomGroupEntityManagerFactory

 

@Servicepublic class CustomGroupEntityManagerFactory implements SessionFactory {    private CustomGroupEntityManager customGroupEntityManager;    @Override    public Class<?> getSessionType() {     //注意此处必须为Activiti原生的类,否则自定义类不会生效        return GroupIdentityManager.class;    }    @Override    public Session openSession() {        return customGroupEntityManager;    }    @Autowired    public void setCustomGroupEntityManager(CustomGroupEntityManager customGroupEntityManager) {        this.customGroupEntityManager = customGroupEntityManager;    }}

 

2)CustomUserEntityManagerFactory

@Servicepublic class CustomUserEntityManagerFactory implements SessionFactory {    private CustomUserEntityManager customUserEntityManager;    @Override    public Class<?> getSessionType() {       //注意此处也必须为Activiti原生类        return UserIdentityManager.class;    }    @Override    public Session openSession() {        return customUserEntityManager;    }    @Autowired    public void setCustomUserEntityManager(CustomUserEntityManager customUserEntityManager) {        this.customUserEntityManager = customUserEntityManager;    }}

3)CustomGroupEntityManager

 1 @Component 2 public class CustomGroupEntityManager extends GroupEntityManager { 3     private static final Log logger = LogFactory 4             .getLog(CustomGroupEntityManager.class); 5  6     @Autowired 7     private UserMapper userMapper;//用于查询实际业务中用户表、角色等表 8  9 10     @Override11     public List<Group> findGroupsByUser(final String userCode) {12         if (userCode == null)13             return null;14 15         List<Role> bGroupList = userMapper.getGroupByUserName(userCode);16 17         List<Group> gs = new java.util.ArrayList<>();18         GroupEntity g;19         String roleId;20         String activitRole;21         for (Role bGroup : bGroupList) {22             g = new GroupEntity();23             g.setRevision(1);24             g.setType("assignment");25             roleId = String.valueOf(bGroup.getRoleID());26             activitRole = bindGroupWithRole.get(roleId);//此处只是根据RoleId获取RoleCode, 因实际表中无RoleCode字段,暂且如此实际,此行可注释掉27             g.setId(activitRole != null ? activitRole : roleId);28             g.setName(bGroup.getRoleName());29             gs.add(g);30         }31         return gs;32     }33 }

4)CustomUserEntityManagerFactory

@Componentpublic class CustomUserEntityManager extends UserEntityManager {    private static final Log logger = LogFactory.getLog(CustomUserEntityManager.class);    @Autowired    private UserMapper userMapper;    @Override    public UserEntity findUserById(String userId) {        UserEntity userEntity = new UserEntity();        com.jimubox.transformers.model.user.User cue = userMapper.getUserByUserName(userId);        ActivitiUtils.toActivitiUser(cue);        return userEntity;    }    @Override    public List<Group> findGroupsByUser(final String userCode) {        if (userCode == null)            return null;        List<String> groupIds = userMapper.getGroupIdsByUserName(userCode);        List<Group> gs = null;        gs = Utils.toActivitiGroups(groupIds);        return gs;    }  }

5)applicationContext.xml

<!-- Activiti begin -->    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">        <property name="databaseType" value="http://www.mamicode.com/mysql"/>        <property name="dataSource" ref="dataSource"/>        <property name="transactionManager" ref="transactionManager"/>        <property name="databaseSchemaUpdate" value="http://www.mamicode.com/true"/>        <property name="jobExecutorActivate" value="http://www.mamicode.com/false"/>        <property name="history" value="http://www.mamicode.com/activity"/>        <property name="processDefinitionCacheLimit" value="http://www.mamicode.com/10"/>        <!-- JPA -->        <property name="jpaEntityManagerFactory" ref="entityManagerFactory"/>        <property name="jpaHandleTransaction" value="http://www.mamicode.com/true"/>        <property name="jpaCloseEntityManager" value="http://www.mamicode.com/true"/>             <property name="customSessionFactories">            <list>                <bean class="com.jimubox.transformers.factory.workflow.CustomUserEntityManagerFactory">                    <property name="customUserEntityManager" ref="customUserEntityManager"/>                </bean>                <bean class="com.jimubox.transformers.factory.workflow.CustomGroupEntityManagerFactory">                    <property name="customGroupEntityManager" ref="customGroupEntityManager"/>                </bean>            </list>        </property>    </bean>