// assign an id to the task instance
Services.assignId(taskInstance);
if (executionContext != null)
{
Token token = executionContext.getToken();
taskInstance.setToken(token);
taskInstance.setProcessInstance(token.getProcessInstance());
taskInstance.initializeVariables();
if (task != null && task.getDueDate() != null)
{
Date baseDate;
String dueDateString = task.getDueDate();
String durationString = null;
if (dueDateString.startsWith("#"))
{
String baseDateEL = dueDateString.substring(0, dueDateString.indexOf("}") + 1);
Object result = JbpmExpressionEvaluator.evaluate(baseDateEL, executionContext);
if (result instanceof Date)
{
baseDate = (Date)result;
}
else if (result instanceof Calendar)
{
baseDate = ((Calendar)result).getTime();
}
else
{
throw new JbpmException("Invalid basedate type: " + baseDateEL + " is of type " + result.getClass().getName()
+ ". Only Date and Calendar are supported");
}
int endOfELIndex = dueDateString.indexOf("}");
if (endOfELIndex < (dueDateString.length() - 1))
{
char durationSeparator = dueDateString.substring(endOfELIndex + 1).trim().charAt(0);
if (durationSeparator != '+' && durationSeparator != '-')
{
throw new JbpmException("Invalid duedate, + or - missing after EL");
}
durationString = dueDateString.substring(endOfELIndex + 1).trim();
}
}
else
{
baseDate = Clock.getCurrentTime();
durationString = dueDateString;
}
Date dueDate;
if (durationString == null || durationString.length() == 0)
{
dueDate = baseDate;
}
else
{
BusinessCalendar businessCalendar = new BusinessCalendar();
dueDate = businessCalendar.add(baseDate, new Duration(durationString));
}
taskInstance.setDueDate(dueDate);
}
try
{
// update the executionContext
executionContext.setTask(task);
executionContext.setTaskInstance(taskInstance);
executionContext.setEventSource(task);
// evaluate the description
if (task != null)
{
String description = task.getDescription();
if ((description != null) && (description.indexOf("#{") != -1))
{
Object result = JbpmExpressionEvaluator.evaluate(description, executionContext);
if (result != null)
{
taskInstance.setDescription(result.toString());
}
}
}
// create the task instance
taskInstance.create(executionContext);
// if this task instance is created for a task, perform assignment
if (task != null)
{
taskInstance.assign(executionContext);
}
}
finally
{
// clean the executionContext
executionContext.setTask(null);
executionContext.setTaskInstance(null);
executionContext.setEventSource(null);
}
// log this creation
// WARNING: The events create and assign are fired in the right order, but
// the logs are still not ordered properly.
token.addLog(new TaskCreateLog(taskInstance, taskInstance.getActorId()));
}
else
{
taskInstance.create();