Java Code Examples for org.camunda.bpm.model.bpmn.BpmnModelInstance#getModelElementById()

The following examples show how to use org.camunda.bpm.model.bpmn.BpmnModelInstance#getModelElementById() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PathCoverageExecutionListener.java    From camunda-bpm-process-test-coverage with Apache License 2.0 6 votes vote down vote up
/**
 * Events aren't reported like SequenceFlows and Activities, so we need
 * special handling. If a sequence flow has an event as the source or the
 * target, we add it to the coverage. It's pretty straight forward if a
 * sequence flow is active, then it's source has been covered anyway and it
 * will most definitely arrive at its target.
 * 
 * @param transitionId
 * @param processDefinition
 * @param repositoryService
 */
private void handleEvent(String transitionId, ProcessDefinition processDefinition,
        RepositoryService repositoryService) {

    final BpmnModelInstance modelInstance = repositoryService.getBpmnModelInstance(processDefinition.getId());

    final ModelElementInstance modelElement = modelInstance.getModelElementById(transitionId);
    if (modelElement.getElementType().getInstanceType() == SequenceFlow.class) {

        final SequenceFlow sequenceFlow = (SequenceFlow) modelElement;

        // If there is an event at the sequence flow source add it to the
        // coverage
        final FlowNode source = sequenceFlow.getSource();
        addEventToCoverage(processDefinition, source);

        // If there is an event at the sequence flow target add it to the
        // coverage
        final FlowNode target = sequenceFlow.getTarget();
        addEventToCoverage(processDefinition, target);

    }

}
 
Example 2
Source File: CompatabilityTest.java    From camunda-bpmn-model with Apache License 2.0 6 votes vote down vote up
@Test
public void modifyingAttributeWithActivitiNsKeepsIt() {
  BpmnModelInstance modelInstance = Bpmn.readModelFromStream(CamundaExtensionsTest.class.getResourceAsStream("CamundaExtensionsCompatabilityTest.xml"));
  ProcessImpl process = modelInstance.getModelElementById(PROCESS_ID);
  String priority = "9000";
  process.setCamundaJobPriority(priority);
  process.setCamundaTaskPriority(priority);
  Integer historyTimeToLive = 10;
  process.setCamundaHistoryTimeToLive(historyTimeToLive);
  process.setCamundaIsStartableInTasklist(false);
  process.setCamundaVersionTag("v1.0.0");
  assertThat(process.getAttributeValueNs(BpmnModelConstants.ACTIVITI_NS, "jobPriority"), is(priority));
  assertThat(process.getAttributeValueNs(BpmnModelConstants.ACTIVITI_NS, "taskPriority"), is(priority));
  assertThat(process.getAttributeValueNs(BpmnModelConstants.ACTIVITI_NS, "historyTimeToLive"), is(historyTimeToLive.toString()));
  assertThat(process.isCamundaStartableInTasklist(), is(false));
  assertThat(process.getCamundaVersionTag(), is("v1.0.0"));
}
 
Example 3
Source File: CompatabilityTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void modifyingAttributeWithActivitiNsKeepsIt() {
  BpmnModelInstance modelInstance = Bpmn.readModelFromStream(CamundaExtensionsTest.class.getResourceAsStream("CamundaExtensionsCompatabilityTest.xml"));
  ProcessImpl process = modelInstance.getModelElementById(PROCESS_ID);
  String priority = "9000";
  process.setCamundaJobPriority(priority);
  process.setCamundaTaskPriority(priority);
  Integer historyTimeToLive = 10;
  process.setCamundaHistoryTimeToLive(historyTimeToLive);
  process.setCamundaIsStartableInTasklist(false);
  process.setCamundaVersionTag("v1.0.0");
  assertThat(process.getAttributeValueNs(BpmnModelConstants.ACTIVITI_NS, "jobPriority"), is(priority));
  assertThat(process.getAttributeValueNs(BpmnModelConstants.ACTIVITI_NS, "taskPriority"), is(priority));
  assertThat(process.getAttributeValueNs(BpmnModelConstants.ACTIVITI_NS, "historyTimeToLive"), is(historyTimeToLive.toString()));
  assertThat(process.isCamundaStartableInTasklist(), is(false));
  assertThat(process.getCamundaVersionTag(), is("v1.0.0"));
}
 
