//if(getRole().getRoleResolutionContext()==null)
throw e;
}
WorkList worklist = instance.getWorkList();
//TODO: change not to read again by the createParameter() if you don't want to cache processinstance
Calendar dueDate = getDueDate(instance);
if(dueDate==null)
setDueDate(instance, instance.calculateDueDate(GlobalContext.getNow(instance.getProcessTransactionContext()), getDuration()), false);
Map kpv = createParameter(instance);
if(defaultStatus!=null){
kpv.put(KeyedParameter.DEFAULT_STATUS, defaultStatus);
}
if(roleMapping!=null){
int dispatchingOption = roleMapping.getDispatchingOption();
if(dispatchingOption == Role.DISPATCHINGOPTION_AUTO)
dispatchingOption = (roleMapping.size() > 1 ? Role.DISPATCHINGOPTION_RACING : Role.DISPATCHINGOPTION_ALL);
kpv.put(KeyedParameter.DISPATCHINGOPTION, ""+dispatchingOption);
if(
roleMapping.getResourceName().equals(roleMapping.getEndpoint())
|| !UEngineUtil.isNotEmpty(roleMapping.getResourceName())
)
roleMapping.fill(instance);
kpv.put("resourceName", roleMapping.getResourceName());
String[] params = roleMapping.getDispatchingParameters();
if(params!=null && params.length > 0){
for(int i=0; i<params.length; i++){
if(params[i]!=null){
kpv.put("dispatchParam" + (i+1), params[i]);
}
}
}
}else
kpv.put(KeyedParameter.DISPATCHINGOPTION, ""+getRole().getDispatchingOption());
if(getRole()!=null)
kpv.put("roleName", getRole().getName());
if(getReferenceRole()!=null){
kpv.put("referenceRoleName", getReferenceRole().getName());
RoleMapping referenceRoleMapping = getReferenceRole().getMapping(instance);
if(referenceRoleMapping!=null/* && instance.getRoleMapping(getReferenceRole().getName())==null*/){
instance.putRoleMapping(referenceRoleMapping);
}
}
if(getRole().getRoleResolutionContext()!=null){
String[] params = getRole().getRoleResolutionContext().getDispatchingParameters();
if(params!=null && params.length > 0 && params[0]!=null)
kpv.put("dispatchParam1", params[0]);
}
KeyedParameter[] parameters = new KeyedParameter[kpv.size()];
int i=0;
for(Iterator iter = kpv.keySet().iterator(); iter.hasNext(); ){
String key = (String)iter.next();
parameters[i] = new KeyedParameter();
parameters[i].setKey(key);
parameters[i].setValue(kpv.get(key));
i++;
}
String[] taskIds = getTaskIds(instance);
String[] taskIds2 = new String[1];//roleMapping.size()];
i=0;
// Dispatching option to all users is deprecated in 2.0
// do{
String tID = null;
if(taskIds!=null && taskIds.length > i)
tID = taskIds[i];
if(tID!=null && (tID.equals("null") || tID.trim().length()==0)){
tID = null;
}
if(tID==null){
tID = worklist.addWorkItem(roleMapping != null ? roleMapping.getEndpoint() : null, parameters, instance.getProcessTransactionContext());
}else if(Activity.STATUS_SUSPENDED.equals(getStatus(instance))){//if suspended
ResultPayload rp = new ResultPayload();
rp.setExtendedValue(new KeyedParameter(KeyedParameter.DEFAULT_STATUS, DefaultWorkList.WORKITEM_STATUS_CONFIRMED)); //or WORKITEM_STATUS_RESUMED
instance.getWorkList().updateWorkItem(taskIds[0], null, rp.getExtendedValues(), instance.getProcessTransactionContext());
}else //if reserved
worklist.addWorkItem(tID, roleMapping != null ? roleMapping.getEndpoint() : null, parameters, instance.getProcessTransactionContext());
taskIds2[i++] = tID;
// }while(roleMapping.next());
setTaskIds(instance, taskIds2);