Search in sources :

Example 1 with BusinessCalendar

use of org.activiti.engine.impl.calendar.BusinessCalendar in project Activiti by Activiti.

the class UserTaskActivityBehavior method execute.

public void execute(ActivityExecution execution) throws Exception {
    TaskEntity task = TaskEntity.createAndInsert(execution);
    task.setExecution(execution);
    Expression activeNameExpression = null;
    Expression activeDescriptionExpression = null;
    Expression activeDueDateExpression = null;
    Expression activePriorityExpression = null;
    Expression activeCategoryExpression = null;
    Expression activeFormKeyExpression = null;
    Expression activeSkipExpression = null;
    Expression activeAssigneeExpression = null;
    Expression activeOwnerExpression = null;
    Set<Expression> activeCandidateUserExpressions = null;
    Set<Expression> activeCandidateGroupExpressions = null;
    if (Context.getProcessEngineConfiguration().isEnableProcessDefinitionInfoCache()) {
        ObjectNode taskElementProperties = Context.getBpmnOverrideElementProperties(userTaskId, execution.getProcessDefinitionId());
        activeNameExpression = getActiveValue(taskDefinition.getNameExpression(), DynamicBpmnConstants.USER_TASK_NAME, taskElementProperties);
        taskDefinition.setNameExpression(activeNameExpression);
        activeDescriptionExpression = getActiveValue(taskDefinition.getDescriptionExpression(), DynamicBpmnConstants.USER_TASK_DESCRIPTION, taskElementProperties);
        taskDefinition.setDescriptionExpression(activeDescriptionExpression);
        activeDueDateExpression = getActiveValue(taskDefinition.getDueDateExpression(), DynamicBpmnConstants.USER_TASK_DUEDATE, taskElementProperties);
        taskDefinition.setDueDateExpression(activeDueDateExpression);
        activePriorityExpression = getActiveValue(taskDefinition.getPriorityExpression(), DynamicBpmnConstants.USER_TASK_PRIORITY, taskElementProperties);
        taskDefinition.setPriorityExpression(activePriorityExpression);
        activeCategoryExpression = getActiveValue(taskDefinition.getCategoryExpression(), DynamicBpmnConstants.USER_TASK_CATEGORY, taskElementProperties);
        taskDefinition.setCategoryExpression(activeCategoryExpression);
        activeFormKeyExpression = getActiveValue(taskDefinition.getFormKeyExpression(), DynamicBpmnConstants.USER_TASK_FORM_KEY, taskElementProperties);
        taskDefinition.setFormKeyExpression(activeFormKeyExpression);
        activeSkipExpression = getActiveValue(taskDefinition.getSkipExpression(), DynamicBpmnConstants.TASK_SKIP_EXPRESSION, taskElementProperties);
        taskDefinition.setSkipExpression(activeSkipExpression);
        activeAssigneeExpression = getActiveValue(taskDefinition.getAssigneeExpression(), DynamicBpmnConstants.USER_TASK_ASSIGNEE, taskElementProperties);
        taskDefinition.setAssigneeExpression(activeAssigneeExpression);
        activeOwnerExpression = getActiveValue(taskDefinition.getOwnerExpression(), DynamicBpmnConstants.USER_TASK_OWNER, taskElementProperties);
        taskDefinition.setOwnerExpression(activeOwnerExpression);
        activeCandidateUserExpressions = getActiveValueSet(taskDefinition.getCandidateUserIdExpressions(), DynamicBpmnConstants.USER_TASK_CANDIDATE_USERS, taskElementProperties);
        taskDefinition.setCandidateUserIdExpressions(activeCandidateUserExpressions);
        activeCandidateGroupExpressions = getActiveValueSet(taskDefinition.getCandidateGroupIdExpressions(), DynamicBpmnConstants.USER_TASK_CANDIDATE_GROUPS, taskElementProperties);
        taskDefinition.setCandidateGroupIdExpressions(activeCandidateGroupExpressions);
    } else {
        activeNameExpression = taskDefinition.getNameExpression();
        activeDescriptionExpression = taskDefinition.getDescriptionExpression();
        activeDueDateExpression = taskDefinition.getDueDateExpression();
        activePriorityExpression = taskDefinition.getPriorityExpression();
        activeCategoryExpression = taskDefinition.getCategoryExpression();
        activeFormKeyExpression = taskDefinition.getFormKeyExpression();
        activeSkipExpression = taskDefinition.getSkipExpression();
        activeAssigneeExpression = taskDefinition.getAssigneeExpression();
        activeOwnerExpression = taskDefinition.getOwnerExpression();
        activeCandidateUserExpressions = taskDefinition.getCandidateUserIdExpressions();
        activeCandidateGroupExpressions = taskDefinition.getCandidateGroupIdExpressions();
    }
    task.setTaskDefinition(taskDefinition);
    if (activeNameExpression != null) {
        String name = null;
        try {
            name = (String) activeNameExpression.getValue(execution);
        } catch (ActivitiException e) {
            name = activeNameExpression.getExpressionText();
            LOGGER.warn("property not found in task name expression " + e.getMessage());
        }
        task.setName(name);
    }
    if (activeDescriptionExpression != null) {
        String description = null;
        try {
            description = (String) activeDescriptionExpression.getValue(execution);
        } catch (ActivitiException e) {
            description = activeDescriptionExpression.getExpressionText();
            LOGGER.warn("property not found in task description expression " + e.getMessage());
        }
        task.setDescription(description);
    }
    if (activeDueDateExpression != null) {
        Object dueDate = activeDueDateExpression.getValue(execution);
        if (dueDate != null) {
            if (dueDate instanceof Date) {
                task.setDueDate((Date) dueDate);
            } else if (dueDate instanceof String) {
                BusinessCalendar businessCalendar = Context.getProcessEngineConfiguration().getBusinessCalendarManager().getBusinessCalendar(taskDefinition.getBusinessCalendarNameExpression().getValue(execution).toString());
                task.setDueDate(businessCalendar.resolveDuedate((String) dueDate));
            } else {
                throw new ActivitiIllegalArgumentException("Due date expression does not resolve to a Date or Date string: " + activeDueDateExpression.getExpressionText());
            }
        }
    }
    if (activePriorityExpression != null) {
        final Object priority = activePriorityExpression.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: " + activePriorityExpression.getExpressionText());
            }
        }
    }
    if (activeCategoryExpression != null) {
        final Object category = activeCategoryExpression.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: " + activeCategoryExpression.getExpressionText());
            }
        }
    }
    if (activeFormKeyExpression != null) {
        final Object formKey = activeFormKeyExpression.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: " + activeFormKeyExpression.getExpressionText());
            }
        }
    }
    boolean skipUserTask = SkipExpressionUtil.isSkipExpressionEnabled(execution, activeSkipExpression) && SkipExpressionUtil.shouldSkipFlowElement(execution, activeSkipExpression);
    if (!skipUserTask) {
        handleAssignments(activeAssigneeExpression, activeOwnerExpression, activeCandidateUserExpressions, activeCandidateGroupExpressions, task, execution);
    }
    task.fireEvent(TaskListener.EVENTNAME_CREATE);
    // All properties set, now firing 'create' events
    if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
        Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_CREATED, task));
    }
    if (skipUserTask) {
        task.complete(null, false);
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) TaskEntity(org.activiti.engine.impl.persistence.entity.TaskEntity) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Date(java.util.Date) 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)

