b/s下显示图片需对jbpm提供的标签改写,通过taskInstanceId来显示当前节点下的图片,在开始节点下无taskInstanceId所以无法显示图片 。
1 action转到显示图片页面
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.shane.commons.core.web.StrutsAction;
public class WorkflowAction extends StrutsAction {
public ActionForward viewImage(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
long taskInstanceId =Long.parseLong(request.getParameter("id"));
request.setAttribute("taskInstanceId", taskInstanceId);
return mapping.findForward("view_image");
}
}
2 view_image.jsp流程图片显示页面,使用改写后的jbpm标签。
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/commons/taglibs.jsp"%>
<%@taglib uri="/WEB-INF/jbpm.tld" prefix="jbpm"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>查看流程的图片</title>
</head>
<body>
<center>
<TABLE class="tableEdit" border="0" cellspacing="1" cellpadding="0" style="width:580px;">
<TBODY>
<TR>
<!-- 这里是添加、编辑界面的标题 -->
<td align="center" class="tdEditTitle">
</TD>
</TR>
<TR>
<td>
<!-- 主输入域开始 -->
<jbpm:processimage task="${taskInstanceId}"/>
<!-- 主输入域结束 -->
</td>
</TR>
</TBODY>
</TABLE>
<TABLE>
<TR align="center">
<TD colspan="3" bgcolor="#EFF3F7">
<input type="button" class="MyButton"
value="关闭窗口" onclick="window.close()">
</TD>
</TR>
</TABLE>
</center>
</body>
</html>
3 改写的标签类继承spring的RequestContextAwareTag类,支持 servlet方式。
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.webapp.tag;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jbpm.graph.exe.Token;
import org.springframework.web.servlet.tags.RequestContextAwareTag;
import com.shane.components.xmlservice.XMLService;
import com.shane.security.service.CurrentNodeImageManager;
public class ProcessImageTag extends RequestContextAwareTag {
private XMLService xmlService = new XMLService("URL");
private static final long serialVersionUID = 1L;
private long taskInstanceId = -1;
private long tokenInstanceId = -1;
private byte[] gpdBytes = null;
private byte[] imageBytes = null;
static String currentTokenColor = "red";
static String childTokenColor = "blue";
static String tokenNameColor = "blue";
private CurrentNodeImageManager currentNodeImageManager;
public void release() {
taskInstanceId = -1;
gpdBytes = null;
imageBytes = null;
// currentToken = null;
}
public int doEndTag() throws JspException {
try {
//initialize();
if(taskInstanceId!=0) {
retrieveByteArrays();
if (gpdBytes != null && imageBytes != null) {
writeTable();
}
}
} catch (IOException e) {
e.printStackTrace();
throw new JspException("table couldn't be displayed", e);
} catch (DocumentException e) {
e.printStackTrace();
throw new JspException("table couldn't be displayed", e);
}
release();
return EVAL_PAGE;
}
private void retrieveByteArrays() {
try {
// FileDefinition fileDefinition = processDefinition.getFileDefinition();
gpdBytes = currentNodeImageManager.getGpdBytes(taskInstanceId);
imageBytes = currentNodeImageManager.getImageBytes(taskInstanceId);
} catch (Exception e) {
e.printStackTrace();
}
}
private void writeTable() throws IOException, DocumentException {
int borderWidth = 4;
Element rootDiagramElement = DocumentHelper.parseText(new String(gpdBytes)).getRootElement();
int[] boxConstraint;
int[] imageDimension = extractImageDimension(rootDiagramElement);
String imageLink = xmlService.get("localurl")+"/processImage.do?definitionId=" + currentNodeImageManager.getProcessDefinitionID(taskInstanceId);
JspWriter jspOut = pageContext.getOut();
if (tokenInstanceId > 0&&tokenInstanceId!=0) {
List allTokens = new ArrayList();
allTokens = currentNodeImageManager.walkTokens(allTokens, taskInstanceId);
jspOut.println("<div style='position:relative; background-image:url(" + imageLink + "); width: " + imageDimension[0] + "px; height: " + imageDimension[1] + "px;'>");
for (int i = 0; i < allTokens.size(); i++)
{
Token token = (Token) allTokens.get(i);
//check how many tokens are on teh same level (= having the same parent)
int offset = i;
if(i > 0) {
while(offset > 0 && (currentNodeImageManager.getParentToken((Token) allTokens.get(offset - 1))).equals(currentNodeImageManager.getParentToken(token))) {
offset--;
}
}
boxConstraint = currentNodeImageManager.extractBoxConstraint(rootDiagramElement, token);
//Adjust for borders
//boxConstraint[2]-=borderWidth*2;
//boxConstraint[3]-=borderWidth*2;
jspOut.println("<div style='position:absolute; left: "+ boxConstraint[0] +"px; top: "+ boxConstraint[1] +"px; ");
if (i == (allTokens.size() - 1)) {
jspOut.println("border: " + currentTokenColor);
}
else {
jspOut.println("border: " + childTokenColor);
}
jspOut.println(" " + borderWidth + "px groove; "+
"width: "+ boxConstraint[2] +"px; height: "+ boxConstraint[3] +"px;'>");
if(token.getName()!=null)
{
jspOut.println("<span style='color:" + tokenNameColor + ";font-style:italic;position:absolute;left:"+ (boxConstraint[2] + 10) +"px;top:" +((i - offset) * 20) +";'> " + token.getName() +"</span>");
}
jspOut.println("</div>");
}
jspOut.println("</div>");
}
else
{
boxConstraint = currentNodeImageManager.extractBoxConstraint(rootDiagramElement,taskInstanceId);
jspOut.println("<table border=0 cellspacing=0 cellpadding=0 width=" + imageDimension[0] + " height=" + imageDimension[1] + ">");
jspOut.println(" <tr>");
jspOut.println(" <td width=" + imageDimension[0] + " height=" + imageDimension[1] + " style=/"background-image:url(" + imageLink + ")/" valign=top>");
jspOut.println(" <table border=0 cellspacing=0 cellpadding=0>");
jspOut.println(" <tr>");
jspOut.println(" <td width=" + (boxConstraint[0] - borderWidth) + " height=" + (boxConstraint[1] - borderWidth)
+ " style=/"background-color:transparent;/"></td>");
jspOut.println(" </tr>");
jspOut.println(" <tr>");
jspOut.println(" <td style=/"background-color:transparent;/"></td>");
jspOut.println(" <td style=/"border-color:" + currentTokenColor + "; border-width:" + borderWidth + "px; border-style:groove; background-color:transparent;/" width="
+ boxConstraint[2] + " height=" + (boxConstraint[3] + (2 * borderWidth)) + "> </td>");
jspOut.println(" </tr>");
jspOut.println(" </table>");
jspOut.println(" </td>");
jspOut.println(" </tr>");
jspOut.println("</table>");
}
}
/* private int[] extractBoxConstraint(Element root) {
int[] result = new int[4];
String nodeName = currentToken.getNode().getName();
XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
Element node = (Element) xPath.selectSingleNode(root);
result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue();
result[1] = Integer.valueOf(node.attribute("y").getValue()).intValue();
result[2] = Integer.valueOf(node.attribute("width").getValue()).intValue();
result[3] = Integer.valueOf(node.attribute("height").getValue()).intValue();
return result;
}*/
/*private int[] extractBoxConstraint(Element root, Token token) {
int[] result = new int[4];
String nodeName = token.getNode().getName();
XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
Element node = (Element) xPath.selectSingleNode(root);
result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue();
result[1] = Integer.valueOf(node.attribute("y").getValue()).intValue();
result[2] = Integer.valueOf(node.attribute("width").getValue()).intValue();
result[3] = Integer.valueOf(node.attribute("height").getValue()).intValue();
return result;
}*/
private int[] extractImageDimension(Element root) {
int[] result = new int[2];
result[0] = Integer.valueOf(root.attribute("width").getValue()).intValue();
result[1] = Integer.valueOf(root.attribute("height").getValue()).intValue();
return result;
}
/* private void initialize() {
JbpmContext jbpmContext = currentNodeImageManager.getJbpmContext();
if (this.taskInstanceId > 0) {
TaskInstance taskInstance = jbpmContext.getTaskMgmtSession().loadTaskInstance(taskInstanceId);
currentToken = taskInstance.getToken();
}
else
{
if (this.tokenInstanceId > 0)
currentToken = jbpmContext.getGraphSession().loadToken(this.tokenInstanceId);
}
processDefinition = currentToken.getProcessInstance().getProcessDefinition();
}
*/
/*private void walkTokens(Token parent, List allTokens)
{
Map children = parent.getChildren();
if(children != null && children.size() > 0)
{
Collection childTokens = children.values();
for (Iterator iterator = childTokens.iterator(); iterator.hasNext();)
{
Token child = (Token) iterator.next();
walkTokens(child, allTokens);
}
}
allTokens.add(parent);
}*/
public void setTask(long id) {
this.taskInstanceId = id;
}
public void setToken(long id) {
this.tokenInstanceId = id;
}
@Override
protected int doStartTagInternal() throws Exception {
currentNodeImageManager = (CurrentNodeImageManager)this.getRequestContext().getWebApplicationContext().getBean("currentNodeImageManager");
return EVAL_BODY_INCLUDE;
}
}
4 判断当前节点service类实现
4.1 定义接口
import java.util.List;
import org.dom4j.Element;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.Token;
import org.jbpm.taskmgmt.exe.TaskInstance;
public interface CurrentNodeImageManager {
//public TaskInstance getTaskInstance(long taskInstanceId);
public JbpmContext getJbpmContext();
public Token getCurrentToken(long taskInstanceId);
public byte[] getGpdBytes(long taskInstanceId);
public byte[] getImageBytes(long taskInstanceId);
public long getProcessDefinitionID(long taskInstanceId);
public List walkTokens(List allTokens,long taskInstanceId);
public Token getParentToken(Token token);
public int[] extractBoxConstraint(Element root,Token token);
public int[] extractBoxConstraint(Element root,long taskInstanceId);
}
4.2 实现类
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.xpath.DefaultXPath;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.Token;
import org.jbpm.taskmgmt.exe.TaskInstance;
import com.shane.commons.core.dao.HibernateEntityDao;
public class CurrentNodeImageManagerImpl extends HibernateEntityDao implements CurrentNodeImageManager{
private JbpmConfiguration jbpmConfiguration;
private ProcessDefinition processDefinition;
private static long taskInstanceId;
public JbpmContext getJbpmContext(){
JbpmContext context = jbpmConfiguration.createJbpmContext();
context.setSession(this.getSession());
return context;
}
public Token getCurrentToken(long taskInstanceId) {
JbpmContext jbpmContext = this.getJbpmContext();
TaskInstance taskInstance = jbpmContext.getTaskMgmtSession().loadTaskInstance(taskInstanceId);
return taskInstance.getToken();
}
public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) {
this.jbpmConfiguration = jbpmConfiguration;
}
private ProcessDefinition getProcessDefinition(long taskInstanceId) {
return this.getCurrentToken(taskInstanceId).getProcessInstance().getProcessDefinition();
}
public long getProcessDefinitionID(long taskInstanceId) {
return this.getProcessDefinition(taskInstanceId).getId();
}
public byte[] getGpdBytes(long taskInstanceId) {
return this.getProcessDefinition(taskInstanceId).getFileDefinition().getBytes("gpd.xml");
}
public byte[] getImageBytes(long taskInstanceId) {
return this.getProcessDefinition(taskInstanceId).getFileDefinition().getBytes("processimage.jpg");
}
public List walkTokens(List allTokens,long taskInstanceId)
{
Map children = this.getCurrentToken(taskInstanceId).getChildren();
if(children != null && children.size() > 0)
{
Collection childTokens = children.values();
for (Iterator iterator = childTokens.iterator(); iterator.hasNext();)
{
Token child = (Token) iterator.next();
walkTokens(allTokens,taskInstanceId);
}
}
allTokens.add(this.getCurrentToken(taskInstanceId));
return allTokens;
}
public Token getParentToken(Token token) {
return token.getParent();
}
public int[] extractBoxConstraint(Element root,Token token) {
int[] result = new int[4];
String nodeName = token.getNode().getName();
XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
Element node = (Element) xPath.selectSingleNode(root);
result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue();
result[1] = Integer.valueOf(node.attribute("y").getValue()).intValue();
result[2] = Integer.valueOf(node.attribute("width").getValue()).intValue();
result[3] = Integer.valueOf(node.attribute("height").getValue()).intValue();
return result;
}
public int[] extractBoxConstraint(Element root,long taskInstanceId) {
int[] result = new int[4];
String nodeName = this.getCurrentToken(taskInstanceId).getNode().getName();
XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
Element node = (Element) xPath.selectSingleNode(root);
result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue();
result[1] = Integer.valueOf(node.attribute("y").getValue()).intValue();
result[2] = Integer.valueOf(node.attribute("width").getValue()).intValue();
result[3] = Integer.valueOf(node.attribute("height").getValue()).intValue();
return result;
}
}
5 处理图片显示类的实现
5.1 action类的实现
package com.shane.security.web;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import com.shane.commons.core.web.StrutsAction;
import com.shane.security.service.ProcessImageManager;
public class ProcessImageAction extends StrutsAction {
private ProcessImageManager processImageManager;
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
long processDefinitionId = Long.parseLong( request.getParameter( "definitionId" ) );
byte[] bytes = processImageManager.getImage(processDefinitionId);
OutputStream out = response.getOutputStream();
out.write(bytes);
out.flush();
return null;
}
public void setProcessImageManager(ProcessImageManager processImageManager) {
this.processImageManager = processImageManager;
}
}
5.2 service接口的定义与实现
package com.shane.security.service;
public interface ProcessImageManager {
public byte[] getImage(long processDefinitionId);
}
5.3 实现类
package com.shane.security.service;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import com.shane.commons.core.dao.HibernateEntityDao;
public class ProcessImageManagerImpl extends HibernateEntityDao implements
ProcessImageManager {
private JbpmConfiguration jbpmConfiguration;
/**
* 根据流程定义ID获取流程图片
*/
public byte[] getImage(long processDefinitionId) {
JbpmContext jbpmContext = this.getJbpmContext();
ProcessDefinition processDefinition = jbpmContext.getGraphSession().loadProcessDefinition(processDefinitionId);
byte[] bytes = processDefinition.getFileDefinition().getBytes("processimage.jpg");
return bytes;
}
private JbpmContext getJbpmContext(){
JbpmContext context = jbpmConfiguration.createJbpmContext();
context.setSession(this.getSession());
return context;
}
public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) {
this.jbpmConfiguration = jbpmConfiguration;
}
}
到此:就可以根据index_demo.jsp页面中的链接来测试整个流程了。Jbpm是一个复杂系统,其本身有33张业务表,此例只是一个简单的测试示例。希望对不完善的地方,大家给一些修改意见。