1 package org.jbpm.bpel.def; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 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 19 public class Switch extends StructuredActivity { 20 21 private static final long serialVersionUID = 1L; 22 23 private List conditions; 24 25 public Switch() { 26 super(); 27 conditions = new ArrayList (); 28 } 29 30 public Switch(String name) { 31 super(name); 32 conditions = new ArrayList (); 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 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 public void reorderNode(int oldIndex, int newIndex) { 89 super.reorderNode(oldIndex, newIndex); 90 91 int otherwiseIndex = conditions.size(); 93 94 if(oldIndex < otherwiseIndex && newIndex < otherwiseIndex) { 95 Object reorderedCondition = conditions.remove(oldIndex); 96 conditions.add(newIndex, reorderedCondition); 97 } 98 } 99 100 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.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 getCases() { 143 List cases = nodes; 144 145 if(containsOtherwise()) { 146 cases = new ArrayList (nodes); 147 cases.remove(conditions.size()); 148 } 149 150 return cases; 151 } 152 153 List getConditions() { 155 return conditions; 156 } 157 158 159 public void accept(BpelVisitor visitor) { 160 visitor.visit(this); 161 } 162 } 163 | Popular Tags |