扩展activiti原表增加新字段

ctiviti自带了很多表,如图:

Activiti工作流引擎的数据库表中的表名称都是以 ACT_.第二部分两个字母表示表的类型。使用模糊匹配的方式说明表的类型匹配activiti的服务API.

·         ACT_RE_*: RE代表仓储(Repository).这种表前缀以static表示流程定义信息或者流程资源信息(如流程的图表和规则等).

·         ACT_RU_*: RU标识为运行(Runtime)时表。包含流程实例,用户任务和变量任务等在运行时的数据信息。这些表只存储Activiti在流程实例运行执行的数据,在流程结束的时候从表中去除数据。从而保持运行时候数据的表的快速和小数据量.

·         ACT_ID_*:ID标识为唯一(Identity)的。包含一些唯一的信息如用户,用户做等信息。

·         ACT_HI_*:HI表示历史数据(History)表,包括过期的流程实例,过期的变量和过期的任务等。

·         ACT_GE_*:GE表示公用(General data)的数据库表类型。

        ACT_GE_BYTEARRAY 表保存了开发时候的文件,在工作流部署的时候需要上传相关的工作流文件到相关的项目中。其中如果是文件采用方式如下,将图片和或者文件转换为二进制字节流存储。

    bytes_字段保存了文件内容,如果是图片,则是保存了二进制。

    由于各个项目的业务特殊性,想扩展ACT_GE_BYTEARRAY 的字段,增加2个新字段SYS_,SWITCHBOARD_字段。

怎么把数据保存到表中,这里采用的是修改源码的办法:

步骤1:修改ACT_GE_BYTEARRAY 表对应实体org.activiti.engine.impl.persistence.entity.ResourceEntity,添加SYS_,SWITCHBOARD_字段:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class ResourceEntity implements Serializable, PersistentObject {
 
   private static final long serialVersionUID = 1L;
 
   protected String id;
   protected String name;
   protected byte [] bytes;
   protected String deploymentId;
   protected boolean generated = false ;
   
   //-------------------------------
   private String switchboard;
   private boolean sys;
   ...
}

步骤2:修改相应的sql的配置,添加SYS_,SWITCHBOARD_字段。

文件org.activiti.db.mapping.entity.Resource.xml:


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?xml version= "1.0" encoding= "UTF-8" ?>
 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
   
