1.演示
2.代码
@Override
@Transactional
public Object modelDeploy(ReqModel reqModel) throws IllegalAccessException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, ClassNotFoundException {
/**
* 1.根据modelId获取模型信息
* 2.转换为xml对象
* 3.部署对象以模型对象的名称命名
* 4.部署转换出来的xml对象
* */
RetBean retbean = new RetBean();
FlowModel model = this.queryFlowModelById(reqModel.getModelId());
if (model == null) {
return retbean.backError("No model found with the given id: " + reqModel.getModelId());
}
BpmnModel bpmnModel = getBpmnModel(model);
for (Process process : bpmnModel.getProcesses()) {
if (StringUtils.isNotEmpty(process.getId())) {
char firstCharacter = process.getId().charAt(0);
if (Character.isDigit(firstCharacter)) {
process.setId("a" + process.getId());
}
}
}
byte[] bytes = BPMN_XML_CONVERTER.convertToXML(bpmnModel);
String processName = model.getName();
if (!StringUtil.endsWithIgnoreCase(processName, FlowEngineConstant.SUFFIX)) {
processName += FlowEngineConstant.SUFFIX;
}
String finalProcessName = processName;
String[] ids = null;
if (!StringUtil.isEmpty(reqModel.getTenantIds())) {
ids = reqModel.getTenantIds().split(",");
}
if (!StringUtil.isEmpty(ids)) {
for (String tenantId : ids) {
Deployment deployment = repositoryService.createDeployment().addBytes(finalProcessName, bytes).name(model.getName()).key(model.getModel_key()).tenantId(tenantId).deploy();
deploy(deployment, reqModel.getCategory());
}
} else {
Deployment deployment = repositoryService.createDeployment().addBytes(finalProcessName, bytes).name(model.getName()).key(model.getModel_key()).deploy();
deploy(deployment, reqModel.getCategory());
}
return retbean;
}
private BpmnModel getBpmnModel(FlowModel model) throws IllegalAccessException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, ClassNotFoundException {
BpmnModel bpmnModel;
try {
Map<String, FlowModel> formMap = new HashMap<>(16);
Map<String, FlowModel> decisionTableMap = new HashMap<>(16);
List<FlowModel> referencedModels = this.findByParentModelId(model.getId());
for (FlowModel childModel : referencedModels) {
if (FlowModel.MODEL_TYPE_FORM == childModel.getModel_type()) {
formMap.put(childModel.getId(), childModel);
} else if (FlowModel.MODEL_TYPE_DECISION_TABLE == childModel.getModel_type()) {
decisionTableMap.put(childModel.getId(), childModel);
}
}
bpmnModel = getBpmnModel(model, formMap, decisionTableMap);
} catch (Exception e) {
throw new ServiceException("Could not generate BPMN 2.0 model");
}
return bpmnModel;
}
private BpmnModel getBpmnModel(FlowModel model, Map<String, FlowModel> formMap, Map<String, FlowModel> decisionTableMap) throws JsonProcessingException {
ObjectNode editorJsonNode = (ObjectNode) objectMapper.readTree(model.getModel_editor_json());
Map<String, String> formKeyMap = new HashMap<>(16);
for (FlowModel formModel : formMap.values()) {
formKeyMap.put(formModel.getId(), formModel.getModel_key());
}
Map<String, String> decisionTableKeyMap = new HashMap<>(16);
for (FlowModel decisionTableModel : decisionTableMap.values()) {
decisionTableKeyMap.put(decisionTableModel.getId(), decisionTableModel.getModel_key());
}
return BPMN_JSON_CONVERTER.convertToBpmnModel(editorJsonNode, formKeyMap, decisionTableKeyMap);
}
private boolean deploy(Deployment deployment, String category) {
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).list();
// 设置流程分类
for (ProcessDefinition processDefinition : list) {
if (StringUtil.isNotBlank(category)) {
repositoryService.setProcessDefinitionCategory(processDefinition.getId(), category);
}
}
if (list.size() == 0) {
throw new ServiceException("部署失败,未找到流程");
} else {
return true;
}
}