Java Code Examples for org.activiti.bpmn.model.BpmnModel#getMainProcess()

The following examples show how to use org.activiti.bpmn.model.BpmnModel#getMainProcess() . 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: JobDefinitionServiceImpl.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that the first asyncable task in the given model is indeed asynchronous. Only asserts when the configuration is set to true.
 *
 * @param bpmnModel The BPMN model
 */
private void assertFirstTaskIsAsync(BpmnModel bpmnModel)
{
    if (Boolean.TRUE.equals(configurationHelper.getProperty(ConfigurationValue.ACTIVITI_JOB_DEFINITION_ASSERT_ASYNC, Boolean.class)))
    {
        Process process = bpmnModel.getMainProcess();
        for (StartEvent startEvent : process.findFlowElementsOfType(StartEvent.class))
        {
            for (SequenceFlow sequenceFlow : startEvent.getOutgoingFlows())
            {
                String targetRef = sequenceFlow.getTargetRef();
                FlowElement targetFlowElement = process.getFlowElement(targetRef);
                if (targetFlowElement instanceof Activity)
                {
                    Assert.isTrue(((Activity) targetFlowElement).isAsynchronous(), "Element with id \"" + targetRef +
                        "\" must be set to activiti:async=true. All tasks which start the workflow must be asynchronous to prevent certain undesired " +
                        "transactional behavior, such as records of workflow not being saved on errors. Please refer to Activiti and herd documentations " +
                        "for details.");
                }
            }
        }
    }
}
 
Example 2
Source File: LaneExtensionTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Test
@Deployment
public void testLaneExtensionElement() {
  ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("swimlane-extension").singleResult();
  BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
  byte[] xml = new BpmnXMLConverter().convertToXML(bpmnModel);
  System.out.println(new String(xml));
  Process bpmnProcess = bpmnModel.getMainProcess();
  for (Lane l : bpmnProcess.getLanes()) {
    Map<String, List<ExtensionElement>> extensions = l.getExtensionElements();
    Assert.assertTrue(extensions.size() > 0);
  }
}
 
Example 3
Source File: BpmnParseTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Deployment
public void testParseDiagramInterchangeElements() {

  // Graphical information is not yet exposed publicly, so we need to do some plumbing
  
  BpmnModel bpmnModel = repositoryService.getBpmnModel(repositoryService.createProcessDefinitionQuery().singleResult().getId());
  Process process = bpmnModel.getMainProcess();

  // Check if diagram has been created based on Diagram Interchange when  it's not a headless instance
  List<String> resourceNames = repositoryService.getDeploymentResourceNames(repositoryService.createProcessDefinitionQuery().singleResult().getDeploymentId());
  assertEquals(2, resourceNames.size());
  
  assertActivityBounds(bpmnModel, "theStart", 70, 255, 30, 30);
  assertActivityBounds(bpmnModel, "task1", 176, 230, 100, 80);
  assertActivityBounds(bpmnModel, "gateway1", 340, 250, 40, 40);
  assertActivityBounds(bpmnModel, "task2", 445, 138, 100, 80);
  assertActivityBounds(bpmnModel, "gateway2", 620, 250, 40, 40);
  assertActivityBounds(bpmnModel, "task3", 453, 304, 100, 80);
  assertActivityBounds(bpmnModel, "theEnd", 713, 256, 28, 28);
  
  assertSequenceFlowWayPoints(bpmnModel, "flowStartToTask1", 100, 270, 176, 270);
  assertSequenceFlowWayPoints(bpmnModel, "flowTask1ToGateway1", 276, 270, 340, 270);
  assertSequenceFlowWayPoints(bpmnModel, "flowGateway1ToTask2", 360, 250, 360, 178, 445, 178);
  assertSequenceFlowWayPoints(bpmnModel, "flowGateway1ToTask3", 360, 290, 360, 344, 453, 344);
  assertSequenceFlowWayPoints(bpmnModel, "flowTask2ToGateway2", 545, 178, 640, 178, 640, 250);
  assertSequenceFlowWayPoints(bpmnModel, "flowTask3ToGateway2", 553, 344, 640, 344, 640, 290);
  assertSequenceFlowWayPoints(bpmnModel, "flowGateway2ToEnd", 660, 270, 713, 270);
  
}
 
Example 4
Source File: BpmnParseTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Deployment
public void testParseDiagramInterchangeElementsForUnknownModelElements() {
  ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("TestAnnotation").singleResult();
  BpmnModel model = repositoryService.getBpmnModel(processDefinition.getId());
  Process mainProcess = model.getMainProcess();
  assertEquals(0, mainProcess.getExtensionElements().size());
}
 
