1 package org.jbpm.context.exe; 2 3 import java.util.HashMap ; 4 import java.util.Map ; 5 6 import org.jbpm.graph.exe.Token; 7 import org.jbpm.module.exe.ModuleInstance; 8 9 19 public class ContextInstance extends ModuleInstance { 20 21 private static final long serialVersionUID = 1L; 22 23 protected Map tokenVariableMaps = null; 25 protected transient Map transientVariables = null; 27 28 public ContextInstance() { 29 } 30 31 34 38 public void createVariable(String name, Object value) { 39 createVariable(name, value, getRootToken()); 40 } 41 42 46 public void createVariable(String name, Object value, Token token) { 47 TokenVariableMap tokenVariableMap = getOrCreateTokenVariableMap(token); 48 tokenVariableMap.createVariableInstance(name, value); 49 } 50 51 54 public Map getVariables() { 55 return getVariables(getRootToken()); 56 } 57 58 61 public Map getVariables(Token token) { 62 Map variables = new HashMap (); 63 64 TokenVariableMap tokenVariableMap = getTokenVariableMap(token); 65 if (tokenVariableMap != null) { 66 tokenVariableMap.collectAllVariables(variables); 67 } 68 69 return variables; 70 } 71 72 75 public void addVariables(Map variables) { 76 addVariables(variables, getRootToken()); 77 } 78 79 82 public void addVariables(Map variables, Token token) { 83 TokenVariableMap tokenVariableMap = getOrCreateTokenVariableMap(token); 84 tokenVariableMap.addVariables(variables); 85 } 86 87 91 public Object getVariable(String name) { 92 return getVariable(name, getRootToken()); 93 } 94 95 100 public Object getVariable(String name, Token token) { 101 Object variable = null; 102 TokenVariableMap tokenVariableMap = getTokenVariableMap(token); 103 if (tokenVariableMap != null) { 104 variable = tokenVariableMap.getVariable(name); 105 } 106 return variable; 107 } 108 109 112 public Object getLocalVariable(String name, Token token) { 113 Object variable = null; 114 if (tokenVariableMaps!=null && tokenVariableMaps.containsKey(token)) { 115 TokenVariableMap tokenVariableMap = (TokenVariableMap) tokenVariableMaps.get(token); 116 if (tokenVariableMap != null) { 117 VariableInstance variableInstance = tokenVariableMap.getVariableInstance(name); 118 if(variableInstance != null) { 119 variable = variableInstance.getValue(); 120 } 121 } 122 } 123 return variable; 124 } 125 126 129 public void setVariable(String name, Object value) { 130 setVariable(name, value, getRootToken()); 131 } 132 133 138 public void setVariable(String name, Object value, Token token) { 139 TokenVariableMap tokenVariableMap = getOrCreateTokenVariableMap(token); 140 tokenVariableMap.setVariable(name, value); 141 } 142 143 147 public boolean hasVariable(String name) { 148 return hasVariable(name, getRootToken()); 149 } 150 151 155 public boolean hasVariable(String name, Token token) { 156 boolean hasVariable = false; 157 TokenVariableMap tokenVariableMap = getTokenVariableMap(token); 158 if (tokenVariableMap != null) { 159 hasVariable = tokenVariableMap.hasVariable(name); 160 } 161 return hasVariable; 162 } 163 164 167 public void deleteVariable(String name) { 168 deleteVariable(name, getRootToken()); 169 } 170 171 176 public void deleteVariable(String name, Token token) { 177 TokenVariableMap tokenVariableMap = getTokenVariableMap(token); 178 if (tokenVariableMap != null) { 179 tokenVariableMap.deleteVariable(name); 180 } 181 } 182 183 186 189 public Object getTransientVariable(String name) { 190 Object transientVariable = null; 191 if (transientVariables!= null) { 192 transientVariable = transientVariables.get(name); 193 } 194 return transientVariable; 195 } 196 197 200 public void setTransientVariable(String name, Object value) { 201 if (transientVariables == null) { 202 transientVariables = new HashMap (); 203 } 204 transientVariables.put(name, value); 205 } 206 207 210 public boolean hasTransientVariable(String name) { 211 if (transientVariables == null) { 212 return false; 213 } 214 return transientVariables.containsKey(name); 215 } 216 217 222 public Map getTransientVariables() { 223 return transientVariables; 224 } 225 226 229 public void setTransientVariables(Map transientVariables) { 230 this.transientVariables = transientVariables; 231 } 232 233 236 public void deleteTransientVariable(String name) { 237 if (transientVariables == null) 238 return; 239 transientVariables.remove(name); 240 } 241 242 243 Token getRootToken() { 244 return processInstance.getRootToken(); 245 } 246 247 252 TokenVariableMap getOrCreateTokenVariableMap(Token token) { 253 TokenVariableMap tokenVariableMap = null; 254 if (tokenVariableMaps==null) { 255 tokenVariableMaps = new HashMap (); 256 } 257 if (tokenVariableMaps.containsKey(token)) { 258 tokenVariableMap = (TokenVariableMap) tokenVariableMaps.get(token); 259 } else { 260 tokenVariableMap = new TokenVariableMap(token, this); 261 tokenVariableMaps.put( token, tokenVariableMap ); 262 263 Token parent = token.getParent(); 264 TokenVariableMap parentVariableMap = null; 265 while (parent!=null) { 266 if (! tokenVariableMaps.containsKey(parent) ) { 267 parentVariableMap = new TokenVariableMap(parent, this); 268 tokenVariableMaps.put( parent, parentVariableMap ); 269 } 270 parent = parent.getParent(); 271 } 272 } 273 return tokenVariableMap; 274 } 275 276 280 TokenVariableMap getTokenVariableMap(Token token) { 281 TokenVariableMap tokenVariableMap = null; 282 if (tokenVariableMaps!=null) { 283 if (tokenVariableMaps.containsKey(token)) { 284 tokenVariableMap = (TokenVariableMap) tokenVariableMaps.get(token); 285 } else if (! token.isRoot()) { 286 tokenVariableMap = getTokenVariableMap(token.getParent()); 287 } 288 } 289 return tokenVariableMap; 290 } 291 } 292 | Popular Tags |