long start = System.currentTimeMillis();
if (!config.getEnabled()) {
throw new IllegalArgumentException("Process definition has been disabled!");
}
ProcessInstance newProcessInstance = new ProcessInstance();
newProcessInstance.setDefinition(config);
newProcessInstance.setCreator(creator);
newProcessInstance.addOwner(creator.getLogin());
newProcessInstance.setDefinitionName(config.getBpmDefinitionKey());
newProcessInstance.setCreateDate(new Date());
newProcessInstance.setExternalKey(externalKey);
newProcessInstance.setDescription(description);
newProcessInstance.setKeyword(keyword);
newProcessInstance.setStatus(ProcessStatus.NEW);
{
ProcessInstanceSimpleAttribute attr = new ProcessInstanceSimpleAttribute();
attr.setKey("creator");
attr.setValue(creator.getLogin());
newProcessInstance.addAttribute(attr);
attr = new ProcessInstanceSimpleAttribute();
attr.setKey("creatorName");
attr.setValue(creator.getRealName());
newProcessInstance.addAttribute(attr);
}
ProcessInstanceSimpleAttribute attr = new ProcessInstanceSimpleAttribute();
attr.setKey("source");
attr.setValue(source);
newProcessInstance.addAttribute(attr);
ctx.getProcessInstanceDAO().saveProcessInstance(newProcessInstance);
if(internalId == null)
newProcessInstance = startProcessInstance(config, externalKey, ctx, newProcessInstance);
else
newProcessInstance.setInternalId(internalId);
creator = findOrCreateUser(creator, ctx);
ProcessInstanceLog log = new ProcessInstanceLog();
log.setState(null);
log.setEntryDate(Calendar.getInstance());
log.setEventI18NKey("process.log.process-started");
log.setUser(creator);
log.setLogType(ProcessInstanceLog.LOG_TYPE_START_PROCESS);
log.setOwnProcessInstance(newProcessInstance);
newProcessInstance.getRootProcessInstance().addProcessLog(log);
List<BpmTask> findProcessTasks = findProcessTasks(newProcessInstance, ctx);
String taskName = findProcessTasks.get(0).getTaskName();
newProcessInstance.setState(taskName);
ctx.getProcessInstanceDAO().saveProcessInstance(newProcessInstance);
Collection<IEvent> events = new ArrayList<IEvent>();
events.add(new BpmEvent(Type.NEW_PROCESS, newProcessInstance, creator));