KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > webapp > tag > ProcessImageTag


1 package org.jbpm.webapp.tag;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.servlet.jsp.JspException JavaDoc;
6 import javax.servlet.jsp.JspWriter JavaDoc;
7 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
8
9 import org.dom4j.DocumentException;
10 import org.dom4j.DocumentHelper;
11 import org.dom4j.Element;
12 import org.dom4j.XPath;
13 import org.dom4j.xpath.DefaultXPath;
14 import org.jbpm.db.JbpmSession;
15 import org.jbpm.file.def.FileDefinition;
16 import org.jbpm.graph.def.ProcessDefinition;
17 import org.jbpm.graph.exe.Token;
18 import org.jbpm.taskmgmt.exe.TaskInstance;
19
20 public class ProcessImageTag extends TagSupport JavaDoc {
21
22   private static final long serialVersionUID = 1L;
23   private long taskInstanceId = -1;
24
25   private byte[] gpdBytes = null;
26   private byte[] imageBytes = null;
27   private Token currentToken = null;
28   private ProcessDefinition processDefinition = null;
29
30   public void release() {
31     taskInstanceId = -1;
32     gpdBytes = null;
33     imageBytes = null;
34     currentToken = null;
35   }
36
37   public int doEndTag() throws JspException JavaDoc {
38     try {
39       initialize();
40       retrieveByteArrays();
41       if (gpdBytes != null && imageBytes != null) {
42         writeTable();
43       }
44     } catch (IOException JavaDoc e) {
45       e.printStackTrace();
46       throw new JspException JavaDoc("table couldn't be displayed", e);
47     } catch (DocumentException e) {
48       e.printStackTrace();
49       throw new JspException JavaDoc("table couldn't be displayed", e);
50     }
51     release();
52     return EVAL_PAGE;
53   }
54
55   private void retrieveByteArrays() {
56     try {
57       FileDefinition fileDefinition = processDefinition.getFileDefinition();
58       gpdBytes = fileDefinition.getBytes("gpd.xml");
59       imageBytes = fileDefinition.getBytes("processimage.jpg");
60     } catch (Exception JavaDoc e) {
61       e.printStackTrace();
62     }
63   }
64
65   private void writeTable() throws IOException JavaDoc, DocumentException {
66
67     int borderWidth = 4;
68     Element rootDiagramElement = DocumentHelper.parseText(new String JavaDoc(gpdBytes)).getRootElement();
69     int[] boxConstraint = extractBoxConstraint(rootDiagramElement);
70     int[] imageDimension = extractImageDimension(rootDiagramElement);
71     String JavaDoc imageLink = "processimage?definitionId=" + processDefinition.getId();
72     JspWriter JavaDoc jspOut = pageContext.getOut();
73
74     jspOut.println("<table border=0 cellspacing=0 cellpadding=0 width=" + imageDimension[0] + " height=" + imageDimension[1] + ">");
75     jspOut.println(" <tr>");
76     jspOut.println(" <td width=" + imageDimension[0] + " height=" + imageDimension[1] + " style=\"background-image:url(" + imageLink + ")\" valign=top>");
77     jspOut.println(" <table border=0 cellspacing=0 cellpadding=0>");
78     jspOut.println(" <tr>");
79     jspOut.println(" <td width=" + (boxConstraint[0] - borderWidth) + " height=" + (boxConstraint[1] - borderWidth)
80             + " style=\"background-color:transparent;\"></td>");
81     jspOut.println(" </tr>");
82     jspOut.println(" <tr>");
83     jspOut.println(" <td style=\"background-color:transparent;\"></td>");
84     jspOut.println(" <td style=\"border-color:red; border-width:" + borderWidth + "px; border-style:groove; background-color:transparent;\" width="
85             + boxConstraint[2] + " height=" + (boxConstraint[3] + (2 * borderWidth)) + ">&nbsp;</td>");
86     jspOut.println(" </tr>");
87     jspOut.println(" </table>");
88     jspOut.println(" </td>");
89     jspOut.println(" </tr>");
90     jspOut.println("</table>");
91   }
92
93   private int[] extractBoxConstraint(Element root) {
94     int[] result = new int[4];
95     String JavaDoc nodeName = currentToken.getNode().getName();
96     XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
97     Element node = (Element) xPath.selectSingleNode(root);
98     result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue();
99     result[1] = Integer.valueOf(node.attribute("y").getValue()).intValue();
100     result[2] = Integer.valueOf(node.attribute("width").getValue()).intValue();
101     result[3] = Integer.valueOf(node.attribute("height").getValue()).intValue();
102     return result;
103   }
104
105   private int[] extractImageDimension(Element root) {
106     int[] result = new int[2];
107     result[0] = Integer.valueOf(root.attribute("width").getValue()).intValue();
108     result[1] = Integer.valueOf(root.attribute("height").getValue()).intValue();
109     return result;
110   }
111
112   private void initialize() {
113     JbpmSession jbpmSession = JbpmSession.getCurrentJbpmSession();
114     TaskInstance taskInstance = jbpmSession.getTaskMgmtSession().loadTaskInstance(taskInstanceId);
115     currentToken = taskInstance.getToken();
116     processDefinition = currentToken.getProcessInstance().getProcessDefinition();
117   }
118
119   public void setTask(long id) {
120     this.taskInstanceId = id;
121   }
122
123 }
124
Popular Tags