论坛首页 Java企业应用论坛

JBPM4得到所有已经完成节点的主数据类以及数据记录信息

浏览 2489 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-02-03  

找了好久的得到流程中所有已完成ActivityInstance信息的实现方式,结果没找到相关的思路,就试着自己实现了下,发现有点复杂,一个下午才搞定。。。诶,啥时候才能达到如丝袜般顺滑的写代码。。。

为了完成得到流程中所有已经完成的ActivityInstance的信息,主要思路为先寻找所有父流程的所有ActivityInstance的信息,再寻找兄弟节点的ActivityInstance的信息,最后寻找所有子流程的ActivityInstance的信息。

 

/**
* <p>Description: 当前节点外键与已经执行完毕的节点自动关联.</p>
* <p>Author: PanDuanDuan</p>
* @param relationColumns 所有外键
* @param piId 当前流程Id
* @param processInstanceService 流程实例操作Service
* @param processEngine 流程引擎
* @param formReader 流程表单控制
* <p>Date: 2012-2-3</p>
*/
public void autoMakeContent(List<Column> relationColumns, String executionId,
ProcessInstanceService processInstanceService,
ProcessEngine processEngine, XmlFormReader formReader) {
Map<String,String> result = new HashMap<String,String>();
//向上寻找 找到所有父类的节点 信息
result = getParentTableIdAndDataId(executionId,processInstanceService,processEngine,result);
//得到流程历史Service
HistoryService historyService = processEngine.getHistoryService();
//得到Excution的Service
ExecutionService executionService = processEngine.getExecutionService();
//得到已经完成的所有节点
List<HistoryActivityInstance> historyActivityInstances = historyService.createHistoryActivityInstanceQuery().executionId(executionId).list();
//得到当前的Execution
Execution execution = executionService.findExecutionById(executionId);
//递归寻找父Execution直至主流程
Execution parentExecution = execution.getParent();
String tempExecutionId = executionId;
while(null != parentExecution)
{
//找到当前节点的兄弟节点
Collection<? extends Execution> sons = parentExecution.getExecutions();
if(sons.size() > 1)
{
for(Execution son : sons)
{
String sonExecutionId = son.getId();
if(!tempExecutionId.equals(sonExecutionId))
{
historyActivityInstances.addAll(historyService.createHistoryActivityInstanceQuery().executionId(sonExecutionId).list());
}
}
}
historyActivityInstances.addAll(historyService.createHistoryActivityInstanceQuery().executionId(parentExecution.getId()).list());
tempExecutionId = parentExecution.getId();
parentExecution = parentExecution.getParent();
}

HistoryActivityInstanceImpl haiImpl = null;
//遍历所有节点 找出tableId 以及 dataId
for(HistoryActivityInstance historyActivityInstance : historyActivityInstances)
{
haiImpl = (HistoryActivityInstanceImpl)historyActivityInstance;
String type = haiImpl.getType();
if("task".equals(type))
{
String taskName = haiImpl.getActivityName();
XmlForm xmlForm = formReader.getFormByName(taskName);
String tableId = xmlForm.getTableid();
ProcessInstance proIns = (ProcessInstance) executionService.findExecutionById(haiImpl.getExecutionId()).getProcessInstance();
String dataId = CommonTools.Obj2String(executionService.getVariable(proIns.getId(), tableId));
if(!result.containsKey(tableId) && !"".equals(dataId))
{
result.put(tableId, dataId);
}
}else if("sub-process".equals(type))
{
String subName = haiImpl.getActivityName();
String acExecutionId = haiImpl.getExecutionId();
ProcessInstance proIns = (ProcessInstance) executionService.findExecutionById(acExecutionId).getProcessInstance();
String subPiId = processInstanceService.getSubPiId(proIns.getId(), subName);
//向下寻找 找到所有子流程的节点信息
result = getSubTableIdAndDataId(subPiId,processInstanceService,processEngine,result);
}
}

}

/**
* <p>Description: 得到所有子流程的tableId 以及 dataId.</p>
* <p>Author: PanDuanDuan</p>
* @param subPiId
* @param processInstanceService
* @param processEngine
* @param result
* @return
* <p>Date: 2012-2-3</p>
*/
private Map<String, String> getSubTableIdAndDataId(String subPiId,
ProcessInstanceService processInstanceService,
ProcessEngine processEngine, Map<String, String> result) {
try{
//得到流程历史Service
HistoryService historyService = processEngine.getHistoryService();
//得到executionService
ExecutionService executionService = processEngine.getExecutionService();
//得到流程定义ID
String pdId = executionService.findProcessInstanceById(subPiId).getProcessDefinitionId();
//得到FormReader
RepositoryService repositoryService = processEngine.getRepositoryService();
// 得到流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(pdId)
.uniqueResult();
String deploymentId = repositoryService.createProcessDefinitionQuery().processDefinitionId(pdId).list().get(0)
.getDeploymentId();
// 通过图片名获得form.xml文件名
String imageName = processDefinition.getImageResourceName();
String fileName = imageName.replace(".png", "_form.xml");
// 得到文件输入流
InputStream in = repositoryService.getResourceAsStream(deploymentId, fileName);
// 解析xml文档
XmlFormReader formReader = new XmlFormReader(in);
//得到已经完成的所有节点
List<HistoryActivityInstance> historyActivityInstances = historyService.createHistoryActivityInstanceQuery().processInstanceId((subPiId)).list();
HistoryActivityInstanceImpl haiImpl = null;
//遍历所有节点 找出tableId 以及 dataId
for(HistoryActivityInstance historyActivityInstance : historyActivityInstances)
{
haiImpl = (HistoryActivityInstanceImpl)historyActivityInstance;
String type = haiImpl.getType();
if("task".equals(type))
{
String taskName = haiImpl.getActivityName();
XmlForm xmlForm = formReader.getFormByName(taskName);
String tableId = xmlForm.getTableid();
String dataId = CommonTools.Obj2String(executionService.getVariable(haiImpl.getExecutionId(), tableId));
if(!result.containsKey(tableId) && !"".equals(dataId))
{
result.put(tableId, dataId);
}
}else if("sub-process".equals(type))
{
String subName = haiImpl.getActivityName();
String acExecutionId = haiImpl.getExecutionId();
ProcessInstance proIns = (ProcessInstance) executionService.findExecutionById(acExecutionId).getProcessInstance();
String subSubPiId = processInstanceService.getSubPiId(proIns.getId(), subName);
//向下寻找 找到所有子流程的节点信息
result = getSubTableIdAndDataId(subSubPiId,processInstanceService,processEngine,result);
}
}
}catch (Exception e) {
e.printStackTrace();
}
return result;
}

