public UserTaskActivityBehavior(TaskDefinition taskDefinition) {
this.taskDefinition = taskDefinition;
}
public void execute(ActivityExecution execution) throws Exception {
TaskEntity task = TaskEntity.createAndInsert(execution);
task.setExecution(execution);
task.setTaskDefinition(taskDefinition);
if (taskDefinition.getNameExpression() != null) {
String name = (String) taskDefinition.getNameExpression().getValue(execution);
task.setName(name);
}
if (taskDefinition.getDescriptionExpression() != null) {
String description = (String) taskDefinition.getDescriptionExpression().getValue(execution);
task.setDescription(description);
}
if(taskDefinition.getDueDateExpression() != null) {
Object dueDate = taskDefinition.getDueDateExpression().getValue(execution);
if(dueDate != null) {
if (dueDate instanceof Date) {
task.setDueDate((Date) dueDate);
} else if (dueDate instanceof String) {
BusinessCalendar businessCalendar = Context
.getProcessEngineConfiguration()
.getBusinessCalendarManager()
.getBusinessCalendar(DueDateBusinessCalendar.NAME);
task.setDueDate(businessCalendar.resolveDuedate((String) dueDate));
} else {
throw new ActivitiIllegalArgumentException("Due date expression does not resolve to a Date or Date string: " +
taskDefinition.getDueDateExpression().getExpressionText());
}
}
}
if (taskDefinition.getPriorityExpression() != null) {
final Object priority = taskDefinition.getPriorityExpression().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: " +
taskDefinition.getPriorityExpression().getExpressionText());
}
}
}
if (taskDefinition.getCategoryExpression() != null) {
final Object category = taskDefinition.getCategoryExpression().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: " +
taskDefinition.getCategoryExpression().getExpressionText());
}
}
}
if (taskDefinition.getFormKeyExpression() != null) {
final Object formKey = taskDefinition.getFormKeyExpression().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: " +
taskDefinition.getFormKeyExpression().getExpressionText());
}
}
}
handleAssignments(task, execution);
// All properties set, now firing 'create' events
if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_CREATED, task));
}
task.fireEvent(TaskListener.EVENTNAME_CREATE);
}