1 64 65 package com.jcorporate.expresso.core.controller; 66 67 import com.jcorporate.expresso.kernel.exception.ChainedException; 68 import org.apache.struts.Globals; 69 import org.apache.struts.action.ActionError; 70 import org.apache.struts.action.ActionErrors; 71 72 import java.io.Serializable ; 73 import java.util.Iterator ; 74 75 81 public class ErrorCollection 82 extends ActionErrors 83 implements Serializable , 84 Cloneable { 85 public static final String ERRORCOLLECTIONKEY = Globals.ERROR_KEY; 86 87 90 public ErrorCollection() { 91 super(); 92 } 93 94 101 public void addError(String errorMessage) { 102 if (!hasErrorMessage(errorMessage)) { 103 add(ActionErrors.GLOBAL_ERROR, new ActionError(errorMessage)); 104 } 105 } 106 107 114 public void addError(String errorMessage, Object errorMessageParam1) { 115 if (!hasErrorMessage(errorMessage)) { 116 add(ActionErrors.GLOBAL_ERROR, new ActionError(errorMessage, errorMessageParam1)); 117 } 118 } 119 120 121 129 public void addError(String errorMessage, Object errorMessageParam1, Object errorMessageParam2) { 130 if (!hasErrorMessage(errorMessage)) { 131 add(ActionErrors.GLOBAL_ERROR, new ActionError(errorMessage, errorMessageParam1, errorMessageParam2)); 132 } 133 } 134 135 136 145 public void addError(String errorMessage, 146 Object errorMessageParam1, 147 Object errorMessageParam2, 148 Object errorMessageParam3) { 149 if (!hasErrorMessage(errorMessage)) { 150 add(ActionErrors.GLOBAL_ERROR, 151 new ActionError(errorMessage, errorMessageParam1, errorMessageParam2, errorMessageParam3)); 152 } 153 } 154 155 160 public void addError(String errorMessage, Object [] args) { 161 if (!hasErrorMessage(errorMessage)) { 162 add(ActionErrors.GLOBAL_ERROR, new ActionError(errorMessage, args)); 163 } 164 } 165 166 167 173 private boolean hasErrorMessage(String errorMessage) { 174 String oneMessage = null; 175 176 177 ActionError oneError = null; 178 179 for (Iterator allErrors = get(); allErrors.hasNext();) { 180 oneError = (ActionError) allErrors.next(); 181 oneMessage = oneError.getKey(); 182 183 if (oneMessage.equals(errorMessage)) { 184 return true; 185 } 186 } 187 return false; 188 } 189 190 191 196 public void addError(ChainedException ex) { 197 boolean done = false; 198 199 while (!done) { 200 String msg = ex.getMessage(); 201 if (msg != null) { 202 addError(ex.getMessage()); 203 } 204 205 Throwable t = ex.getNested(); 206 207 if (t == null) { 208 done = true; 209 } else if (t instanceof ChainedException) { 210 ex = (ChainedException) t; 211 } else { 213 done = true; 216 } 217 } 218 219 } 220 221 228 public void addError(String propName, String errorMessage) { 229 add(propName, new ActionError(errorMessage)); 230 } 231 232 233 239 public int getErrorCount() { 240 return size(); 241 } 242 243 249 public Iterator getErrors() { 250 return get(); 251 } 252 253 258 public String [] getErrorStrings() { 259 if (size() == 0) { 260 return null; 261 } 262 263 String returnValue[] = new String [size()]; 264 int index = 0; 265 for (Iterator i = this.getErrors(); i.hasNext();) { 266 returnValue[index] = ((ActionError) i.next()).getKey(); 267 index++; 268 } 269 270 return returnValue; 271 } 272 273 274 277 public Iterator getErrors(String label) { 278 return get(label); 279 } 280 281 286 public void removeError(String label) { 287 for (Iterator i = get(label); i.hasNext();) { 288 i.next(); 289 i.remove(); 290 } 291 } 292 293 294 297 public Object clone() 298 throws CloneNotSupportedException { 299 ErrorCollection e; 300 301 synchronized (this) { 302 e = (ErrorCollection) super.clone(); 303 } 304 305 return e; 306 } 307 } 308 | Popular Tags |