1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.finalist.util.struts;
19
20 import com.finalist.util.ejb.validation.ModelError;
21 import com.finalist.util.ejb.validation.ModelErrors;
22 import org.apache.struts.action.ActionError;
23 import org.apache.struts.action.ActionErrors;
24
25 import java.util.Iterator;
26
27 /*** This class provides some static methodes that do model - struts data translations
28 *
29 * @author Gerard van de Weerd
30 * @created 26 march 2002
31 * @version
32 */
33 public class StrutsUtil {
34
35 /*** Creates an (equivalent) action error for each model error in the given model errors object, and adds them
36 * at the specified property value (in the model errors) to the action errors object.
37 */
38
39 public static void addModelErrorsToActionErrors(ModelErrors modelErrors, ActionErrors actionErrors) {
40 Iterator properties = modelErrors.properties();
41 while (properties.hasNext()) {
42 String currentPropName = (String) properties.next();
43 Iterator modelErrorIter = modelErrors.get(currentPropName);
44 if (currentPropName.equals(ModelErrors.GLOBAL_ERROR)) {
45 currentPropName = ActionErrors.GLOBAL_ERROR;
46 }
47
48 while (modelErrorIter.hasNext()) {
49 ModelError currentModelError = (ModelError) modelErrorIter.next();
50 actionErrors.add(currentPropName, createActionError(currentModelError));
51 }
52 }
53 }
54
55
56 /*** Creates an (equivalent) action errors object for the given model errors object.
57 */
58 public static ActionErrors createActionErrorsFromModelErrors(ModelErrors modelErrors) {
59 ActionErrors actionErrors = new ActionErrors();
60 addModelErrorsToActionErrors(modelErrors, actionErrors);
61 return actionErrors;
62 }
63
64
65 /*** Creates an (equivalent) action error object for the given model error object. */
66 public static ActionError createActionError(ModelError modelError) {
67 Object[] params = modelError.getValues();
68 if (params == null || params.length == 0)
69 return new ActionError(modelError.getKey());
70 switch (params.length) {
71 case 1:
72 return new ActionError(modelError.getKey(), params[0]);
73 case 2:
74 return new ActionError(modelError.getKey(), params[0], params[1]);
75 case 3:
76 return new ActionError(modelError.getKey(), params[0], params[1], params[2]);
77 case 4:
78 return new ActionError(modelError.getKey(), params[0], params[1], params[2], params[3]);
79 default:
80 throw new Error("Passed too many parameters: " + params.length + ". Maximum is 4.");
81 }
82 }
83
84
85 }