Example 5
Source File: LaneExtensionTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Test
@Deployment
public void testLaneExtensionElement() {
  ProcessDefinition processDefinition = activitiRule.getRepositoryService().createProcessDefinitionQuery()
      .processDefinitionKey("swimlane-extension").singleResult();
  BpmnModel bpmnModel = activitiRule.getRepositoryService().getBpmnModel(processDefinition.getId());
  byte[] xml = new BpmnXMLConverter().convertToXML(bpmnModel);
  System.out.println(new String(xml));
  Process bpmnProcess = bpmnModel.getMainProcess();
  for (Lane l : bpmnProcess.getLanes()) {
    Map<String, List<ExtensionElement>> extensions = l.getExtensionElements();
    Assert.assertTrue(extensions.size() > 0);
  }
}
 
Example 6
Source File: BpmnParseTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Deployment
public void testParseDiagramInterchangeElementsForUnknownModelElements() {
  ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("TestAnnotation").singleResult();
  BpmnModel model = repositoryService.getBpmnModel(processDefinition.getId());
  Process mainProcess = model.getMainProcess();
  assertEquals(0, mainProcess.getExtensionElements().size());
}
 
Example 7
Source File: BpmnModelTest.java    From activiti-in-action-codes with Apache License 2.0 5 votes vote down vote up
/**
 * 把XML转换成BpmnModel对象
 * @throws Exception
 */
@Test
@Deployment(resources = "chapter6/dynamic-form/leave.bpmn")
public void testXmlToBpmnModel() throws Exception {

    // 验证是否部署成功
    long count = repositoryService.createProcessDefinitionQuery().count();
    assertEquals(1, count);

    // 根据流程定义获取XML资源文件流对象
    /*ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leave").singleResult();
    String resourceName = processDefinition.getResourceName();
    InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName);*/

    // 从classpath中获取
    InputStream inputStream = getClass().getClassLoader().getResourceAsStream("chapter6/dynamic-form/leave.bpmn");

    // 创建转换对象
    BpmnXMLConverter converter = new BpmnXMLConverter();

    // 创建XMLStreamReader读取XML资源
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader reader = factory.createXMLStreamReader(inputStream);

    // 把XML转换成BpmnModel对象
    BpmnModel bpmnModel = converter.convertToBpmnModel(reader);

    // 验证BpmnModel对象不为空
    assertNotNull(bpmnModel);
    Process process = bpmnModel.getMainProcess();
    assertEquals("leave", process.getId());
}
 
Example 8
Source File: SyncProcessCmd.java    From lemon with Apache License 2.0 5 votes vote down vote up
/**
 * 全局配置.
 */
public void processGlobal(BpmnModel bpmnModel, int priority,
        BpmConfBase bpmConfBase) {
    Process process = bpmnModel.getMainProcess();
    BpmConfNodeManager bpmConfNodeManager = getBpmConfNodeManager();
    BpmConfNode bpmConfNode = bpmConfNodeManager.findUnique(
            "from BpmConfNode where code=? and bpmConfBase=?",
            process.getId(), bpmConfBase);

    if (bpmConfNode == null) {
        bpmConfNode = new BpmConfNode();
        bpmConfNode.setCode("");
        bpmConfNode.setName("全局");
        bpmConfNode.setType("process");
        bpmConfNode.setConfUser(2);
        bpmConfNode.setConfListener(0);
        bpmConfNode.setConfRule(2);
        bpmConfNode.setConfForm(0);
        bpmConfNode.setConfOperation(2);
        bpmConfNode.setConfNotice(2);
        bpmConfNode.setPriority(priority);
        bpmConfNode.setBpmConfBase(bpmConfBase);
        bpmConfNodeManager.save(bpmConfNode);
    }

    // 配置监听器
    processListener(process.getExecutionListeners(), bpmConfNode);
}
 
