use of org.flowable.engine.repository.Deployment in project plumdo-work by wengwh.
the class DeployModelCmd method execute.
public Deployment execute(CommandContext commandContext) {
Deployment deployment = null;
RepositoryService repositoryService = commandContext.getProcessEngineConfiguration().getRepositoryService();
Model model = repositoryService.getModel(modelId);
if (model == null) {
throw new FlowableObjectNotFoundException("Could not find a model with id '" + modelId + "'.", Model.class);
}
byte[] editorSource = commandContext.getModelEntityManager().findEditorSourceByModelId(modelId);
if (editorSource == null) {
throw new FlowableObjectNotFoundException("Model with id '" + modelId + "' does not have source available.", String.class);
}
try {
DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(editorSource);
BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);
byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(bpmnModel);
String fileName = model.getId() + ".bpmn20.xml";
ByteArrayInputStream bis = new ByteArrayInputStream(bpmnBytes);
deploymentBuilder.addInputStream(fileName, bis);
deploymentBuilder.name(fileName);
// modelId设置为部署的分类字段作为后续关联的需要
deploymentBuilder.category(model.getId());
if (model.getTenantId() != null) {
deploymentBuilder.tenantId(model.getTenantId());
}
deployment = deploymentBuilder.deploy();
// 设置模型的部署ID
model.setDeploymentId(deployment.getId());
} catch (Exception e) {
if (e instanceof FlowableException) {
throw (FlowableException) e;
}
throw new FlowableException(e.getMessage(), e);
}
return deployment;
}
use of org.flowable.engine.repository.Deployment in project plumdo-work by wengwh.
the class ModelDeployResource method deployModel.
@RequestMapping(value = "/model/{modelId}/deploy", method = RequestMethod.POST, produces = "application/json", name = "模型部署")
@ResponseStatus(value = HttpStatus.CREATED)
@Transactional(propagation = Propagation.REQUIRED)
public ProcessDefinitionResponse deployModel(@PathVariable String modelId) {
Model model = getModelFromRequest(modelId);
Deployment deployment = managementService.executeCommand(new DeployModelCmd(model.getId()));
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
return restResponseFactory.createProcessDefinitionResponse(processDefinition);
}
use of org.flowable.engine.repository.Deployment in project plumdo-work by wengwh.
the class ProcessDefinitionXmlResource method getProcessDefinitionXml.
@RequestMapping(value = "/process-definition/{processDefinitionId}/xml", method = RequestMethod.GET, name = "流程定义XML")
public ResponseEntity<byte[]> getProcessDefinitionXml(@PathVariable String processDefinitionId) {
ProcessDefinition processDefinition = getProcessDefinitionFromRequest(processDefinitionId);
String deploymentId = processDefinition.getDeploymentId();
String resourceId = processDefinition.getResourceName();
if (deploymentId == null) {
throw new FlowableIllegalArgumentException("No deployment id provided");
}
if (resourceId == null) {
throw new FlowableIllegalArgumentException("No resource id provided");
}
Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(deploymentId).singleResult();
if (deployment == null) {
throw new FlowableObjectNotFoundException("Could not find a deployment with id '" + deploymentId + "'.", Deployment.class);
}
List<String> resourceList = repositoryService.getDeploymentResourceNames(deploymentId);
if (resourceList.contains(resourceId)) {
final InputStream resourceStream = repositoryService.getResourceAsStream(deploymentId, resourceId);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.TEXT_XML);
try {
return new ResponseEntity<byte[]>(IOUtils.toByteArray(resourceStream), responseHeaders, HttpStatus.OK);
} catch (Exception e) {
throw new FlowableException("Error converting resource stream", e);
}
} else {
throw new FlowableObjectNotFoundException("Could not find a resource with id '" + resourceId + "' in deployment '" + deploymentId + "'.", String.class);
}
}
use of org.flowable.engine.repository.Deployment in project plumdo-work by wengwh.
the class ProcessDefinitionGetModelResource method processDefinitionGetModel.
@RequestMapping(value = "/process-definition/{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);
}
}
Aggregations