final ProcessEngineImpl defaultProcessEngine = (ProcessEngineImpl) ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
ProcessEngineConfiguration processEngineConfiguration = defaultProcessEngine.getProcessEngineConfiguration();
ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
Model model = null;
if (modelId == null) { // new process
model = repositoryService.newModel();
} else { // update existing process
model = repositoryService.getModel(modelId);
}
model.setName(workflowDefinition.getName());
model.setCategory(SimpleTableEditorConstants.TABLE_EDITOR_CATEGORY);
repositoryService.saveModel(model);
// Store model entity
WorkflowDefinitionConversion conversion =
ExplorerApp.get().getWorkflowDefinitionConversionFactory().createWorkflowDefinitionConversion(workflowDefinition);
conversion.convert();
try {
// Write JSON to byte-array and set as editor-source
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ExplorerApp.get().getSimpleWorkflowJsonConverter().writeWorkflowDefinition(workflowDefinition, new OutputStreamWriter(baos));
repositoryService.addModelEditorSource(model.getId(), baos.toByteArray());
// Store process image
// TODO: we should really allow the service to take an inputstream as input. Now we load it into memory ...
repositoryService.addModelEditorSourceExtra(model.getId(), IOUtils.toByteArray(
diagramGenerator.generateDiagram(conversion.getBpmnModel(), "png", processEngineConfiguration.getActivityFontName(),
processEngineConfiguration.getLabelFontName(), processEngineConfiguration.getClassLoader())));
} catch (IOException e) {
logger.warn("Could not generate process image. Image is not stored and will not be shown.", e);
}
ExplorerApp.get().getViewManager().showEditorProcessDefinitionPage(model.getId());
}