@ -67,7 +67,7 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
@Autowired private AgentService agentService ;
@Autowired private ApplicationContext applicationContext ;
@Autowired private ObjectMapper jsonMapper ;
@Override
@Transactional
public ProcessInstance startProcessByKey ( String processDefinitionKey , String bussinessKey , Map < String , Object > variables , Map < String , Object > transientVariables ) throws Exception {
@ -81,7 +81,6 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
ProcessInstanceBuilder builder = runtimeService . createProcessInstanceBuilder ( ) ;
builder . processDefinitionKey ( processDefinitionKey ) ;
builder . businessKey ( bussinessKey ) ;
//builder.name(getNameByProcessDefinitionKey(processDefinitionKey,bussinessKey));
if ( variables ! = null ) {
builder . variables ( variables ) ;
}
@ -91,7 +90,7 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
insertProcInsExtByDefinitionKey ( processDefinitionKey , processInstance . getId ( ) , bussinessKey ) ;
return startProcess ( processInstance , variables , transientVariables , autoCompleteFirstTask ) ;
}
@Override
@Transactional
public ProcessInstance startProcessById ( String processDefinitionId , String bussinessKey , Map < String , Object > variables , Map < String , Object > transientVariables ) throws Exception {
@ -105,37 +104,36 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
ProcessInstanceBuilder builder = runtimeService . createProcessInstanceBuilder ( ) ;
builder . processDefinitionId ( processDefinitionId ) ;
builder . businessKey ( bussinessKey ) ;
//builder.name(getNameByProcessDefinitionId(processDefinitionId,bussinessKey));
if ( variables ! = null ) {
builder . variables ( variables ) ;
}
builder . transientVariables ( convertTransientVariables ( variables , transientVariables ) ) ;
ProcessInstance processInstance = builder . start ( ) ;
insertProcInsExtByDefinitionId ( processDefinitionId , processInstance . getId ( ) , bussinessKey ) ;
return startProcess ( processInstance , variables , transientVariables , autoCompleteFirstTask ) ;
}
private Map < String , Object > convertTransientVariables ( Map < String , Object > variables , Map < String , Object > transientVariables ) {
Map < String , Object > _transientVariables = new HashMap < String , Object > ( ) ;
if ( transientVariables ! = null ) {
_transientVariables . putAll ( transientVariables ) ;
}
if ( ! _transientVariables . containsKey ( FrameworkVariableNames . GO_BACK ) & & ( variables = = null | | ! variables . containsKey ( FrameworkVariableNames . GO_BACK ) ) ) {
_transientVariables . put ( FrameworkVariableNames . GO_BACK , Integer . MIN_VALUE ) ;
}
if ( ! _transientVariables . containsKey ( FrameworkVariableNames . ASSIGNEE ) & & ( variables = = null | | ! variables . containsKey ( FrameworkVariableNames . ASSIGNEE ) ) ) {
_transientVariables . put ( FrameworkVariableNames . ASSIGNEE , null ) ;
}
if ( _transientVariables . containsKey ( FrameworkVariableNames . FIRST_TASK_ASSIGNEE ) ) {
_transientVariables . put ( FrameworkVariableNames . ASSIGNEE , _transientVariables . get ( FrameworkVariableNames . FIRST_TASK_ASSIGNEE ) ) ;
}
return _transientVariables ;
}
private ProcessInstance startProcess ( ProcessInstance processInstance , Map < String , Object > variables , Map < String , Object > transientVariables , boolean autoCompleteFirstTask ) throws Exception {
List < Task > tasks = taskService . createTaskQuery ( ) . processInstanceId ( processInstance . getId ( ) ) . includeProcessVariables ( ) . list ( ) ;
//如果未找到新任务,表示流程结束,正常返回
@ -148,14 +146,14 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
}
//获取到产生的新任务
Task newTask = tasks . get ( 0 ) ;
//如果需要,自动完成第一个任务
if ( autoCompleteFirstTask ) {
newTask . setAssignee ( ( String ) transientVariables . get ( FrameworkVariableNames . FIRST_TASK_ASSIGNEE ) ) ;
completeTask ( newTask . getId ( ) , variables , transientVariables ) ;
return processInstance ;
}
//如果新任务已经成功分配到了处理人,正常返回
if ( StringUtils . hasText ( newTask . getAssignee ( ) ) ) {
//查找代理人
@ -165,7 +163,7 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
}
return processInstance ;
}
String taskDefinitionKey = newTask . getTaskDefinitionKey ( ) ;
String procDefinitionId = newTask . getProcessDefinitionId ( ) ;
//获取到流程定义模型对象
@ -183,10 +181,10 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
if ( newUserTaskDefinition = = null ) {
return processInstance ;
}
//获取查询任务候选处理人 spring 服务 bean
AssigneeQueryService assigneeQueryService = getAssignmentStrategySpringBean ( model , processInstance , newUserTaskDefinition ) ;
//查找候选处理人
List < Assignee > assignees = assigneeQueryService . query ( null , newTask , newUserTaskDefinition ) ;
@ -216,23 +214,23 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
if ( transientVariables = = null ) {
transientVariables = new HashMap < String , Object > ( ) ;
}
if ( ! transientVariables . containsKey ( FrameworkVariableNames . GO_BACK ) & & ( variables = = null | | ! variables . containsKey ( FrameworkVariableNames . GO_BACK ) ) ) {
transientVariables . put ( FrameworkVariableNames . GO_BACK , Integer . MIN_VALUE ) ;
}
if ( ! transientVariables . containsKey ( FrameworkVariableNames . ASSIGNEE ) & & ( variables = = null | | ! variables . containsKey ( FrameworkVariableNames . ASSIGNEE ) ) ) {
transientVariables . put ( FrameworkVariableNames . ASSIGNEE , null ) ;
}
//获取当前任务
Task task = taskService . createTaskQuery ( ) . taskId ( taskId ) . singleResult ( ) ;
//设置处理意见
Object treatment = transientVariables . get ( FrameworkVariableNames . TASK_TREATMENT ) ;
if ( treatment ! = null & & StringUtils . hasText ( ( String ) treatment ) ) {
taskService . addComment ( taskId , task . getProcessInstanceId ( ) , ( String ) treatment ) ;
}
//尝试完成任务
taskService . complete ( taskId , variables , transientVariables ) ;
//获取流程是否允许领取任务
@ -240,10 +238,10 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
if ( canClaimTask ) {
return ;
}
//通过当前任务获取流程实例ID
String procInstId = task . getProcessInstanceId ( ) ;
//获取当前任务完成后产生的的新任务
List < Task > tasks = taskService . createTaskQuery ( ) . processInstanceId ( procInstId ) . list ( ) ;
//如果未找到新任务,表示流程结束,正常返回
@ -256,7 +254,7 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
}
//获取到产生的新任务
Task newTask = tasks . get ( 0 ) ;
//如果新任务已经成功分配到了处理人,正常返回
if ( StringUtils . hasText ( newTask . getAssignee ( ) ) ) {
AgentEntity agent = getAgent ( newTask . getAssignee ( ) ) ;
@ -265,7 +263,10 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
}
return ;
}
//是否是退回操作
boolean isReturn = isReturn ( task , newTask ) ;
//如果未分配到单一的一个处理人,需要根据流程定义查找可用的处理人
String taskDefinitionKey = newTask . getTaskDefinitionKey ( ) ;
String procDefinitionId = newTask . getProcessDefinitionId ( ) ;
@ -284,7 +285,7 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
if ( newUserTaskDefinition = = null ) {
return ;
}
//如果该节点存在历史任务,表示流程重新回到此节点,
//此时应该将任务尽可能的分配给原处理人
List < HistoricTaskInstance > historyTasks = historyService . createHistoricTaskInstanceQuery ( ) . processInstanceId ( procInstId ) . taskDefinitionKey ( taskDefinitionKey ) . orderByHistoricTaskInstanceEndTime ( ) . desc ( ) . list ( ) ;
@ -299,7 +300,7 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
}
}
String historyAssignee = historyTask . getAssignee ( ) ;
if ( StringUtils . hasText ( historyAssignee ) ) {
if ( StringUtils . hasText ( historyAssignee ) & & isReturn ) {
AgentEntity agent = getAgent ( historyAssignee ) ;
if ( agent ! = null ) {
taskService . setAssignee ( newTask . getId ( ) , agent . getAgentLoginName ( ) ) ;
@ -309,11 +310,11 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
return ;
}
}
//获取查询任务候选处理人 spring 服务 bean
ProcessInstance processInstance = runtimeService . createProcessInstanceQuery ( ) . processInstanceId ( task . getProcessInstanceId ( ) ) . singleResult ( ) ;
AssigneeQueryService assigneeQueryService = getAssignmentStrategySpringBean ( model , processInstance , newUserTaskDefinition ) ;
//查找候选处理人
List < Assignee > assignees = assigneeQueryService . query ( task , newTask , newUserTaskDefinition ) ;
//是否强制选择候选处理人(即只有一个候选人也需要用户选择)
@ -337,13 +338,13 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
public void claimTask ( String taskId ) throws Exception {
taskService . claim ( taskId , SecurityUtil . getLoginName ( ) ) ;
}
@Override
@Transactional
public void unClaimTask ( String taskId ) throws Exception {
taskService . unclaim ( taskId ) ;
}
@Override
@Transactional
public void terminateProcessInstance ( String taskId , String deleteReason ) throws Exception {
@ -353,6 +354,84 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
}
}
/ * *
* 判断节点间连线是否是回退操作
* @param fromTask from 任务
* @param toTask to 任务
* @return 是否是回退操作
* /
private boolean isReturn ( Task fromTask , Task toTask ) {
if ( fromTask = = null | | toTask = = null ) {
return false ;
}
// from 节点 KEY
String fromTaskDefinitionKey = fromTask . getTaskDefinitionKey ( ) ;
if ( ! StringUtils . hasText ( fromTaskDefinitionKey ) ) {
return false ;
}
// to 节点 Key
String toTaskDefinitionKey = toTask . getTaskDefinitionKey ( ) ;
if ( ! StringUtils . hasText ( toTaskDefinitionKey ) ) {
return false ;
}
//获取流程定义模型对象
BpmnModel model = repositoryService . getBpmnModel ( fromTask . getProcessDefinitionId ( ) ) ;
if ( model = = null ) {
return false ;
}
//获取所有元素
Collection < FlowElement > elements = model . getMainProcess ( ) . getFlowElements ( ) ;
if ( elements = = null | | elements . size ( ) = = 0 ) {
return false ;
}
//查找 from 任务的节点
UserTask fromUserTaskDefinition = null ;
if ( elements ! = null & & elements . size ( ) > 0 ) {
for ( FlowElement element : elements ) {
if ( fromTaskDefinitionKey . equals ( element . getId ( ) ) & & ( element instanceof UserTask ) ) {
fromUserTaskDefinition = ( UserTask ) element ;
break ;
}
}
}
if ( fromUserTaskDefinition = = null ) {
return false ;
}
//查找 to 任务的节点
UserTask toUserTaskDefinition = null ;
if ( elements ! = null & & elements . size ( ) > 0 ) {
for ( FlowElement element : elements ) {
if ( toTaskDefinitionKey . equals ( element . getId ( ) ) & & ( element instanceof UserTask ) ) {
toUserTaskDefinition = ( UserTask ) element ;
break ;
}
}
}
if ( toUserTaskDefinition = = null ) {
return false ;
}
// 查找从 from 到 to 的线
SequenceFlow from2toFlow = null ;
List < SequenceFlow > flows = fromUserTaskDefinition . getOutgoingFlows ( ) ;
for ( SequenceFlow flow : flows ) {
if ( toTaskDefinitionKey . equals ( flow . getTargetFlowElement ( ) . getId ( ) ) ) {
from2toFlow = flow ;
break ;
}
}
if ( from2toFlow = = null ) {
return false ;
}
// 获取线上配置的 goback 值
Integer value = parseGobackVariableValue ( from2toFlow . getConditionExpression ( ) ) ;
if ( value = = null ) {
return false ;
}
return value < 0 ;
}
@Override
@Transactional
public List < Goback > getGobacks ( String taskId ) throws Exception {
@ -364,23 +443,23 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
if ( task = = null ) {
return null ;
}
String taskDefinitionKey = task . getTaskDefinitionKey ( ) ;
if ( ! StringUtils . hasText ( taskDefinitionKey ) ) {
return null ;
}
//获取流程定义模型对象
BpmnModel model = repositoryService . getBpmnModel ( task . getProcessDefinitionId ( ) ) ;
if ( model = = null ) {
return null ;
}
Collection < FlowElement > elements = model . getMainProcess ( ) . getFlowElements ( ) ;
if ( elements = = null | | elements . size ( ) = = 0 ) {
return null ;
}
UserTask userTaskDefinition = null ;
if ( elements ! = null & & elements . size ( ) > 0 ) {
for ( FlowElement element : elements ) {
@ -397,7 +476,7 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
if ( flows = = null | | flows . size ( ) = = 0 ) {
return null ;
}
List < Goback > gobacks = new ArrayList < Goback > ( ) ;
for ( SequenceFlow flow : flows ) {
Integer value = parseGobackVariableValue ( flow . getConditionExpression ( ) ) ;
@ -423,7 +502,7 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
} ) ;
return gobacks ;
}
@Override
@Transactional
public void jumpTask ( String taskId , String targetActivityId , String targetAssignee ) throws Exception {
@ -472,7 +551,7 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
}
jdbcTemplate . update ( "insert into SYS_PROCESS_INST_EXT(PROC_INST_ID_,CUST_NO_,CUST_NAME_) values(?,?,?)" , processInstanceId , wrapper . getCustNo ( ) , wrapper . getCustName ( ) ) ;
}
private AssigneeQueryService getAssignmentStrategySpringBean ( BpmnModel model , ProcessInstance processInstance , UserTask taskDefine ) {
String beanName = DEFAULT_ASSIGNMENT_STRATEGY_SPRING_BEAN_NAME ;
String processDefinitionKey = processInstance . getProcessDefinitionKey ( ) ;
@ -531,7 +610,7 @@ public class ProcessOperationServiceImpl implements ProcessOperationService {
}
return null ;
}
private Integer parseGobackVariableValue ( String conditionExpression ) {
if ( StringUtils . hasText ( conditionExpression ) ) {
Matcher matcher = GO_BACK_VARIABLE_PATTERN . matcher ( conditionExpression ) ;