@Override
public String invoke(BpmStep step, Map<String, String> params) throws Exception
{
ProcessInstance processInstance = step.getProcessInstance();
ProcessToolContext ctx = ProcessToolContext.Util.getThreadProcessToolContext();
List<String> taskNames = Strings.hasText(taskName) ? Arrays.asList(taskName.split(",")) : step.getOutgoingTransitions();
Date dueDate = extractDate("dueDate", processInstance, params);
if (dueDate == null) {
Date baseDate = extractDate("baseDate", processInstance, params);
if (!Strings.hasText(value)) {
throw new ProcessToolException("Unable to calculate due date");
}
Calendar cal = Calendar.getInstance();
cal.setTime(baseDate != null ? baseDate : new Date());
if(useWorkingDays())
{
int nv = Integer.valueOf(Integer.valueOf(value));
nv = getWeekOffset(cal.getTime(),nv);
cal.add(Calendar.DAY_OF_YEAR, nv);
dueDate = cal.getTime();
}
else
{
cal.add("min".equals(unit) ? Calendar.MINUTE : Calendar.DAY_OF_YEAR, Integer.valueOf(value));
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.SECOND, 0);
if (!"min".equals(unit)) {
cal.set(Calendar.MINUTE, 0);
}
dueDate = cal.getTime();
}
}
for (String tn : taskNames)
{
String attrKey = "deadline_" + tn;
ProcessDeadline pid = null;
for (ProcessInstanceAttribute attr : processInstance.getProcessAttributes()) {
if (attr.getKey() == null) {
logger.info("Attribute key is null! Process instance: " + processInstance.getInternalId());
}
if (attrKey.equals(attr.getKey())) {
pid = (ProcessDeadline) attr;
break;
}
}
if (pid == null) {
pid = new ProcessDeadline();
pid.setKey("deadline_" + tn);
pid.setProcessInstance(processInstance);
processInstance.getProcessAttributes().add(pid);
}
pid.setProfileName(profileName);
pid.setNotifyUsersWithLogin(notifyUsersWithLogin);
pid.setNotifyUsersWithRole(notifyUsersWithRole);
pid.setSkipAssignee("true".equalsIgnoreCase(skipAssignee));
pid.setTemplateName(templateName);
pid.setTaskName(tn);
pid.setAlreadyNotified(false);
pid.setDueDate(dueDate);
}
ctx.getProcessInstanceDAO().saveProcessInstance(processInstance);
return STATUS_OK;
}