use of org.flowable.bpmn.model.BpmnModel in project plumdo-work by wengwh.
the class ProcessDefinitionToModelResource method processDefinitionToModel.
@RequestMapping(value = "/process-definition/{processDefinitionId}/toModel", method = RequestMethod.PUT, produces = "application/json", name = "流程定义转换模型")
@ResponseStatus(value = HttpStatus.OK)
@Transactional(propagation = Propagation.REQUIRED)
public ModelResponse processDefinitionToModel(@PathVariable String processDefinitionId) {
ProcessDefinition processDefinition = getProcessDefinitionFromRequest(processDefinitionId);
try {
InputStream bpmnStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());
XMLInputFactory xif = XMLInputFactory.newInstance();
InputStreamReader in = new InputStreamReader(bpmnStream, "UTF-8");
XMLStreamReader xtr = xif.createXMLStreamReader(in);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
BpmnJsonConverter converter = new BpmnJsonConverter();
ObjectNode modelNode = converter.convertToJson(bpmnModel);
Model modelData = repositoryService.newModel();
modelData.setKey(processDefinition.getKey());
modelData.setName(processDefinition.getName());
modelData.setCategory(processDefinition.getCategory());
ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, processDefinition.getName());
modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, processDefinition.getDescription());
modelData.setMetaInfo(modelObjectNode.toString());
repositoryService.saveModel(modelData);
repositoryService.addModelEditorSource(modelData.getId(), modelNode.toString().getBytes("utf-8"));
repositoryService.addModelEditorSourceExtra(modelData.getId(), IOUtils.toByteArray(repositoryService.getProcessDiagram(processDefinition.getId())));
return restResponseFactory.createModelResponse(modelData);
} catch (Exception e) {
throw new FlowableException("Error converting process-definition to model", e);
}
}
use of org.flowable.bpmn.model.BpmnModel in project syncope by apache.
the class FlowableDeployUtils method deployModel.
public static void deployModel(final ProcessEngine engine, final ProcessDefinition procDef) {
XMLStreamReader xtr = null;
try (InputStream bpmnStream = engine.getRepositoryService().getResourceAsStream(procDef.getDeploymentId(), procDef.getResourceName());
InputStreamReader isr = new InputStreamReader(bpmnStream)) {
xtr = XMLInputFactory.newInstance().createXMLStreamReader(isr);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
Model model = engine.getRepositoryService().newModel();
ObjectNode modelObjectNode = OBJECT_MAPPER.createObjectNode();
modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, procDef.getName());
model.setMetaInfo(modelObjectNode.toString());
model.setName(procDef.getName());
model.setDeploymentId(procDef.getDeploymentId());
model.setVersion(procDef.getVersion());
engine.getRepositoryService().saveModel(model);
engine.getRepositoryService().addModelEditorSource(model.getId(), new BpmnJsonConverter().convertToJson(bpmnModel).toString().getBytes());
} catch (Exception e) {
throw new WorkflowException("While importing " + procDef.getResourceName(), e);
} finally {
if (xtr != null) {
try {
xtr.close();
} catch (XMLStreamException e) {
// ignore
}
}
}
}
use of org.flowable.bpmn.model.BpmnModel in project plumdo-work by wengwh.
the class ProcessDefinitionGetModelResource method processDefinitionGetModel.
@RequestMapping(value = "/process-definitions/{processDefinitionId}/getModel", method = RequestMethod.GET, produces = "application/json", name = "流程定义获取对应模型")
public ModelResponse processDefinitionGetModel(@PathVariable String processDefinitionId) {
ProcessDefinition processDefinition = getProcessDefinitionFromRequest(processDefinitionId);
try {
Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(processDefinition.getDeploymentId()).singleResult();
if (deployment == null) {
throw new FlowableObjectNotFoundException("Could not find a process deployment with id '" + processDefinition.getDeploymentId() + "'.", Deployment.class);
}
Model modelData = null;
if (deployment.getCategory() != null) {
modelData = repositoryService.getModel(deployment.getCategory());
}
// 如果不存在,创建对应模型
if (modelData == null) {
InputStream bpmnStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());
XMLInputFactory xif = XMLInputFactory.newInstance();
InputStreamReader in = new InputStreamReader(bpmnStream, "UTF-8");
XMLStreamReader xtr = xif.createXMLStreamReader(in);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
BpmnJsonConverter converter = new BpmnJsonConverter();
ObjectNode modelNode = converter.convertToJson(bpmnModel);
modelData = repositoryService.newModel();
modelData.setKey(processDefinition.getKey());
modelData.setName(processDefinition.getName());
modelData.setCategory(processDefinition.getCategory());
ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, processDefinition.getName());
modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, processDefinition.getDescription());
modelData.setMetaInfo(modelObjectNode.toString());
repositoryService.saveModel(modelData);
repositoryService.addModelEditorSource(modelData.getId(), modelNode.toString().getBytes("utf-8"));
repositoryService.addModelEditorSourceExtra(modelData.getId(), IOUtils.toByteArray(managementService.executeCommand(new GetDeploymentProcessDiagramCmd(processDefinitionId))));
repositoryService.setDeploymentCategory(processDefinition.getDeploymentId(), modelData.getId());
}
return restResponseFactory.createModelResponse(modelData);
} catch (Exception e) {
throw new FlowableException("Error process-definition get model", e);
}
}
use of org.flowable.bpmn.model.BpmnModel in project plumdo-work by wengwh.
the class SaveModelEditorCmd method execute.
@Override
public Void execute(CommandContext commandContext) {
ProcessEngineConfiguration processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
RepositoryService repositoryService = processEngineConfiguration.getRepositoryService();
try {
byte[] bytes = editorJson.getBytes("utf-8");
repositoryService.addModelEditorSource(modelId, bytes);
ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(bytes);
BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);
BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
byte[] bpmnBytes = xmlConverter.convertToXML(bpmnModel);
XMLInputFactory xif = XMLInputFactory.newInstance();
InputStreamReader xmlIn = new InputStreamReader(new ByteArrayInputStream(bpmnBytes), "UTF-8");
XMLStreamReader xtr = xif.createXMLStreamReader(xmlIn);
bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
InputStream resource = diagramGenerator.generateDiagram(bpmnModel, "png", Collections.emptyList(), Collections.emptyList(), processEngineConfiguration.getActivityFontName(), processEngineConfiguration.getLabelFontName(), processEngineConfiguration.getAnnotationFontName(), processEngineConfiguration.getClassLoader(), 1.0);
repositoryService.addModelEditorSourceExtra(modelId, IOUtils.toByteArray(resource));
} catch (Exception e) {
throw new FlowableException("create model exception :" + e.getMessage());
}
return null;
}
use of org.flowable.bpmn.model.BpmnModel in project plumdo-work by wengwh.
the class ModelImportResource method importModel.
@PostMapping(value = "/models/import", name = "流程模型导入")
@ResponseStatus(value = HttpStatus.CREATED)
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public ModelResponse importModel(HttpServletRequest request) {
if (!(request instanceof MultipartHttpServletRequest)) {
exceptionFactory.throwIllegalArgument(ErrorConstant.REQUEST_NOT_MULTIPART);
}
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
if (multipartRequest.getFileMap().size() == 0) {
exceptionFactory.throwIllegalArgument(ErrorConstant.MULTIPART_CONTENT_EMPTY);
}
MultipartFile file = multipartRequest.getFileMap().values().iterator().next();
String fileName = file.getOriginalFilename();
if (fileName == null || (!fileName.endsWith(".bpmn") && !fileName.endsWith(".bpmn20.xml"))) {
exceptionFactory.throwIllegalArgument(ErrorConstant.FILE_NOT_BPMN, fileName);
}
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
InputStreamReader xmlIn = new InputStreamReader(file.getInputStream(), "UTF-8");
XMLStreamReader xtr = xif.createXMLStreamReader(xmlIn);
BpmnModel bpmnModel = bpmnXmlConverter.convertToBpmnModel(xtr);
if (ObjectUtils.isEmpty(bpmnModel.getProcesses())) {
exceptionFactory.throwObjectNotFound(ErrorConstant.MODEL_NOT_FOUND_PROCESS, fileName);
}
Process process = bpmnModel.getMainProcess();
Model modelData = repositoryService.newModel();
modelData.setKey(process.getId());
Model lastModel = repositoryService.createModelQuery().modelKey(modelData.getKey()).latestVersion().singleResult();
if (lastModel == null) {
modelData.setVersion(TableConstant.MODEL_VESION_START);
} else {
modelData.setVersion(lastModel.getVersion() + 1);
}
if (ObjectUtils.isNotEmpty(process.getName())) {
modelData.setName(process.getName());
} else {
modelData.setName(process.getId());
}
ObjectNode metaInfo = new ObjectMapper().createObjectNode();
metaInfo.put("name", modelData.getName());
metaInfo.put("description", process.getDocumentation());
modelData.setMetaInfo(metaInfo.toString());
repositoryService.saveModel(modelData);
managementService.executeCommand(new SaveModelEditorCmd(modelData.getId(), bpmnJsonConverter.convertToJson(bpmnModel).toString()));
return restResponseFactory.createModelResponse(modelData);
} catch (Exception e) {
logger.error("导入流程文件异常", e);
exceptionFactory.throwDefinedException(ErrorConstant.MODEL_IMPORT_FILE_ERROR, fileName, e.getMessage());
}
return null;
}
Aggregations