View Javadoc

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