org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl Java Examples

The following examples show how to use org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl. 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: ActivitiEventDispatcherTest.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
/**
 * Test that adding a listener with a null-type is never called.
 */
public void addAndRemoveEventListenerTypedNullType() throws Exception {

  // Create a listener that just adds the events to a list
  TestActivitiEventListener newListener = new TestActivitiEventListener();

  // Add event-listener to dispatcher
  dispatcher.addEventListener(newListener, (ActivitiEventType) null);

  ActivitiEntityEventImpl event1 = new ActivitiEntityEventImpl(processEngineConfiguration.getTaskEntityManager().create(), ActivitiEventType.ENTITY_CREATED);
  ActivitiEntityEventImpl event2 = new ActivitiEntityEventImpl(processEngineConfiguration.getTaskEntityManager().create(), ActivitiEventType.ENTITY_DELETED);

  // Dispatch events, all should have entered the listener
  dispatcher.dispatchEvent(event1);
  dispatcher.dispatchEvent(event2);

  assertTrue(newListener.getEventsReceived().isEmpty());
}
 
Example #2
Source File: FlowableEventDispatcherTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
/**
 * Test that adding a listener with a null-type is never called.
 */
public void addAndRemoveEventListenerTypedNullType() throws Exception {

    // Create a listener that just adds the events to a list
    TestFlowableEventListener newListener = new TestFlowableEventListener();

    // Add event-listener to dispatcher
    dispatcher.addEventListener(newListener, (FlowableEngineEventType) null);

    ActivitiEntityEventImpl event1 = new ActivitiEntityEventImpl(new TaskEntity(), FlowableEngineEventType.ENTITY_CREATED);
    ActivitiEntityEventImpl event2 = new ActivitiEntityEventImpl(new TaskEntity(), FlowableEngineEventType.ENTITY_DELETED);

    // Dispatch events, all should have entered the listener
    dispatcher.dispatchEvent(event1);
    dispatcher.dispatchEvent(event2);

    assertTrue(newListener.getEventsReceived().isEmpty());
}
 
