Activiti6-IdentityService(学习笔记)

时间:2024-07-18 22:36:44

Activiti6-IdentityService(学习笔记)

IdentityService并不依赖我们的流程部署文件,所以

直接放使用方法的测试代码了:

public class IdentityServiceTest {
private static final Logger LOGGER = LoggerFactory.getLogger(IdentityServiceTest.class); @Rule
public ActivitiRule activitiRule = new ActivitiRule(); @Test
public void testIdentity(){
//其实IdentityService并不依赖我们的流程部署文件
IdentityService identityService = activitiRule.getIdentityService(); //通过IdentityService来new出user
User user1 = identityService.newUser("user1");
user1.setEmail("user1@136.com");
User user2 = identityService.newUser("user2");
user2.setEmail("user2@136.com");
//设置完用户信息以后,保存到数据库一下
identityService.saveUser(user1);
identityService.saveUser(user2); //通过IdentityService来new出group
Group group1 = identityService.newGroup("group1");
Group group2 = identityService.newGroup("group2");
//同样将用户组也存进数据库
identityService.saveGroup(group1);
identityService.saveGroup(group2); //将刚创建的两个user与group设立关系;
identityService.createMembership("user1","group1");
identityService.createMembership("user2","group1");
identityService.createMembership("user1","group2"); //通过identityService的createUserQuery()方法创建查询对象并通过用户组去 查询用户
List<User> userList = identityService.createUserQuery().memberOfGroup("group1").listPage(0, 100);
for (User user : userList) {
LOGGER.info("user = {}", ToStringBuilder.reflectionToString(user, ToStringStyle.JSON_STYLE));
} //查询一下组
List<Group> groupList = identityService.createGroupQuery().groupMember("user1").listPage(0, 100);
for (Group group : groupList) {
LOGGER.info("group = {}",ToStringBuilder.reflectionToString(group, ToStringStyle.JSON_STYLE));
}
}
}

输出结果如下:

user = {"firstName":null,"lastName":null,"email":"user1@136.com","password":null,"pictureByteArrayRef":"ByteArrayRef[id=null, name=null, entity=null]","id":"user1","revision":,"isInserted":false,"isUpdated":false,"isDeleted":false}
user = {"firstName":null,"lastName":null,"email":"user2@136.com","password":null,"pictureByteArrayRef":"ByteArrayRef[id=null, name=null, entity=null]","id":"user2","revision":,"isInserted":false,"isUpdated":false,"isDeleted":false}
group = {"name":null,"type":null,"id":"group1","revision":,"isInserted":false,"isUpdated":false,"isDeleted":false}
group = {"name":null,"type":null,"id":"group2","revision":,"isInserted":false,"isUpdated":false,"isDeleted":false}

ps:修改user名字的方式:

        //修改一下用户名
User user11 = identityService.createUserQuery().userId("user1").singleResult();
user11.setLastName("lyf");
identityService.saveUser(user11);

输出如下:

user = {"firstName":null,"lastName":"lyf","email":"user1@136.com","password":null,"pictureByteArrayRef":"ByteArrayRef[id=null, name=null, entity=null]","id":"user1","revision":,"isInserted":false,"isUpdated":false,"isDeleted":false}
user = {"firstName":null,"lastName":null,"email":"user2@136.com","password":null,"pictureByteArrayRef":"ByteArrayRef[id=null, name=null, entity=null]","id":"user2","revision":,"isInserted":false,"isUpdated":false,"isDeleted":false}
group = {"name":null,"type":null,"id":"group1","revision":,"isInserted":false,"isUpdated":false,"isDeleted":false}
group = {"name":null,"type":null,"id":"group2","revision":,"isInserted":false,"isUpdated":false,"isDeleted":false}

在我们执行方法保存用户的过程中,IdentityService 具体的实现做的是什么事情呢?流程如下图:

Activiti6-IdentityService(学习笔记)

先调用一个命令执行器,把saveUser这个对象封装成一个命令对象,也就是把user这个对象,构建成一个SaveUserCmd,就是把参数组装成了一个命令。

组装成命令以后,用命令执行器去执行这个命令,那么在执行这个命令的过程中,真正执行命令的方式是

实现了一个UserEntityManager 这样一个接口,它的实现会做一个调用我们底层数据库的修改,底层数据库的修改回去间接的调用UserDataManger这个又一次封装的接口,它接口的实现也就是MybatisUserDataManger,也就是经过多层的调用,IdentityService的saveUser经过一个链式调用以后,最终通过mybatis的操作方式DbSqlSession执行了一个insert操作把我们的数据保存到了数据库中。

源码如下:

Activiti6-IdentityService(学习笔记)

Activiti6-IdentityService(学习笔记)

Activiti6-IdentityService(学习笔记)

这execute中  用UserEntityManger的isNewUser()判断了user是不是新用户,

判断方式是获取这个user的版本号vision是否为0,是就为新用户,否则就为老用户。

Activiti6-IdentityService(学习笔记)

这里的判断其实是为了确认是用写入(insert语句)还是更新(update语句)的判断。

在这个判断中还判断了user是否实现了UserEntity接口

如果实现了就会同时触发一个创建user的事件,如果没有就直接写入数据库了。

而UserEntityManger的insert的方法,它其实是继承了父类EntityManager的insert,而EntityManager的insert方法其实调用了DataManager的insert方法,源码如下:

Activiti6-IdentityService(学习笔记)

Activiti6-IdentityService(学习笔记)

Activiti6-IdentityService(学习笔记)

接着再看DataManager的insert方法

会发现是调的UserDataManager接口,而它的实现类为

Activiti6-IdentityService(学习笔记)

然后,UserDataManager的实现类MybatisUserDataManager的insert方法如下:

Activiti6-IdentityService(学习笔记)

调用的是DbSqlSession的insert

Activiti6-IdentityService(学习笔记)

如果有特殊的需求,可以定义命令执行器CommandExecutor,去执行一些特殊的操作。