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 12 import java.io.PrintWriter ; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 19 23 public class ResultDescriptor extends AbstractDescriptor implements Validatable { 24 26 protected List postFunctions = new ArrayList (); 27 protected List preFunctions = new ArrayList (); 28 protected List validators = new ArrayList (); 29 protected String dueDate; 30 protected String oldStatus; 31 protected String owner; 32 protected String status; 33 protected boolean hasStep = false; 34 protected int join; 35 protected int split; 36 protected int step = 0; 37 38 40 public ResultDescriptor() { 41 } 42 43 public ResultDescriptor(Element result) { 44 init(result); 45 } 46 47 49 public String getDueDate() { 50 return dueDate; 51 } 52 53 public void setJoin(int join) { 54 this.join = join; 55 } 56 57 public int getJoin() { 58 return join; 59 } 60 61 public void setOldStatus(String oldStatus) { 62 this.oldStatus = oldStatus; 63 } 64 65 public String getOldStatus() { 66 return oldStatus; 67 } 68 69 public void setOwner(String owner) { 70 this.owner = owner; 71 } 72 73 public String getOwner() { 74 return owner; 75 } 76 77 public List getPostFunctions() { 78 return postFunctions; 79 } 80 81 public List getPreFunctions() { 82 return preFunctions; 83 } 84 85 public void setSplit(int split) { 86 this.split = split; 87 } 88 89 public int getSplit() { 90 return split; 91 } 92 93 public void setStatus(String status) { 94 this.status = status; 95 } 96 97 public String getStatus() { 98 return status; 99 } 100 101 public void setStep(int step) { 102 this.step = step; 103 hasStep = true; 104 } 105 106 public int getStep() { 107 return step; 108 } 109 110 public List getValidators() { 111 return validators; 112 } 113 114 public void validate() throws InvalidWorkflowDescriptorException { 115 ValidationHelper.validate(preFunctions); 116 ValidationHelper.validate(postFunctions); 117 ValidationHelper.validate(validators); 118 119 if ((split == 0) && (join == 0)) { 121 StringBuffer error = new StringBuffer ("Result "); 122 123 if (getId() > 0) { 124 error.append("#").append(getId()); 125 } 126 127 error.append(" is not a split or join, and has no "); 128 129 if (!hasStep) { 130 throw new InvalidWorkflowDescriptorException(error.append("next step").toString()); 131 } 132 133 if ((status == null) || (status.length() == 0)) { 134 throw new InvalidWorkflowDescriptorException(error.append("status").toString()); 135 } 136 } 137 138 } 147 148 public void writeXML(PrintWriter out, int indent) { 149 XMLUtil.printIndent(out, indent++); 150 151 StringBuffer buf = new StringBuffer (); 152 buf.append("<unconditional-result"); 153 154 if (hasId()) { 155 buf.append(" id=\"").append(getId()).append("\""); 156 } 157 158 buf.append(" old-status=\"").append(oldStatus).append("\""); 159 160 if (join != 0) { 161 buf.append(" join=\"").append(join).append("\""); 162 } else if (split != 0) { 163 buf.append(" split=\"").append(split).append("\""); 164 } else { 165 buf.append(" status=\"").append(status).append("\""); 166 buf.append(" step=\"").append(step).append("\""); 167 168 if ((owner != null) && (owner.length() > 0)) { 169 buf.append(" owner=\"").append(owner).append("\""); 170 } 171 } 172 173 if ((preFunctions.size() == 0) && (postFunctions.size() == 0)) { 174 buf.append("/>"); 175 out.println(buf.toString()); 176 } else { 177 buf.append(">"); 178 out.println(buf.toString()); 179 printPreFunctions(out, indent); 180 printPostFunctions(out, indent); 181 XMLUtil.printIndent(out, --indent); 182 out.println("</unconditional-result>"); 183 } 184 } 185 186 protected void init(Element result) { 187 oldStatus = result.getAttribute("old-status"); 188 status = result.getAttribute("status"); 189 190 try { 191 setId(Integer.parseInt(result.getAttribute("id"))); 192 } catch (NumberFormatException e) { 193 } 194 195 dueDate = result.getAttribute("due-date"); 196 197 try { 198 split = Integer.parseInt(result.getAttribute("split")); 199 } catch (Exception ex) { 200 } 201 202 try { 203 join = Integer.parseInt(result.getAttribute("join")); 204 } catch (Exception ex) { 205 } 206 207 try { 208 step = Integer.parseInt(result.getAttribute("step")); 209 hasStep = true; 210 } catch (Exception ex) { 211 } 212 213 owner = result.getAttribute("owner"); 214 215 Element v = XMLUtil.getChildElement(result, "validators"); 217 218 if (v != null) { 219 List validators = XMLUtil.getChildElements(v, "validator"); 220 221 for (int k = 0; k < validators.size(); k++) { 222 Element validator = (Element ) validators.get(k); 223 ValidatorDescriptor validatorDescriptor = new ValidatorDescriptor(validator); 224 validatorDescriptor.setParent(this); 225 this.validators.add(validatorDescriptor); 226 } 227 } 228 229 Element pre = XMLUtil.getChildElement(result, "pre-functions"); 231 232 if (pre != null) { 233 List preFunctions = XMLUtil.getChildElements(pre, "function"); 234 235 for (int k = 0; k < preFunctions.size(); k++) { 236 Element preFunction = (Element ) preFunctions.get(k); 237 FunctionDescriptor functionDescriptor = new FunctionDescriptor(preFunction); 238 functionDescriptor.setParent(this); 239 this.preFunctions.add(functionDescriptor); 240 } 241 } 242 243 Element post = XMLUtil.getChildElement(result, "post-functions"); 245 246 if (post != null) { 247 List postFunctions = XMLUtil.getChildElements(post, "function"); 248 249 for (int k = 0; k < postFunctions.size(); k++) { 250 Element postFunction = (Element ) postFunctions.get(k); 251 FunctionDescriptor functionDescriptor = new FunctionDescriptor(postFunction); 252 functionDescriptor.setParent(this); 253 this.postFunctions.add(functionDescriptor); 254 } 255 } 256 } 257 258 protected void printPostFunctions(PrintWriter out, int indent) { 259 if (postFunctions.size() > 0) { 260 XMLUtil.printIndent(out, indent++); 261 out.println("<post-functions>"); 262 263 Iterator iter = postFunctions.iterator(); 264 265 while (iter.hasNext()) { 266 FunctionDescriptor function = (FunctionDescriptor) iter.next(); 267 function.writeXML(out, indent); 268 } 269 270 XMLUtil.printIndent(out, --indent); 271 out.println("</post-functions>"); 272 } 273 } 274 275 protected void printPreFunctions(PrintWriter out, int indent) { 276 if (preFunctions.size() > 0) { 277 XMLUtil.printIndent(out, indent++); 278 out.println("<pre-functions>"); 279 280 Iterator iter = preFunctions.iterator(); 281 282 while (iter.hasNext()) { 283 FunctionDescriptor function = (FunctionDescriptor) iter.next(); 284 function.writeXML(out, indent); 285 } 286 287 XMLUtil.printIndent(out, --indent); 288 out.println("</pre-functions>"); 289 } 290 } 291 } 292 | Popular Tags |