Search in sources :

Example 56 with Expression

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

the class ExpressionResolverTest method resolveExpressionsMap_should_replaceExpressionByValue_when_ObjectNodeContainsAnExpression.

@Test
public void resolveExpressionsMap_should_replaceExpressionByValue_when_ObjectNodeContainsAnExpression() throws IOException {
    // given
    Expression nameExpression = buildExpression("${name}");
    given(expressionEvaluator.evaluate(nameExpression, expressionManager, delegateInterceptor)).willReturn("John");
    Expression placeExpression = buildExpression("${place}");
    given(expressionEvaluator.evaluate(placeExpression, expressionManager, delegateInterceptor)).willReturn(null);
    Expression ageExpression = buildExpression("${age}");
    given(expressionEvaluator.evaluate(ageExpression, expressionManager, delegateInterceptor)).willReturn(30);
    JsonNode node = mapper.readTree("{\"name\":\"${name}\",\"place\":\"${place}\",\"age\":\"${age}\"}");
    // when
    Map<String, Object> result = expressionResolver.resolveExpressionsMap(expressionEvaluator, singletonMap("node", node));
    // then
    assertThat(result).containsEntry("node", map("name", "John", "place", null, "age", 30));
}
Also used : Expression(org.activiti.engine.delegate.Expression) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test)

Example 57 with Expression

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

