KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > def > Switch


1 package org.jbpm.bpel.def;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.jbpm.graph.def.Transition;
7 import org.jbpm.graph.exe.ExecutionContext;
8 import org.jbpm.graph.exe.Token;
9
10 import org.jbpm.bpel.data.def.Snippet;
11 import org.jbpm.bpel.xml.util.DatatypeUtil;
12
13 /**
14  * The switch structured activity supports conditional behavior.
15  * @see "WS-BPEL 2.0 §12.2"
16  * @author Juan Cantú
17  * @version $Revision: 1.4 $ $Date: 2005/06/23 02:22:46 $
18  */

19 public class Switch extends StructuredActivity {
20
21   private static final long serialVersionUID = 1L;
22   
23   private List JavaDoc conditions;
24   
25   public Switch() {
26     super();
27     conditions = new ArrayList JavaDoc();
28   }
29   
30   public Switch(String JavaDoc name) {
31     super(name);
32     conditions = new ArrayList JavaDoc();
33   }
34   
35   public void execute(ExecutionContext context) {
36     Transition transition = null;
37     Token token = context.getToken();
38     
39     for(int i = 0, n = conditions.size(); i < n; i++ ) {
40       Activity theCase = (Activity)nodes.get(i);
41       Snippet condition = (Snippet)conditions.get(i);
42       
43       if(transition == null &&
44           DatatypeUtil.toBoolean(condition.getScript().evaluate(token)).booleanValue()) {
45         transition = theCase.getDefaultArrivingTransition();
46       }
47       else {
48         theCase.setNegativeLinkStatus(token);
49       }
50     }
51     Activity otherwise = getOtherwise();
52     
53     if( transition != null) {
54       getOtherwise().setNegativeLinkStatus(token);
55     }
56     else if(otherwise != null){
57       transition = getOtherwise().getDefaultArrivingTransition();
58     }
59     else {
60       transition = end.getDefaultArrivingTransition();
61     }
62     
63     getStart().leave(context, transition);
64   }
65   
66   // children management /////////////////////////////////////////////////////////////////////////////
67

68   public void attachChild(Activity activity) {
69     super.attachChild(activity);
70     conditions.add(null);
71     
72     if(containsOtherwise()) {
73       int lastIndex = conditions.size();
74       nodes.add(nodes.remove(lastIndex - 1));
75     }
76   }
77   
78   public void detachChild(Activity activity) {
79     int index = nodes.indexOf(activity);
80     if( index < conditions.size()) {
81       conditions.remove(index);
82     }
83     
84     super.detachChild(activity);
85   }
86   
87   // javadoc description in NodeCollection
88
public void reorderNode(int oldIndex, int newIndex) {
89     super.reorderNode(oldIndex, newIndex);
90     
91     //reorder conditions if necessary
92
int otherwiseIndex = conditions.size();
93     
94     if(oldIndex < otherwiseIndex && newIndex < otherwiseIndex) {
95       Object JavaDoc reorderedCondition = conditions.remove(oldIndex);
96       conditions.add(newIndex, reorderedCondition);
97     }
98   }
99   
100   // Switch getter and setters /////////////////////////////////////////////////////////////////////////////
101

102   public void setOtherwise(Activity otherwise) {
103     int conditionCount = conditions.size();
104     
105     if(nodes != null) {
106       int currentIndex = nodes.indexOf(otherwise);
107       if(currentIndex != -1) {
108         if(currentIndex != conditionCount) {
109           nodes.add(nodes.remove(currentIndex));
110           conditions.remove(currentIndex);
111         }
112         return;
113       }
114       else if(containsOtherwise()) {
115         detachChild((Activity) nodes.get(conditionCount));
116       }
117     }
118     
119     //super method is invoked to omit the creation of an empty condition
120
super.attachChild(otherwise);
121   }
122   
123   public void setCondition(Activity activity, Snippet condition) {
124     int index = nodes.indexOf(activity);
125     conditions.remove(index);
126     conditions.add(index, condition);
127   }
128   
129   public Snippet getCondition(Activity activity) {
130     int index = nodes.indexOf(activity);
131     return (Snippet) conditions.get(index);
132   }
133   
134   public Activity getOtherwise() {
135     return containsOtherwise() ?(Activity) nodes.get(conditions.size()) : null;
136   }
137   
138   private boolean containsOtherwise() {
139     return (nodes != null && nodes.size() > conditions.size());
140   }
141   
142   public List JavaDoc getCases() {
143     List JavaDoc cases = nodes;
144     
145     if(containsOtherwise()) {
146       cases = new ArrayList JavaDoc(nodes);
147       cases.remove(conditions.size());
148     }
149     
150     return cases;
151   }
152
153   //test purposes
154
List JavaDoc getConditions() {
155     return conditions;
156   }
157
158   /**{@inheritDoc}*/
159   public void accept(BpelVisitor visitor) {
160     visitor.visit(this);
161   }
162 }
163
Popular Tags