View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts.chain.commands;
22  
23  import org.apache.struts.action.ActionErrors;
24  import org.apache.struts.action.ActionForm;
25  import org.apache.struts.action.InvalidCancelException;
26  import org.apache.struts.chain.contexts.ActionContext;
27  import org.apache.struts.config.ActionConfig;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
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$ $Date: 2005-06-04 10:58:46 -0400 (Sat, 04 Jun 2005)
37   *          $
38   */
39  public abstract class AbstractValidateActionForm extends ActionCommandBase {
40      // ------------------------------------------------------ Instance Variables
41  
42      /**
43       * The {@code Log} instance for this class.
44       */
45      private final Logger log =
46          LoggerFactory.getLogger(AbstractSelectForward.class);
47  
48      // ------------------------------------------------------ Protected Methods
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      // ---------------------------------------------------------- Public Methods
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      @Override
92      protected boolean execute_(ActionContext actionCtx)
93          throws Exception {
94          // Set form valid until found otherwise
95          actionCtx.setFormValid(Boolean.TRUE);
96  
97          // Is there a form bean for this request?
98          ActionForm actionForm = actionCtx.getActionForm();
99  
100         if (actionForm == null) {
101             return CONTINUE_PROCESSING;
102         }
103 
104         // Is validation disabled on this request?
105         ActionConfig actionConfig = actionCtx.getActionConfig();
106 
107         if (!actionConfig.getValidate()) {
108             return CONTINUE_PROCESSING;
109         }
110 
111         // Was this request cancelled?
112         if (isCancelled(actionCtx, actionConfig)) {
113             log.debug("Cancelled transaction, skipping validation");
114             return CONTINUE_PROCESSING;
115         }
116 
117         // Call the validate() method of this form bean
118         ActionErrors errors = validate(actionCtx, actionConfig, actionForm);
119 
120         // If there were no errors, proceed normally
121         if ((errors == null) || (errors.isEmpty())) {
122             return CONTINUE_PROCESSING;
123         }
124 
125         // Flag the validation failure and proceed
126         /* NOTE: Is there any concern that there might have already
127          * been errors, or that other errors might be coming?
128          */
129         actionCtx.saveErrors(errors);
130         actionCtx.setFormValid(Boolean.FALSE);
131 
132         return CONTINUE_PROCESSING;
133     }
134 
135     // ------------------------------------------------------- Protected Methods
136 
137     /**
138      * <p>Call the <code>validate()</code> method of the specified form bean,
139      * and return the resulting <code>ActionErrors</code> object.</p>
140      *
141      * @param context      The context for this request
142      * @param actionConfig The <code>ActionConfig</code> for this request
143      * @param actionForm   The form bean for this request
144      * @return ActionErrors object, if any
145      */
146     protected abstract ActionErrors validate(ActionContext context,
147         ActionConfig actionConfig, ActionForm actionForm);
148 }