the class UserTaskActivityBehavior method handleAssignments.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected void handleAssignments(TaskEntityManager taskEntityManager, String assignee, String owner, List<String> candidateUsers, List<String> candidateGroups, TaskEntity task, ExpressionManager expressionManager, DelegateExecution execution) {
    if (StringUtils.isNotEmpty(assignee)) {
        Object assigneeExpressionValue = expressionManager.createExpression(assignee).getValue(execution);
        String assigneeValue = null;
        if (assigneeExpressionValue != null) {
            assigneeValue = assigneeExpressionValue.toString();
        }
        taskEntityManager.changeTaskAssigneeNoEvents(task, assigneeValue);
    }
    if (StringUtils.isNotEmpty(owner)) {
        Object ownerExpressionValue = expressionManager.createExpression(owner).getValue(execution);
        String ownerValue = null;
        if (ownerExpressionValue != null) {
            ownerValue = ownerExpressionValue.toString();
        }
        taskEntityManager.changeTaskOwner(task, ownerValue);
    }
    if (candidateGroups != null && !candidateGroups.isEmpty()) {
        for (String candidateGroup : candidateGroups) {
            Expression groupIdExpr = expressionManager.createExpression(candidateGroup);
            Object value = groupIdExpr.getValue(execution);
            if (value instanceof String) {
                List<String> candidates = extractCandidates((String) value);
                task.addCandidateGroups(candidates);
            } else if (value instanceof Collection) {
                task.addCandidateGroups((Collection) value);
            } else {
                throw new ActivitiIllegalArgumentException("Expression did not resolve to a string or collection of strings");
            }
        }
    }
    if (candidateUsers != null && !candidateUsers.isEmpty()) {
        for (String candidateUser : candidateUsers) {
            Expression userIdExpr = expressionManager.createExpression(candidateUser);
            Object value = userIdExpr.getValue(execution);
            if (value instanceof String) {
                List<String> candidates = extractCandidates((String) value);
                task.addCandidateUsers(candidates);
            } else if (value instanceof Collection) {
                task.addCandidateUsers((Collection) value);
            } else {
                throw new ActivitiException("Expression did not resolve to a string or collection of strings");
            }
        }
    }
    if (userTask.getCustomUserIdentityLinks() != null && !userTask.getCustomUserIdentityLinks().isEmpty()) {
        for (String customUserIdentityLinkType : userTask.getCustomUserIdentityLinks().keySet()) {
            for (String userIdentityLink : userTask.getCustomUserIdentityLinks().get(customUserIdentityLinkType)) {
                Expression idExpression = expressionManager.createExpression(userIdentityLink);
                Object value = idExpression.getValue(execution);
                if (value instanceof String) {
                    List<String> userIds = extractCandidates((String) value);
                    for (String userId : userIds) {
                        task.addUserIdentityLink(userId, customUserIdentityLinkType);
                    }
                } else if (value instanceof Collection) {
                    Iterator userIdSet = ((Collection) value).iterator();
                    while (userIdSet.hasNext()) {
                        task.addUserIdentityLink((String) userIdSet.next(), customUserIdentityLinkType);
                    }
                } else {
                    throw new ActivitiException("Expression did not resolve to a string or collection of strings");
                }
            }
        }
    }
    if (userTask.getCustomGroupIdentityLinks() != null && !userTask.getCustomGroupIdentityLinks().isEmpty()) {
        for (String customGroupIdentityLinkType : userTask.getCustomGroupIdentityLinks().keySet()) {
            for (String groupIdentityLink : userTask.getCustomGroupIdentityLinks().get(customGroupIdentityLinkType)) {
                Expression idExpression = expressionManager.createExpression(groupIdentityLink);
                Object value = idExpression.getValue(execution);
                if (value instanceof String) {
                    List<String> groupIds = extractCandidates((String) value);
                    for (String groupId : groupIds) {
                        task.addGroupIdentityLink(groupId, customGroupIdentityLinkType);
                    }
                } else if (value instanceof Collection) {
                    Iterator groupIdSet = ((Collection) value).iterator();
                    while (groupIdSet.hasNext()) {
                        task.addGroupIdentityLink((String) groupIdSet.next(), customGroupIdentityLinkType);
                    }
                } else {
                    throw new ActivitiException("Expression did not resolve to a string or collection of strings");
                }
            }
        }
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Expression(org.activiti.engine.delegate.Expression) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) Iterator(java.util.Iterator) Collection(java.util.Collection)

Example 58 with Expression

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

the class UserTaskActivityBehavior method execute.

public void execute(DelegateExecution execution) {
    CommandContext commandContext = Context.getCommandContext();
    TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
    TaskEntity task = taskEntityManager.create();
    ExecutionEntity executionEntity = (ExecutionEntity) execution;
    task.setExecution(executionEntity);
    task.setTaskDefinitionKey(userTask.getId());
    task.setBusinessKey(executionEntity.getProcessInstanceBusinessKey());
    String activeTaskName = null;
    String activeTaskDescription = null;
    String activeTaskDueDate = null;
    String activeTaskPriority = null;
    String activeTaskCategory = null;
    String activeTaskFormKey = null;
    String activeTaskSkipExpression = null;
    String activeTaskAssignee = null;
    String activeTaskOwner = null;
    List<String> activeTaskCandidateUsers = null;
    List<String> activeTaskCandidateGroups = null;
    ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
    ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager();
    if (Context.getProcessEngineConfiguration().isEnableProcessDefinitionInfoCache()) {
        ObjectNode taskElementProperties = Context.getBpmnOverrideElementProperties(userTask.getId(), execution.getProcessDefinitionId());
        activeTaskName = getActiveValue(userTask.getName(), DynamicBpmnConstants.USER_TASK_NAME, taskElementProperties);
        activeTaskDescription = getActiveValue(userTask.getDocumentation(), DynamicBpmnConstants.USER_TASK_DESCRIPTION, taskElementProperties);
        activeTaskDueDate = getActiveValue(userTask.getDueDate(), DynamicBpmnConstants.USER_TASK_DUEDATE, taskElementProperties);
        activeTaskPriority = getActiveValue(userTask.getPriority(), DynamicBpmnConstants.USER_TASK_PRIORITY, taskElementProperties);
        activeTaskCategory = getActiveValue(userTask.getCategory(), DynamicBpmnConstants.USER_TASK_CATEGORY, taskElementProperties);
        activeTaskFormKey = getActiveValue(userTask.getFormKey(), DynamicBpmnConstants.USER_TASK_FORM_KEY, taskElementProperties);
        activeTaskSkipExpression = getActiveValue(userTask.getSkipExpression(), DynamicBpmnConstants.TASK_SKIP_EXPRESSION, taskElementProperties);
        activeTaskAssignee = getActiveValue(userTask.getAssignee(), DynamicBpmnConstants.USER_TASK_ASSIGNEE, taskElementProperties);
        activeTaskOwner = getActiveValue(userTask.getOwner(), DynamicBpmnConstants.USER_TASK_OWNER, taskElementProperties);
        activeTaskCandidateUsers = getActiveValueList(userTask.getCandidateUsers(), DynamicBpmnConstants.USER_TASK_CANDIDATE_USERS, taskElementProperties);
        activeTaskCandidateGroups = getActiveValueList(userTask.getCandidateGroups(), DynamicBpmnConstants.USER_TASK_CANDIDATE_GROUPS, taskElementProperties);
    } else {
        activeTaskName = userTask.getName();
        activeTaskDescription = userTask.getDocumentation();
        activeTaskDueDate = userTask.getDueDate();
        activeTaskPriority = userTask.getPriority();
        activeTaskCategory = userTask.getCategory();
        activeTaskFormKey = userTask.getFormKey();
        activeTaskSkipExpression = userTask.getSkipExpression();
        activeTaskAssignee = userTask.getAssignee();
        activeTaskOwner = userTask.getOwner();
        activeTaskCandidateUsers = userTask.getCandidateUsers();
        activeTaskCandidateGroups = userTask.getCandidateGroups();
    }
    if (StringUtils.isNotEmpty(activeTaskName)) {
        String name = null;
        try {
            name = (String) expressionManager.createExpression(activeTaskName).getValue(execution);
        } catch (ActivitiException e) {
            name = activeTaskName;
            LOGGER.warn("property not found in task name expression " + e.getMessage());
        }
        task.setName(name);
    }
    if (StringUtils.isNotEmpty(activeTaskDescription)) {
        String description = null;
        try {
            description = (String) expressionManager.createExpression(activeTaskDescription).getValue(execution);
        } catch (ActivitiException e) {
            description = activeTaskDescription;
            LOGGER.warn("property not found in task description expression " + e.getMessage());
        }
        task.setDescription(description);
    }
    if (StringUtils.isNotEmpty(activeTaskDueDate)) {
        Object dueDate = expressionManager.createExpression(activeTaskDueDate).getValue(execution);
        if (dueDate != null) {
            if (dueDate instanceof Date) {
                task.setDueDate((Date) dueDate);
            } else if (dueDate instanceof String) {
                String businessCalendarName = null;
                if (StringUtils.isNotEmpty(userTask.getBusinessCalendarName())) {
                    businessCalendarName = expressionManager.createExpression(userTask.getBusinessCalendarName()).getValue(execution).toString();
                } else {
                    businessCalendarName = DueDateBusinessCalendar.NAME;
                }
                BusinessCalendar businessCalendar = Context.getProcessEngineConfiguration().getBusinessCalendarManager().getBusinessCalendar(businessCalendarName);
                task.setDueDate(businessCalendar.resolveDuedate((String) dueDate));
            } else {
                throw new ActivitiIllegalArgumentException("Due date expression does not resolve to a Date or Date string: " + activeTaskDueDate);
            }
        }
    }
    if (StringUtils.isNotEmpty(activeTaskPriority)) {
        final Object priority = expressionManager.createExpression(activeTaskPriority).getValue(execution);
        if (priority != null) {
            if (priority instanceof String) {
                try {
                    task.setPriority(Integer.valueOf((String) priority));
                } catch (NumberFormatException e) {
                    throw new ActivitiIllegalArgumentException("Priority does not resolve to a number: " + priority, e);
                }
            } else if (priority instanceof Number) {
                task.setPriority(((Number) priority).intValue());
            } else {
                throw new ActivitiIllegalArgumentException("Priority expression does not resolve to a number: " + activeTaskPriority);
            }
        }
    }
    if (StringUtils.isNotEmpty(activeTaskCategory)) {
        final Object category = expressionManager.createExpression(activeTaskCategory).getValue(execution);
        if (category != null) {
            if (category instanceof String) {
                task.setCategory((String) category);
            } else {
                throw new ActivitiIllegalArgumentException("Category expression does not resolve to a string: " + activeTaskCategory);
            }
        }
    }
    if (StringUtils.isNotEmpty(activeTaskFormKey)) {
        final Object formKey = expressionManager.createExpression(activeTaskFormKey).getValue(execution);
        if (formKey != null) {
            if (formKey instanceof String) {
                task.setFormKey((String) formKey);
            } else {
                throw new ActivitiIllegalArgumentException("FormKey expression does not resolve to a string: " + activeTaskFormKey);
            }
        }
    }
    task.setAppVersion(executionEntity.getProcessInstance().getAppVersion());
    taskEntityManager.insert(task, executionEntity);
    task.setVariablesLocal(calculateInputVariables(execution));
    boolean skipUserTask = false;
    if (StringUtils.isNotEmpty(activeTaskSkipExpression)) {
        Expression skipExpression = expressionManager.createExpression(activeTaskSkipExpression);
        skipUserTask = SkipExpressionUtil.isSkipExpressionEnabled(execution, skipExpression) && SkipExpressionUtil.shouldSkipFlowElement(execution, skipExpression);
    }
    // Handling assignments need to be done after the task is inserted, to have an id
    if (!skipUserTask) {
        handleAssignments(taskEntityManager, activeTaskAssignee, activeTaskOwner, activeTaskCandidateUsers, activeTaskCandidateGroups, task, expressionManager, execution);
    }
    processEngineConfiguration.getListenerNotificationHelper().executeTaskListeners(task, TaskListener.EVENTNAME_CREATE);
    // All properties set, now fire events
    if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
        ActivitiEventDispatcher eventDispatcher = Context.getProcessEngineConfiguration().getEventDispatcher();
        eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_CREATED, task));
        if (task.getAssignee() != null) {
            eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_ASSIGNED, task));
        }
    }
    if (skipUserTask) {
        taskEntityManager.deleteTask(task, null, false, false);
        leave(execution);
    }
}
Also used : ExpressionManager(org.activiti.engine.impl.el.ExpressionManager) ActivitiException(org.activiti.engine.ActivitiException) TaskEntity(org.activiti.engine.impl.persistence.entity.TaskEntity) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Date(java.util.Date) ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) Expression(org.activiti.engine.delegate.Expression) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) DueDateBusinessCalendar(org.activiti.engine.impl.calendar.DueDateBusinessCalendar) BusinessCalendar(org.activiti.engine.impl.calendar.BusinessCalendar) TaskEntityManager(org.activiti.engine.impl.persistence.entity.TaskEntityManager) ProcessEngineConfigurationImpl(org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl) ActivitiEventDispatcher(org.activiti.engine.delegate.event.ActivitiEventDispatcher)

