The example in this article shows the implementation method of Activiti flow chart viewing. The specific steps are as follows:
1. The test case viewing picture code is as follows:
public void viewImage() throws Exception { // Create a warehouse service object RepositoryService repositoryService = processEngine.getRepositoryService(); // Find the files that need to be displayed from the warehouse 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); // Get the input stream of the file through the deployment ID and file name InputStream in = repositoryService.getResourceAsStream(deploymentId, imageName); FileUtils.copyInputStreamToFile(in, f); }
illustrate:
1) deploymentId is the process deployment ID
2) resourceName is the value of the NAME_ column in the act_ge_bytearray table
3) Use the getDeploymentResourceNames method of repositoryService to obtain the names of all files under the specified deployment
4) Use the getResourceAsStream method of repositoryService to pass in the deployment ID and file name to obtain the input stream of the file with the specified name under the deployment.
5) For the final IO stream operation, use the copyInputStreamToFile method of the FileUtils tool to complete the copy of the process flow to the file.
2. View the picture on the process definition page in the web project:
public String viewImage(){InputStream in = repositoryService.getResourceAsStream.getImageStream(deploymentId,imageName);//The actual project of the method here should be placed in the service HttpServletResponse resp = ServletActionContext.getResponse();try { OutputStream out = resp.getOutputStream( ); // Write the image input process into the resp output stream byte[] b = new byte[1024]; for (int len = -1; (len= in.read(b))!=-1; ) { out.write(b, 0, len); } // Close the stream out.close( ); in.close();} catch (IOException e) { e.printStackTrace();}return null;}
illustrate:
1) deploymentId is the process deployment ID, imageName is the image name
2) Because the image is viewed from the process definition list page, the id and imageName can be obtained from the process definition (ProcessDefinition) (String getDeploymentId(); and String getDiagramResourceName();)
3) Web page tag <a target="_blank" href="viewImage?deploymentId=1&imageName=imageName.png" rel="external nofollow" >View flow chart</a>
3. View the current flow chart of the web project
public String viewCurrentImage(){ProcessDefinition pd = service.getProcessDefinitionByTaskId(taskId);// 1. Get the process deployment ID putContext("deploymentId", pd.getDeploymentId());// 2. Get the name of the process image putContext("imageName" , pd.getDiagramResourceName());// 3. Get the coordinates of the current activity Map<String,Object> currentActivityCoordinates =service.getCurrentActivityCoordinates(taskId);putContext("acs", currentActivityCoordinates);return "image";}
The code implementation of service.getProcessDefinitionByTaskId(taskId); is:
public ProcessDefinition getProcessDefinitionByTaskId(String taskId) {// 1. Get taskTask task = taskService.createTaskQuery().taskId(taskId).singleResult();// 2. Get the process definition object ProcessDefinition through the pdid of the task object pd = repositoryService.getProcessDefinition (task.getProcessDefinitionId());return pd;}
The code implementation of service.getCurrentActivityCoordinates(taskId); is:
public Map<String, Object> getCurrentActivityCoordinates(String taskId) {Map<String, Object> coordinates = new HashMap<String, Object>(); // 1. Get the currently active IDTask task = taskService.createTaskQuery().taskId (taskId).singleResult();ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();String currentActivitiId = pi.getActivityId();// 2. Get the process definition ProcessDefinitionEntity pd = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(task.getProcessDefinitionId( ));// 3. Use the process definition to get the activity object ActivityImpl through currentActivitiId activity = pd.findActivity(currentActivitiId);// 4. Get the coordinates of the activity coordinates.put("x", activity.getX()); coordinates.put("y" , activity.getY());coordinates.put("width", activity.getWidth());coordinates.put("height", activity.getHeight());return coordinates;}
image page part:
From the personal task list page, click <a target="_blank" href="/viewCurrentImage?taskId=1" rel="external nofollow" >View current flow chart</a> to jump to the following page:
<body><!-- 1. Obtain the rule flow chart. Here we use the strust2 tag to get the value put into the value stack above--><img style="position: absolute;top: 0px;left: 0px; " src="viewImage?deploymentId=<s:property value='#deploymentId'/>&imageName=<s:property value='#imageName'/>"><!-- 2. Dynamically draw DIV according to the coordinates of the current activity --><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>