activiti5.17使用自定义的user和group表的几种方法

时间:2023-02-10 00:25:40

一、不使用原有的表

1、在spring.xml里添加:

      <property name="customSessionFactories">

                 <list>

                            <bean class="com.gs.workflow.util.CustomUserEntityManagerFactory"/>

                             <bean class="com.gs.workflow.util.CustomGroupEntityManagerFactory"/>

                 </list>

        </property>

2、用户管理工厂类CustomUserEntityManagerFactory.java:

         

package com.gs.workflow.util;

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

/**
* Created by gj.hu on 2015/8/24.
*/
public class CustomUserEntityManagerFactory implements SessionFactory {
private UserEntityManager userEntityManager;

@Autowired
public void setUserEntityManager(UserEntityManager userEntityManager) {
this.userEntityManager = userEntityManager;
}

public Class<?> getSessionType() {
// 返回原始的UserManager类型
return UserEntityManager.class;
}
    public Session openSession() {
// 返回自定义的UserManager实例
return userEntityManager;
}
}
 
3、组管理工厂类CustomGroupEntityManagerFactory.java:
   
package com.gs.workflow.util;

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

/**
* Created by gj.hu on 2015/8/24.
*/
public class CustomGroupEntityManagerFactory implements SessionFactory{
private GroupEntityManager groupEntityManager;

@Autowired
public void setGroupEntityManager(GroupEntityManager groupEntityManager) {
this.groupEntityManager = groupEntityManager;
}

public Class<?> getSessionType() {
// 返回原始的GroupEntityManager类型
return GroupEntityManager.class;
}

public Session openSession() {
// 返回自定义的GroupEntityManager实例
return groupEntityManager;
}
}
4、覆盖用户管理类CustomUserEntityManager.java
  @Service 
public class CustomUserEntityManager extends UserEntityManager{    private static final Log logger = LogFactory            .getLog(CustomUserEntityManager.class);    @Autowired    private UserManager userManager;//自己实现的获取用户数据的Service    @Override    public UserEntity findUserById(final String userCode) {        if (userCode == null)            return null;        try {            UserEntity userEntity = null;            com.gs.entity.User bUser = userManager.getUser(Long.valueOf(userCode));            userEntity = ActivitiUtils.toActivitiUser(bUser);            return userEntity;        } catch (EmptyResultDataAccessException e) {            return null;        }    }    @Override    public List<Group> findGroupsByUser(final String userCode) {        if (userCode == null)            return null;        List<com.gs.entity.Group> bGroups = userManager.getUser(Long.valueOf(userCode)).getGroupList();        List<Group> gs = null;        gs = ActivitiUtils.toActivitiGroups(bGroups);        return gs;    }    @Override    public List<User> findUserByQueryCriteria(UserQueryImpl query, Page page) {        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 long findUserCountByQueryCriteria(UserQueryImpl query) {        throw new RuntimeException("not implement method.");    }}
5、覆盖组管理类CustomGroupEntityManager.java:
 
@Servicepublic class CustomGroupEntityManager extends GroupEntityManager {    private static final Log logger = LogFactory            .getLog(CustomGroupEntityManager.class);    @Autowired    private GroupManager groupManager;    @Override    public GroupEntity findGroupById(final String groupCode) {        if (groupCode == null)            return null;        try {            com.sanshen.js.entity.account.Group bGroup = groupManager.getGroupByGroupId(groupCode);            GroupEntity e = new GroupEntity();            e.setRevision(1);            // activiti有3种预定义的组类型:security-role、assignment、user  // 如果使用Activiti  // Explorer,需要security-role才能看到manage页签,需要assignment才能claim任务  e.setType("assignment");            e.setId(bGroup.getGroupId());            e.setName(bGroup.getName());            return e;        } catch (EmptyResultDataAccessException e) {            return null;        }    }    @Override    public List<Group> findGroupsByUser(final String userCode) {        if (userCode == null)            return null;        List<com.sanshen.js.entity.account.Group> bGroupList = groupManager.getUser(Long.valueOf(userCode)).getGroupList();        List<Group> gs = new ArrayList<Group>();        GroupEntity g;        for (com.sanshen.js.entity.account.Group bGroup : bGroupList) {            g = new GroupEntity();            g.setRevision(1);            g.setType("assignment");            g.setId(bGroup.getGroupId());            g.setName(bGroup.getName());            gs.add(g);        }        return gs;    }    @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.");    }}  
    参考:http://124444154-qq-com.iteye.com/blog/1830995
二、调用IdentifyService接口完成同步
    参考:http://blog.csdn.net/szwangdf/article/details/25640357
三、覆盖IdentifyService接口实现:一个一个覆盖就行了
四、用视图覆盖同名的ACT_ID_*表:
     1、必须先删除ACT_ID_*表
     2、分别创建同名视图
DROP VIEW  IF EXISTS act_id_membership;  DROP VIEW  IF EXISTS act_id_user;  DROP VIEW  IF EXISTS act_id_group;CREATE or replace VIEW act_id_user(ID_,REV_,FIRST_,LAST_,EMAIL_,PWD_,PICTURE_ID_ ,CONSTRAINT id_pk PRIMARY KEY(ID_) RELY DISABLE NOVALIDATE) AS     SELECT      TO_CHAR(au.login_name) AS ID_,      0          AS REV_,      to_char(au.user_name_cn)   AS FIRST_,      '' AS LAST_,      to_char(au.email)      AS EMAIL_,      to_char(au.PASSWORD)   AS PWD_,      ''          AS PICTURE_ID_    FROM base_user au;    CREATE VIEW act_id_group   AS    SELECT ar.name AS ID_,         NULL AS REV_,         ar.name AS NAME_,         'assignment' AS TYPE_  FROM s_role ar;    CREATE VIEW act_id_membership  AS     SELECT (SELECT u.username FROM s_user u WHERE u.id=ur.user_id) AS USER_ID_,       (SELECT r.name FROM s_role r WHERE r.id=ur.role_id) AS GROUP_ID_     FROM s_user_role ur;

 创建的视图要保证数据类型一致,例如用户的ACT_ID_MEMBERSHIP表的两个字段都是字符型,一般系统中都是用NU MBER作为用户、角色的主键类型,所以创建视图的时候要把数字类型转换为字符型。
 3、在spring.xml里添加:

     
     
  1. <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">    ...    <property name="dbIdentityUsed" value="false">    ...</property></bean>