Example 59 with Expression

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

the class WebServiceActivityBehavior method createDataOutputAssociation.

protected AbstractDataAssociation createDataOutputAssociation(DataAssociation dataAssociationElement) {
    if (StringUtils.isNotEmpty(dataAssociationElement.getSourceRef())) {
        return new MessageImplicitDataOutputAssociation(dataAssociationElement.getTargetRef(), dataAssociationElement.getSourceRef());
    } else {
        ExpressionManager expressionManager = Context.getProcessEngineConfiguration().getExpressionManager();
        Expression transformation = expressionManager.createExpression(dataAssociationElement.getTransformation());
        AbstractDataAssociation dataOutputAssociation = new TransformationDataOutputAssociation(null, dataAssociationElement.getTargetRef(), transformation);
        return dataOutputAssociation;
    }
}
Also used : MessageImplicitDataOutputAssociation(org.activiti.engine.impl.bpmn.webservice.MessageImplicitDataOutputAssociation) ExpressionManager(org.activiti.engine.impl.el.ExpressionManager) TransformationDataOutputAssociation(org.activiti.engine.impl.bpmn.data.TransformationDataOutputAssociation) AbstractDataAssociation(org.activiti.engine.impl.bpmn.data.AbstractDataAssociation) Expression(org.activiti.engine.delegate.Expression)

