Search in sources :

Example 21 with Expression

use of org.activiti.engine.delegate.Expression in project Activiti by Activiti.

the class VariableScopeExpressionEvaluatorTest method evaluate_should_returnResultOfGetValueWithVariableScope.

@Test
public void evaluate_should_returnResultOfGetValueWithVariableScope() {
    // given
    VariableScope variableScope = mock(VariableScope.class);
    VariableScopeExpressionEvaluator evaluator = new VariableScopeExpressionEvaluator(variableScope);
    Expression expression = mock(Expression.class);
    given(expression.getValue(variableScope)).willReturn("London");
    // when
    Object value = evaluator.evaluate(expression, expressionManager, delegateInterceptor);
    // then
    assertThat(value).isEqualTo("London");
}
Also used : Expression(org.activiti.engine.delegate.Expression) VariableScope(org.activiti.engine.delegate.VariableScope) Test(org.junit.jupiter.api.Test)

Example 22 with Expression

use of org.activiti.engine.delegate.Expression in project Activiti by Activiti.

the class DefaultJobManager method restoreExtraData.

protected void restoreExtraData(JobEntity timerEntity, VariableScope variableScope) {
    String activityId = timerEntity.getJobHandlerConfiguration();
    if (timerEntity.getJobHandlerType().equalsIgnoreCase(TimerStartEventJobHandler.TYPE) || timerEntity.getJobHandlerType().equalsIgnoreCase(TriggerTimerEventJobHandler.TYPE)) {
        activityId = TimerEventHandler.getActivityIdFromConfiguration(timerEntity.getJobHandlerConfiguration());
        String endDateExpressionString = TimerEventHandler.getEndDateFromConfiguration(timerEntity.getJobHandlerConfiguration());
        if (endDateExpressionString != null) {
            Expression endDateExpression = processEngineConfiguration.getExpressionManager().createExpression(endDateExpressionString);
            String endDateString = null;
            BusinessCalendar businessCalendar = processEngineConfiguration.getBusinessCalendarManager().getBusinessCalendar(getBusinessCalendarName(TimerEventHandler.geCalendarNameFromConfiguration(timerEntity.getJobHandlerConfiguration()), variableScope));
            if (endDateExpression != null) {
                Object endDateValue = endDateExpression.getValue(variableScope);
                if (endDateValue instanceof String) {
                    endDateString = (String) endDateValue;
                } else if (endDateValue instanceof Date) {
                    timerEntity.setEndDate((Date) endDateValue);
                } else {
                    throw new ActivitiException("Timer '" + ((ExecutionEntity) variableScope).getActivityId() + "' was not configured with a valid duration/time, either hand in a java.util.Date or a String in format 'yyyy-MM-dd'T'hh:mm:ss'");
                }
                if (timerEntity.getEndDate() == null) {
                    timerEntity.setEndDate(businessCalendar.resolveEndDate(endDateString));
                }
            }
        }
    }
    int maxIterations = 1;
    if (timerEntity.getProcessDefinitionId() != null) {
        org.activiti.bpmn.model.Process process = ProcessDefinitionUtil.getProcess(timerEntity.getProcessDefinitionId());
        maxIterations = getMaxIterations(process, activityId);
        if (maxIterations <= 1) {
            maxIterations = getMaxIterations(process, activityId);
        }
    }
    timerEntity.setMaxIterations(maxIterations);
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) Expression(org.activiti.engine.delegate.Expression) CycleBusinessCalendar(org.activiti.engine.impl.calendar.CycleBusinessCalendar) BusinessCalendar(org.activiti.engine.impl.calendar.BusinessCalendar) Date(java.util.Date)

Example 23 with Expression

use of org.activiti.engine.delegate.Expression in project Activiti by Activiti.

the class BoundarySignalEventActivityBehavior method execute.

