KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > domain > WorkflowRenderer


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.domain;
18
19 import info.jtrac.util.XmlUtils;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.TreeMap JavaDoc;
27 import org.dom4j.Document;
28 import org.dom4j.Element;
29
30 /**
31  * Class that is used to render a workflow to the GUI
32  * currently designed to work with an HTML table in a JSP
33  */

34 public class WorkflowRenderer implements Serializable JavaDoc {
35     
36     Map JavaDoc<String JavaDoc, Role> rolesMap;
37     Map JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>> transitionRoles;
38     Map JavaDoc<Integer JavaDoc, Set JavaDoc<Integer JavaDoc>> stateTransitions;
39     private Map JavaDoc<Integer JavaDoc, String JavaDoc> stateNames;
40     Document document;
41     
42     public WorkflowRenderer(Map JavaDoc<String JavaDoc, Role> rolesMap, Map JavaDoc<Integer JavaDoc, String JavaDoc> stateNames) {
43         this.rolesMap = rolesMap;
44         this.stateNames = stateNames;
45         init();
46     }
47     
48     private void init() {
49         // transitions <--> roleNames map
50
// the key is a string concatenation <fromstate>_<tostate> for convenience
51
transitionRoles = new TreeMap JavaDoc<String JavaDoc, Set JavaDoc<String JavaDoc>>();
52         // for each state <--> a union of transitions across all roles
53
stateTransitions = new TreeMap JavaDoc<Integer JavaDoc, Set JavaDoc<Integer JavaDoc>>();
54         for(Role r : rolesMap.values()) {
55             for(State s: r.getStates().values()) {
56                 Set JavaDoc<Integer JavaDoc> transitions = stateTransitions.get(s.getStatus());
57                 if (transitions == null) {
58                     transitions = new HashSet JavaDoc<Integer JavaDoc>();
59                     stateTransitions.put(s.getStatus(), transitions);
60                 }
61                 transitions.addAll(s.getTransitions());
62                 for (int i : s.getTransitions()) {
63                     String JavaDoc transitionKey = s.getStatus() + "_" + i;
64                     Set JavaDoc<String JavaDoc> roleNames = transitionRoles.get(transitionKey);
65                     if (roleNames == null) {
66                         roleNames = new HashSet JavaDoc<String JavaDoc>();
67                         transitionRoles.put(transitionKey, roleNames);
68                     }
69                     roleNames.add(r.getName());
70                 }
71             }
72         }
73         document = XmlUtils.getNewDocument("state");
74         Element e = document.getRootElement();
75         e.addAttribute("name", stateNames.get(State.NEW));
76         e.addAttribute("key", State.NEW + "");
77         addTransitions(e, State.NEW);
78     }
79     
80     /* has the state already been added to the tree? */
81     private boolean stateExists(int key) {
82         return document.selectNodes("//state[@key='" + key + "']").size() > 0;
83     }
84     
85     /* main recursive function */
86     private void addTransitions(Element parent, int state) {
87         Set JavaDoc<Integer JavaDoc> transitions = stateTransitions.get(state);
88         if (transitions == null) {
89             return;
90         }
91         for(int i : transitions) {
92             boolean exists = stateExists(i);
93             Element child = parent.addElement("state");
94             child.addAttribute("name", stateNames.get(i));
95             child.addAttribute("key", i + "");
96             if (exists) {
97                 child.addAttribute("mirror", "true");
98             } else {
99                 addTransitions(child, i);
100             }
101         }
102     }
103
104     public Document getDocument() {
105         return document;
106     }
107     
108     public String JavaDoc getAsString() {
109         return XmlUtils.getAsPrettyXml(document);
110     }
111     
112     private String JavaDoc getAsHtml(Element e) {
113         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
114         List JavaDoc<Element> childElements = (List JavaDoc<Element>) e.elements();
115         String JavaDoc stateClass = e.attributeValue("mirror") != null ? "mirror" : "state";
116         sb.append("<table class='workflow'><tr><td rowspan='" + childElements.size() + "' class='" + stateClass + "'>");
117         sb.append(e.attributeValue("name"));
118         sb.append("</td>");
119         boolean first = true;
120         String JavaDoc fromState = e.attributeValue("key");
121         for(Element child : childElements) {
122             if (!first) {
123                 sb.append("<tr>");
124             }
125             String JavaDoc toState = child.attributeValue("key");
126             sb.append("<td class='transition'>");
127             for(String JavaDoc roleKey : transitionRoles.get(fromState + "_" + toState)) {
128                 sb.append(roleKey).append("<br/>");
129             }
130             sb.append("</td><td>");
131             sb.append(getAsHtml(child));
132             sb.append("</td></tr>");
133             if (first) {
134               first = false;
135             }
136         }
137         sb.append("</table>");
138         return sb.toString();
139     }
140     
141     public String JavaDoc getAsHtml() {
142         return getAsHtml(document.getRootElement());
143     }
144 }
145
Popular Tags