cancel
Showing results for 
Search instead for 
Did you mean: 

Getting 'deployed' process from Repository/RuntimeService

iravanchi
Champ in-the-making
Champ in-the-making
Hi,

I couldn't find any way for getting the "deployed" version of the process definition from the services provided by the engine.
Is there any way to do so?

As I browsed the code, ProcessDefinitionEntity contains the parsed structure for the process execution.
When a new execution is requested from RuntimeService, it uses DbRepositorySession, and loads and re-parses (or re-deploys) the process definition from the deployments, so that the activities are placed inside the ProcessDefinitionEntity and can be executed. (using deployExisting method in DbRepositorySession)

When loading the process definition using RepositoryService.findProcessDefinitionById, it is directly fetched from DB and returned, without that deployExisting thing, so the returned ProcessDefinitionEntity doesn't contain parsed activities.

Maybe adding these methods to RepositoryService can be a good idea:
* findDeployedProcessDefinitionById (parallel to findDeployedProcessDefinitionById in DbRepositorySession)
* find[Deployed]ProcessDefinitionByKey (there is no "by-key" alternative for this)

-Hamed
8 REPLIES 8

jbarrez
Star Contributor
Star Contributor
This is alredy possible with the current API:


@Deployment
  public void testGetBpmnXmlFileThroughService() {
    String deploymentId = repositoryService.createDeploymentQuery().singleResult().getId();
    List<String> deploymentResources = repositoryService.getDeploymentResourceNames(deploymentId);
   
    // verify bpmn file name
    assertEquals(1, deploymentResources.size());
    String bpmnResourceName = "org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testGetBpmnXmlFileThroughService.bpmn20.xml";
    assertEquals(bpmnResourceName, deploymentResources.get(0));
   
    // verify content
    InputStream deploymentInputStream = repositoryService.getResourceAsStream(deploymentId, bpmnResourceName);
    String contentFromDeployment = readInputStreamToString(deploymentInputStream);
    assertTrue(contentFromDeployment.length() > 0);
    assertTrue(contentFromDeployment.contains("process id=\"emptyProcess\""));
   
    InputStream fileInputStream = this.getClass().getClassLoader().getResourceAsStream("org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testGetBpmnXmlFileThroughService.bpmn20.xml");
    String contentFromFile = readInputStreamToString(fileInputStream);
    assertEquals(contentFromFile, contentFromDeployment);
  }

iravanchi
Champ in-the-making
Champ in-the-making
I didn't mean the content of the deployed XML files.
I mean, there could be a way to get the Parsed object model of the activities inside the process instance. This is the result of what BpmnParser does (creating a BpmnParse, reading the XML content, and instantiating Activities and Behaviors)

For example, if I wanted to know what is the name of the first activity in the process definition, having access to the parsed version would be of a great help. (The same object model which is handed to the PVM for execution)

tombaeyens
Champ in-the-making
Champ in-the-making
We could add a RepositoryService.getDeployedProcessDefinition(String processDefinitionId).  Singular form is important.  We don't want to force deployment of all the process definition when the UI is just showing a list of all process definitions.

We still have to analyst if there is a potential security problem with exposing the whole process definition model to the external clients.

tombaeyens
Champ in-the-making
Champ in-the-making

awhobbes
Champ in-the-making
Champ in-the-making
I'm trying to do what is described above, to get the xml for a deployed process.  I'm using the following code, since I know the processDefinitionId:
InputStream is= repositoryservice.getProcessDiagram("myProcess:1:4");
           log("****" + readInputStreamToString(is));

Where:
  private String readInputStreamToString(InputStream inputStream) {
    byte[] bytes = IoUtil.readInputStream(inputStream, "input stream");
    return new String(bytes);
  }

And I want to make sure that this should give me the bmpn xml file. 
Secondly, I must have the wrong encoding of "UTF-8" because I'm getting garbage characters out.
Thanks,
Axel

jbarrez
Star Contributor
Star Contributor
"getProcessDiagram" gives you the process .. diagram … which is an image (png).

You need to use the getResourceAsStream() method:

repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());

awhobbes
Champ in-the-making
Champ in-the-making
OK, great, thanks.  I'm using the history service, is it possible to get a processDefinition object from that?
I'm doing the following to get the  ProcessDefinitionId from the (first) instance I find (a user will eventually select one).

But I don't see a way to get the resourceName from the HistoricProcessInstanceEnity:

HistoryService hs=getProcessEngine().getHistoryService();
List q= hs.createHistoricProcessInstanceQuery().orderByProcessDefinitionId().processInstanceId(instanceID).desc().list();
HistoricProcessInstanceEntity hi=(HistoricProcessInstanceEntity)q.get(0);
String deploymentId=hi.getProcessDefinitionId();
String resourceName=hi.????

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,


  HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();

    final String processDefinitionId  = historicProcessInstance.getProcessDefinitionId();    
    ProcessDefinitionEntity pde = (ProcessDefinitionEntity) ((RepositoryServiceImpl)
        ActivitiUtil.getRepositoryService()).getDeployedProcessDefinition(processDefinitionId);

    if (pde != null && pde.isGraphicalNotationDefined()) {
      BpmnModel bpmnModel = ActivitiUtil.getRepositoryService().getBpmnModel(pde.getId());
      ProcessDiagramGenerator diagramGenerator = ((ProcessEngineImpl) ActivitiUtil.getProcessEngine()).getProcessEngineConfiguration().getProcessDiagramGenerator();
      InputStream resource = diagramGenerator.generateDiagram(bpmnModel, "png", ActivitiUtil.getRuntimeService().getActiveActivityIds(processInstanceId));
….

at the end you can use any list to highlight tasks.
Regards
Martin