提交 41ace484 编写于 作者: sq3536's avatar sq3536

工作流审批意见

上级 dd174a92
package cn.ibizlab.core.extensions.domain;
import cn.ibizlab.util.security.AuthenticationUser;
import org.flowable.common.engine.api.identity.AuthenticationContext;
import org.flowable.common.engine.impl.identity.Authentication;
import java.security.Principal;
public class FlowUser implements Principal {
public String getName() {
return this.user.getUserid();
}
private AuthenticationUser user;
private String token;
public AuthenticationUser getUser() {
return user;
}
public void setUser(AuthenticationUser user) {
this.user = user;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public static FlowUser getCurUser()
{
AuthenticationContext context = Authentication.getAuthenticationContext();
if(context!=null&&context.getPrincipal()!=null&&context.getPrincipal() instanceof FlowUser)
return (FlowUser)context.getPrincipal();
else
{
FlowUser user = new FlowUser();
user.setUser(AuthenticationUser.getAuthenticationUser());
return user;
}
}
}
package cn.ibizlab.core.extensions.listener;
import cn.ibizlab.core.extensions.domain.FlowUser;
import cn.ibizlab.core.extensions.service.WFCoreService;
import cn.ibizlab.core.extensions.service.WFModelService;
import cn.ibizlab.util.security.AuthTokenUtil;
......@@ -11,7 +12,9 @@ import org.flowable.bpmn.model.UserTask;
import org.flowable.common.engine.api.delegate.event.*;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.impl.event.FlowableEntityEventImpl;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.event.impl.FlowableActivityEventImpl;
import org.flowable.engine.delegate.event.impl.FlowableEntityWithVariablesEventImpl;
import org.flowable.engine.delegate.event.impl.FlowableProcessStartedEventImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
......@@ -27,11 +30,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 流程实例监听器,监听流程实例启动、流转、结束状态
* 1.流程状态发生变化时,将实例信息同步到统一工作流平台
* 2.到达流程节点时,调用统一接口,查询当前节点办理人并设置到引擎中
*/
@Slf4j
@Component
public class ProcessInstanceListener implements FlowableEventListener {
......@@ -45,11 +43,37 @@ public class ProcessInstanceListener implements FlowableEventListener {
@Lazy
private RemoteService remoteService;
@Autowired
@Lazy
private TaskService taskService;
@Override
public void onEvent(FlowableEvent evt) {
if(evt instanceof FlowableProcessStartedEventImpl)
if(evt instanceof FlowableEntityWithVariablesEventImpl)
{
FlowableEntityWithVariablesEventImpl event = (FlowableEntityWithVariablesEventImpl) evt;
if(event.getType() == FlowableEngineEventType.TASK_COMPLETED)
{
TaskEntity taskEntity = (TaskEntity)event.getEntity();
if(taskEntity.getTaskDefinitionKey().startsWith("tid-"))
{
Object data=taskEntity.getVariable("activedata");
Object link=taskEntity.getVariable("sequenceFlowName");
if(data!=null&&(data instanceof Map)&&link!=null)
{
Map activedata=(Map)data;
String wfprocdesc="";
if(activedata.get("wfprocdesc")!=null)
wfprocdesc=activedata.get("wfprocdesc").toString();
taskService.addComment(taskEntity.getId(),taskEntity.getProcessInstanceId(),link.toString(),wfprocdesc);
}
}
}
}
else if(evt instanceof FlowableProcessStartedEventImpl)
{
FlowableProcessStartedEventImpl event=(FlowableProcessStartedEventImpl)evt;
if(event.getEntity() instanceof ExecutionEntityImpl){
......@@ -57,9 +81,9 @@ public class ProcessInstanceListener implements FlowableEventListener {
if(executionEntity.getVariable("cloud-serviceid")==null)
return;
String businessKey=(String)executionEntity.getVariable("businessKey");
HashMap curUser=(HashMap) executionEntity.getVariable("curuser");
//HashMap curUser=(HashMap) executionEntity.getVariable("curuser");
FlowUser curUser=FlowUser.getCurUser();
String entity=executionEntity.getVariable("entitys").toString();
String cloudServiceid=executionEntity.getVariable("cloud-serviceid").toString();
Map setting=wfModelService.getProcessGlobalSetting(executionEntity.getProcessDefinitionId());
String wfstepfield="";
......@@ -103,9 +127,9 @@ public class ProcessInstanceListener implements FlowableEventListener {
callbackArg.put(wfverfield,Integer.parseInt(executionEntity.getVariable("wfversion").toString()));
if(callbackArg.size()>0) {
if(curUser.get("bearer-token")==null)
if(StringUtils.isEmpty(curUser.getToken()))
return;
String token=curUser.get("bearer-token").toString();
String token=curUser.getToken();
remoteService.getClient(cloudServiceid).put(entity + "/" + businessKey, token,callbackArg);
}
}
......@@ -113,18 +137,14 @@ public class ProcessInstanceListener implements FlowableEventListener {
else if(evt instanceof FlowableEntityEventImpl)
{
FlowableEntityEventImpl event=((FlowableEntityEventImpl) evt);
// 当前节点任务实体
// TODO 获取到了taskEntity 自己做每个节点的前置操作
FlowableEventType eventType = event.getType();
//待办创建
if(eventType == FlowableEngineEventType.PROCESS_COMPLETED){
ExecutionEntityImpl executionEntity= (ExecutionEntityImpl) event.getEntity();
if(executionEntity.getVariable("cloud-serviceid")==null)
return;
String businessKey=(String)executionEntity.getVariable("businessKey");
HashMap curUser=(HashMap) executionEntity.getVariable("curuser");
//HashMap curUser=(HashMap) executionEntity.getVariable("curuser");
FlowUser curUser=FlowUser.getCurUser();
String entity=executionEntity.getVariable("entitys").toString();
String cloudServiceid=executionEntity.getVariable("cloud-serviceid").toString();
String wfstepfield = executionEntity.getVariable("wfstepfield").toString();
......@@ -138,23 +158,14 @@ public class ProcessInstanceListener implements FlowableEventListener {
if(!StringUtils.isEmpty(wfstepfield))
callbackArg.put(wfstepfield,"");
if(callbackArg.size()>0) {
if(curUser.get("bearer-token")==null)
if(StringUtils.isEmpty(curUser.getToken()))
return;
String token=curUser.get("bearer-token").toString();
String token=curUser.getToken();
remoteService.getClient(cloudServiceid).put(entity + "/" + businessKey, token,callbackArg);
}
System.out.println("流程结束");
}
if(eventType == FlowableEngineEventType.PROCESS_COMPLETED_WITH_ERROR_END_EVENT){
event.getEntity();
System.out.println("流程异常结束");
}
if(eventType == FlowableEngineEventType.TASK_COMPLETED){
TaskEntity taskEntity = (TaskEntity) event.getEntity();
event.getEntity();
System.out.println("流程异常结束");
}
......@@ -162,15 +173,13 @@ public class ProcessInstanceListener implements FlowableEventListener {
else if(evt instanceof FlowableActivityEventImpl)
{
FlowableActivityEventImpl event=((FlowableActivityEventImpl) evt);
// 当前节点任务实体
// TODO 获取到了taskEntity 自己做每个节点的前置操作
FlowableEventType eventType = event.getType();
if(eventType == FlowableEngineEventType.ACTIVITY_STARTED && "userTask".equals(event.getActivityType())){
if(event.getExecution().getVariable("cloud-serviceid")==null)
return;
String businessKey=(String)event.getExecution().getVariable("businessKey");
HashMap curUser=(HashMap) event.getExecution().getVariable("curuser");
//HashMap curUser=(HashMap) event.getExecution().getVariable("curuser");
FlowUser curUser=FlowUser.getCurUser();
String entity=event.getExecution().getVariable("entitys").toString();
String cloudServiceid=event.getExecution().getVariable("cloud-serviceid").toString();
String wfstepfield = event.getExecution().getVariable("wfstepfield").toString();
......@@ -178,12 +187,11 @@ public class ProcessInstanceListener implements FlowableEventListener {
if(!StringUtils.isEmpty(wfstepfield)&&event.getActivityId().startsWith("tid-"))
callbackArg.put(wfstepfield,event.getActivityId().split("-")[1]);
if(callbackArg.size()>0) {
if(curUser.get("bearer-token")==null)
if(StringUtils.isEmpty(curUser.getToken()))
return;
String token=curUser.get("bearer-token").toString();
String token=curUser.getToken();
remoteService.getClient(cloudServiceid).put(entity + "/" + businessKey, token,callbackArg);
}
}
}
......
package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.extensions.domain.FlowUser;
import cn.ibizlab.core.workflow.domain.*;
import cn.ibizlab.core.workflow.service.IWFGroupService;
import cn.ibizlab.core.workflow.service.IWFProcessDefinitionService;
import cn.ibizlab.util.domain.EntityBase;
import cn.ibizlab.core.workflow.service.IWFUserService;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.helper.RuleUtils;
import cn.ibizlab.util.security.AuthTokenUtil;
import cn.ibizlab.util.security.AuthenticationUser;
import cn.ibizlab.util.service.RemoteService;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.BpmnAutoLayout;
import org.flowable.bpmn.converter.BpmnXMLConverter;
import org.flowable.bpmn.model.*;
import org.flowable.bpmn.model.Process;
import org.flowable.common.engine.api.history.HistoricData;
import org.flowable.common.engine.api.identity.AuthenticationContext;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.common.engine.impl.identity.UserIdAuthenticationContext;
import org.flowable.editor.language.json.converter.BpmnJsonConverter;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.history.ProcessInstanceHistoryLog;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.DeploymentBuilder;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;
import org.flowable.engine.task.Comment;
import org.flowable.identitylink.api.IdentityLink;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.flowable.task.service.impl.persistence.entity.HistoricTaskInstanceEntity;
import org.flowable.ui.common.security.SecurityUtils;
import org.flowable.ui.modeler.domain.AbstractModel;
import org.flowable.ui.modeler.domain.AppModelDefinition;
......@@ -37,8 +45,6 @@ import org.flowable.ui.modeler.model.AppDefinitionRepresentation;
import org.flowable.ui.modeler.model.AppDefinitionSaveRepresentation;
import org.flowable.ui.modeler.model.ModelKeyRepresentation;
import org.flowable.ui.modeler.model.ModelRepresentation;
import org.flowable.ui.modeler.repository.ModelRepository;
import org.flowable.ui.modeler.service.ModelServiceImpl;
import org.flowable.ui.modeler.serviceapi.AppDefinitionService;
import org.flowable.ui.modeler.serviceapi.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -51,6 +57,7 @@ import org.springframework.util.StringUtils;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import java.io.*;
import java.security.Principal;
import java.sql.Timestamp;
import java.util.*;
......@@ -74,6 +81,10 @@ public class WFCoreService
@Autowired
private ModelService modelService;
@Autowired
private IWFUserService iwfUserService;
@Autowired
protected AppDefinitionService appDefinitionService;
......@@ -86,23 +97,19 @@ public class WFCoreService
@Autowired
private AuthTokenUtil jwtTokenUtil;
private HashMap getCurUser()
private AuthenticationContext createAuthenticationContext()
{
HashMap curUser=new HashMap();
UserIdAuthenticationContext context=new UserIdAuthenticationContext();
AuthenticationUser user=AuthenticationUser.getAuthenticationUser();
curUser.put("userid",user.getUserid());
curUser.put("username",user.getUsername());
curUser.put("personname",user.getPersonname());
curUser.put("orgid",user.getOrgid());
curUser.put("mdeptid",user.getMdeptid());
curUser.put("bcode",user.getBcode());
curUser.put("orgInfo",user.getOrgInfo());
curUser.put("sessionParams",user.getSessionParams());
curUser.put("bearer-token","Bearer "+jwtTokenUtil.generateToken(user));
return curUser;
FlowUser principal=new FlowUser();
principal.setUser(user);
principal.setToken("Bearer "+jwtTokenUtil.generateToken(user));
context.setPrincipal((Principal) principal);
return context;
}
public List<WFProcessDefinition> getWorkflow(String system,String appname,String entity) {
return wfModelService.getWorkflow(system,entity);
}
......@@ -138,6 +145,7 @@ public class WFCoreService
if(StringUtils.isEmpty(userId))
return wfStepCnt;
//runtimeService.createNativeProcessInstanceQuery().
TaskQuery query=taskService.createTaskQuery().taskCandidateOrAssigned(userId);
if(!StringUtils.isEmpty(processDefinitionKey))
query.processDefinitionKey(processDefinitionKey);
......@@ -203,7 +211,7 @@ public class WFCoreService
Map<String, Object> variables = new LinkedHashMap<>();
variables.put("businessKey",businessKey);
variables.put("activedata",instance.get("activedata"));
variables.put("curuser",getCurUser());
//variables.put("curuser",getCurUser());
variables.put("cloud-serviceid",system+"-"+appname);
variables.put("system",system);
variables.put("appname",appname);
......@@ -211,6 +219,7 @@ public class WFCoreService
variables.put("wfversion",version);
//根据流程定义启动流程
Authentication.setAuthenticatedUserId(userId);
Authentication.setAuthenticationContext(createAuthenticationContext());
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey,processInstanceBusinessKey, variables);//流程定时标识、业务标识、变量
instance.setBusinesskey(processInstanceBusinessKey);
instance.setId(processInstance.getId());
......@@ -344,22 +353,151 @@ public class WFCoreService
}
Map<String, Object> transientVariables = new LinkedHashMap<>();
transientVariables.put("activedata",activedata);
transientVariables.put("curuser",getCurUser());
//transientVariables.put("curuser",getCurUser());
//根据流程定义启动流程
Authentication.setAuthenticatedUserId(userId);
String wfprocdesc="";
if(activedata.get("wfprocdesc")!=null)
wfprocdesc=activedata.get("wfprocdesc").toString();
Authentication.setAuthenticationContext(createAuthenticationContext());
taskService.addComment(taskId,taskWay.getProcessinstanceid(),taskWay.getSequenceflowname(),wfprocdesc);
taskService.complete(taskId, variables,transientVariables);
WFProcessInstance instance = new WFProcessInstance();
instance.setBusinesskey(processInstanceBusinessKey);
return instance;
}
public WFProcessInstance getWFHistory(String system,String appname,
String entity,String businessKey,String processInstanceId)
{
WFProcessInstance wfProcessInstance=new WFProcessInstance();
String processInstanceBusinessKey=system+":"+entity+":k-"+businessKey;
List<String> processInstanceIds=new ArrayList<>();
if(StringUtils.isEmpty(processInstanceId))
{
List<HistoricProcessInstance> instances=historyService.createHistoricProcessInstanceQuery().processInstanceBusinessKey(processInstanceBusinessKey).orderByProcessInstanceStartTime().asc().list();
if(instances.size()==0)
return wfProcessInstance;
for(HistoricProcessInstance instance:instances)
{
processInstanceIds.add(instance.getId());
}
}
else
processInstanceIds.add(processInstanceId);
Map<String,WFProcessNode> nodes=new LinkedHashMap<>();
List<Comment> comments = new ArrayList<>();
Map<String,HistoricTaskInstanceEntity> tasks=new LinkedHashMap<>();
Set<String> waitTaskId=new LinkedHashSet<>();
Map<String,WFUser> wfUserMap = new HashMap<>();
Timestamp endTime=null;
for(String id:processInstanceIds) {
ProcessInstanceHistoryLog processInstanceHistoryLog = historyService.createProcessInstanceHistoryLogQuery(id).includeTasks().includeActivities().includeComments().singleResult();
if(!StringUtils.isEmpty(processInstanceHistoryLog.getStartUserId()))
{
WFUser user=new WFUser();
user.setId(processInstanceHistoryLog.getStartUserId());
wfUserMap.put(processInstanceHistoryLog.getStartUserId(),user);
}
if(StringUtils.isEmpty(wfProcessInstance.getId())) {
wfProcessInstance.setId(processInstanceHistoryLog.getId());
wfProcessInstance.setStarttime(new Timestamp(processInstanceHistoryLog.getStartTime().getTime()));
wfProcessInstance.setBusinesskey(processInstanceHistoryLog.getBusinessKey());
wfProcessInstance.setStartuserid(processInstanceHistoryLog.getStartUserId());
}
if(processInstanceHistoryLog.getEndTime()!=null)
{
if(endTime==null||processInstanceHistoryLog.getEndTime().getTime()>=endTime.getTime())
endTime = (new Timestamp(processInstanceHistoryLog.getEndTime().getTime()));
}
List<HistoricData> historicData = processInstanceHistoryLog.getHistoricData();
for (HistoricData data :historicData){
if (data instanceof HistoricActivityInstance){
HistoricActivityInstance hai= (HistoricActivityInstance) data;
if(!hai.getActivityType().equals("userTask"))
continue;
if(!nodes.containsKey(hai.getActivityId()))
{
WFProcessNode node=new WFProcessNode();
node.setUsertaskid(hai.getActivityId());
node.setUsertaskname(hai.getActivityName());
node.set("comments",new ArrayList<WFHistory>());
node.set("identityLinks",new ArrayList<WFUser>());
nodes.put(hai.getActivityId(),node);
}
}
else if(data instanceof Comment)
{
Comment comment=(Comment)data;
if(!StringUtils.isEmpty(comment.getUserId())){
WFUser user=new WFUser();
user.setId(comment.getUserId());
wfUserMap.put(comment.getUserId(),user);
comments.add(comment);
}
}
else if(data instanceof HistoricTaskInstanceEntity)
{
HistoricTaskInstanceEntity historicTaskInstanceEntity= (HistoricTaskInstanceEntity) data;
tasks.put(historicTaskInstanceEntity.getId(),historicTaskInstanceEntity);
if(historicTaskInstanceEntity.getEndTime()==null)
waitTaskId.add(historicTaskInstanceEntity.getId());
}
}
}
if(waitTaskId.size()>0){
for(String taskid:waitTaskId) {
List<IdentityLink> idlinks = taskService.getIdentityLinksForTask(taskid);
for(IdentityLink idlink:idlinks)
{
WFUser user=new WFUser();
user.setId(idlink.getUserId());
wfUserMap.put(idlink.getUserId(),user);
((ArrayList)nodes.get(tasks.get(taskid).getTaskDefinitionKey()).get("identityLinks")).add(user);
}
}
}
else
wfProcessInstance.setEndtime(endTime);
if(wfUserMap.size()>0)
iwfUserService.listByIds(wfUserMap.keySet()).forEach(item ->wfUserMap.get(item.getId()).setDisplayname(item.getDisplayname()));
for(Comment comment:comments)
{
if(tasks.containsKey(comment.getTaskId())&&(nodes.containsKey(tasks.get(comment.getTaskId()).getTaskDefinitionKey()))) {
if(wfUserMap.containsKey(comment.getUserId())) {
WFHistory history = new WFHistory();
history.setId(comment.getId());
history.setAuthor(comment.getUserId());
history.setAuthorname(wfUserMap.get(comment.getUserId()).getDisplayname());
history.setFullmessage(comment.getFullMessage());
history.setTime(new Timestamp(comment.getTime().getTime()));
history.setType(comment.getType());
//history.setTaskid(comment.getTaskId());
//history.setProcessinstanceid(comment.getProcessInstanceId());
//history.setProcessinstancebusinesskey(businessKey);
((ArrayList) nodes.get(tasks.get(comment.getTaskId()).getTaskDefinitionKey()).get("comments")).add(history);
}
}
}
wfProcessInstance.set("userTasks",nodes.values());
if(!StringUtils.isEmpty(wfProcessInstance.getStartuserid()))
{
wfProcessInstance.setStartusername(wfUserMap.get(wfProcessInstance.getStartuserid()).getDisplayname());
}
wfProcessInstance.setBusinesskey(businessKey);
return wfProcessInstance;
}
@Value("${ibiz.filePath:/app/file/}")
private String fileRoot;
......@@ -619,7 +757,8 @@ public class WFCoreService
if(delegateExecution.getCurrentFlowElement() instanceof ServiceTask && activedata instanceof Map)
{
ServiceTask task=(ServiceTask)delegateExecution.getCurrentFlowElement();
HashMap curUser=(HashMap)delegateExecution.getVariable("curuser");
//HashMap curUser=(HashMap)delegateExecution.getVariable("curuser");
FlowUser curUser=FlowUser.getCurUser();
String businessKey=(String)delegateExecution.getVariable("businessKey");
String cloudServiceId=(String)delegateExecution.getVariable("cloud-serviceid");
Map entity=(Map) activedata;
......@@ -644,9 +783,9 @@ public class WFCoreService
}
if(StringUtils.isEmpty(serviceEntity)||StringUtils.isEmpty(serviceDEAction))
return;
if(curUser.get("bearer-token")==null)
if(StringUtils.isEmpty(curUser.getToken()))
return;
String token=curUser.get("bearer-token").toString();
String token=curUser.getToken();
String path=serviceEntity;
if(serviceDEAction.equalsIgnoreCase("create"))
remoteService.getClient(cloudServiceId).post(path,token,entity);
......
......@@ -65,6 +65,16 @@ public class WFCoreResource
return ResponseEntity.status(HttpStatus.OK).body(wfCoreService.getTaskLink(system,appname,entity,businessKey,taskId));
}
@ApiOperation(value = "getWFHistory", tags = {"getWFHistory" }, notes = "根据业务主键获取审批意见记录")
@RequestMapping(method = RequestMethod.GET, value = "/{system}-app-{appname}/{entity}/{businessKey}/process-instances/{processInstanceId}/history")
public ResponseEntity<WFProcessInstance> getWFHistory(@PathVariable("system") String system,@PathVariable("appname") String appname,
@PathVariable("entity") String entity,
@PathVariable("businessKey") String businessKey,@PathVariable("processInstanceId") String processInstanceId) {
if(StringUtils.isEmpty(processInstanceId)||"null".equals(processInstanceId)||"alls".equals(processInstanceId))
processInstanceId="";
return ResponseEntity.status(HttpStatus.OK).body(wfCoreService.getWFHistory(system,appname,entity,businessKey,processInstanceId));
}
//@PreAuthorize("hasPermission(#entity,'WFSTART',this.getEntity())")
@ApiOperation(value = "wfsubmit", tags = {"WFProcessInstance" }, notes = "工作流执行步骤")
@RequestMapping(method = RequestMethod.POST, value = "/{system}-app-{appname}/{entity}/{businessKey}/tasks/{taskId}")
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册