Example 4
Source File: ProcessDefinitionQueryImpl.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected void addProcessDefinitionToCacheAndRetrieveDocumentation(List<ProcessDefinition> list) {
  for (ProcessDefinition processDefinition : list) {

    BpmnModelInstance bpmnModelInstance = Context.getProcessEngineConfiguration()
        .getDeploymentCache()
        .findBpmnModelInstanceForProcessDefinition((ProcessDefinitionEntity) processDefinition);

    ModelElementInstance processElement = bpmnModelInstance.getModelElementById(processDefinition.getKey());
    if (processElement != null) {
      Collection<Documentation> documentations = processElement.getChildElementsByType(Documentation.class);
      List<String> docStrings = new ArrayList<String>();
      for (Documentation documentation : documentations) {
        docStrings.add(documentation.getTextContent());
      }

      ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) processDefinition;
      processDefinitionEntity.setProperty(BpmnParse.PROPERTYNAME_DOCUMENTATION, BpmnParse.parseDocumentation(docStrings));
    }

  }
}
 
Example 5
Source File: TaskEntity.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Override
public UserTask getBpmnModelElementInstance() {
  BpmnModelInstance bpmnModelInstance = getBpmnModelInstance();
  if(bpmnModelInstance != null) {
    ModelElementInstance modelElementInstance = bpmnModelInstance.getModelElementById(taskDefinitionKey);
    try {
      return (UserTask) modelElementInstance;
    } catch(ClassCastException e) {
      ModelElementType elementType = modelElementInstance.getElementType();
      throw LOG.castModelInstanceException(modelElementInstance, "UserTask", elementType.getTypeName(),
        elementType.getTypeNamespace(), e);
    }
  } else {
    return null;
  }
}
 
Example 6
Source File: ExecutionEntity.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public FlowElement getBpmnModelElementInstance() {
  BpmnModelInstance bpmnModelInstance = getBpmnModelInstance();
  if (bpmnModelInstance != null) {

    ModelElementInstance modelElementInstance = null;
    if (ExecutionListener.EVENTNAME_TAKE.equals(eventName)) {
      modelElementInstance = bpmnModelInstance.getModelElementById(transition.getId());
    } else {
      modelElementInstance = bpmnModelInstance.getModelElementById(activityId);
    }

    try {
      return (FlowElement) modelElementInstance;

    } catch (ClassCastException e) {
      ModelElementType elementType = modelElementInstance.getElementType();
      throw LOG.castModelInstanceException(modelElementInstance, "FlowElement", elementType.getTypeName(),
        elementType.getTypeNamespace(), e);
    }

  } else {
    return null;
  }
}
 
Example 7
Source File: CompensateEventOrderTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
private void addServiceTaskCompensationHandler(BpmnModelInstance modelInstance, String boundaryEventId, String compensationHandlerId) {

    BoundaryEvent boundaryEvent = modelInstance.getModelElementById(boundaryEventId);
    BaseElement scope = (BaseElement) boundaryEvent.getParentElement();

    ServiceTask compensationHandler = modelInstance.newInstance(ServiceTask.class);
    compensationHandler.setId(compensationHandlerId);
    compensationHandler.setForCompensation(true);
    compensationHandler.setCamundaClass(IncreaseCurrentTimeServiceTask.class.getName());
    scope.addChildElement(compensationHandler);

    Association association = modelInstance.newInstance(Association.class);
    association.setAssociationDirection(AssociationDirection.One);
    association.setSource(boundaryEvent);
    association.setTarget(compensationHandler);
    scope.addChildElement(association);

  }
 
