1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.chain.commands;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.struts.action.ActionErrors;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.InvalidCancelException;
28 import org.apache.struts.chain.contexts.ActionContext;
29 import org.apache.struts.config.ActionConfig;
30
31 /**
32 * <p>Validate the properties of the form bean for this request. If there are
33 * any validation errors, execute the specified command; otherwise, proceed
34 * normally.</p>
35 *
36 * @version $Rev: 481833 $ $Date: 2005-06-04 10:58:46 -0400 (Sat, 04 Jun 2005)
37 * $
38 */
39 public abstract class AbstractValidateActionForm extends ActionCommandBase {
40
41
42 /**
43 * <p> Provide Commons Logging instance for this class. </p>
44 */
45 private static final Log LOG =
46 LogFactory.getLog(AbstractSelectForward.class);
47
48
49
50 /**
51 * <p>Helper method to verify the Cancel state.</p>
52 *
53 * <p>If the state is invalid, Cancel is unset and an
54 * InvalidCancelException is thrown.</p>
55 *
56 * @param actionCtx Our ActionContext
57 * @param actionConfig Our ActionConfig
58 * @return true if cancel is set, false otherwise.
59 * @throws InvalidCancelException
60 */
61 private boolean isCancelled(ActionContext actionCtx,
62 ActionConfig actionConfig)
63 throws InvalidCancelException {
64 Boolean cancel = actionCtx.getCancelled();
65 boolean cancelled = ((cancel != null) && cancel.booleanValue());
66 boolean cancellable = actionConfig.getCancellable();
67
68 boolean invalidState = (cancelled && !cancellable);
69
70 if (invalidState) {
71 actionCtx.setCancelled(Boolean.FALSE);
72 actionCtx.setFormValid(Boolean.FALSE);
73 throw new InvalidCancelException();
74 }
75
76 return cancelled;
77 }
78
79
80
81 /**
82 * <p>Validate the properties of the form bean for this request. If there
83 * are any validation errors, execute the child commands in our chain;
84 * otherwise, proceed normally.</p>
85 *
86 * @param actionCtx The <code>Context</code> for the current request
87 * @return <code>false</code> so that processing continues, if there are
88 * no validation errors; otherwise <code>true</code>
89 * @throws Exception if thrown by the Action class
90 */
91 public boolean execute(ActionContext actionCtx)
92 throws Exception {
93
94 actionCtx.setFormValid(Boolean.TRUE);
95
96
97 ActionForm actionForm = actionCtx.getActionForm();
98
99 if (actionForm == null) {
100 return false;
101 }
102
103
104 ActionConfig actionConfig = actionCtx.getActionConfig();
105
106 if (!actionConfig.getValidate()) {
107 return false;
108 }
109
110
111 if (isCancelled(actionCtx, actionConfig)) {
112 if (LOG.isDebugEnabled()) {
113 LOG.debug(" Cancelled transaction, skipping validation");
114 }
115
116 return false;
117 }
118
119
120 ActionErrors errors = validate(actionCtx, actionConfig, actionForm);
121
122
123 if ((errors == null) || (errors.isEmpty())) {
124 return false;
125 }
126
127
128
129
130
131 actionCtx.saveErrors(errors);
132 actionCtx.setFormValid(Boolean.FALSE);
133
134 return false;
135 }
136
137
138
139 /**
140 * <p>Call the <code>validate()</code> method of the specified form bean,
141 * and return the resulting <code>ActionErrors</code> object.</p>
142 *
143 * @param context The context for this request
144 * @param actionConfig The <code>ActionConfig</code> for this request
145 * @param actionForm The form bean for this request
146 * @return ActionErrors object, if any
147 */
148 protected abstract ActionErrors validate(ActionContext context,
149 ActionConfig actionConfig, ActionForm actionForm);
150 }