/**
* <p>Description: 得到所有父流程的tableId 以及 dataId.</p>
* <p>Author: PanDuanDuan</p>
* @param executionId
* @param processInstanceService
* @param processEngine
* @param result
* @return
* <p>Date: 2012-2-3</p>
*/
public Map<String,String> getParentTableIdAndDataId(String executionId, ProcessInstanceService processInstanceService, ProcessEngine processEngine, Map<String,String> result)
{
//得到Excution的Service
ExecutionService executionService = processEngine.getExecutionService();
Execution execution = executionService.findExecutionById(executionId);
//递归寻找父Execution直至主流程
Execution parentExecution = execution.getParent();
while(null != parentExecution)
{
if(null != parentExecution.getParent())
{
parentExecution = parentExecution.getParent();
}else
break;
}
//主的执行Id 防止分支
String mainExecutionId = null==parentExecution?executionId:parentExecution.getId();
//存放所有父流程
List<ProcessInstance> fatherProcessInstances = new ArrayList<ProcessInstance>();
ProcessInstance fatherProcessInstance = processInstanceService.getParentProcessInstance(mainExecutionId);
//循环查找所有父流程
while(null != fatherProcessInstance)
{
fatherProcessInstances.add(fatherProcessInstance);
fatherProcessInstance = processInstanceService.getParentProcessInstance(fatherProcessInstance.getId());
}
//如果存在父流程
if(fatherProcessInstances.size()>0)
{
for(ProcessInstance processInstance : fatherProcessInstances)
{
result.putAll(getTableIdAndDataIdByProcessInstance(processInstance,processInstanceService,processEngine));
}
}
return result;
}

/**
* <p>Description: 根据流程实例 得到所有的tableId 以及 dataId.</p>
* <p>Author: PanDuanDuan</p>
* @param processInstance
* @param processInstanceService
* @param processEngine
* @return Map<tableId,dataId>
* <p>Date: 2012-2-3</p>
*/
public Map<String,String> getTableIdAndDataIdByProcessInstance(ProcessInstance processInstance, ProcessInstanceService processInstanceService, ProcessEngine processEngine)
{
Map<String,String> result = new HashMap<String,String>();
try{
String pdId = processInstance.getProcessDefinitionId();
//得到流程历史Service
HistoryService historyService = processEngine.getHistoryService();
//得到Excution的Service
ExecutionService executionService = processEngine.getExecutionService();
//得到已经完成的所有节点
List<HistoryActivityInstance> historyActivityInstances = historyService.createHistoryActivityInstanceQuery().processInstanceId(processInstance.getId()).list();
//得到FormReader
RepositoryService repositoryService = processEngine.getRepositoryService();
// 得到流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(pdId)
.uniqueResult();
String deploymentId = repositoryService.createProcessDefinitionQuery().processDefinitionId(pdId).list().get(0)
.getDeploymentId();
// 通过图片名获得form.xml文件名
String imageName = processDefinition.getImageResourceName();
String fileName = imageName.replace(".png", "_form.xml");
// 得到文件输入流
InputStream in = repositoryService.getResourceAsStream(deploymentId, fileName);
// 解析xml文档
XmlFormReader formReader = new XmlFormReader(in);
//遍历所有节点 找出tableId 以及 dataId
HistoryActivityInstanceImpl haiImpl = null;
for(HistoryActivityInstance historyActivityInstance : historyActivityInstances)
{
haiImpl = (HistoryActivityInstanceImpl)historyActivityInstance;
String type = haiImpl.getType();
//只有是任务的节点 才读取tableId 以及 dataId
if("task".equals(type))
{
String taskName = haiImpl.getActivityName();
XmlForm xmlForm = formReader.getFormByName(taskName);
String tableId = xmlForm.getTableid();
ProcessInstance proIns = (ProcessInstance) executionService.findExecutionById(haiImpl.getExecutionId()).getProcessInstance();
String dataId = CommonTools.Obj2String(executionService.getVariable(proIns.getId(), tableId));
if(!result.containsKey(tableId))
{
result.put(tableId, dataId);
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
return result;
}

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics