最近工作上一直在用activiti作工作流,發現工作流程使用起來真的挺費勁的!
這不,業務需求有來有,使用者希望在回複溝通時,能觸發回複溝通事件,而使用者通過該事件觸發他的業務事件。
回複溝通是這邊流程的自定義的操作, activti沒這種操作,怎麼辦?看來隻能動手自己擴充了
1.Web端
要擴充,首先要在頁面上要有地方配置,因為這個跟任務綁定在一起的,放在任務監聽器中
找到頁面task-listeners-popup.html(這邊用的是activiti-explorer),發現很簡單,隻需要在下拉裡加入配置即可
<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中擴充了自己的操作
顯示結果如下

clipboard.png
OK,前端改造完成,使用者可以選擇了.
2.後端代碼
跟一下代碼,發現對象TaskEntity有釋出事件的方法fireEvent,那急急的加入以下代碼
TaskEntity task = (TaskEntity) taskService.createTaskQuery().taskId(taskId).singleResult();
//釋出操作事件
task.fireEvent("drafter_submit");
成功了嗎?測試一下,Oh,Shit,報錯了!
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)
經分析,是裡面的Context沒有,怎麼辦呢??考慮了一下,指令模式有這個東東呀,那我直接用指令模式來實作試試.
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);
// 釋出操作事件
task.fireEvent(operationCode);
return true;
}
public OperationCommand(String taskId, Map<String, Object> formData, String operationCode) {
super(taskId);
this.formData = formData;
this.operationCode = operationCode;
}
}
原來的執行的代碼改為
((RuntimeServiceImpl)runtimeService).getCommandExecutor().execute(new OperationCommand(taskId,formData,“specCode”));
經測試,意外驚喜,成功了!
至此,流程任務自定義事件擴充成功!