1 5 package com.opensymphony.workflow.loader; 6 7 import com.opensymphony.workflow.InvalidWorkflowDescriptorException; 8 import com.opensymphony.workflow.util.Validatable; 9 10 import org.w3c.dom.Element ; 11 import org.w3c.dom.Node ; 12 import org.w3c.dom.NodeList ; 13 14 import java.io.PrintWriter ; 15 16 import java.util.*; 17 18 19 23 public class StepDescriptor extends AbstractDescriptor implements Validatable { 24 26 protected List actions = new ArrayList(); 27 28 33 protected List commonActions = new ArrayList(); 34 protected List permissions = new ArrayList(); 35 protected List postFunctions = new ArrayList(); 36 protected List preFunctions = new ArrayList(); 37 protected Map metaAttributes = new HashMap(); 38 protected String name; 39 protected boolean hasActions = false; 40 41 43 public StepDescriptor() { 44 } 45 46 public StepDescriptor(Element step) { 47 init(step); 48 } 49 50 51 public StepDescriptor(Element step, AbstractDescriptor parent) { 52 setParent(parent); 53 init(step); 54 } 55 56 58 public ActionDescriptor getAction(int id) { 59 for (Iterator iterator = actions.iterator(); iterator.hasNext();) { 60 ActionDescriptor action = (ActionDescriptor) iterator.next(); 61 62 if (action.getId() == id) { 63 return action; 64 } 65 } 66 67 return null; 68 } 69 70 73 public List getActions() { 74 return actions; 75 } 76 77 public void setMetaAttributes(Map metaAttributes) { 78 this.metaAttributes = metaAttributes; 79 } 80 81 public Map getMetaAttributes() { 82 return metaAttributes; 83 } 84 85 public void setName(String name) { 86 this.name = name; 87 } 88 89 public String getName() { 90 return name; 91 } 92 93 96 public List getPermissions() { 97 return permissions; 98 } 99 100 public void setPostFunctions(List postFunctions) { 101 this.postFunctions = postFunctions; 102 } 103 104 public List getPostFunctions() { 105 return postFunctions; 106 } 107 108 public void setPreFunctions(List preFunctions) { 109 this.preFunctions = preFunctions; 110 } 111 112 public List getPreFunctions() { 113 return preFunctions; 114 } 115 116 public boolean resultsInJoin(int join) { 117 for (Iterator iterator = actions.iterator(); iterator.hasNext();) { 118 ActionDescriptor actionDescriptor = (ActionDescriptor) iterator.next(); 119 120 if (actionDescriptor.getUnconditionalResult().getJoin() == join) { 121 return true; 122 } 123 124 List results = actionDescriptor.getConditionalResults(); 125 126 for (Iterator iterator2 = results.iterator(); iterator2.hasNext();) { 127 ConditionalResultDescriptor resultDescriptor = (ConditionalResultDescriptor) iterator2.next(); 128 129 if (resultDescriptor.getJoin() == join) { 130 return true; 131 } 132 } 133 } 134 135 return false; 136 } 137 138 public void validate() throws InvalidWorkflowDescriptorException { 139 if ((commonActions.size() == 0) && (actions.size() == 0) && hasActions) { 140 throw new InvalidWorkflowDescriptorException("Step '" + name + "' actions element must contain at least one action or common-action"); 141 } 142 143 if (getId() == -1) { 144 throw new InvalidWorkflowDescriptorException("Cannot use a step ID of -1 as it is a reserved value"); 145 } 146 147 ValidationHelper.validate(actions); 148 ValidationHelper.validate(permissions); 149 ValidationHelper.validate(preFunctions); 150 ValidationHelper.validate(postFunctions); 151 152 Iterator iter = commonActions.iterator(); 153 154 while (iter.hasNext()) { 155 Object o = iter.next(); 156 157 try { 158 Integer actionId = new Integer (o.toString()); 159 ActionDescriptor commonActionReference = (ActionDescriptor) ((WorkflowDescriptor) getParent()).getCommonActions().get(actionId); 160 161 if (commonActionReference == null) { 162 throw new InvalidWorkflowDescriptorException("Common action " + actionId + " specified in step " + getName() + " does not exist"); 163 } 164 } catch (NumberFormatException ex) { 165 throw new InvalidWorkflowDescriptorException("Common action " + o + " is not a valid action ID"); 166 } 167 } 168 } 169 170 public void writeXML(PrintWriter out, int indent) { 171 XMLUtil.printIndent(out, indent++); 172 out.print("<step id=\"" + getId() + "\""); 173 174 if ((name != null) && (name.length() > 0)) { 175 out.print(" name=\"" + name + "\""); 176 } 177 178 out.println(">"); 179 180 Iterator iter = metaAttributes.entrySet().iterator(); 181 182 while (iter.hasNext()) { 183 Map.Entry entry = (Map.Entry) iter.next(); 184 XMLUtil.printIndent(out, indent); 185 out.print("<meta name=\""); 186 out.print(entry.getKey()); 187 out.print("\">"); 188 out.print(entry.getValue()); 189 out.println("</meta>"); 190 } 191 192 if (preFunctions.size() > 0) { 193 XMLUtil.printIndent(out, indent++); 194 out.println("<pre-functions>"); 195 196 for (int i = 0; i < preFunctions.size(); i++) { 197 FunctionDescriptor function = (FunctionDescriptor) preFunctions.get(i); 198 function.writeXML(out, indent); 199 } 200 201 XMLUtil.printIndent(out, --indent); 202 out.println("</pre-functions>"); 203 } 204 205 if (permissions.size() > 0) { 206 XMLUtil.printIndent(out, indent++); 207 out.println("<external-permissions>"); 208 209 for (int i = 0; i < permissions.size(); i++) { 210 PermissionDescriptor permission = (PermissionDescriptor) permissions.get(i); 211 permission.writeXML(out, indent); 212 } 213 214 XMLUtil.printIndent(out, --indent); 215 out.println("</external-permissions>"); 216 } 217 218 if ((actions.size() > 0) || (commonActions.size() > 0)) { 219 XMLUtil.printIndent(out, indent++); 220 out.println("<actions>"); 221 222 for (int i = 0; i < commonActions.size(); i++) { 224 out.println("<common-action id=\"" + commonActions.get(i) + "\" />"); 225 } 226 227 for (int i = 0; i < actions.size(); i++) { 228 ActionDescriptor action = (ActionDescriptor) actions.get(i); 229 action.writeXML(out, indent); 230 } 231 232 XMLUtil.printIndent(out, --indent); 233 out.println("</actions>"); 234 } 235 236 if (postFunctions.size() > 0) { 237 XMLUtil.printIndent(out, indent++); 238 out.println("<post-functions>"); 239 240 for (int i = 0; i < postFunctions.size(); i++) { 241 FunctionDescriptor function = (FunctionDescriptor) postFunctions.get(i); 242 function.writeXML(out, indent); 243 } 244 245 XMLUtil.printIndent(out, --indent); 246 out.println("</post-functions>"); 247 } 248 249 XMLUtil.printIndent(out, --indent); 250 out.println("</step>"); 251 } 252 253 protected void init(Element step) { 254 try { 255 setId(Integer.parseInt(step.getAttribute("id"))); 256 } catch (Exception ex) { 257 throw new IllegalArgumentException ("Invalid step id value " + step.getAttribute("id")); 258 } 259 260 name = step.getAttribute("name"); 261 262 NodeList children = step.getChildNodes(); 263 264 for (int i = 0; i < children.getLength(); i++) { 265 Node child = (Node ) children.item(i); 266 267 if (child.getNodeName().equals("meta")) { 268 Element meta = (Element ) child; 269 String value = XMLUtil.getText(meta); 270 this.metaAttributes.put(meta.getAttribute("name"), value); 271 } 272 } 273 274 Element pre = XMLUtil.getChildElement(step, "pre-functions"); 276 277 if (pre != null) { 278 List preFunctions = XMLUtil.getChildElements(pre, "function"); 279 280 for (int k = 0; k < preFunctions.size(); k++) { 281 Element preFunction = (Element ) preFunctions.get(k); 282 FunctionDescriptor functionDescriptor = new FunctionDescriptor(preFunction); 283 functionDescriptor.setParent(this); 284 this.preFunctions.add(functionDescriptor); 285 } 286 } 287 288 Element p = XMLUtil.getChildElement(step, "external-permissions"); 290 291 if (p != null) { 292 List permissions = XMLUtil.getChildElements(p, "permission"); 293 294 for (int i = 0; i < permissions.size(); i++) { 295 Element permission = (Element ) permissions.get(i); 296 PermissionDescriptor permissionDescriptor = new PermissionDescriptor(permission); 297 permissionDescriptor.setParent(this); 298 this.permissions.add(permissionDescriptor); 299 } 300 } 301 302 Element a = XMLUtil.getChildElement(step, "actions"); 304 305 if (a != null) { 306 hasActions = true; 307 308 List actions = XMLUtil.getChildElements(a, "action"); 309 310 for (int i = 0; i < actions.size(); i++) { 311 Element action = (Element ) actions.get(i); 312 ActionDescriptor actionDescriptor = new ActionDescriptor(action); 313 actionDescriptor.setParent(this); 314 this.actions.add(actionDescriptor); 315 } 316 317 List commonActions = XMLUtil.getChildElements(a, "common-action"); 319 320 for (int i = 0; i < commonActions.size(); i++) { 321 Element commonAction = (Element ) commonActions.get(i); 322 323 WorkflowDescriptor workflowDescriptor = (WorkflowDescriptor) (getParent()); 324 325 try { 326 Integer actionId = new Integer (commonAction.getAttribute("id")); 327 328 ActionDescriptor commonActionReference = (ActionDescriptor) workflowDescriptor.getCommonActions().get(actionId); 329 330 if (commonActionReference != null) { 331 this.actions.add(commonActionReference); 332 } 333 334 this.commonActions.add(actionId); 335 } catch (Exception ex) { 336 } 338 } 339 } 340 341 Element post = XMLUtil.getChildElement(step, "post-functions"); 343 344 if (post != null) { 345 List postFunctions = XMLUtil.getChildElements(post, "function"); 346 347 for (int k = 0; k < postFunctions.size(); k++) { 348 Element postFunction = (Element ) postFunctions.get(k); 349 FunctionDescriptor functionDescriptor = new FunctionDescriptor(postFunction); 350 functionDescriptor.setParent(this); 351 this.postFunctions.add(functionDescriptor); 352 } 353 } 354 } 355 } 356 | Popular Tags |