1. 程式人生 > >activiti靈活跳轉駁回回退操作動態修改流程

activiti靈活跳轉駁回回退操作動態修改流程

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.TransitionImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
*
* @Title: SpecialTaskService.java
* @Description: 自由跳轉流程的處理類

* @date 2017年2月4日 下午5:34:49
* @version V1.0
*/
@Service("specialTaskService")
public class SpecialTaskService {

@Autowired
private RepositoryService repositoryService;
@Autowired
private TaskService taskService;

private Object clone(final Object value) throws IOException, ClassNotFoundException {
// 位元組陣列輸出流,暫存到記憶體中
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 序列化
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(value);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
// 反序列化
return ois.readObject();
}

/**
* 通過指定目標節點,實現任務的跳轉
* @param taskId 任務ID
* @param destNodeIds 跳至的目標節點ID
* @param vars 流程變數
*/
public void completeTask(String taskId, String[] destNodeIds) {
TaskEntity task = (TaskEntity) taskService.createTaskQuery().taskId(taskId).singleResult();

String curNodeId = task.getTaskDefinitionKey();
String actDefId = task.getProcessDefinitionId();

Map activityMap = prepare(actDefId, curNodeId, destNodeIds);
try {
taskService.complete(taskId);
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
// 恢復
restore(activityMap);
}
}

/**
* 將節點之後的節點刪除然後指向新的節點。
* @param actDefId 流程定義ID
* @param nodeId 流程節點ID
* @param aryDestination 需要跳轉的節點
* @return Map 返回節點和需要恢復節點的集合。
* @throws IOException
* @throws ClassNotFoundException
*/
private Map prepare(String actDefId, String nodeId, String[] aryDestination) {
Map map = new HashMap();

// 修改流程定義
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) repositoryService
.getProcessDefinition(actDefId);

ActivityImpl curAct = processDefinition.findActivity(nodeId);
List outTrans = curAct.getOutgoingTransitions();
try {
List cloneOutTrans = (List) clone(outTrans);
map.put("outTrans", cloneOutTrans);
} catch (Exception ex) {

}

curAct.getOutgoingTransitions().clear();

if (aryDestination != null && aryDestination.length > 0) {
for (String dest : aryDestination) {
// 建立一個連線
ActivityImpl destAct = processDefinition.findActivity(dest);
TransitionImpl transitionImpl = curAct.createOutgoingTransition();
transitionImpl.setDestination(destAct);
}
}

map.put("activity", curAct);

return map;

}

/**
* 將臨時節點清除掉,加回原來的節點。
* @param map
* void
*/
@SuppressWarnings("unchecked")
private void restore(Map map) {
ActivityImpl curAct = (ActivityImpl) map.get("activity");
List outTrans = (List) map.get("outTrans");
curAct.getOutgoingTransitions().clear();
curAct.getOutgoingTransitions().addAll(outTrans);
// for (Iterator it = outTrans.iterator(); it.hasNext();) {
// // 回覆刪除的INCOMING
// PvmTransition transition = it.next();
// PvmActivity activity = transition.getDestination();
// List incomes = (List) map.get("Income_" + activity.getId());
// activity.getIncomingTransitions().clear();
// activity.getIncomingTransitions().addAll(incomes);
// }
}
}