AnsweredAssumed Answered

get itemDefinition inside custom parseListener

Question asked by mcella on Nov 21, 2011
In custom parseListener we can have access to all BPMN element inside process element; can we get access to elements outside process scope like <itemDefinition> element ?
Take as example:

BPMN 2.0 Workflow:

<?xml version="1.0" encoding="UTF-8"?>
<definitions id="Definition" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" …>
 
  <itemDefinition id="_Double" structureRef="xsd:double" />

  <process id="Main" name="Main" isExecutable="true">
    <dataObject id="x" itemSubjectRef="_Double" />

    ….
  </process>
</definitions>

Custom Parse Listener (postParseListener):

import org.activiti.engine.impl.bpmn.parser.BpmnParseListener;

public class MyParseListener implements BpmnParseListener {
  public void parseProcess(Element processElement, ProcessDefinitionEntity processDefinition){
    //  defaut process engine Resources (.bpmn20.xml)  —>  empty
    ArrayList<String> resources = (ArrayList<String>)ProcessEngines.getDefaultProcessEngine().getRepositoryService().getDeploymentResourceNames(processDefinition.getDeploymentId() );
  }
  …
}


I have found  a simple solution: create a new instance of BpmnParse and execute the parse of the .bpmn20.xml file inside a process execution listener:

Custom Parse Listener (postParseListener):

public class MyParseListener implements BpmnParseListener {
  protected static final ProcessHandler CUSTOM_PROCESS_HANDLER = new ProcessHandler(); 

  public void parseProcess(Element processElement, ProcessDefinitionEntity processDefinition){
    processDefinition.addExecutionListener(org.activiti.engine.impl.pvm.PvmEvent.EVENTNAME_START, CUSTOM_PROCESS_HANDLER);
  }
}

Process execution handler:

public class ProcessHandler implements ExecutionListener {
   
  public void notify(DelegateExecution execution) throws Exception {
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    ActivityImpl activityImpl = executionEntity.getActivity();

    ArrayList<String> resources = (ArrayList<String>)ProcessEngines.getDefaultProcessEngine().getRepositoryService().getDeploymentResourceNames( executionEntity.getProcessDefinition().getDeploymentId() );
    String nameFileWorkflow = resources.get(0);

    BpmnParse bpmnParse = getBpmnParse( new File( nameFileWorkflow ) );
    bpmnParse.execute();
    Element root = bpmnParse.getRootElement();
    ArrayList<Element> listItemDefinition = (ArrayList<Element>)root.elements("itemDefinition");
    //  set activityImpl properties…
  }

  private BpmnParse getBpmnParse(File filename) throws Exception{
    BpmnParser bpmnParser = new BpmnParser( new ExpressionManager() );
    BpmnParse parse = bpmnParser.createParse();
   
    DeploymentEntity deploymentEntity = new DeploymentEntity();
    parse.sourceResource( filename.getName() );
    parse.deployment( deploymentEntity );
   
    return parse;
  }
}

Outcomes