@Override
public void execute(DelegateExecution execution) {
    CommandContext commandContext = Context.getCommandContext();
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    String signalName = null;
    if (StringUtils.isNotEmpty(signalEventDefinition.getSignalRef())) {
        signalName = signalEventDefinition.getSignalRef();
    } else {
        Expression signalExpression = commandContext.getProcessEngineConfiguration().getExpressionManager().createExpression(signalEventDefinition.getSignalExpression());
        signalName = signalExpression.getValue(execution).toString();
    }
    commandContext.getEventSubscriptionEntityManager().insertSignalEvent(signalName, signal, executionEntity);
}
Also used : CommandContext(org.activiti.engine.impl.interceptor.CommandContext) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) Expression(org.activiti.engine.delegate.Expression)

Example 24 with Expression

use of org.activiti.engine.delegate.Expression in project Activiti by Activiti.

the class TimerUtil method createTimerEntityForTimerEventDefinition.

/**
 * The event definition on which the timer is based.
 *
 * Takes in an optional execution, if missing the {@link NoExecutionVariableScope} will be used (eg Timer start event)
 */
public static TimerJobEntity createTimerEntityForTimerEventDefinition(TimerEventDefinition timerEventDefinition, boolean isInterruptingTimer, ExecutionEntity executionEntity, String jobHandlerType, String jobHandlerConfig) {
    ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
    String businessCalendarRef = null;
    Expression expression = null;
    ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager();
    // ACT-1415: timer-declaration on start-event may contain expressions NOT
    // evaluating variables but other context, evaluating should happen nevertheless
    VariableScope scopeForExpression = executionEntity;
    if (scopeForExpression == null) {
        scopeForExpression = NoExecutionVariableScope.getSharedInstance();
    }
    if (StringUtils.isNotEmpty(timerEventDefinition.getTimeDate())) {
        businessCalendarRef = DueDateBusinessCalendar.NAME;
        expression = expressionManager.createExpression(timerEventDefinition.getTimeDate());
    } else if (StringUtils.isNotEmpty(timerEventDefinition.getTimeCycle())) {
        businessCalendarRef = CycleBusinessCalendar.NAME;
        expression = expressionManager.createExpression(timerEventDefinition.getTimeCycle());
    } else if (StringUtils.isNotEmpty(timerEventDefinition.getTimeDuration())) {
        businessCalendarRef = DurationBusinessCalendar.NAME;
        expression = expressionManager.createExpression(timerEventDefinition.getTimeDuration());
    }
    if (StringUtils.isNotEmpty(timerEventDefinition.getCalendarName())) {
        businessCalendarRef = timerEventDefinition.getCalendarName();
        Expression businessCalendarExpression = expressionManager.createExpression(businessCalendarRef);
        businessCalendarRef = businessCalendarExpression.getValue(scopeForExpression).toString();
    }
    if (expression == null) {
        throw new ActivitiException("Timer needs configuration (either timeDate, timeCycle or timeDuration is needed) (" + timerEventDefinition.getId() + ")");
    }
    BusinessCalendar businessCalendar = processEngineConfiguration.getBusinessCalendarManager().getBusinessCalendar(businessCalendarRef);
    String dueDateString = null;
    Date duedate = null;
    Object dueDateValue = expression.getValue(scopeForExpression);
    if (dueDateValue instanceof String) {
        dueDateString = (String) dueDateValue;
    } else if (dueDateValue instanceof Date) {
        duedate = (Date) dueDateValue;
    } else if (dueDateValue instanceof DateTime) {
        // JodaTime support
        duedate = ((DateTime) dueDateValue).toDate();
    } else if (dueDateValue != null) {
        throw new ActivitiException("Timer '" + executionEntity.getActivityId() + "' was not configured with a valid duration/time, either hand in a java.util.Date or a String in format 'yyyy-MM-dd'T'hh:mm:ss'");
    }
    if (duedate == null && dueDateString != null) {
        duedate = businessCalendar.resolveDuedate(dueDateString);
    }
    TimerJobEntity timer = null;
    if (duedate != null) {
        timer = Context.getCommandContext().getTimerJobEntityManager().create();
        timer.setJobType(JobEntity.JOB_TYPE_TIMER);
        timer.setRevision(1);
        timer.setJobHandlerType(jobHandlerType);
        timer.setJobHandlerConfiguration(jobHandlerConfig);
        timer.setExclusive(true);
        timer.setRetries(processEngineConfiguration.getAsyncExecutorNumberOfRetries());
        timer.setDuedate(duedate);
        if (executionEntity != null) {
            timer.setExecution(executionEntity);
            timer.setProcessDefinitionId(executionEntity.getProcessDefinitionId());
            timer.setProcessInstanceId(executionEntity.getProcessInstanceId());
            // Inherit tenant identifier (if applicable)
            if (executionEntity.getTenantId() != null) {
                timer.setTenantId(executionEntity.getTenantId());
            }
        }
    }
    if (StringUtils.isNotEmpty(timerEventDefinition.getTimeCycle())) {
        // See ACT-1427: A boundary timer with a cancelActivity='true', doesn't need to repeat itself
        boolean repeat = !isInterruptingTimer;
        // ACT-1951: intermediate catching timer events shouldn't repeat according to spec
        if (executionEntity != null) {
            FlowElement currentElement = executionEntity.getCurrentFlowElement();
            if (currentElement instanceof IntermediateCatchEvent) {
                repeat = false;
            }
        }
        if (repeat) {
            String prepared = prepareRepeat(dueDateString);
            timer.setRepeat(prepared);
        }
    }
    if (timer != null && executionEntity != null) {
        timer.setExecution(executionEntity);
        timer.setProcessDefinitionId(executionEntity.getProcessDefinitionId());
        // Inherit tenant identifier (if applicable)
        if (executionEntity.getTenantId() != null) {
            timer.setTenantId(executionEntity.getTenantId());
        }
    }
    return timer;
}
Also used : ExpressionManager(org.activiti.engine.impl.el.ExpressionManager) ActivitiException(org.activiti.engine.ActivitiException) IntermediateCatchEvent(org.activiti.bpmn.model.IntermediateCatchEvent) Date(java.util.Date) DateTime(org.joda.time.DateTime) Expression(org.activiti.engine.delegate.Expression) FlowElement(org.activiti.bpmn.model.FlowElement) DueDateBusinessCalendar(org.activiti.engine.impl.calendar.DueDateBusinessCalendar) CycleBusinessCalendar(org.activiti.engine.impl.calendar.CycleBusinessCalendar) DurationBusinessCalendar(org.activiti.engine.impl.calendar.DurationBusinessCalendar) BusinessCalendar(org.activiti.engine.impl.calendar.BusinessCalendar) ProcessEngineConfigurationImpl(org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl) TimerJobEntity(org.activiti.engine.impl.persistence.entity.TimerJobEntity) VariableScope(org.activiti.engine.delegate.VariableScope) NoExecutionVariableScope(org.activiti.engine.impl.el.NoExecutionVariableScope)