Example 9
Source File: CustomExtensionsConverterTest.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
private void validateModel(BpmnModel model) {
  Process process = model.getMainProcess();
  assertNotNull(process.getAttributes());
  assertEquals(1, process.getAttributes().size());
  List<ExtensionAttribute> attributes = process.getAttributes().get("version");
  assertNotNull(attributes);
  assertEquals(1, attributes.size());
  ExtensionAttribute attribute = attributes.get(0);
  // custom:version = "9"
  assertNotNull(attribute);
  assertEquals("http://custom.org/bpmn", attribute.getNamespace());
  assertEquals("custom", attribute.getNamespacePrefix());
  assertEquals("version", attribute.getName());
  assertEquals("9", attribute.getValue());

  List<ActivitiListener> listeners = model.getMainProcess().getExecutionListeners();
  validateExecutionListeners(listeners);
  Map<String, List<ExtensionElement>> extensionElementMap = model.getMainProcess().getExtensionElements();
  validateExtensionElements(extensionElementMap);

  FlowElement flowElement = model.getMainProcess().getFlowElement("servicetask");
  assertNotNull(flowElement);
  assertTrue(flowElement instanceof ServiceTask);
  assertEquals("servicetask", flowElement.getId());
  ServiceTask serviceTask = (ServiceTask) flowElement;
  assertEquals("servicetask", serviceTask.getId());
  assertEquals("Service task", serviceTask.getName());

  List<FieldExtension> fields = serviceTask.getFieldExtensions();
  assertEquals(2, fields.size());
  FieldExtension field = (FieldExtension) fields.get(0);
  assertEquals("testField", field.getFieldName());
  assertEquals("test", field.getStringValue());
  field = (FieldExtension) fields.get(1);
  assertEquals("testField2", field.getFieldName());
  assertEquals("${test}", field.getExpression());

  listeners = serviceTask.getExecutionListeners();
  validateExecutionListeners(listeners);

  extensionElementMap = serviceTask.getExtensionElements();
  validateExtensionElements(extensionElementMap);

  assertEquals(1, serviceTask.getBoundaryEvents().size());
  BoundaryEvent boundaryEvent = serviceTask.getBoundaryEvents().get(0);
  assertEquals("timerEvent", boundaryEvent.getId());
  assertEquals(1, boundaryEvent.getEventDefinitions().size());
  assertTrue(boundaryEvent.getEventDefinitions().get(0) instanceof TimerEventDefinition);
  extensionElementMap = boundaryEvent.getEventDefinitions().get(0).getExtensionElements();
  validateExtensionElements(extensionElementMap);
}
 
Example 10
Source File: CustomNamespaceAttributeConverterTest.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
private void validateModel(BpmnModel model) {
  Process process = model.getMainProcess();
  assertNotNull(process.getAttributes());
  assertEquals(1, process.getAttributes().size());
  List<ExtensionAttribute> attributes = process.getAttributes().get("version");
  assertNotNull(attributes);
  assertEquals(1, attributes.size());
  ExtensionAttribute attribute = attributes.get(0);
  // custom:version = "9"
  assertNotNull(attribute);
  assertEquals("http://custom.org/bpmn", attribute.getNamespace());
  assertEquals("custom", attribute.getNamespacePrefix());
  assertEquals("version", attribute.getName());
  assertEquals("9", attribute.getValue());

  FlowElement flowElement = model.getMainProcess().getFlowElement("usertask");
  assertNotNull(flowElement);
  assertTrue(flowElement instanceof UserTask);
  assertEquals("usertask", flowElement.getId());
  UserTask userTask = (UserTask) flowElement;
  assertEquals("usertask", userTask.getId());
  assertEquals("User Task", userTask.getName());

  Map<String, List<ExtensionAttribute>> attributesMap = userTask.getAttributes();
  assertNotNull(attributesMap);
  assertEquals(2, attributesMap.size());

  attributes = attributesMap.get("id");
  assertNotNull(attributes);
  assertEquals(1, attributes.size());
  ExtensionAttribute a = attributes.get(0);
  assertNotNull(a);
  assertEquals("id", a.getName());
  assertEquals("test", a.getValue());
  assertEquals("custom2", a.getNamespacePrefix());
  assertEquals("http://custom2.org/bpmn", a.getNamespace());

  attributes = attributesMap.get("attr");
  assertNotNull(attributes);
  assertEquals(1, attributes.size());
  a = attributes.get(0);
  assertNotNull(a);
  assertEquals("attr", a.getName());
  assertEquals("attrValue", a.getValue());
  assertEquals("custom2", a.getNamespacePrefix());
  assertEquals("http://custom2.org/bpmn", a.getNamespace());
}
 
