use of org.activiti.engine.impl.persistence.entity.ExecutionEntity in project Activiti by Activiti.
the class AtomicOperationActivityEnd method eventNotificationsCompleted.
@SuppressWarnings("unchecked")
@Override
protected void eventNotificationsCompleted(InterpretableExecution execution) {
ActivityImpl activity = (ActivityImpl) execution.getActivity();
ActivityImpl parentActivity = activity.getParentActivity();
if (Context.getProcessEngineConfiguration() != null && Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
if (execution instanceof ExecutionEntity) {
ExecutionEntity executionEntity = (ExecutionEntity) execution;
Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createActivityEvent(ActivitiEventType.ACTIVITY_COMPLETED, execution.getActivity().getId(), (String) executionEntity.getActivity().getProperties().get("name"), execution.getId(), execution.getProcessInstanceId(), execution.getProcessDefinitionId(), (String) executionEntity.getActivity().getProperties().get("type"), executionEntity.getActivity().getActivityBehavior().getClass().getCanonicalName()));
}
}
// if the execution is a single path of execution inside the process definition scope
if ((parentActivity != null) && (!parentActivity.isScope())) {
execution.setActivity(parentActivity);
execution.performOperation(ACTIVITY_END);
} else if (execution.isProcessInstanceType()) {
// dispatch process completed event
if (Context.getProcessEngineConfiguration() != null && Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.PROCESS_COMPLETED, execution));
}
execution.performOperation(PROCESS_END);
} else if (execution.isScope()) {
ActivityBehavior parentActivityBehavior = (parentActivity != null ? parentActivity.getActivityBehavior() : null);
if (parentActivityBehavior instanceof CompositeActivityBehavior) {
CompositeActivityBehavior compositeActivityBehavior = (CompositeActivityBehavior) parentActivity.getActivityBehavior();
if (activity.isScope() && activity.getOutgoingTransitions().isEmpty()) {
// there is no transition destroying the scope
InterpretableExecution parentScopeExecution = (InterpretableExecution) execution.getParent();
execution.destroy();
execution.remove();
parentScopeExecution.setActivity(parentActivity);
compositeActivityBehavior.lastExecutionEnded(parentScopeExecution);
} else {
execution.setActivity(parentActivity);
compositeActivityBehavior.lastExecutionEnded(execution);
}
} else {
// default destroy scope behavior
InterpretableExecution parentScopeExecution = (InterpretableExecution) execution.getParent();
execution.destroy();
execution.remove();
// and have no outgoing transitions: end the process instance here
if (activity.getParent() == activity.getProcessDefinition()) {
parentScopeExecution.setActivity(activity);
if (activity.getOutgoingTransitions().isEmpty()) {
// we call end() because it sets isEnded on the execution
parentScopeExecution.end();
} else {
// dispatch process completed event
if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.PROCESS_COMPLETED, execution));
}
parentScopeExecution.performOperation(PROCESS_END);
}
} else {
parentScopeExecution.setActivity(parentActivity);
parentScopeExecution.performOperation(ACTIVITY_END);
}
}
} else {
// execution.isConcurrent() && !execution.isScope()
execution.remove();
// prune if necessary
InterpretableExecution concurrentRoot = (InterpretableExecution) execution.getParent();
if (concurrentRoot.getExecutions().size() == 1) {
InterpretableExecution lastConcurrent = (InterpretableExecution) concurrentRoot.getExecutions().get(0);
if (!lastConcurrent.isScope()) {
concurrentRoot.setActivity((ActivityImpl) lastConcurrent.getActivity());
lastConcurrent.setReplacedBy(concurrentRoot);
// Move children of lastConcurrent one level up
if (!lastConcurrent.getExecutions().isEmpty()) {
concurrentRoot.getExecutions().clear();
for (ActivityExecution childExecution : lastConcurrent.getExecutions()) {
InterpretableExecution childInterpretableExecution = (InterpretableExecution) childExecution;
// casting ... damn generics
((List) concurrentRoot.getExecutions()).add(childExecution);
childInterpretableExecution.setParent(concurrentRoot);
}
lastConcurrent.getExecutions().clear();
}
// Copy execution-local variables of lastConcurrent
concurrentRoot.setVariablesLocal(lastConcurrent.getVariablesLocal());
// Make sure parent execution is re-activated when the last concurrent child execution is active
if (!concurrentRoot.isActive() && lastConcurrent.isActive()) {
concurrentRoot.setActive(true);
}
lastConcurrent.remove();
} else {
lastConcurrent.setConcurrent(false);
}
}
}
}
use of org.activiti.engine.impl.persistence.entity.ExecutionEntity in project Activiti by Activiti.
the class ActivitiEventDispatcherTest method baseEntityEventListener.
/**
* 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(), ActivitiEventType.ENTITY_CREATED);
ActivitiEntityEventImpl deleteEvent = new ActivitiEntityEventImpl(new TaskEntity(), ActivitiEventType.ENTITY_DELETED);
ActivitiEntityEventImpl updateEvent = new ActivitiEntityEventImpl(new TaskEntity(), ActivitiEventType.ENTITY_UPDATED);
ActivitiEntityEventImpl otherEvent = new ActivitiEntityEventImpl(new TaskEntity(), 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 ExecutionEntity(), ActivitiEventType.ENTITY_CREATED);
dispatcher.dispatchEvent(createEventForExecution);
assertFalse(listener.isCreateReceived());
}
use of org.activiti.engine.impl.persistence.entity.ExecutionEntity in project Activiti by Activiti.
the class TerminateEndEventActivityBehavior method dispatchExecutionCancelled.
private void dispatchExecutionCancelled(ActivityExecution execution, ActivityImpl causeActivity) {
// subprocesses
for (ActivityExecution subExecution : execution.getExecutions()) {
dispatchExecutionCancelled(subExecution, causeActivity);
}
// call activities
ExecutionEntity subProcessInstance = Context.getCommandContext().getExecutionEntityManager().findSubProcessInstanceBySuperExecutionId(execution.getId());
if (subProcessInstance != null) {
dispatchExecutionCancelled(subProcessInstance, causeActivity);
}
// activity with message/signal boundary events
ActivityImpl activity = (ActivityImpl) execution.getActivity();
if (activity != null && activity.getActivityBehavior() != null && activity != causeActivity) {
dispatchActivityCancelled(execution, activity, causeActivity);
}
}
use of org.activiti.engine.impl.persistence.entity.ExecutionEntity in project Activiti by Activiti.
the class BpmnActivityBehavior method performOutgoingBehavior.
/**
* Actual implementation of leaving an activity.
*
* @param execution
* The current execution context
* @param checkConditions
* Whether or not to check conditions before determining whether or
* not to take a transition.
* @param throwExceptionIfExecutionStuck
* If true, an {@link ActivitiException} will be thrown in case no
* transition could be found to leave the activity.
*/
protected void performOutgoingBehavior(ActivityExecution execution, boolean checkConditions, boolean throwExceptionIfExecutionStuck, List<ActivityExecution> reusableExecutions) {
if (log.isDebugEnabled()) {
log.debug("Leaving activity '{}'", execution.getActivity().getId());
}
String defaultSequenceFlow = (String) execution.getActivity().getProperty("default");
List<PvmTransition> transitionsToTake = new ArrayList<PvmTransition>();
List<PvmTransition> outgoingTransitions = execution.getActivity().getOutgoingTransitions();
for (PvmTransition outgoingTransition : outgoingTransitions) {
Expression skipExpression = outgoingTransition.getSkipExpression();
if (!SkipExpressionUtil.isSkipExpressionEnabled(execution, skipExpression)) {
if (defaultSequenceFlow == null || !outgoingTransition.getId().equals(defaultSequenceFlow)) {
Condition condition = (Condition) outgoingTransition.getProperty(BpmnParse.PROPERTYNAME_CONDITION);
if (condition == null || !checkConditions || condition.evaluate(outgoingTransition.getId(), execution)) {
transitionsToTake.add(outgoingTransition);
}
}
} else if (SkipExpressionUtil.shouldSkipFlowElement(execution, skipExpression)) {
transitionsToTake.add(outgoingTransition);
}
}
if (transitionsToTake.size() == 1) {
execution.take(transitionsToTake.get(0));
} else if (transitionsToTake.size() >= 1) {
execution.inactivate();
if (reusableExecutions == null || reusableExecutions.isEmpty()) {
execution.takeAll(transitionsToTake, Arrays.asList(execution));
} else {
execution.takeAll(transitionsToTake, reusableExecutions);
}
} else {
if (defaultSequenceFlow != null) {
PvmTransition defaultTransition = execution.getActivity().findOutgoingTransition(defaultSequenceFlow);
if (defaultTransition != null) {
execution.take(defaultTransition);
} else {
throw new ActivitiException("Default sequence flow '" + defaultSequenceFlow + "' could not be not found");
}
} else {
Object isForCompensation = execution.getActivity().getProperty(BpmnParse.PROPERTYNAME_IS_FOR_COMPENSATION);
if (isForCompensation != null && (Boolean) isForCompensation) {
if (execution instanceof ExecutionEntity) {
Context.getCommandContext().getHistoryManager().recordActivityEnd((ExecutionEntity) execution);
}
InterpretableExecution parentExecution = (InterpretableExecution) execution.getParent();
((InterpretableExecution) execution).remove();
parentExecution.signal("compensationDone", null);
} else {
if (log.isDebugEnabled()) {
log.debug("No outgoing sequence flow found for {}. Ending execution.", execution.getActivity().getId());
}
execution.end();
if (throwExceptionIfExecutionStuck) {
throw new ActivitiException("No outgoing sequence flow of the inclusive gateway '" + execution.getActivity().getId() + "' could be selected for continuing the process");
}
}
}
}
}
use of org.activiti.engine.impl.persistence.entity.ExecutionEntity in project Activiti by Activiti.
the class ScopeUtil method throwCompensationEvent.
/**
* we create a separate execution for each compensation handler invocation.
*/
public static void throwCompensationEvent(List<CompensateEventSubscriptionEntity> eventSubscriptions, ActivityExecution execution, boolean async) {
// first spawn the compensating executions
for (EventSubscriptionEntity eventSubscription : eventSubscriptions) {
ExecutionEntity compensatingExecution = null;
// and holds snapshot data).
if (eventSubscription.getConfiguration() != null) {
compensatingExecution = Context.getCommandContext().getExecutionEntityManager().findExecutionById(eventSubscription.getConfiguration());
// move the compensating execution under this execution:
compensatingExecution.setParent((InterpretableExecution) execution);
compensatingExecution.setEventScope(false);
} else {
compensatingExecution = (ExecutionEntity) execution.createExecution();
eventSubscription.setConfiguration(compensatingExecution.getId());
}
compensatingExecution.setConcurrent(true);
}
// signal compensation events in reverse order of their 'created' timestamp
Collections.sort(eventSubscriptions, new Comparator<EventSubscriptionEntity>() {
public int compare(EventSubscriptionEntity o1, EventSubscriptionEntity o2) {
return o2.getCreated().compareTo(o1.getCreated());
}
});
for (CompensateEventSubscriptionEntity compensateEventSubscriptionEntity : eventSubscriptions) {
compensateEventSubscriptionEntity.eventReceived(null, async);
}
}
Aggregations