Example 60 with Expression

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

the class WebServiceActivityBehavior method createDataInputAssociation.

protected AbstractDataAssociation createDataInputAssociation(DataAssociation dataAssociationElement) {
    if (dataAssociationElement.getAssignments().isEmpty()) {
        return new MessageImplicitDataInputAssociation(dataAssociationElement.getSourceRef(), dataAssociationElement.getTargetRef());
    } else {
        SimpleDataInputAssociation dataAssociation = new SimpleDataInputAssociation(dataAssociationElement.getSourceRef(), dataAssociationElement.getTargetRef());
        ExpressionManager expressionManager = Context.getProcessEngineConfiguration().getExpressionManager();
        for (org.activiti.bpmn.model.Assignment assignmentElement : dataAssociationElement.getAssignments()) {
            if (StringUtils.isNotEmpty(assignmentElement.getFrom()) && StringUtils.isNotEmpty(assignmentElement.getTo())) {
                Expression from = expressionManager.createExpression(assignmentElement.getFrom());
                Expression to = expressionManager.createExpression(assignmentElement.getTo());
                Assignment assignment = new Assignment(from, to);
                dataAssociation.addAssignment(assignment);
            }
        }
        return dataAssociation;
    }
}
Also used : Assignment(org.activiti.engine.impl.bpmn.data.Assignment) ExpressionManager(org.activiti.engine.impl.el.ExpressionManager) Expression(org.activiti.engine.delegate.Expression) SimpleDataInputAssociation(org.activiti.engine.impl.bpmn.data.SimpleDataInputAssociation) MessageImplicitDataInputAssociation(org.activiti.engine.impl.bpmn.webservice.MessageImplicitDataInputAssociation)

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