Example 25 with Expression

use of org.activiti.engine.delegate.Expression in project Activiti by Activiti.

the class CallActivityBehavior method execute.

public void execute(DelegateExecution execution) {
    String finalProcessDefinitonKey = null;
    if (processDefinitionExpression != null) {
        finalProcessDefinitonKey = (String) processDefinitionExpression.getValue(execution);
    } else {
        finalProcessDefinitonKey = processDefinitonKey;
    }
    ProcessDefinition processDefinition = findProcessDefinition(finalProcessDefinitonKey, execution.getTenantId());
    // Get model from cache
    Process subProcess = ProcessDefinitionUtil.getProcess(processDefinition.getId());
    if (subProcess == null) {
        throw new ActivitiException("Cannot start a sub process instance. Process model " + processDefinition.getName() + " (id = " + processDefinition.getId() + ") could not be found");
    }
    FlowElement initialFlowElement = subProcess.getInitialFlowElement();
    if (initialFlowElement == null) {
        throw new ActivitiException("No start element found for process definition " + processDefinition.getId());
    }
    // Do not start a process instance if the process definition is suspended
    if (ProcessDefinitionUtil.isProcessDefinitionSuspended(processDefinition.getId())) {
        throw new ActivitiException("Cannot start process instance. Process definition " + processDefinition.getName() + " (id = " + processDefinition.getId() + ") is suspended");
    }
    ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
    ExecutionEntityManager executionEntityManager = Context.getCommandContext().getExecutionEntityManager();
    ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager();
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    CallActivity callActivity = (CallActivity) executionEntity.getCurrentFlowElement();
    String businessKey = null;
    if (!StringUtils.isEmpty(callActivity.getBusinessKey())) {
        Expression expression = expressionManager.createExpression(callActivity.getBusinessKey());
        businessKey = expression.getValue(execution).toString();
    } else if (callActivity.isInheritBusinessKey()) {
        ExecutionEntity processInstance = executionEntityManager.findById(execution.getProcessInstanceId());
        businessKey = processInstance.getBusinessKey();
    }
    ExecutionEntity subProcessInstance = Context.getCommandContext().getExecutionEntityManager().createSubprocessInstance(processDefinition, executionEntity, businessKey);
    Context.getCommandContext().getHistoryManager().recordSubProcessInstanceStart(executionEntity, subProcessInstance, initialFlowElement);
    // process template-defined data objects
    Map<String, Object> variables = processDataObjects(subProcess.getDataObjects());
    if (callActivity.isInheritVariables()) {
        Map<String, Object> executionVariables = execution.getVariables();
        for (Map.Entry<String, Object> entry : executionVariables.entrySet()) {
            variables.put(entry.getKey(), entry.getValue());
        }
    }
    Map<String, Object> variablesFromExtensionFile = calculateInboundVariables(execution, processDefinition);
    variables.putAll(variablesFromExtensionFile);
    if (!variables.isEmpty()) {
        initializeVariables(subProcessInstance, variables);
    }
    // Create the first execution that will visit all the process definition elements
    ExecutionEntity subProcessInitialExecution = executionEntityManager.createChildExecution(subProcessInstance);
    subProcessInitialExecution.setCurrentFlowElement(initialFlowElement);
    Context.getAgenda().planContinueProcessOperation(subProcessInitialExecution);
    Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createProcessStartedEvent(subProcessInitialExecution, variables, false));
}
Also used : ExpressionManager(org.activiti.engine.impl.el.ExpressionManager) ActivitiException(org.activiti.engine.ActivitiException) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) Process(org.activiti.bpmn.model.Process) CallActivity(org.activiti.bpmn.model.CallActivity) ExecutionEntityManager(org.activiti.engine.impl.persistence.entity.ExecutionEntityManager) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) Expression(org.activiti.engine.delegate.Expression) FlowElement(org.activiti.bpmn.model.FlowElement) ValuedDataObject(org.activiti.bpmn.model.ValuedDataObject) ProcessEngineConfigurationImpl(org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Expression (org.activiti.engine.delegate.Expression)61 ActivitiException (org.activiti.engine.ActivitiException)17 DelegateExecution (org.activiti.engine.delegate.DelegateExecution)13 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)12 Test (org.junit.Test)12 Test (org.junit.jupiter.api.Test)12 ExpressionManager (org.activiti.engine.impl.el.ExpressionManager)11 ExecutionEntity (org.activiti.engine.impl.persistence.entity.ExecutionEntity)8 Condition (org.activiti.engine.impl.Condition)6 Date (java.util.Date)5 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)5 BusinessCalendar (org.activiti.engine.impl.calendar.BusinessCalendar)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ProcessEngineConfigurationImpl (org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl)4 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ArrayList (java.util.ArrayList)3 VariableScope (org.activiti.engine.delegate.VariableScope)3 CycleBusinessCalendar (org.activiti.engine.impl.calendar.CycleBusinessCalendar)3 DueDateBusinessCalendar (org.activiti.engine.impl.calendar.DueDateBusinessCalendar)3