原理其实很简单,利用jawe将xpdl处理为jpeg,然后在页面上显示,
在web页面上显示shark的流程图的步骤:
1、写一个servlet,将流程在web页面上以图形的方式显示。代码如下:
package com.paiao.shark;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintStream;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
import org.enhydra.shark.Shark;
import org.enhydra.shark.api.client.wfservice.AdminInterface;
import org.enhydra.shark.api.client.wfservice.PackageAdministration;
import org.enhydra.shark.api.client.wfbase.*;
import java.io.*;
import org.enhydra.jawe.*;
import org.enhydra.jawe.graph.Process;
import org.enhydra.jawe.xml.elements.Package;
import org.enhydra.jawe.xml.elements.WorkflowProcess;
import org.enhydra.jawe.xml.elements.WorkflowProcesses;
import java.util.Iterator;
public class ProcessImageServlet
extends HttpServlet {
private static boolean _debug = false;
private static final long serialVersionUID = 1L;
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init() throws ServletException {
System.out.println("init process image servlet");
JaWEConfig conf = JaWEConfig.getInstance();
conf.setBackgroundColor("Color.white");
conf.setGridColor("Color.white");
conf.setGenericActivityColor("Color.lightGray");
conf.setActivityWidth(75);
conf.setActivityHeight(40);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
try {
String pkgId = request.getParameter("pkgId");
String processId = request.getParameter("procId");
String actSelect = request.getParameter("actId");
AbstractGraph g = createImage(pkgId, processId);
if (g != null) {
if (actSelect != null && !actSelect.equals(""))
selectCell(g, actSelect);
response.setContentType("image/png");
BufferedImage image = drawImage(g, true);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(image, "png", out);
out.flush();
out.close();
}
else {
System.err.println("g == null");
}
}
catch (Exception e) {
e.printStackTrace();
return;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
public void destroy() {
}
private static AbstractGraph createImage(String pkgId, String processId) {
if (pkgId == null || processId == null)
return null;
String filename="D://javatest//sharkjspclient//defaultroot//repository//external//gaoli.xpdl";
JaWE jawe=JaWE.getInstance();
Package pkg=jawe.openDocument(filename,true,false);
PackageEditor pkgEd=jawe.getPackageEditor();
pkgEd.setNewPackage(pkg,true);
WorkflowProcesses wps=(WorkflowProcesses)pkg.get("WorkflowProcesses");
Iterator it=wps.toCollection().iterator();
AbstractGraph currentGraph=null;
while (it.hasNext()) {
WorkflowProcess wp = (WorkflowProcess) it.next();
Process p = pkgEd.getProcessObject(wp);
AbstractEditor editor = p.getImplementationEditor();
currentGraph = editor.getGraph();
currentGraph.setSize(currentGraph.getPreferredSize());
}
return currentGraph;
}
public static String GBKToUTF(String strIn) {
String strOut;
strOut = null;
if (strIn == null || strIn.trim().equals(""))
return "";
byte[] b = null;
try {
b = strIn.getBytes("GB2312");
strOut = new String(b, "utf-8");
}
catch (UnsupportedEncodingException ex) {
}
return strOut;
}
public static String GBKToUTF(byte strIn[]) {
String strOut;
strOut = null;
if (strIn == null)
return "";
try {
strOut = new String(strIn, "utf-8");
}
catch (UnsupportedEncodingException ex) {
}
return strOut;
}
private static void selectCell(AbstractGraph graph, String activityId) {
try {
WorkflowManager wm = graph.getWorkflowManager();
Object go = wm.getActivity(activityId);
if (go != null)
graph.addSelectionCell(go);
}
catch (Exception e) {
System.err.println(e);
return;
}
}
private static BufferedImage drawImage(Component c, boolean resize) {
if (resize)
c.setSize(c.getPreferredSize());
Dimension size = c.getSize();
BufferedImage image = new BufferedImage(size.width - 50, size.height - 50,
1);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
c.paint(g2);
g2.dispose();
return image;
}
}
2、写一个调用servlet的jsp页面,代码如下:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page language = "java" import="java.util.*" %>
<%@ page import="test.JSPClientUtilities" %>
<%
String activityId = request.getParameter("activityId");
String g_ContextPath = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+g_ContextPath+"/jsp/workflow/";
String xpdl_process_id = "请假申请";
String []array = JSPClientUtilities.getGraphpicParameter(activityId);
String workflowOpeningUrl = g_ContextPath + "/jsp/workflow/ProcessImage.png?" +
"pkgId=" + array[0] + "&" +
"procId=" + array[1] + "&" +
"actId=" + array[2];
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title><%=xpdl_process_id%>流程图</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<img src="<%=workflowOpeningUrl%>">
</body>
</html>
3、在处理流程的页面上调用显示流程的jsp,要传一个当前活动的id,部分代码如下:
function viewproc(activatyId){
window.open("/jsp/workflow/processImage.jsp?activityId="+activatyId,"查看流程信息","toolbar=no, status=no,menubar=no, scrollbars=yes,resizable=no,width=800,height=600,left=80,top=60");
}