Json格式转换成Bpmn协议

This commit is contained in:
lvshaohua 2023-06-05 09:48:56 +08:00
parent 7596017adf
commit 21e48ea486
3 changed files with 188 additions and 5 deletions

View File

@ -40,7 +40,9 @@ public interface BpmModelService {
/**
* 部署模型
* */
Boolean deployBpmModel(String modelId);
Boolean deployBpmModelById(String modelId);
Boolean deployBpmModelByKey(String modelKey);
/**
* 删除模型

View File

@ -15,6 +15,7 @@ import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Model;
import org.flowable.engine.repository.ModelQuery;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.springframework.context.annotation.Lazy;
@ -43,7 +44,15 @@ public class BpmModelServiceImpl implements BpmModelService {
model.setTenantId(createDTO.getTenantId());
Model existModel = repositoryService.createModelQuery().modelKey(createDTO.getKey()).singleResult();
repositoryService.createDeploymentQuery().singleResult();
repositoryService.createModelQuery().modelKey("asdfasdf")
.singleResult();
if (!ObjectUtils.isEmpty(existModel)) {
throw new RuntimeException("模型已存在");
}
@ -69,16 +78,41 @@ public class BpmModelServiceImpl implements BpmModelService {
return BpmModelConverter.convert(model);
}
@Override
public Boolean updateBpmModel(BpmModelUpdateDTO updateDTO) {
return null;
}
@Override
public Boolean deployBpmModel(String modelId) {
public Boolean deployBpmModelById(String modelId) {
Model model = repositoryService.getModel(modelId);
byte[] bpmnBytes = this.repositoryService.getModelEditorSource(model.getId());
if (bpmnBytes == null) {
throw new RuntimeException("模型文件不存在");
} else {
BpmFormDO form = this.checkFormConfig(model.getMetaInfo());
this.taskAssignRuleService.checkTaskAssignRuleAllConfig(id);
BpmProcessDefinitionCreateReqDTO definitionCreateReqDTO = BpmModelConvert.INSTANCE.convert2(model, form).setBpmnBytes(bpmnBytes);
if (this.processDefinitionService.isProcessDefinitionEquals(definitionCreateReqDTO)) {
ProcessDefinition oldProcessInstance = this.processDefinitionService.getProcessDefinitionByDeploymentId(model.getDeploymentId());
if (oldProcessInstance != null && this.taskAssignRuleService.isTaskAssignRulesEquals(model.getId(), oldProcessInstance.getId())) {
throw ServiceExceptionUtil.exception(ErrorCodeConstants.MODEL_DEPLOY_FAIL_TASK_INFO_EQUALS);
}
}
return null;
String definitionId = this.processDefinitionService.createProcessDefinition(definitionCreateReqDTO);
this.updateProcessDefinitionSuspended(model.getDeploymentId());
ProcessDefinition definition = this.processDefinitionService.getProcessDefinition(definitionId);
model.setDeploymentId(definition.getDeploymentId());
this.repositoryService.saveModel(model);
this.taskAssignRuleService.copyTaskAssignRules(id, definition.getId());
}
@Override
public Boolean deployBpmModelByKey(String modelKey) {
Model model = repositoryService.createModelQuery().modelKey(modelKey).singleResult();
repositoryService.de
}
@Override

View File

@ -0,0 +1,147 @@
package cn.axzo.server.service.util;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.flowable.bpmn.converter.BpmnXMLConverter;
import org.flowable.bpmn.model.*;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;
import static org.flowable.bpmn.model.ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION;
public class BpmTransformUtil {
private static String create(String fromId, com.alibaba.fastjson.JSONObject flowNode, BpmnModel model, Process process, List<SequenceFlow> sequenceFlows) throws InvocationTargetException, IllegalAccessException {
String nodeType = flowNode.getString("type");
if ("userTask".equals(nodeType)) {
String id = fromId;
// 如果当前任务还有后续任务则遍历创建后续任务
com.alibaba.fastjson.JSONObject nextNode = flowNode.getJSONObject("childNode");
if (Objects.nonNull(nextNode)) {
FlowElement flowElement = model.getFlowElement(id);
return create(id, nextNode,model,process,sequenceFlows);
} else {
return id;
}
}
else if ("condition".equals(nodeType)) {
flowNode.put("incoming", Collections.singletonList(fromId));
String id = createTask(flowNode,process,sequenceFlows);
// 如果当前任务还有后续任务则遍历创建后续任务
com.alibaba.fastjson.JSONObject nextNode = flowNode.getJSONObject("childNode");
if (Objects.nonNull(nextNode)&& Objects.nonNull(nextNode.get("nodeId"))) {
FlowElement flowElement = model.getFlowElement(id);
return create(id, nextNode,model,process,sequenceFlows);
} else {
return id;
}
}
else {
throw new RuntimeException("未知节点类型: nodeType=" + nodeType);
}
}
private static String createTask(com.alibaba.fastjson.JSONObject flowNode, Process process, List<SequenceFlow> sequenceFlows) {
List<String> incoming = flowNode.getJSONArray("incoming").toJavaList(String.class);
// 自动生成id
String id = flowNode.getString("nodeId");
if (incoming != null && !incoming.isEmpty()) {
UserTask userTask = new UserTask();
userTask.setName(flowNode.getString("nodeName"));
userTask.setId(id);
process.addFlowElement(userTask);
if(DDFlowNodeType.NODE_STARTER.name().equals(flowNode.getString("type"))){
id = WorkflowConstants.START_EVENT_ID;
}
else{
// if ((!ObjectUtils.isEmpty(flowNode.getJSONArray("nodeUserList")) && flowNode.getJSONArray("nodeUserList").size() > 1)
// || flowNode.getBoolean(""))
MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics=new MultiInstanceLoopCharacteristics();
String examineMode = flowNode.getString("examineMode");
// 审批人集合参数
multiInstanceLoopCharacteristics.setInputDataItem(userTask.getId()+"_assigneeList");
// 迭代集合
multiInstanceLoopCharacteristics.setElementVariable("assignee");
// 并行
multiInstanceLoopCharacteristics.setSequential(false);
// userTask.setAssignee("${assigneeName}");
// 设置多实例属性
if("NEXT".equals(examineMode)){
multiInstanceLoopCharacteristics.setSequential(true);
} else if("OR".equals(examineMode)){
multiInstanceLoopCharacteristics.setCompletionCondition("${nrOfCompletedInstances==1}");
} else if ("AND".equals(examineMode)) {
multiInstanceLoopCharacteristics.setCompletionCondition("${nrOfCompletedInstances==nrOfInstances}");
}
ArrayList<FlowableListener> taskListeners = new ArrayList<>();
FlowableListener taskListener = new FlowableListener();
// 事件类型,
taskListener.setEvent(TaskListener.EVENTNAME_CREATE);
// 监听器类型
taskListener.setImplementationType(IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
// 设置实现了这里设置监听器的类型是delegateExpression这样可以在实现类注入Spring bean.
taskListener.setImplementation("${DDTaskCreateListener}");
taskListeners.add(taskListener);
userTask.setLoopCharacteristics(multiInstanceLoopCharacteristics);
userTask.setTaskListeners(taskListeners);
}
SequenceFlow sequenceFlow = connect(incoming.get(0), id,sequenceFlows);
if (sequenceFlow != null) {
process.addFlowElement(sequenceFlow);
}
}
return id;
}
@SneakyThrows
private static String id(String prefix) {
long qutient = (System.currentTimeMillis() - DateUtils.parseDate("2020-08-01", "yyyy-MM-dd").getTime());
qutient += Math.ceil(Math.random() * 1000); // 防止重複
String chars = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz";
int radix = chars.length();
StringBuilder stringBuffer = new StringBuilder(prefix + "_");
do {
int mod = (int) (qutient % radix);
qutient = ( qutient - mod ) / radix;
stringBuffer.append(chars.charAt(mod));
} while ( qutient > 0 );
return stringBuffer.toString();
}
private static ServiceTask serviceTask(String name) {
ServiceTask serviceTask = new ServiceTask();
serviceTask.setName(name);
return serviceTask;
}
protected static SequenceFlow connect(String from, String to,List<SequenceFlow> sequenceFlows) {
SequenceFlow flow = new SequenceFlow();
flow.setId(id("sequenceFlow"));
flow.setSourceRef(from);
flow.setTargetRef(to);
// 连接线必须同时有 source target
if (from == null || to == null) {
return null;
}
sequenceFlows.add(flow);
return flow;
}
}