Example 8
Source File: BpmnModelInstanceCmdTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = "org/camunda/bpm/engine/test/repository/one.bpmn20.xml")
public void testRepositoryService() {
  String processDefinitionId = repositoryService.createProcessDefinitionQuery().processDefinitionKey(PROCESS_KEY).singleResult().getId();

  BpmnModelInstance modelInstance = repositoryService.getBpmnModelInstance(processDefinitionId);
  assertNotNull(modelInstance);

  Collection<ModelElementInstance> events = modelInstance.getModelElementsByType(modelInstance.getModel().getType(Event.class));
  assertEquals(2, events.size());

  Collection<ModelElementInstance> sequenceFlows = modelInstance.getModelElementsByType(modelInstance.getModel().getType(SequenceFlow.class));
  assertEquals(1, sequenceFlows.size());

  StartEvent startEvent = modelInstance.getModelElementById("start");
  assertNotNull(startEvent);
}
 
Example 9
Source File: CompensationModels.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public static void addUserTaskCompensationHandler(BpmnModelInstance modelInstance, String boundaryEventId, String compensationHandlerId) {

    BoundaryEvent boundaryEvent = modelInstance.getModelElementById(boundaryEventId);
    BaseElement scope = (BaseElement) boundaryEvent.getParentElement();

    UserTask compensationHandler = modelInstance.newInstance(UserTask.class);
    compensationHandler.setId(compensationHandlerId);
    compensationHandler.setForCompensation(true);
    scope.addChildElement(compensationHandler);

    Association association = modelInstance.newInstance(Association.class);
    association.setAssociationDirection(AssociationDirection.One);
    association.setSource(boundaryEvent);
    association.setTarget(compensationHandler);
    scope.addChildElement(association);

  }
 
Example 10
Source File: TransactionTest.java    From camunda-bpmn-model with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldWriteTransaction() throws ParserConfigurationException, SAXException, IOException {
  // given a model
  BpmnModelInstance newModel = Bpmn.createProcess("process").done();

  Process process = newModel.getModelElementById("process");

  Transaction transaction = newModel.newInstance(Transaction.class);
  transaction.setId("transaction");
  transaction.setMethod(TransactionMethod.Store);
  process.addChildElement(transaction);

  // that is written to a stream
  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  Bpmn.writeModelToStream(outStream, newModel);

  // when reading from that stream
  ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());

  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  Document actualDocument = docBuilder.parse(inStream);

  // then it possible to traverse to the transaction element and assert its attributes
  NodeList transactionElements = actualDocument.getElementsByTagName("transaction");
  assertThat(transactionElements.getLength()).isEqualTo(1);

  Node transactionElement = transactionElements.item(0);
  assertThat(transactionElement).isNotNull();
  Node methodAttribute = transactionElement.getAttributes().getNamedItem("method");
  assertThat(methodAttribute.getNodeValue()).isEqualTo("##Store");

}
 
Example 11
Source File: FoxJobRetryCmdTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void testRetryOnServiceTaskLikeMessageThrowEvent() {
  // given
  BpmnModelInstance bpmnModelInstance = Bpmn.createExecutableProcess("process")
      .startEvent()
      .intermediateThrowEvent()
        .camundaAsyncBefore()
        .camundaFailedJobRetryTimeCycle("R10/PT5S")
        .messageEventDefinition("messageDefinition")
          .message("message")
        .messageEventDefinitionDone()
      .endEvent()
      .done();

  MessageEventDefinition messageDefinition = bpmnModelInstance.getModelElementById("messageDefinition");
  messageDefinition.setCamundaClass(FailingDelegate.class.getName());

  deployment(bpmnModelInstance);

  runtimeService.startProcessInstanceByKey("process");

  Job job = managementService.createJobQuery().singleResult();

  // when job fails
  try {
    managementService.executeJob(job.getId());
  } catch (Exception e) {
    // ignore
  }

  // then
  job = managementService.createJobQuery().singleResult();
  Assert.assertEquals(9, job.getRetries());
}
 