<mapper namespace= "org.activiti.engine.impl.persistence.entity.ResourceEntity" >
   
   <!-- RESOURCE INSERT -->
 
   <insert id= "insertResource" parameterType= "org.activiti.engine.impl.persistence.entity.ResourceEntity" >
     insert into ${prefix}ACT_GE_BYTEARRAY(ID_, REV_, NAME_, BYTES_, DEPLOYMENT_ID_, GENERATED_,SWITCHBOARD_,SYS_) <span style= "font-size:9pt;line-height:1.5;" > values (#{id, jdbc<span></span></span><span style= "font-size:9pt;line-height:1.5;" >Type=VARCHAR}, 1 , #{name, jdbcType=VARCHAR}, #{bytes, jdbcType=BLOB}, #{deploymentId, jdbcType=VARCHAR}, #{generated, jdbcType=BOOLEAN}, #{switchboard, jdbcType=VARCHAR},#{sys, jdbcType=BOOLEAN}) </span> </insert>
   
   <!-- RESOURCE UPDATE -->
 
   <!-- RESOURCE DELETE -->
 
   <delete id= "deleteResourcesByDeploymentId" parameterType= "string" >
     delete from ${prefix}ACT_GE_BYTEARRAY where DEPLOYMENT_ID_ = #{id}
   </delete>
   
   <!-- RESOURCE RESULTMAP -->
 
   <resultMap id= "resourceResultMap" type= "org.activiti.engine.impl.persistence.entity.ResourceEntity" >
     <id property= "id" column= "ID_" jdbcType= "VARCHAR" />
     <result property= "name" column= "NAME_" jdbcType= "VARCHAR" />
     <result property= "bytes" column= "BYTES_" jdbcType= "BLOB" />
     <result property= "generated" column= "GENERATED_" jdbcType= "BOOLEAN" />
     <result property= "switchboard" column= "SWITCHBOARD_" jdbcType= "VARCHAR" />
     <result property= "sys" column= "SYS_" jdbcType= "BOOLEAN" />
   </resultMap>
   
   <!-- RESOURCE SELECT -->
 
   <select id= "selectResourceNamesByDeploymentId" parameterType= "org.activiti.engine.impl.db.ListQueryParameterObject" resultType= "string" >
     select NAME_ from ${prefix}ACT_GE_BYTEARRAY where DEPLOYMENT_ID_ = #{parameter} order by NAME_ asc
   </select>
   
   <select id= "selectResourceByDeploymentIdAndResourceName" parameterType= "map" resultMap= "resourceResultMap" >
     select * from ${prefix}ACT_GE_BYTEARRAY
     where DEPLOYMENT_ID_ = #{deploymentId}
           AND NAME_ = #{resourceName}
   </select>
 
   <select id= "selectResourcesByDeploymentId" parameterType= "org.activiti.engine.impl.db.ListQueryParameterObject" resultMap= "resourceResultMap" >
     select * from ${prefix}ACT_GE_BYTEARRAY where DEPLOYMENT_ID_ = #{parameter} order by NAME_ asc
   </select> 
 
   <!-- postgresql specific -->
   <resultMap id= "resourceResultMap_postgres" type= "org.activiti.engine.impl.persistence.entity.ResourceEntity" >
     <id property= "id" column= "ID_" jdbcType= "VARCHAR" />
     <result property= "name" column= "NAME_" jdbcType= "VARCHAR" />
     <result property= "bytes" column= "BYTES_" jdbcType= "BINARY" />
     <result property= "generated" column= "GENERATED_" jdbcType= "BOOLEAN" />
     <result property= "switchboard" column= "SWITCHBOARD_" jdbcType= "VARCHAR" />
     <result property= "sys" column= "SYS_" jdbcType= "BOOLEAN" />
   </resultMap>
     
   <!-- postgresql specific -->
   <select id= "selectResourceByDeploymentIdAndResourceName_postgres" parameterType= "map" resultMap= "resourceResultMap_postgres" >
     select * from ${prefix}ACT_GE_BYTEARRAY
     where DEPLOYMENT_ID_ = #{deploymentId}
           AND NAME_ = #{resourceName}
   </select>
   
   <!-- postgresql specific -->
   <select id= "selectResourcesByDeploymentId_postgres" parameterType= "org.activiti.engine.impl.db.ListQueryParameterObject" resultMap= "resourceResultMap_postgres" >
     select * from ${prefix}ACT_GE_BYTEARRAY where DEPLOYMENT_ID_ = #{parameter} order by NAME_ asc
   </select> 
   
</mapper>


主要就是修改<insert><resultMap>的内容

步骤3:加载数据文件时候,设置SYS_,SWITCHBOARD_的值

?
1
2
3
4
5
6
7
8
9
10
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  RepositoryService repositoryService = processEngine.getRepositoryService();
  DeploymentBuilderImpl deploymentBuilder =(DeploymentBuilderImpl) repositoryService.createDeployment()
  .addClasspathResource( "activiti/leave.bpmn" );
  DeploymentEntity deploymentEntity = deploymentBuilder.getDeployment();
  ResourceEntity resourceEntity = deploymentEntity.getResource( "activiti/leave.bpmn" );
  resourceEntity.setSwitchboard(getSwitchboard());
  resourceEntity.setSys( true );
  deploymentEntity.addResource(resourceEntity);
  deploymentBuilder.deploy();
注:这里主要是通过ResourceEntity resourceEntity = deploymentEntity.getResource("activiti/leave.bpmn");获得数据库获得对应的实体,然后设置我们新添加的字段的值。 

测试结果:

看到上面的数据,SYS_,SWITCHBOARD_字段都有值了,activiti/leave.bpmn是正确的,但是activiti/leave.leave.png对应的值是不正确的。因为这2个添加的值因该是一样的。

下面继续修改:

步骤4:org.activiti.engine.impl.bpmn.deployer.BpmnDeployer,加载activiti/leave.bpmn中的图片

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
     public void deploy(DeploymentEntity deployment) {
...
   createResource(resourceName,diagramResourceName, diagramBytes, deployment);
...
}
 
protected void createResource(String resourceName ,String name, byte [] bytes, DeploymentEntity deploymentEntity) {
     ResourceEntity resource = new ResourceEntity();
     resource.setName(name);
     resource.setBytes(bytes);
     resource.setDeploymentId(deploymentEntity.getId());
     
     ResourceEntity resourceEntity = deploymentEntity.getResource(resourceName);
     if (resourceEntity!= null ){
         resource.setSwitchboard(resourceEntity.getSwitchboard());
         resource.setSys(resourceEntity.isSys());
     }
     // Mark the resource as 'generated'
     resource.setGenerated( true );
     
     Context
       .getCommandContext()
       .getDbSqlSession()
       .insert(resource);
   }
有人要问,问什么要这么修改,没办法,我是一步一步debug,一步一步看源码。

步骤5:文件org.activiti.db.mapping.entity.VariableInstance.xml,添加SYS_,SWITCHBOARD_字段:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- BYTE ARRAY INSERT -->
 
<insert id= "insertByteArray" parameterType= "org.activiti.engine.impl.persistence.entity.ByteArrayEntity" >
   insert into ${prefix}ACT_GE_BYTEARRAY(ID_, REV_, NAME_, BYTES_, DEPLOYMENT_ID_,SWITCHBOARD_,SYS_)
   values (
     #{id, jdbcType=VARCHAR},
     1 ,
     #{name, jdbcType=VARCHAR},
     #{bytes, jdbcType=BLOB},
     #{deploymentId, jdbcType=VARCHAR},
     #{switchboard, jdbcType=VARCHAR},
     #{sys, jdbcType=BOOLEAN}
  
</insert>
?
1
...
然后测试结果: 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值