1 23 package org.infoglue.cms.applications.workflowtool.util; 24 25 import java.util.Collections ; 26 import java.util.Map ; 27 28 import org.apache.log4j.Logger; 29 import org.exolab.castor.jdo.Database; 30 import org.infoglue.cms.util.workflow.DatabaseSession; 31 32 import com.opensymphony.module.propertyset.PropertySet; 33 import com.opensymphony.provider.BeanProvider; 34 import com.opensymphony.provider.bean.DefaultBeanProvider; 35 import com.opensymphony.workflow.WorkflowException; 36 38 43 public abstract class InfoglueWorkflowBase 44 { 45 48 private final static Logger logger = Logger.getLogger(InfoglueWorkflowBase.class.getName()); 49 50 53 protected static final String UTF8_ENCODING = "utf-8"; 54 55 58 public static final String WORKFLOW_PROPERTYSET_PREFIX = "workflow_"; 59 60 63 public static final String FUNCTION_STATUS_PROPERTYSET_KEY = WORKFLOW_PROPERTYSET_PREFIX + "status"; 64 65 68 public static final String ERROR_PROPERTYSET_PREFIX = "error_"; 69 70 73 private static final String DB_PARAMETER = "db"; 74 75 78 private Map parameters; 79 80 83 private Map arguments; 84 85 88 private InfogluePropertySet propertySet; 89 90 93 private DatabaseSession workflowDatabase; 94 95 99 private Exception lastException; 100 101 104 public InfoglueWorkflowBase() 105 { 106 super(); 107 } 108 109 115 protected void initialize() throws WorkflowException 116 { 117 workflowDatabase = (DatabaseSession) getParameter(DB_PARAMETER); 118 } 119 120 127 protected void storeContext(final Map transientVars, final Map args, final PropertySet ps) 128 { 129 this.parameters = transientVars; 130 this.arguments = Collections.unmodifiableMap(args); 131 this.propertySet = new InfogluePropertySet(ps); 132 } 133 134 141 protected void throwException(final String message) throws WorkflowException 142 { 143 throwException(new WorkflowException(message)); 144 } 145 146 154 protected void throwException(final Exception e) throws WorkflowException 155 { 156 if(lastException != e) 157 { 158 logger.error(e.getMessage(), e); 159 } 160 lastException = e; 161 workflowDatabase.setRollbackOnly(); 162 throw (e instanceof WorkflowException) ? (WorkflowException) e : new WorkflowException(e); 163 } 164 165 171 protected final boolean argumentExists(final String name) 172 { 173 return arguments.containsKey(name); 174 } 175 176 184 protected final String getArgument(final String name) throws WorkflowException 185 { 186 if(!arguments.containsKey(name)) 187 { 188 throwException("Required argument " + name + " is missing."); 189 } 190 return arguments.get(name).toString(); 191 } 192 193 200 protected final String getArgument(final String name, final String defaultValue) throws WorkflowException 201 { 202 return arguments.containsKey(name) ? arguments.get(name).toString() : defaultValue; 203 } 204 205 211 protected final boolean parameterExists(final String key) 212 { 213 return parameters.containsKey(key); 214 } 215 216 224 protected final Object getParameter(final String key) throws WorkflowException 225 { 226 return getParameter(key, true); 227 } 228 229 236 protected final Object getParameter(final String key, final Object defaultValue) throws WorkflowException 237 { 238 return parameters.containsKey(key) ? parameters.get(key) : defaultValue; 239 } 240 241 249 protected final Object getParameter(final String key, final boolean required) throws WorkflowException 250 { 251 final Object parameter = parameters.get(key); 252 if(required && parameter == null) 253 { 254 final WorkflowException e = new WorkflowException("Required parameter " + key + " is missing."); 255 logger.error(e.toString()); 256 throw e; 257 } 258 return parameter; 259 } 260 261 267 protected final void setParameter(final String key, final Object value) 268 { 269 parameters.put(key, value); 270 } 271 272 277 protected final Map getParameters() 278 { 279 return parameters; 280 } 281 282 287 protected final boolean propertySetContains(final String key) 288 { 289 return propertySet.exists(key); 290 } 291 292 297 protected final String getPropertySetDataString(final String key) 298 { 299 return propertySet.getDataString(key); 300 } 301 302 308 protected final void setPropertySetDataString(final String key, final String value) 309 { 310 propertySet.setDataString(key, value); 311 } 312 313 318 protected final String getPropertySetString(final String key) 319 { 320 return propertySet.getString(key); 321 } 322 323 329 protected final void setPropertySetString(final String key, final String value) 330 { 331 propertySet.setString(key, value); 332 } 333 334 339 protected final void removeFromPropertySet(final String key) 340 { 341 removeFromPropertySet(key, false); 342 } 343 344 352 protected final void removeFromPropertySet(final String key, final boolean isPrefix) 353 { 354 propertySet.removeKeys(key, isPrefix); 355 } 356 357 360 361 protected final String translate(final String s) 362 { 363 final Object o = translateVariables(s, parameters, propertySet); 364 return (o == null) ? null : o.toString(); 365 } 366 367 372 protected final InfogluePropertySet getPropertySet() 373 { 374 return propertySet; 375 } 376 377 383 protected final Database getDatabase() throws WorkflowException 384 { 385 return workflowDatabase.getDB(); 386 } 387 388 394 public static Object translateVariables(String s, Map transientVars, PropertySet ps) 395 { 396 Object result = null; 397 398 String temp = s.trim(); 399 400 if (temp.startsWith("${") && temp.endsWith("}") && (temp.indexOf('$', 1) == -1)) 401 { 402 String var = temp.substring(2, temp.length() - 1); 404 405 result = getVariableFromMaps(var, transientVars, ps); 406 } 407 else 408 { 409 while (true) 411 { 412 int x = s.indexOf("${"); 413 int y = s.indexOf("}", x); 414 415 if ((x != -1) && (y != -1)) 416 { 417 String var = s.substring(x + 2, y); 418 String t = null; 419 Object o = getVariableFromMaps(var, transientVars, ps); 420 421 if (o != null) 422 { 423 t = o.toString(); 424 } 425 426 if (t != null) 427 { 428 s = s.substring(0, x) + t + s.substring(y + 1); 429 } 430 else 431 { 432 s = s.substring(0, x) + s.substring(y + 1); 434 } 435 } 436 else 437 { 438 break; 439 } 440 } 441 442 result = s; 443 } 444 445 return result; 446 } 447 448 private static BeanProvider beanProvider = new DefaultBeanProvider(); 449 450 452 public static Object getVariableFromMaps(String var, Map transientVars, PropertySet ps) { 453 int firstDot = var.indexOf('.'); 454 String actualVar = var; 455 456 if (firstDot != -1) { 457 actualVar = var.substring(0, firstDot); 458 } 459 460 Object o = transientVars.get(actualVar); 461 462 if (o == null) { 463 o = ps.getAsActualType(actualVar); 464 } 465 466 if (firstDot != -1) { 467 o = beanProvider.getProperty(o, var.substring(firstDot + 1)); 468 } 469 470 return o; 471 } 472 } 473 | Popular Tags |