taskinstance.java
来自「Fire-Workflow-Engine-All-In-One-20090208」· Java 代码 · 共 599 行 · 第 1/2 页
JAVA
599 行
this.fireTaskInstanceEvent(e);
//第二步,尝试结束对应的activityInstance
List<IToken> tokens = persistenceService.findTokensForProcessInstance(this.getProcessInstanceId(), this.getActivityId());
// System.out.println("Inside TaskInstance.complete(targetActivityInstance):: tokens.size is "+tokens.size());
if (tokens == null || tokens.size() == 0) {
return;//表明activityInstance已经结束了。
}
if (tokens.size() > 1) {
throw new EngineException("与activityId=" + this.getActivityId() + "对应的token数量(=" + tokens.size() + ")不正确,正确值能为1,因此无法完成complete操作");
}
IToken token = tokens.get(0);
if (token.isAlive() == false) {
throw new EngineException("与activityId=" + this.getActivityId() + "对应的token.alive=false,因此无法完成complete操作");
}
// System.out.println("Inside TaskInstance :: this.workflowSession is " + (workflowSession == null ? 0 : workflowSession.hashCode()));
INetInstance netInstance = rtCtx.getKenelManager().getNetInstance(this.getProcessId(), this.getVersion());
Object obj = netInstance.getWFElementInstance(this.getActivityId());
if (obj == null) {
throw new EngineException("系统没有找到与activityId=" + this.getActivityId() + "对应activityInstance,无法执行complete操作。");
}
List<ITaskInstance> taskInstanceList = persistenceService.findTaskInstancesForProcessInstance(this.getProcessInstanceId(), this.getActivityId());
boolean activityInstanceCanBeCompleted = true;
WorkflowDefinition workflowDef = rtCtx.getDefinitionService().getWorkflowDefinitionByProcessIdAndVersion(this.getProcessId(), this.getVersion());
WorkflowProcess workflowProcess = null;
workflowProcess = workflowDef.getWorkflowProcess();
Activity activity = (Activity) workflowProcess.findWFElementById(this.getActivityId());
if (activity.getCompletionStrategy().equals(Activity.ALL)) {
for (int i = 0; taskInstanceList != null && i < taskInstanceList.size(); i++) {
ITaskInstance taskInst = taskInstanceList.get(i);
if (taskInst.getState().intValue() == ITaskInstance.INITIALIZED || taskInst.getState().intValue() == ITaskInstance.STARTED) {
activityInstanceCanBeCompleted = false;
}
}
}
if (!activityInstanceCanBeCompleted) {
return;
}
token.setProcessInstance(this.getProcessInstance());
// System.out.println("Inside TaskInstance.complete:: token is " + token.hashCode());
// System.out.println("Inside TaskInstance:: token.getProcessInstance is " + token.getProcessInstance().hashCode());
// System.out.println("Inside TaskInstance :: this.workflowSession is " + (workflowSession == null ? 0 : workflowSession.hashCode()));
// System.out.println("Inside TaskInstance.complete(),before call activityInstance.complete:: token is " + token.hashCode());
// System.out.println("Inside TaskInstance.complete(),before call activityInstance.complete:: token.getProcessInstance is " + token.getProcessInstance().hashCode());
// System.out.println("Inside TaskInstance.complete(),before call activityInstance.complete:: workflowSession is " + (workflowSession == null ? 0 : workflowSession.hashCode()));
((IActivityInstance) obj).complete(token, targetActivityInstance);
}
public void start() throws EngineException, KenelException {
//触发事件
TaskInstanceEvent e = new TaskInstanceEvent();
e.setEventType(TaskInstanceEvent.BEFORE_TASK_INSTANCE_START);
this.fireTaskInstanceEvent(e);
if (Task.FORM.equals(this.getTaskType())) {
startManulTask();
} else if (Task.TOOL.equals(this.getTaskType())) {
startToolTask();
} else if (Task.SUBFLOW.equals(this.getTaskType())) {
startSubflowTask();
} else {
throw new EngineException("无法识别的TaskType值,taskType=" + this.getTaskType());
}
}
protected void startManulTask() throws EngineException {
if (this.getState().intValue() == ITaskInstance.INITIALIZED) {
IPersistenceService persistenceService = rtCtx.getPersistenceService();
this.setState(ITaskInstance.STARTED);
this.setStartedTime(rtCtx.getCalendarService().getSysDate());
persistenceService.saveOrUpdateTaskInstance(this);
}
}
protected void startToolTask() throws EngineException, KenelException {
Task task = this.getTask();
if (task == null) {
throw new EngineException("The Task is null,can NOT start the taskinstance,");
}
if (task.getApplication() == null || task.getApplication().getHandler() == null) {
throw new EngineException("The task.getApplication() is null or task.getApplication().getHandler() is null,can NOT start the taskinstance,");
}
Object obj = rtCtx.getBeanByClassName(task.getApplication().getHandler());
try {
((IApplicationHandler) obj).execute(this);
} catch (Exception e) {
//TODO, 对tool类型的task抛出的错误应该怎么处理?
}
this.complete();
}
public Task getTask() throws EngineException {
WorkflowDefinition workflowDef = rtCtx.getDefinitionService().getWorkflowDefinitionByProcessIdAndVersion(this.getProcessId(), this.getVersion());
if (workflowDef == null) {
return null;
}
try {
return (Task) workflowDef.getWorkflowProcess().findWFElementById(this.getTaskId());
} catch (EngineException ex) {
ex.printStackTrace();
return null;
}
}
public Activity getActivity() throws EngineException {
WorkflowDefinition workflowDef = rtCtx.getDefinitionService().getWorkflowDefinitionByProcessIdAndVersion(this.getProcessId(), this.getVersion());
if (workflowDef == null) {
return null;
}
try {
return (Activity) workflowDef.getWorkflowProcess().findWFElementById(this.getActivityId());
} catch (EngineException ex) {
ex.printStackTrace();
return null;
}
}
public WorkflowProcess getWorkflowProcess() throws EngineException {
WorkflowDefinition workflowDef = rtCtx.getDefinitionService().getWorkflowDefinitionByProcessIdAndVersion(this.getProcessId(), this.getVersion());
if (workflowDef == null) {
return null;
}
try {
return workflowDef.getWorkflowProcess();
} catch (EngineException ex) {
Logger.getLogger(TaskInstance.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
protected IProcessInstance startSubflowTask() throws EngineException, KenelException {
IPersistenceService persistenceService = rtCtx.getPersistenceService();
this.setState(ITaskInstance.STARTED);
this.setStartedTime(rtCtx.getCalendarService().getSysDate());
persistenceService.saveOrUpdateTaskInstance(this);
Task task = this.getTask();
SubWorkflowProcess subWorkflowProcess = task.getSubWorkflowProcess();
WorkflowDefinition workflowDef = rtCtx.getDefinitionService().getTheLatestVersionOfWorkflowDefinition(subWorkflowProcess.getWorkflowProcessId());
WorkflowProcess wfProcess = workflowDef.getWorkflowProcess();
if (wfProcess == null) {
throw new EngineException("系统中没有Id为" + subWorkflowProcess.getWorkflowProcessId() + "的流程定义");
}
IProcessInstance processInstance = this.workflowSession.createProcessInstance(wfProcess.getName());
// ProcessInstance processInstance = new ProcessInstance();
// processInstance.setProcessId(wfProcess.getId());
// processInstance.setVersion(workflowDef.getVersion());
// processInstance.setDisplayName(wfProcess.getDisplayName());
// processInstance.setName(wfProcess.getName());
// processInstance.setState(IProcessInstance.INITIALIZED);
//初始化流程变量,从父实例获得初始值
Map processVars = this.getProcessInstance().getProcessInstanceVariables();
List datafields = wfProcess.getDataFields();
for (int i = 0; datafields != null && i < datafields.size(); i++) {
DataField df = (DataField) datafields.get(i);
if (df.getDataType().equals(DataField.STRING)) {
if (processVars.get(df.getName()) != null && (processVars.get(df.getName()) instanceof String)) {
processInstance.setProcessInstanceVariable(df.getName(), processVars.get(df.getName()));
} else if (df.getInitialValue() != null) {
processInstance.setProcessInstanceVariable(df.getName(), df.getInitialValue());
} else {
processInstance.setProcessInstanceVariable(df.getName(), "");
}
} else if (df.getDataType().equals(DataField.INTEGER)) {
if (processVars.get(df.getName()) != null && (processVars.get(df.getName()) instanceof Integer)) {
processInstance.setProcessInstanceVariable(df.getName(), processVars.get(df.getName()));
} else if (df.getInitialValue() != null) {
try {
Integer intValue = new Integer(df.getInitialValue());
processInstance.setProcessInstanceVariable(df.getName(), intValue);
} catch (Exception e) {
}
} else {
processInstance.setProcessInstanceVariable(df.getName(), new Integer(0));
}
} else if (df.getDataType().equals(DataField.FLOAT)) {
if (processVars.get(df.getName()) != null && (processVars.get(df.getName()) instanceof Float)) {
processInstance.setProcessInstanceVariable(df.getName(), processVars.get(df.getName()));
} else if (df.getInitialValue() != null) {
Float floatValue = new Float(df.getInitialValue());
processInstance.setProcessInstanceVariable(df.getName(), floatValue);
} else {
processInstance.setProcessInstanceVariable(df.getName(), new Float(0));
}
} else if (df.getDataType().equals(DataField.BOOLEAN)) {
if (processVars.get(df.getName()) != null && (processVars.get(df.getName()) instanceof Boolean)) {
processInstance.setProcessInstanceVariable(df.getName(), processVars.get(df.getName()));
} else if (df.getInitialValue() != null) {
Boolean booleanValue = new Boolean(df.getInitialValue());
processInstance.setProcessInstanceVariable(df.getName(), booleanValue);
} else {
processInstance.setProcessInstanceVariable(df.getName(), Boolean.FALSE);
}
} else if (df.getDataType().equals(DataField.DATETIME)) {
//TODO 需要完善一下
}
}
((ProcessInstance)processInstance).setParentProcessInstanceId(this.getProcessInstanceId());
((ProcessInstance)processInstance).setParentTaskInstanceId(this.getId());
processInstance.run();
rtCtx.getPersistenceService().saveOrUpdateProcessInstance(processInstance);
return processInstance;
}
/**
* 触发task instance相关的事件
* @param e
* @throws org.fireflow.engine.EngineException
*/
protected void fireTaskInstanceEvent(TaskInstanceEvent e) throws EngineException {
Task task = this.getTask();
if (task == null) {
return;
}
List listeners = task.getEventListeners();
for (int i = 0; i < listeners.size(); i++) {
EventListener listener = (EventListener) listeners.get(i);
Object obj = rtCtx.getBeanByClassName(listener.getClassName());
if (obj != null && (obj instanceof ITaskInstanceEventListener)) {
((ITaskInstanceEventListener) obj).onTaskInstanceFired(e);
}
}
}
public void abort() throws EngineException, KenelException {
}
public void asignToActor(String id, boolean needSign) throws EngineException, KenelException {
IWorkItem wi = createWorkItem(id);
if (!needSign) {
wi.sign();
}
}
public IWorkflowSession getCurrentWorkflowSession() {
return this.workflowSession;
}
public void setCurrentWorkflowSession(IWorkflowSession session) {
this.workflowSession = session;
}
public String getProcessInstanceId() {
return this.processInstanceId;
}
public void setProcessInstanceId(String s) {
this.processInstanceId = s;
}
public String getProcessId() {
return this.processId;
}
public void setProcessId(String s) {
this.processId = s;
}
public Integer getVersion() {
return this.version;
}
public void setVersion(Integer v) {
this.version = v;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?