Example 2 with BusinessCalendar

use of org.activiti.engine.impl.calendar.BusinessCalendar in project Activiti by Activiti.

the class TimerEntity method restoreExtraData.

protected void restoreExtraData(CommandContext commandContext, String jobHandlerConfiguration) {
    String embededActivityId = jobHandlerConfiguration;
    if (jobHandlerType.equalsIgnoreCase(TimerExecuteNestedActivityJobHandler.TYPE) || jobHandlerType.equalsIgnoreCase(TimerCatchIntermediateEventJobHandler.TYPE) || jobHandlerType.equalsIgnoreCase(TimerStartEventJobHandler.TYPE)) {
        embededActivityId = TimerEventHandler.getActivityIdFromConfiguration(jobHandlerConfiguration);
        String endDateExpressionString = TimerEventHandler.getEndDateFromConfiguration(jobHandlerConfiguration);
        if (endDateExpressionString != null) {
            Expression endDateExpression = Context.getProcessEngineConfiguration().getExpressionManager().createExpression(endDateExpressionString);
            String endDateString = null;
            BusinessCalendar businessCalendar = Context.getProcessEngineConfiguration().getBusinessCalendarManager().getBusinessCalendar(getBusinessCalendarName(TimerEventHandler.geCalendarNameFromConfiguration(jobHandlerConfiguration)));
            VariableScope executionEntity = null;
            if (executionId != null) {
                executionEntity = commandContext.getExecutionEntityManager().findExecutionById(executionId);
            }
            if (executionEntity == null) {
                executionEntity = NoExecutionVariableScope.getSharedInstance();
            }
            if (endDateExpression != null) {
                Object endDateValue = endDateExpression.getValue(executionEntity);
                if (endDateValue instanceof String) {
                    endDateString = (String) endDateValue;
                } else if (endDateValue instanceof Date) {
                    endDate = (Date) endDateValue;
                } else {
                    throw new ActivitiException("Timer '" + ((ExecutionEntity) 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 (endDate == null) {
                    endDate = businessCalendar.resolveEndDate(endDateString);
                }
            }
        }
    }
    if (processDefinitionId != null) {
        ProcessDefinition definition = commandContext.getProcessEngineConfiguration().getDeploymentManager().findDeployedProcessDefinitionById(processDefinitionId);
        maxIterations = checkStartEventDefinitions(definition, embededActivityId);
        if (maxIterations <= 1) {
            maxIterations = checkBoundaryEventsDefinitions(definition, embededActivityId);
        }
    } else {
        maxIterations = 1;
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Expression(org.activiti.engine.delegate.Expression) CycleBusinessCalendar(org.activiti.engine.impl.calendar.CycleBusinessCalendar) BusinessCalendar(org.activiti.engine.impl.calendar.BusinessCalendar) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) VariableScope(org.activiti.engine.delegate.VariableScope) NoExecutionVariableScope(org.activiti.engine.impl.el.NoExecutionVariableScope) Date(java.util.Date)

Example 3 with BusinessCalendar

use of org.activiti.engine.impl.calendar.BusinessCalendar in project Activiti by Activiti.

the class TimerDeclarationImpl method prepareTimerEntity.

public TimerEntity prepareTimerEntity(ExecutionEntity executionEntity) {
    // 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();
    }
    String calendarNameValue = type.calendarName;
    if (this.calendarNameExpression != null) {
        calendarNameValue = (String) this.calendarNameExpression.getValue(scopeForExpression);
    }
    BusinessCalendar businessCalendar = Context.getProcessEngineConfiguration().getBusinessCalendarManager().getBusinessCalendar(calendarNameValue);
    if (description == null) {
        // Prevent NPE from happening in the next line
        throw new ActivitiIllegalArgumentException("Timer '" + executionEntity.getActivityId() + "' was not configured with a valid duration/time");
    }
    String endDateString = null;
    String dueDateString = null;
    Date duedate = null;
    Date endDate = null;
    if (endDateExpression != null && !(scopeForExpression instanceof NoExecutionVariableScope)) {
        Object endDateValue = endDateExpression.getValue(scopeForExpression);
        if (endDateValue instanceof String) {
            endDateString = (String) endDateValue;
        } else if (endDateValue instanceof Date) {
            endDate = (Date) endDateValue;
        } else if (endDateValue instanceof DateTime) {
            // Joda DateTime support
            duedate = ((DateTime) endDateValue).toDate();
        } else {
            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 (endDate == null) {
            endDate = businessCalendar.resolveEndDate(endDateString);
        }
    }
    Object dueDateValue = description.getValue(scopeForExpression);
    if (dueDateValue instanceof String) {
        dueDateString = (String) dueDateValue;
    } else if (dueDateValue instanceof Date) {
        duedate = (Date) dueDateValue;
    } else if (dueDateValue instanceof DateTime) {
        // Joda DateTime support
        duedate = ((DateTime) dueDateValue).toDate();
    } else if (dueDateValue != null) {
        //dueDateValue==null is OK - but unexpected class type must throw an error.
        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);
    }
    TimerEntity timer = null;
    // if dueDateValue is null -> this is OK - timer will be null and job not scheduled
    if (duedate != null) {
        timer = new TimerEntity(this);
        timer.setDuedate(duedate);
        timer.setEndDate(endDate);
        if (executionEntity != null) {
            timer.setExecution(executionEntity);
            timer.setProcessDefinitionId(executionEntity.getProcessDefinitionId());
            timer.setProcessInstanceId(executionEntity.getProcessInstanceId());
            // Inherit tenant identifier (if applicable)
            if (executionEntity != null && executionEntity.getTenantId() != null) {
                timer.setTenantId(executionEntity.getTenantId());
            }
        }
        if (type == TimerDeclarationType.CYCLE) {
            // 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 (TimerCatchIntermediateEventJobHandler.TYPE.equals(jobHandlerType)) {
                repeat = false;
                if (endDate != null) {
                    long endDateMiliss = endDate.getTime();
                    long dueDateMiliss = duedate.getTime();
                    long dueDate = Math.min(endDateMiliss, dueDateMiliss);
                    timer.setDuedate(new Date(dueDate));
                }
            }
            if (repeat) {
                String prepared = prepareRepeat(dueDateString);
                timer.setRepeat(prepared);
            }
        }
    }
    return timer;
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) TimerEntity(org.activiti.engine.impl.persistence.entity.TimerEntity) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) NoExecutionVariableScope(org.activiti.engine.impl.el.NoExecutionVariableScope) BusinessCalendar(org.activiti.engine.impl.calendar.BusinessCalendar) VariableScope(org.activiti.engine.delegate.VariableScope) NoExecutionVariableScope(org.activiti.engine.impl.el.NoExecutionVariableScope) Date(java.util.Date) DateTime(org.joda.time.DateTime)

Aggregations

Date (java.util.Date)3 ActivitiException (org.activiti.engine.ActivitiException)3 BusinessCalendar (org.activiti.engine.impl.calendar.BusinessCalendar)3 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)2 Expression (org.activiti.engine.delegate.Expression)2 VariableScope (org.activiti.engine.delegate.VariableScope)2 NoExecutionVariableScope (org.activiti.engine.impl.el.NoExecutionVariableScope)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 CycleBusinessCalendar (org.activiti.engine.impl.calendar.CycleBusinessCalendar)1 DueDateBusinessCalendar (org.activiti.engine.impl.calendar.DueDateBusinessCalendar)1 TaskEntity (org.activiti.engine.impl.persistence.entity.TaskEntity)1 TimerEntity (org.activiti.engine.impl.persistence.entity.TimerEntity)1 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)1 DateTime (org.joda.time.DateTime)1