Example 11
Source File: EventListenerConverterTest.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
private void validateModel(BpmnModel model) {
  Process process = model.getMainProcess();
  assertNotNull(process);
  assertNotNull(process.getEventListeners());
  assertEquals(8, process.getEventListeners().size());

  // Listener with class
  EventListener listener = process.getEventListeners().get(0);
  assertEquals("ENTITY_CREATE", listener.getEvents());
  assertEquals("org.activiti.test.MyListener", listener.getImplementation());
  assertEquals(ImplementationType.IMPLEMENTATION_TYPE_CLASS, listener.getImplementationType());

  // Listener with class, but no specific event (== all events)
  listener = process.getEventListeners().get(1);
  assertNull(listener.getEvents());
  assertEquals("org.activiti.test.AllEventTypesListener", listener.getImplementation());
  assertEquals(ImplementationType.IMPLEMENTATION_TYPE_CLASS, listener.getImplementationType());

  // Listener with delegate expression
  listener = process.getEventListeners().get(2);
  assertEquals("ENTITY_DELETE", listener.getEvents());
  assertEquals("${myListener}", listener.getImplementation());
  assertEquals(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION, listener.getImplementationType());

  // Listener that throws a signal-event
  listener = process.getEventListeners().get(3);
  assertEquals("ENTITY_DELETE", listener.getEvents());
  assertEquals("theSignal", listener.getImplementation());
  assertEquals(ImplementationType.IMPLEMENTATION_TYPE_THROW_SIGNAL_EVENT, listener.getImplementationType());

  // Listener that throws a global signal-event
  listener = process.getEventListeners().get(4);
  assertEquals("ENTITY_DELETE", listener.getEvents());
  assertEquals("theSignal", listener.getImplementation());
  assertEquals(ImplementationType.IMPLEMENTATION_TYPE_THROW_GLOBAL_SIGNAL_EVENT, listener.getImplementationType());

  // Listener that throws a message-event
  listener = process.getEventListeners().get(5);
  assertEquals("ENTITY_DELETE", listener.getEvents());
  assertEquals("theMessage", listener.getImplementation());
  assertEquals(ImplementationType.IMPLEMENTATION_TYPE_THROW_MESSAGE_EVENT, listener.getImplementationType());

  // Listener that throws an error-event
  listener = process.getEventListeners().get(6);
  assertEquals("ENTITY_DELETE", listener.getEvents());
  assertEquals("123", listener.getImplementation());
  assertEquals(ImplementationType.IMPLEMENTATION_TYPE_THROW_ERROR_EVENT, listener.getImplementationType());

  // Listener restricted to a specific entity
  listener = process.getEventListeners().get(7);
  assertEquals("ENTITY_DELETE", listener.getEvents());
  assertEquals("123", listener.getImplementation());
  assertEquals(ImplementationType.IMPLEMENTATION_TYPE_THROW_ERROR_EVENT, listener.getImplementationType());
  assertEquals("job", listener.getEntityType());
}
 
Example 12
Source File: FlowableModelManagerController.java    From hsweb-framework with Apache License 2.0 4 votes vote down vote up
/**
 * 导出model对象为指定类型
 *
 * @param modelId 模型ID
 * @param type    导出文件类型(bpmn\json)
 */
@GetMapping(value = "export/{modelId}/{type}")
@ApiOperation("导出模型")
@Authorize(action = "export")
@SneakyThrows
public void export(@PathVariable("modelId") @ApiParam("模型ID") String modelId,
                   @PathVariable("type") @ApiParam(value = "类型", allowableValues = "bpmn,json", example = "json")
                           ModelType type,
                   @ApiParam(hidden = true) HttpServletResponse response) {
    Model modelData = repositoryService.getModel(modelId);
    if (modelData == null) {
        throw new NotFoundException("模型不存在");
    }
    BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
    byte[] modelEditorSource = repositoryService.getModelEditorSource(modelData.getId());

    JsonNode editorNode = new ObjectMapper().readTree(modelEditorSource);
    BpmnModel bpmnModel = jsonConverter.convertToBpmnModel(editorNode);

    // 处理异常
    if (bpmnModel.getMainProcess() == null) {
        throw new UnsupportedOperationException("无法导出模型文件:" + type);
    }

    String filename = "";
    byte[] exportBytes = null;

    String mainProcessId = bpmnModel.getMainProcess().getId();

    if (type == ModelType.bpmn) {
        BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
        exportBytes = xmlConverter.convertToXML(bpmnModel);
        filename = mainProcessId + ".bpmn20.xml";
    } else if (type == ModelType.json) {
        exportBytes = modelEditorSource;
        filename = mainProcessId + ".json";

    } else {
        throw new UnsupportedOperationException("不支持的格式:" + type);
    }

    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));

    /*创建输入流*/
    try (ByteArrayInputStream in = new ByteArrayInputStream(exportBytes)) {
        IOUtils.copy(in, response.getOutputStream());
        response.flushBuffer();
    }
}