001: /*
002: * $Id: AbstractValidateActionForm.java 481833 2006-12-03 17:32:52Z niallp $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.chain.commands;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.struts.action.ActionErrors;
026: import org.apache.struts.action.ActionForm;
027: import org.apache.struts.action.InvalidCancelException;
028: import org.apache.struts.chain.contexts.ActionContext;
029: import org.apache.struts.config.ActionConfig;
030:
031: /**
032: * <p>Validate the properties of the form bean for this request. If there are
033: * any validation errors, execute the specified command; otherwise, proceed
034: * normally.</p>
035: *
036: * @version $Rev: 481833 $ $Date: 2005-06-04 10:58:46 -0400 (Sat, 04 Jun 2005)
037: * $
038: */
039: public abstract class AbstractValidateActionForm extends
040: ActionCommandBase {
041: // ------------------------------------------------------ Instance Variables
042:
043: /**
044: * <p> Provide Commons Logging instance for this class. </p>
045: */
046: private static final Log LOG = LogFactory
047: .getLog(AbstractSelectForward.class);
048:
049: // ------------------------------------------------------ Protected Methods
050:
051: /**
052: * <p>Helper method to verify the Cancel state.</p>
053: *
054: * <p>If the state is invalid, Cancel is unset and an
055: * InvalidCancelException is thrown.</p>
056: *
057: * @param actionCtx Our ActionContext
058: * @param actionConfig Our ActionConfig
059: * @return true if cancel is set, false otherwise.
060: * @throws InvalidCancelException
061: */
062: private boolean isCancelled(ActionContext actionCtx,
063: ActionConfig actionConfig) throws InvalidCancelException {
064: Boolean cancel = actionCtx.getCancelled();
065: boolean cancelled = ((cancel != null) && cancel.booleanValue());
066: boolean cancellable = actionConfig.getCancellable();
067:
068: boolean invalidState = (cancelled && !cancellable);
069:
070: if (invalidState) {
071: actionCtx.setCancelled(Boolean.FALSE);
072: actionCtx.setFormValid(Boolean.FALSE);
073: throw new InvalidCancelException();
074: }
075:
076: return cancelled;
077: }
078:
079: // ---------------------------------------------------------- Public Methods
080:
081: /**
082: * <p>Validate the properties of the form bean for this request. If there
083: * are any validation errors, execute the child commands in our chain;
084: * otherwise, proceed normally.</p>
085: *
086: * @param actionCtx The <code>Context</code> for the current request
087: * @return <code>false</code> so that processing continues, if there are
088: * no validation errors; otherwise <code>true</code>
089: * @throws Exception if thrown by the Action class
090: */
091: public boolean execute(ActionContext actionCtx) throws Exception {
092: // Set form valid until found otherwise
093: actionCtx.setFormValid(Boolean.TRUE);
094:
095: // Is there a form bean for this request?
096: ActionForm actionForm = actionCtx.getActionForm();
097:
098: if (actionForm == null) {
099: return false;
100: }
101:
102: // Is validation disabled on this request?
103: ActionConfig actionConfig = actionCtx.getActionConfig();
104:
105: if (!actionConfig.getValidate()) {
106: return false;
107: }
108:
109: // Was this request cancelled?
110: if (isCancelled(actionCtx, actionConfig)) {
111: if (LOG.isDebugEnabled()) {
112: LOG
113: .debug(" Cancelled transaction, skipping validation");
114: }
115:
116: return false;
117: }
118:
119: // Call the validate() method of this form bean
120: ActionErrors errors = validate(actionCtx, actionConfig,
121: actionForm);
122:
123: // If there were no errors, proceed normally
124: if ((errors == null) || (errors.isEmpty())) {
125: return false;
126: }
127:
128: // Flag the validation failure and proceed
129: /* NOTE: Is there any concern that there might have already
130: * been errors, or that other errors might be coming?
131: */
132: actionCtx.saveErrors(errors);
133: actionCtx.setFormValid(Boolean.FALSE);
134:
135: return false;
136: }
137:
138: // ------------------------------------------------------- Protected Methods
139:
140: /**
141: * <p>Call the <code>validate()</code> method of the specified form bean,
142: * and return the resulting <code>ActionErrors</code> object.</p>
143: *
144: * @param context The context for this request
145: * @param actionConfig The <code>ActionConfig</code> for this request
146: * @param actionForm The form bean for this request
147: * @return ActionErrors object, if any
148: */
149: protected abstract ActionErrors validate(ActionContext context,
150: ActionConfig actionConfig, ActionForm actionForm);
151: }
|