Example #3
Source File: SkipEventListener.java    From lemon with Apache License 2.0 6 votes vote down vote up
public void onEvent(ActivitiEvent event) {
    if (!(event instanceof ActivitiEntityEventImpl)) {
        return;
    }

    ActivitiEntityEventImpl activitiEntityEventImpl = (ActivitiEntityEventImpl) event;
    Object entity = activitiEntityEventImpl.getEntity();

    if (!(entity instanceof TaskEntity)) {
        return;
    }

    TaskEntity taskEntity = (TaskEntity) entity;

    try {
        switch (event.getType()) {
        case TASK_CREATED:
            logger.debug("create : {}", taskEntity.getId());
            this.onCreate(taskEntity);

            break;
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }
}
 
Example #4
Source File: AutoCompleteFirstTaskEventListener.java    From lemon with Apache License 2.0 6 votes vote down vote up
public void onEvent(ActivitiEvent event) {
    if (!(event instanceof ActivitiEntityEventImpl)) {
        return;
    }

    ActivitiEntityEventImpl activitiEntityEventImpl = (ActivitiEntityEventImpl) event;
    Object entity = activitiEntityEventImpl.getEntity();

    if (!(entity instanceof TaskEntity)) {
        return;
    }

    TaskEntity taskEntity = (TaskEntity) entity;

    try {
        switch (event.getType()) {
        case TASK_CREATED:
            logger.debug("create : {}", taskEntity.getId());
            this.onCreate(taskEntity);

            break;
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }
}
 
Example #5
Source File: ActivitiEventDispatcherTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
/**
 * Test adding a listener and check if events are sent to it. Also checks that after removal, no events are received.
 */
public void addAndRemoveEventListenerAllEvents() throws Exception {
  // Create a listener that just adds the events to a list
  TestActivitiEventListener newListener = new TestActivitiEventListener();

  // Add event-listener to dispatcher
  dispatcher.addEventListener(newListener);

  ActivitiEntityEventImpl event1 = new ActivitiEntityEventImpl(processEngineConfiguration.getTaskEntityManager().create(), ActivitiEventType.ENTITY_CREATED);
  ActivitiEntityEventImpl event2 = new ActivitiEntityEventImpl(processEngineConfiguration.getTaskEntityManager().create(), ActivitiEventType.ENTITY_CREATED);

  // Dispatch events
  dispatcher.dispatchEvent(event1);
  dispatcher.dispatchEvent(event2);

  assertEquals(2, newListener.getEventsReceived().size());
  assertEquals(event1, newListener.getEventsReceived().get(0));
  assertEquals(event2, newListener.getEventsReceived().get(1));

  // Remove listener and dispatch events again, listener should not be
  // invoked
  dispatcher.removeEventListener(newListener);
  newListener.clearEventsReceived();
  dispatcher.dispatchEvent(event1);
  dispatcher.dispatchEvent(event2);

  assertTrue(newListener.getEventsReceived().isEmpty());
}
 
Example #6
Source File: ActivitiEventDispatcherTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
/**
 * Test adding a listener and check if events are sent to it, for the types it was registered for. Also checks that after removal, no events are received.
 */
public void addAndRemoveEventListenerTyped() throws Exception {
  // Create a listener that just adds the events to a list
  TestActivitiEventListener newListener = new TestActivitiEventListener();

  // Add event-listener to dispatcher
  dispatcher.addEventListener(newListener, ActivitiEventType.ENTITY_CREATED, ActivitiEventType.ENTITY_DELETED);

  ActivitiEntityEventImpl event1 = new ActivitiEntityEventImpl(processEngineConfiguration.getTaskEntityManager().create(), ActivitiEventType.ENTITY_CREATED);
  ActivitiEntityEventImpl event2 = new ActivitiEntityEventImpl(processEngineConfiguration.getTaskEntityManager().create(), ActivitiEventType.ENTITY_DELETED);
  ActivitiEntityEventImpl event3 = new ActivitiEntityEventImpl(processEngineConfiguration.getTaskEntityManager().create(), ActivitiEventType.ENTITY_UPDATED);

  // Dispatch events, only 2 out of 3 should have entered the listener
  dispatcher.dispatchEvent(event1);
  dispatcher.dispatchEvent(event2);
  dispatcher.dispatchEvent(event3);

  assertEquals(2, newListener.getEventsReceived().size());
  assertEquals(event1, newListener.getEventsReceived().get(0));
  assertEquals(event2, newListener.getEventsReceived().get(1));

  // Remove listener and dispatch events again, listener should not be
  // invoked
  dispatcher.removeEventListener(newListener);
  newListener.clearEventsReceived();
  dispatcher.dispatchEvent(event1);
  dispatcher.dispatchEvent(event2);

  assertTrue(newListener.getEventsReceived().isEmpty());
}
 
Example #7
Source File: FlowableEventDispatcherTest.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Test adding a listener and check if events are sent to it. Also checks that after removal, no events are received.
 */
public void addAndRemoveEventListenerAllEvents() throws Exception {
    // Create a listener that just adds the events to a list
    TestFlowableEventListener newListener = new TestFlowableEventListener();

    // Add event-listener to dispatcher
    dispatcher.addEventListener(newListener);

    ActivitiEntityEventImpl event1 = new ActivitiEntityEventImpl(new TaskEntity(), FlowableEngineEventType.ENTITY_CREATED);
    ActivitiEntityEventImpl event2 = new ActivitiEntityEventImpl(new TaskEntity(), FlowableEngineEventType.ENTITY_CREATED);

    // Dispatch events
    dispatcher.dispatchEvent(event1);
    dispatcher.dispatchEvent(event2);

    assertEquals(2, newListener.getEventsReceived().size());
    assertEquals(event1, newListener.getEventsReceived().get(0));
    assertEquals(event2, newListener.getEventsReceived().get(1));

    // Remove listener and dispatch events again, listener should not be invoked
    dispatcher.removeEventListener(newListener);
    newListener.clearEventsReceived();
    dispatcher.dispatchEvent(event1);
    dispatcher.dispatchEvent(event2);

    assertTrue(newListener.getEventsReceived().isEmpty());
}
 
Example #8
Source File: FlowableEventDispatcherTest.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Test adding a listener and check if events are sent to it, for the types it was registered for. Also checks that after removal, no events are received.
 */
public void addAndRemoveEventListenerTyped() throws Exception {
    // Create a listener that just adds the events to a list
    TestFlowableEventListener newListener = new TestFlowableEventListener();

    // Add event-listener to dispatcher
    dispatcher.addEventListener(newListener, FlowableEngineEventType.ENTITY_CREATED, FlowableEngineEventType.ENTITY_DELETED);

    ActivitiEntityEventImpl event1 = new ActivitiEntityEventImpl(new TaskEntity(), FlowableEngineEventType.ENTITY_CREATED);
    ActivitiEntityEventImpl event2 = new ActivitiEntityEventImpl(new TaskEntity(), FlowableEngineEventType.ENTITY_DELETED);
    ActivitiEntityEventImpl event3 = new ActivitiEntityEventImpl(new TaskEntity(), FlowableEngineEventType.ENTITY_UPDATED);

    // Dispatch events, only 2 out of 3 should have entered the listener
    dispatcher.dispatchEvent(event1);
    dispatcher.dispatchEvent(event2);
    dispatcher.dispatchEvent(event3);

    assertEquals(2, newListener.getEventsReceived().size());
    assertEquals(event1, newListener.getEventsReceived().get(0));
    assertEquals(event2, newListener.getEventsReceived().get(1));

    // Remove listener and dispatch events again, listener should not be invoked
    dispatcher.removeEventListener(newListener);
    newListener.clearEventsReceived();
    dispatcher.dispatchEvent(event1);
    dispatcher.dispatchEvent(event2);

    assertTrue(newListener.getEventsReceived().isEmpty());
}
 
Example #9
Source File: ActivitiEventDispatcherTest.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
/**
 * Test the {@link BaseEntityEventListener} shipped with Activiti.
 */
public void baseEntityEventListener() throws Exception {
  TestBaseEntityEventListener listener = new TestBaseEntityEventListener();

  dispatcher.addEventListener(listener);

  ActivitiEntityEventImpl createEvent = new ActivitiEntityEventImpl(processEngineConfiguration.getTaskEntityManager().create(), ActivitiEventType.ENTITY_CREATED);
  ActivitiEntityEventImpl deleteEvent = new ActivitiEntityEventImpl(processEngineConfiguration.getTaskEntityManager().create(), ActivitiEventType.ENTITY_DELETED);
  ActivitiEntityEventImpl updateEvent = new ActivitiEntityEventImpl(processEngineConfiguration.getTaskEntityManager().create(), ActivitiEventType.ENTITY_UPDATED);
  ActivitiEntityEventImpl otherEvent = new ActivitiEntityEventImpl(processEngineConfiguration.getTaskEntityManager().create(), ActivitiEventType.CUSTOM);

  // Dispatch create event
  dispatcher.dispatchEvent(createEvent);
  assertTrue(listener.isCreateReceived());
  assertFalse(listener.isUpdateReceived());
  assertFalse(listener.isCustomReceived());
  assertFalse(listener.isInitializeReceived());
  assertFalse(listener.isDeleteReceived());
  listener.reset();

  // Dispatch update event
  dispatcher.dispatchEvent(updateEvent);
  assertTrue(listener.isUpdateReceived());
  assertFalse(listener.isCreateReceived());
  assertFalse(listener.isCustomReceived());
  assertFalse(listener.isDeleteReceived());
  listener.reset();

  // Dispatch delete event
  dispatcher.dispatchEvent(deleteEvent);
  assertTrue(listener.isDeleteReceived());
  assertFalse(listener.isCreateReceived());
  assertFalse(listener.isCustomReceived());
  assertFalse(listener.isUpdateReceived());
  listener.reset();

  // Dispatch other event
  dispatcher.dispatchEvent(otherEvent);
  assertTrue(listener.isCustomReceived());
  assertFalse(listener.isCreateReceived());
  assertFalse(listener.isUpdateReceived());
  assertFalse(listener.isDeleteReceived());
  listener.reset();

  // Test typed entity-listener
  listener = new TestBaseEntityEventListener(Task.class);

  // Dispatch event for a task, should be received
  dispatcher.addEventListener(listener);
  dispatcher.dispatchEvent(createEvent);

  assertTrue(listener.isCreateReceived());
  listener.reset();

  // Dispatch event for a execution, should NOT be received
  ActivitiEntityEventImpl createEventForExecution = new ActivitiEntityEventImpl(new ExecutionEntityImpl(), ActivitiEventType.ENTITY_CREATED);

  dispatcher.dispatchEvent(createEventForExecution);
  assertFalse(listener.isCreateReceived());
}
 
Example #10
Source File: FlowableEventDispatcherTest.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
/**
 * Test the {@link BaseEntityEventListener} shipped with Activiti.
 */
public void baseEntityEventListener() throws Exception {
    TestBaseEntityEventListener listener = new TestBaseEntityEventListener();

    dispatcher.addEventListener(listener);

    ActivitiEntityEventImpl createEvent = new ActivitiEntityEventImpl(new TaskEntity(),
            FlowableEngineEventType.ENTITY_CREATED);
    ActivitiEntityEventImpl deleteEvent = new ActivitiEntityEventImpl(new TaskEntity(),
            FlowableEngineEventType.ENTITY_DELETED);
    ActivitiEntityEventImpl updateEvent = new ActivitiEntityEventImpl(new TaskEntity(),
            FlowableEngineEventType.ENTITY_UPDATED);
    ActivitiEntityEventImpl otherEvent = new ActivitiEntityEventImpl(new TaskEntity(), FlowableEngineEventType.CUSTOM);

    // Dispatch create event
    dispatcher.dispatchEvent(createEvent);
    assertTrue(listener.isCreateReceived());
    assertFalse(listener.isUpdateReceived());
    assertFalse(listener.isCustomReceived());
    assertFalse(listener.isInitializeReceived());
    assertFalse(listener.isDeleteReceived());
    listener.reset();

    // Dispatch update event
    dispatcher.dispatchEvent(updateEvent);
    assertTrue(listener.isUpdateReceived());
    assertFalse(listener.isCreateReceived());
    assertFalse(listener.isCustomReceived());
    assertFalse(listener.isDeleteReceived());
    listener.reset();

    // Dispatch delete event
    dispatcher.dispatchEvent(deleteEvent);
    assertTrue(listener.isDeleteReceived());
    assertFalse(listener.isCreateReceived());
    assertFalse(listener.isCustomReceived());
    assertFalse(listener.isUpdateReceived());
    listener.reset();

    // Dispatch other event
    dispatcher.dispatchEvent(otherEvent);
    assertTrue(listener.isCustomReceived());
    assertFalse(listener.isCreateReceived());
    assertFalse(listener.isUpdateReceived());
    assertFalse(listener.isDeleteReceived());
    listener.reset();

    // Test typed entity-listener
    listener = new TestBaseEntityEventListener(org.flowable.task.api.Task.class);

    // Dispatch event for a task, should be received
    dispatcher.addEventListener(listener);
    dispatcher.dispatchEvent(createEvent);

    assertTrue(listener.isCreateReceived());
    listener.reset();

    // Dispatch event for a execution, should NOT be received
    ActivitiEntityEventImpl createEventForExecution = new ActivitiEntityEventImpl(new ExecutionEntity(),
            FlowableEngineEventType.ENTITY_CREATED);

    dispatcher.dispatchEvent(createEventForExecution);
    assertFalse(listener.isCreateReceived());
}
 
Example #11
Source File: HumanTaskEventListener.java    From lemon with Apache License 2.0 4 votes vote down vote up
public void onEvent(ActivitiEvent event) {
    if (!(event instanceof ActivitiEntityEventImpl)) {
        return;
    }

    ActivitiEntityEventImpl activitiEntityEventImpl = (ActivitiEntityEventImpl) event;
    Object entity = activitiEntityEventImpl.getEntity();

    if (!(entity instanceof TaskEntity)) {
        return;
    }

    TaskEntity taskEntity = (TaskEntity) entity;

    try {
        switch (event.getType()) {
        case TASK_CREATED:
            logger.debug("create : {}", taskEntity.getId());
            this.onCreate(taskEntity);

            break;

        case TASK_ASSIGNED:
            logger.debug("assign : {}", taskEntity.getId());
            this.onAssign(taskEntity);

            break;

        case TASK_COMPLETED:
            logger.debug("complete : {}", taskEntity.getId());
            this.onComplete(taskEntity);

            break;

        case ENTITY_DELETED:
            logger.debug("delete : {}", taskEntity.getId());
            this.onDelete(taskEntity);

            break;

        default:
            logger.debug("{} : {}", event.getType(), taskEntity.getId());
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }
}
 
Example #12
Source File: UpdateProcessInstanceNameEventListener.java    From lemon with Apache License 2.0 4 votes vote down vote up
protected void onInitialized(ActivitiEvent event) {
    if (!(event instanceof ActivitiEntityEventImpl)) {
        return;
    }

    ActivitiEntityEventImpl activitiEntityEventImpl = (ActivitiEntityEventImpl) event;
    Object entity = activitiEntityEventImpl.getEntity();

    if (!(entity instanceof ExecutionEntity)) {
        return;
    }

    ActivitiEventType activitiEventType = activitiEntityEventImpl.getType();

    if (activitiEventType != ActivitiEventType.ENTITY_INITIALIZED) {
        return;
    }

    ExecutionEntity executionEntity = (ExecutionEntity) entity;

    if (!executionEntity.isProcessInstanceType()) {
        return;
    }

    String processInstanceId = executionEntity.getId();
    String processDefinitionId = executionEntity.getProcessDefinitionId();
    CommandContext commandContext = Context.getCommandContext();
    ProcessDefinitionEntity processDefinition = new GetDeploymentProcessDefinitionCmd(
            processDefinitionId).execute(commandContext);

    // {流程标题:title}-{发起人:startUser}-{发起时间:startTime}
    String processDefinitionName = processDefinition.getName();

    if (processDefinitionName == null) {
        processDefinitionName = processDefinition.getKey();
    }

    String userId = Authentication.getAuthenticatedUserId();
    String displayName = userConnector.findById(userId).getDisplayName();
    String processInstanceName = processDefinitionName + "-" + displayName
            + "-"
            + new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
    // runtime
    executionEntity.setName(processInstanceName);

    // history
    HistoricProcessInstanceEntity historicProcessInstanceEntity = commandContext
            .getHistoricProcessInstanceEntityManager()
            .findHistoricProcessInstance(processInstanceId);
    historicProcessInstanceEntity.setName(processInstanceName);
}