001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.action.basic;
016:
017: import java.lang.reflect.Method;
018: import java.util.Map;
019:
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import org.apache.struts.Globals;
024: import org.apache.struts.action.ActionForm;
025: import org.apache.struts.action.ActionMapping;
026: import org.apache.struts.action.ActionServlet;
027: import org.apache.struts.actions.LookupDispatchAction;
028: import org.strecks.action.BasicSubmitAction;
029: import org.strecks.context.ActionContext;
030: import org.strecks.controller.LookupDispatchActionController;
031: import org.strecks.controller.annotation.ActionInterface;
032: import org.strecks.dispatch.annotation.ReadDispatchLookups;
033: import org.strecks.exceptions.ApplicationRuntimeException;
034: import org.strecks.form.controller.BindingForm;
035: import org.strecks.util.ReflectHelper;
036: import org.strecks.view.ViewAdapter;
037:
038: /**
039: * Form-submit action controller which mimics the behaviour of <code>LookupDispatchAction</code>. Internally,
040: * actually uses an instance of <code>LookupDispatchAction</code> to do the complex stuff of finding the method name
041: * to execute for a possibly locale variant key. Action beans which use this controller must implement the
042: * <code>BasicSubmitAction</code> interface
043: * @see org.strecks.action.BasicSubmitAction
044: * @author Phil Zoio
045: */
046: @ActionInterface(name=BasicSubmitAction.class)
047: @ReadDispatchLookups
048: public class BasicLookupDispatchController extends
049: BasicDispatchController implements
050: LookupDispatchActionController {
051:
052: private Map<String, String> keyMethodMap;
053:
054: private LookupDispatchActionDelegate delegate;
055:
056: public BasicLookupDispatchController() {
057: super ();
058: setDispatchActionDelegate(new LookupDispatchActionDelegate());
059: }
060:
061: @Override
062: protected ViewAdapter executeAction(Object actionBean,
063: ActionContext context) {
064:
065: BasicSubmitAction action = (BasicSubmitAction) actionBean;
066:
067: ActionForm form = context.getForm();
068:
069: HttpServletRequest request = context.getRequest();
070:
071: boolean cancelled = false;
072: if (request.getAttribute(Globals.CANCEL_KEY) != null) {
073: cancelled = true;
074: }
075:
076: if (form instanceof BindingForm && !cancelled) {
077:
078: action.preBind();
079: BindingForm validBindingForm = (BindingForm) form;
080: validBindingForm.bindInwards(actionBean);
081:
082: }
083:
084: ActionMapping mapping = context.getMapping();
085: String result = null;
086:
087: if (cancelled) {
088: result = action.cancel();
089: } else {
090:
091: // now figure out what method to execute and do so
092:
093: String parameterName = mapping.getParameter();
094:
095: getHelper().checkParameterName(mapping, parameterName);
096:
097: String methodName = getMethodName(context, parameterName);
098:
099: if (methodName == null) {
100: result = action.execute();
101: } else {
102: Method method = getHelper().getMethod(actionBean,
103: methodName);
104: result = ReflectHelper.invokeMethod(actionBean, method,
105: String.class);
106: }
107:
108: }
109:
110: return getActionForward(context, result);
111:
112: }
113:
114: public void setKeyMethodMap(Map<String, String> keyMethodMap) {
115: this .keyMethodMap = keyMethodMap;
116: }
117:
118: /*
119: * ************************************* implementation details ***********************************
120: */
121:
122: protected void setDispatchActionDelegate(
123: LookupDispatchActionDelegate delegate) {
124: this .delegate = delegate;
125: }
126:
127: protected String getMethodName(ActionContext context,
128: String parameterName) {
129: try {
130: return delegate.getMethodName(context.getMapping(), context
131: .getForm(), context.getRequest(), context
132: .getResponse(), parameterName);
133: } catch (Exception e) {
134: throw new ApplicationRuntimeException(e);
135: }
136: }
137:
138: protected Map<String, String> internalGetKeyMethodMap() {
139: return keyMethodMap;
140: }
141:
142: protected class LookupDispatchActionDelegate extends
143: LookupDispatchAction {
144:
145: @Override
146: protected Map getKeyMethodMap() {
147: return internalGetKeyMethodMap();
148: }
149:
150: @Override
151: public String getMethodName(ActionMapping mapping,
152: ActionForm form, HttpServletRequest request,
153: HttpServletResponse response, String parameter)
154: throws Exception {
155: return super .getMethodName(mapping, form, request,
156: response, parameter);
157: }
158:
159: };
160:
161: @Override
162: public void setServlet(ActionServlet servlet) {
163: super.setServlet(servlet);
164: delegate.setServlet(servlet);
165: }
166:
167: }
|