Activiti流程图查看实例
本文实例展示了activiti流程图查看的实现方法,具体步骤如下所示:
1、测试用例查看图片代码如下:
public void viewimage() throws exception { // 创建仓库服务对对象 repositoryservice repositoryservice = processengine.getrepositoryservice(); // 从仓库中找需要展示的文件 string deploymentid = "701"; list<string> names = repositoryservice.getdeploymentresourcenames(deploymentid); string imagename = null; for (string name : names) { if(name.indexof(".png")>=0){ imagename = name; } } if(imagename!=null){ // system.out.println(imagename); file f = new file("e:/"+ imagename); // 通过部署id和文件名称得到文件的输入流 inputstream in = repositoryservice.getresourceasstream(deploymentid, imagename); fileutils.copyinputstreamtofile(in, f); }
说明:
1) deploymentid为流程部署id
2) resourcename为act_ge_bytearray表中name_列的值
3) 使用repositoryservice的getdeploymentresourcenames方法可以获取指定部署下得所有文件的名称
4) 使用repositoryservice的getresourceasstream方法传入部署id和文件名称可以获取部署下指定名称文件的输入流
5) 最后的有关io流的操作,使用fileutils工具的copyinputstreamtofile方法完成流程流程到文件的拷贝
2、web项目中在流程定义页面查看图片:
public string viewimage(){ inputstream in = repositoryservice.getresourceasstream.getimagestream(deploymentid,imagename);//此处方法实际项目应该放在service里面 httpservletresponse resp = servletactioncontext.getresponse(); try { outputstream out = resp.getoutputstream(); // 把图片的输入流程写入resp的输出流中 byte[] b = new byte[1024]; for (int len = -1; (len= in.read(b))!=-1; ) { out.write(b, 0, len); } // 关闭流 out.close(); in.close(); } catch (ioexception e) { e.printstacktrace(); } return null; }
说明:
1) deploymentid为流程部署id,imagename为图片名称
2) 因为是从流程定义列表页面查看图片,id和imagename可以从流程定义(processdefinition)中获取(string getdeploymentid();和 string getdiagramresourcename();)
3) web页面标签<a target="_blank" href="viewimage?deploymentid=1&imagename=imagename.png" rel="external nofollow" >查看流程图</a>
3、web项目查看当前流程图
public string viewcurrentimage(){ processdefinition pd = service.getprocessdefinitionbytaskid(taskid); // 1. 获取流程部署id putcontext("deploymentid", pd.getdeploymentid()); // 2. 获取流程图片的名称 putcontext("imagename", pd.getdiagramresourcename()); // 3.获取当前活动的坐标 map<string,object> currentactivitycoordinates =service.getcurrentactivitycoordinates(taskid); putcontext("acs", currentactivitycoordinates); return "image"; }
其中service.getprocessdefinitionbytaskid(taskid);的代码实现:
public processdefinition getprocessdefinitionbytaskid(string taskid) { // 1. 得到task task task = taskservice.createtaskquery().taskid(taskid).singleresult(); // 2. 通过task对象的pdid获取流程定义对象 processdefinition pd = repositoryservice.getprocessdefinition(task.getprocessdefinitionid()); return pd; }
其中service.getcurrentactivitycoordinates(taskid);的代码实现:
public map<string, object> getcurrentactivitycoordinates(string taskid) { map<string, object> coordinates = new hashmap<string, object>(); // 1. 获取到当前活动的id task task = taskservice.createtaskquery().taskid(taskid).singleresult(); processinstance pi = runtimeservice.createprocessinstancequery().processinstanceid(task.getprocessinstanceid()).singleresult(); string currentactivitiid = pi.getactivityid(); // 2. 获取到流程定义 processdefinitionentity pd = (processdefinitionentity) repositoryservice.getprocessdefinition(task.getprocessdefinitionid()); // 3. 使用流程定义通过currentactivitiid得到活动对象 activityimpl activity = pd.findactivity(currentactivitiid); // 4. 获取活动的坐标 coordinates.put("x", activity.getx()); coordinates.put("y", activity.gety()); coordinates.put("width", activity.getwidth()); coordinates.put("height", activity.getheight()); return coordinates; }
image页面部分:
从个人任务列表页面点击<a target="_blank" href="/viewcurrentimage?taskid=1" rel="external nofollow" >查看当前流程图</a>跳转到下面页面:
<body> <!-- 1.获取到规则流程图 这里是用的strust2的标签得到上面上面放入值栈的值--> <img style="position: absolute;top: 0px;left: 0px;" src="viewimage?deploymentid=<s:property value='#deploymentid'/>&imagename=<s:property value='#imagename'/>"> <!-- 2.根据当前活动的坐标,动态绘制div --> <div style="position: absolute;border:1px solid red;top:<s:property value='#acs.y'/>px;left: <s:property value='#acs.x'/>px;width: <s:property value='#acs.width'/>px;height:<s:property value='#acs.height'/>px; "></div> </body>