use of org.dshubs.odc.workflow.entity.FlowableForm in project ox-data-cloud by ox-data.
the class DeployModelCmd method execute.
@Override
public Deployment execute(CommandContext commandContext) {
RepositoryService repositoryService = CommandContextUtil.getProcessEngineConfiguration(commandContext).getRepositoryService();
Model model = repositoryService.getModel(modelId);
if (model == null) {
throw new FlowableObjectNotFoundException("Could not find a model with id '" + modelId + "'.", Model.class);
}
if (model.getDeploymentId() != null && model.getDeploymentId().length() > 0) {
throw new FlowableException("The model is already deployed");
}
if (!model.hasEditorSource()) {
throw new FlowableObjectNotFoundException("Model with id '" + modelId + "' does not have source " + "available" + ".", String.class);
}
byte[] bpmnBytes = CommandContextUtil.getProcessEngineConfiguration(commandContext).getModelEntityManager().findEditorSourceByModelId(modelId);
if (bpmnBytes == null) {
throw new FlowableObjectNotFoundException("Model with id '" + modelId + "' does not have source " + "available" + ".", String.class);
}
try {
DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
String fileName = model.getId() + ".bpmn20.xml";
ByteArrayInputStream bis = new ByteArrayInputStream(bpmnBytes);
deploymentBuilder.addInputStream(fileName, bis);
deploymentBuilder.name(fileName);
deploymentBuilder.category(model.getCategory());
XMLInputFactory xif = XMLInputFactory.newInstance();
InputStreamReader xmlIn = new InputStreamReader(new ByteArrayInputStream(bpmnBytes), "UTF-8");
XMLStreamReader xtr = xif.createXMLStreamReader(xmlIn);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
org.flowable.bpmn.model.Process process = bpmnModel.getMainProcess();
Collection<FlowElement> flowElements = process.getFlowElements();
Map<String, String> formKeyMap = new HashMap<String, String>(16);
for (FlowElement flowElement : flowElements) {
String formKey = null;
if (flowElement instanceof StartEvent) {
StartEvent startEvent = (StartEvent) flowElement;
if (startEvent.getFormKey() != null && startEvent.getFormKey().length() > 0) {
formKey = startEvent.getFormKey();
}
} else if (flowElement instanceof UserTask) {
UserTask userTask = (UserTask) flowElement;
if (userTask.getFormKey() != null && userTask.getFormKey().length() > 0) {
formKey = userTask.getFormKey();
}
}
if (formKey != null && formKey.length() > 0) {
if (formKeyMap.containsKey(formKey)) {
continue;
} else {
String formKeyDefinition = formKey.replace(".form", "");
FlowableFormService flowableFormService = SpringContextUtils.getBean(FlowableFormService.class);
FlowableForm form = flowableFormService.getById(formKeyDefinition);
if (form != null && form.getFormJson() != null && form.getFormJson().length() > 0) {
byte[] formJson = form.getFormJson().getBytes("UTF-8");
ByteArrayInputStream bi = new ByteArrayInputStream(formJson);
deploymentBuilder.addInputStream(formKey, bi);
formKeyMap.put(formKey, formKey);
} else {
throw new FlowableObjectNotFoundException("Cannot find formJson with formKey " + formKeyDefinition);
}
}
}
}
if (model.getTenantId() != null) {
deploymentBuilder.tenantId(model.getTenantId());
}
Deployment deployment = deploymentBuilder.deploy();
if (deployment != null) {
model.setDeploymentId(deployment.getId());
}
return deployment;
} catch (Exception e) {
if (e instanceof FlowableException) {
throw (FlowableException) e;
}
throw new FlowableException(e.getMessage(), e);
}
}
Aggregations