欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JBPM4.3总结三

程序员文章站 2022-06-08 17:41:04
...

2.5集成自定义用户表
2.5.1 Jbpm4提供了实现接口

1.interface User

2.Interface Group

3.Interface  GroupMember

4.Interface   IdentitySession

想要集成自己的表,首先需要实现jbpm提供的接口,然后再进行配置。
2.5.2 User 用户表

import java.io.Serializable;

import java.sql.Blob;

import org.jbpm.api.identity.User;

/**

* 用户表 MesUser

*

* @version 1.0

*

*/

public class MesUser implements Serializable, User {

private static final long serialVersionUID = 1L;

private String id;

private String userNo;// 员工工号

private String userName;// 员工姓名

private String userSex;// 性别

private String userPassword;// 密码

private String userType;// 类型

private String userMail;// 电子邮件

private String isValid;// 是否有效Y/N

private Blob signaturePic;// 电子签名

private String remarks;// 备注

protected long dbid; // 数据库内部自生成的ID

protected int dbversion;

public int getDbversion() {

return dbversion;

}

public MesUser() {

}

public MesUser(String id, String userName, String userMail) {

this.id = id;

this.userName = userName;

this.userMail = userMail;

}

public void setDbversion(int dbversion) {

this.dbversion = dbversion;

}

public long getDbid() {

return dbid;

}

public void setDbid(long dbid) {

this.dbid = dbid;

}

public void setId(String id) {

this.id = id;

}

public String getUserNo() {

return userNo;

}

public void setUserNo(String userNo) {

this.userNo = userNo;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getUserSex() {

return userSex;

}

public void setUserSex(String userSex) {

this.userSex = userSex;

}

public String getUserPassword() {

return userPassword;

}

public void setUserPassword(String userPassword) {

this.userPassword = userPassword;

}

public String getUserType() {

return userType;

}

public void setUserType(String userType) {

this.userType = userType;

}

public String getUserMail() {

return userMail;

}

public void setUserMail(String userMail) {

this.userMail = userMail;

}

public String getIsValid() {

return isValid;

}

public void setIsValid(String isValid) {

this.isValid = isValid;

}

public Blob getSignaturePic() {

return signaturePic;

}

public void setSignaturePic(Blob signaturePic) {

this.signaturePic = signaturePic;

}

public String getRemarks() {

return remarks;

}

public void setRemarks(String remarks) {

this.remarks = remarks;

}

/**

* 显示用户姓名+工号

*

* @return

*/

public String getDisplayName() {

return userName + "(" + id + ")";

}

// 实现User接口所必须实现的几个方法

public String getId() {

return this.id;

}

public String getGivenName() {

return null;

}

public String getFamilyName() {

return null;

}

public String getBusinessEmail() {

return this.userMail;

}

}
2.5.3 Group部门表

import java.io.Serializable;

import org.jbpm.api.identity.Group;

public class MesGroup implements Serializable,Group {

private static final long serialVersionUID = 1L;

private String id;

private String groupName;//组织名称

private String groupType;//组织类型

private MesGroup parentGroup;//父组织

private String remarks;//备注

protected long dbid;

protected int dbversion;

public int getDbversion() {

return dbversion;

}

public void setDbversion(int dbversion) {

this.dbversion = dbversion;

}

public long getDbid() {

return dbid;

}

public void setDbid(long dbid) {

this.dbid = dbid;

}

public String getParentGroupID() {

return parentGroup!=null?parentGroup.getId():null;

}

public String getParentGroupName() {

return parentGroup==null ? "xxx" : parentGroup.getGroupName();

}

public void setId(String id) {

this.id = id;

}

public String getGroupName() {

return groupName;

}

public void setGroupName(String groupName) {

this.groupName = groupName;

}

public String getGroupType() {

return groupType;

}

public void setGroupType(String groupType) {

this.groupType = groupType;

}

public MesGroup getParentGroup() {

return parentGroup;

}

public void setParentGroup(MesGroup parentGroup) {

this.parentGroup = parentGroup;

}

public String getRemarks() {

return remarks;

}

public void setRemarks(String remarks) {

this.remarks = remarks;

}

//实现Group接口必须的几个方法

public String getName(){

return this.groupName;



public String getType(){

return this.groupType;

}

public String getId() {

return id;

}

}
2.5.4 GroupMember 用户部门关系表

import java.io.Serializable;

public class MesGroupMember implements Serializable {

private static final long serialVersionUID = 1L;

protected long dbid;

protected int dbversion;

private MesUser user;

private MesGroup group;

protected String role;

public int getDbversion() {

return dbversion;

}

public void setDbversion(int dbversion) {

this.dbversion = dbversion;

}

public long getDbid() {

return dbid;

}

public String getRole() {

return role;

}

public void setRole(String role) {

this.role = role;

}

public void setDbid(long dbid) {

this.dbid = dbid;

}

public MesGroup getGroup() {

return group;

}

public void setGroup(MesGroup group) {

this.group = group;

}

public String getUserNo() {

return user.getUserNo();

}

public String getUserID() {

return user.getId();

}

public String getUserName() {

return user.getUserName();

}

public MesUser getUser() {

return user;

}

public void setUser(MesUser user) {

this.user = user;

}

}
2.5.5 IdentitySession 实现类

import java.util.Arrays;

import java.util.List;

import org.hibernate.Session;

import org.hibernate.criterion.Projections;

import org.hibernate.criterion.Restrictions;

import org.jbpm.api.JbpmException;

import org.jbpm.api.identity.Group;

import org.jbpm.api.identity.User;

import org.jbpm.pvm.internal.env.BasicEnvironment;

import org.jbpm.pvm.internal.env.EnvironmentImpl;

import org.jbpm.pvm.internal.id.DbidGenerator;

import org.jbpm.pvm.internal.identity.spi.IdentitySession;

/**

* @author zhangkai

* @version 1.0

*

*/

@SuppressWarnings("unchecked")

public class MesIdentitySessionImpl implements IdentitySession {

protected Session session;

public MesIdentitySessionImpl() {

this.session = BasicEnvironment.getFromCurrent(Session.class);

}

public String createH() {

Test t = new Test();

long dbid = EnvironmentImpl.getFromCurrent(DbidGenerator.class)

.getNextId();

t.setDbid(dbid);

t.setId("abc");

return null;

}

public String createUser(String id, String userName,

String businessEmail, String familName) {

MesUser user = new MesUser(id, userName, businessEmail);

long dbid = EnvironmentImpl.getFromCurrent(DbidGenerator.class)

.getNextId();

user.setDbid(dbid);

session.save(user);

return user.getId();

}

public MesUser findUserById(String userId) {

return (MesUser) session.createCriteria(MesUser.class).add(

Restrictions.eq("id", userId)).uniqueResult();

}

public List<User> findUsersById(String... userIds) {

List<User> users = session.createCriteria(MesUser.class).add(

Restrictions.in("id", userIds)).list();

if (userIds.length != users.size()) {

throw new JbpmException("not all users were found: "

+ Arrays.toString(userIds));

}

return users;

}

public List<User> findUsers() {

return session.createCriteria(MesUser.class).list();

}

public void deleteUser(String userId) {

// lookup the user

MesUser user = findUserById(userId);

// cascade the deletion to the memberships

List<MesGroupMember> memberships = session.createCriteria(

MesGroupMember.class).add(Restrictions.eq("user", user)).list();

// delete the related memberships

for (MesGroupMember membership : memberships) {

session.delete(membership);

}

// delete the user

session.delete(user);

}

public String createGroup(String groupName, String groupType,

String parentGroupId) {

MesGroup group = new MesGroup();

String groupId = groupType != null ? groupType + "." + groupName

: groupName;

group.setId(groupId);

long dbid = EnvironmentImpl.getFromCurrent(DbidGenerator.class)

.getNextId();

group.setDbid(dbid);

group.setGroupName(groupName);

group.setGroupType(groupType);

if (parentGroupId != null) {

MesGroup parentGroup = findGroupById(parentGroupId);

group.setParentGroup(parentGroup);

}

session.save(group);

return group.getId();

}

public List<User> findUsersByGroup(String groupId) {

return session.createCriteria(MesGroupMember.class).createAlias(

"group", "g").add(Restrictions.eq("g.id", groupId))

.setProjection(Projections.property("user")).list();

}

public MesGroup findGroupById(String groupId) {

return (MesGroup) session.createCriteria(MesGroup.class).add(

Restrictions.eq("id", groupId)).uniqueResult();

}

public List<Group> findGroupsByUserAndGroupType(String userId,

String groupType) {

return session.createQuery(

"select distinct m.group" + " from "

+ MesGroupMember.class.getName()

+ " as m where m.user.id = :userId"

+ " and m.group.type = :groupType").setString("userId",

userId).setString("groupType", groupType).list();

}

public List<Group> findGroupsByUser(String userId) {

List<Group> gList = session.createQuery(

"select distinct m.group" + " from "

+ MesGroupMember.class.getName()

+ " as m where m.user.id = :userId").setString(

"userId", userId).list();

return gList;

}

public List<Group> findGroups() {

return session.createCriteria(MesGroup.class).list();

}

public void deleteGroup(String groupId) {

// look up the group

MesGroup group = findGroupById(groupId);

// cascade the deletion to the memberships

List<MesGroupMember> memberships = session.createCriteria(

MesGroupMember.class).add(Restrictions.eq("group", group))

.list();

// delete the related memberships

for (MesGroupMember membership : memberships) {

session.delete(membership);

}

// delete the group

session.delete(group);

}

public void createMembership(String userId, String groupId, String role) {

MesUser user = findUserById(userId);

if (user == null) {

throw new JbpmException("user " + userId + " doesn't exist");

}

MesGroup group = findGroupById(groupId);

if (group == null) {

throw new JbpmException("group " + groupId + " doesn't exist");

}

MesGroupMember membership = new MesGroupMember();

membership.setUser(user);

membership.setGroup(group);

membership.setRole(role);

long dbid = EnvironmentImpl.getFromCurrent(DbidGenerator.class)

.getNextId();

membership.setDbid(dbid);

session.save(membership);

}

public void deleteMembership(String userId, String groupId, String role) {

MesGroupMember membership = (MesGroupMember) session.createCriteria(

MesGroupMember.class).createAlias("user", "u").createAlias(

"group", "g").add(Restrictions.eq("u.id", userId)).add(

Restrictions.eq("g.id", groupId)).uniqueResult();

session.delete(membership);

}

}
2.5.6配置 jbpm.cfg.xml

使用自己的SessionImpl,在文件后面加上

<transaction-context>

<hibernate-session current="true"/>

<object class="xxx . Xxx . xxx . MesIdentitySessionImpl">

</object>

</transaction-context>

1.注释掉jbpm4自己的hbm.xml文件

<!--

<import resource="jbpm.identity.cfg.xml" />

-->

在jbpm.hibernate.cfg.xml 注入自己的

<mapping resource="customize.hbm.xml" />
2.5.7自定义hibernate映射文件

新建 customize.hbm.xml,内容如下

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.jbpm.pvm.internal.identity.impl" default-access="field">

<!-- ### USER ########################################################### -->

<class name="com.t.MesUser" table="JBPM4_CUSTOMIZE_USER">

<id name="dbid" column="DBID_">

<generator class="assigned" />

</id>

<version name="dbversion" column="DBVERSION_" />

<property name="id" column="USERID_" /> <!-- 登陆ID -->

<property name="userNo" column="USERNO_" /> <!-- 员工工号 -->

<property name="userName" column="USERNAME_" /> <!-- 姓名 -->

<property name="userSex" column="USERSEX_" />  <!-- 性别 -->

<property name="userPassword" column="USERPASSWORD_" />  <!-- 密码 -->

<property name="userType" column="USERTYPE_" />  <!-- 类型 -->

<property name="userMail" column="USERMAIL_" />  <!-- 电子邮件 -->

<property name="signaturePic" column="SIGNATUREPIC_" /> <!-- 电子签名 -->

<property name="remarks" column="REMARKS_" /> <!-- 备注 -->

</class>



<!-- ### MEMBERSHIP ##################################################### -->

<class name="com.t.MesGroupMember" table="JBPM4_CUSTOMIZE_MEMBERSHIP">

<id name="dbid" column="DBID_">

<generator class="assigned" />

</id>

<version name="dbversion" column="DBVERSION_" />



<many-to-one name="user"

column="USER_"

class="com.t.MesUser"

foreign-key="FK_MEM_USER"

index="IDX_MEM_USER"/>

<many-to-one name="group"

column="GROUP_"

class="com.t.MesGroup"

foreign-key="FK_MEM_GROUP"

index="IDX_MEM_GROUP"/>

<property name="role" column="NAME_" />

</class>



<!-- ### GROUP ########################################################### -->

<class name="com.t.MesGroup" table="JBPM4_CUSTOMIZE_GROUP">

<id name="dbid" column="DBID_">

<generator class="assigned" />

</id>

<version name="dbversion" column="DBVERSION_" />

<!--



private String groupID;//数据库内部ID号

private String groupName;//组织名称

private String groupType;//组织类型

private MesGroup parentGroup;//父组织

private String remarks;//备注

-->

<property name="id" column="ID_" />

<property name="groupName" column="NAME_" />

<property name="groupType" column="TYPE_" />

<property name="remarks" column="REMARKS_" />

<many-to-one name="parentGroup"

column="PARENT_"

class="com.t.MesGroup"

foreign-key="FK_GROUP_PARENT"

index="IDX_GROUP_PARENT"/>

</class>

</hibernate-mapping>

集成自己的表就完成了。当你调用 identityService.createUser()方法时,就往你自定义的user表里插入了一条记录。
2.6新建用户和组

identityService.createGroup("user_dept"); // 部门

identityService.createUser("user1", "test1", "test1"); // 新建用户1

identityService.createUser("user2", "test2", "test2"); /./新建用户2

identityService.createMembership("user1", "user_dept"); // 绑定用户和部门的关系

identityService.createMembership("user2", "user_dept");
2.7发布流程

发布流程:

ProcessEngine processEngine = Configuration.getProcessEngine();

RepositoryService repositoryService = processEngine

.getRepositoryService();

String deployId = repositoryService.createDeployment().addResourceFromClasspath("com/contract/contract.jpdl.xml").deploy();

删除流程:repositoryService.deleteDeploymentCascade(deployId));

2.8开始流程实例

executionService.startProcessInstanceById(request.getParameter("id"));