最近工作上一直在用activiti作工作流,發(fā)現(xiàn)工作流程使用起來真的挺費(fèi)勁的!
這不,業(yè)務(wù)需求有來有,用戶希望在回復(fù)溝通時(shí),能觸發(fā)回復(fù)溝通事件,而用戶通過該事件觸發(fā)他的業(yè)務(wù)事件。
回復(fù)溝通是這邊流程的自定義的操作, activti沒這種操作,怎么辦?看來只能動手自己擴(kuò)展了
1.Web端
要擴(kuò)展,首先要在頁面上要有地方配置,因?yàn)檫@個(gè)跟任務(wù)綁定在一起的,放在任務(wù)監(jiān)聽器中
找到頁面task-listeners-popup.html(這邊用的是activiti-explorer),發(fā)現(xiàn)很簡單,只需要在下拉里加入配置即可
<div class="form-group">
<label for="eventField">{{'PROPERTY.TASKLISTENERS.EVENT' | translate}}</label>
<select id="eventField" class="form-control" ng-model="selectedListeners[0].event">
<option>create</option>
<option>assignment</option>
<option>complete</option>
<option>delete</option>
<option>specCode</option>
</select>
</div>
如上表格所示,在select中擴(kuò)展了自己的操作
顯示結(jié)果如下

clipboard.png
OK,前端改造完成,用戶可以選擇了.
2.后端代碼
跟一下代碼,發(fā)現(xiàn)對象TaskEntity有發(fā)布事件的方法fireEvent,那急急的加入以下代碼
TaskEntity task = (TaskEntity) taskService.createTaskQuery().taskId(taskId).singleResult();
//發(fā)布操作事件
task.fireEvent("drafter_submit");
成功了嗎?測試一下,Oh,Shit,報(bào)錯(cuò)了!
java.lang.NullPointerException: null
at org.activiti.engine.impl.persistence.entity.TaskEntity.getTaskDefinition(TaskEntity.java:797)
at org.activiti.engine.impl.persistence.entity.TaskEntity.fireEvent(TaskEntity.java:728)
經(jīng)分析,是里面的Context沒有,怎么辦呢??考慮了一下,命令模式有這個(gè)東東呀,那我直接用命令模式來實(shí)現(xiàn)試試.
import java.util.Map;
import org.activiti.engine.impl.cmd.NeedsActiveTaskCmd;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
public class OperationCommand extends NeedsActiveTaskCmd<Boolean> {
public OperationCommand(String taskId) {
super(taskId);
// TODO Auto-generated constructor stub
}
private Map<String, Object> formData;
private String operationCode;
@Override
protected Boolean execute(CommandContext commandContext, TaskEntity task) {
// TODO Auto-generated method stub
Context.getCommandContext().getProcessEngineConfiguration().getTaskService().complete(task.getId(), formData);
// 發(fā)布操作事件
task.fireEvent(operationCode);
return true;
}
public OperationCommand(String taskId, Map<String, Object> formData, String operationCode) {
super(taskId);
this.formData = formData;
this.operationCode = operationCode;
}
}
原來的執(zhí)行的代碼改為
((RuntimeServiceImpl)runtimeService).getCommandExecutor().execute(new OperationCommand(taskId,formData,“specCode”));
經(jīng)測試,意外驚喜,成功了!
至此,流程任務(wù)自定義事件擴(kuò)展成功!