Example 12
Source File: MigrationUserTaskTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected static void addTaskListener(BpmnModelInstance targetModel, String activityId, String event, String className) {
  CamundaTaskListener taskListener = targetModel.newInstance(CamundaTaskListener.class);
  taskListener.setCamundaClass(className);
  taskListener.setCamundaEvent(event);

  UserTask task = targetModel.getModelElementById(activityId);
  task.builder().addExtensionElement(taskListener);
}
 
Example 13
Source File: DecisionMetricsTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void testBusinessRuleTask() {
  BpmnModelInstance modelInstance = Bpmn.createExecutableProcess("testProcess")
      .startEvent()
      .businessRuleTask("task")
      .endEvent()
      .done();

  BusinessRuleTask task = modelInstance.getModelElementById("task");
  task.setCamundaDecisionRef("decision");

  testRule.deploy(repositoryService.createDeployment()
      .addModelInstance("process.bpmn", modelInstance)
      .addClasspathResource(DMN_FILE));

  assertEquals(0l, getExecutedDecisionInstances());
  assertEquals(0l, getExecutedDecisionElements());
  assertEquals(0l, getExecutedDecisionInstancesFromDmnEngine());
  assertEquals(0l, getExecutedDecisionElementsFromDmnEngine());

  runtimeService.startProcessInstanceByKey("testProcess", VARIABLES);

  assertEquals(1l, getExecutedDecisionInstances());
  assertEquals(16l, getExecutedDecisionElements());
  assertEquals(1l, getExecutedDecisionInstancesFromDmnEngine());
  assertEquals(16l, getExecutedDecisionElementsFromDmnEngine());

  processEngineConfiguration.getDbMetricsReporter().reportNow();

  assertEquals(1l, getExecutedDecisionInstances());
  assertEquals(16l, getExecutedDecisionElements());
  assertEquals(1l, getExecutedDecisionInstancesFromDmnEngine());
  assertEquals(16l, getExecutedDecisionElementsFromDmnEngine());
}
 
Example 14
Source File: TransactionTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldWriteTransaction() throws ParserConfigurationException, SAXException, IOException {
  // given a model
  BpmnModelInstance newModel = Bpmn.createProcess("process").done();

  Process process = newModel.getModelElementById("process");

  Transaction transaction = newModel.newInstance(Transaction.class);
  transaction.setId("transaction");
  transaction.setMethod(TransactionMethod.Store);
  process.addChildElement(transaction);

  // that is written to a stream
  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  Bpmn.writeModelToStream(outStream, newModel);

  // when reading from that stream
  ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());

  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  Document actualDocument = docBuilder.parse(inStream);

  // then it possible to traverse to the transaction element and assert its attributes
  NodeList transactionElements = actualDocument.getElementsByTagName("transaction");
  assertThat(transactionElements.getLength()).isEqualTo(1);

  Node transactionElement = transactionElements.item(0);
  assertThat(transactionElement).isNotNull();
  Node methodAttribute = transactionElement.getAttributes().getNamedItem("method");
  assertThat(methodAttribute.getNodeValue()).isEqualTo("##Store");

}
 
Example 15
Source File: TransactionModels.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
protected static void makeCancelEvent(BpmnModelInstance model, String eventId) {
  ModelElementInstance element = model.getModelElementById(eventId);

  CancelEventDefinition eventDefinition = model.newInstance(CancelEventDefinition.class);
  element.addChildElement(eventDefinition);
}
 
Example 16
Source File: SelfCancellationTest.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
public static void initEndEvent(BpmnModelInstance modelInstance, String endEventId) {
  EndEvent endEvent = modelInstance.getModelElementById(endEventId);
  TerminateEventDefinition terminateDefinition = modelInstance.newInstance(TerminateEventDefinition.class);
  endEvent.addChildElement(terminateDefinition);
}
 
Example 17
Source File: HistoricProcessInstanceStateTest.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
protected static void initEndEvent(BpmnModelInstance modelInstance, String endEventId) {
  EndEvent endEvent = modelInstance.getModelElementById(endEventId);
  TerminateEventDefinition terminateDefinition = modelInstance.newInstance(TerminateEventDefinition.class);
  endEvent.addChildElement(terminateDefinition);
}