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.navigable;
016:
017: import java.lang.reflect.Method;
018: import java.util.Map;
019:
020: import javax.servlet.http.HttpServletRequest;
021:
022: import org.apache.struts.Globals;
023: import org.apache.struts.action.ActionForm;
024: import org.apache.struts.action.ActionMapping;
025: import org.strecks.action.NavigableSubmitAction;
026: import org.strecks.action.basic.BasicLookupDispatchController;
027: import org.strecks.context.ActionContext;
028: import org.strecks.controller.NavigableControllerAction;
029: import org.strecks.controller.annotation.ActionInterface;
030: import org.strecks.dispatch.annotation.ReadDispatchLookups;
031: import org.strecks.form.controller.BindingForm;
032: import org.strecks.navigate.NavigateUtils;
033: import org.strecks.navigate.NavigationHolder;
034: import org.strecks.navigate.annotation.ReadNavigation;
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>NavigableSubmitAction</code> interface
043: * @see org.strecks.action.NavigableSubmitAction
044: * @author Phil Zoio
045: */
046: @ActionInterface(name=NavigableSubmitAction.class)
047: @ReadDispatchLookups
048: @ReadNavigation
049: public class NavigableLookupDispatchController extends
050: BasicLookupDispatchController implements
051: NavigableControllerAction {
052:
053: private NavigationHolder navigationHolder;
054:
055: /* *********************** NavigableControllerAction methods ************************ */
056:
057: public NavigableLookupDispatchController() {
058: super ();
059: }
060:
061: public void setNavigationHolder(NavigationHolder holder) {
062: this .navigationHolder = holder;
063: }
064:
065: public NavigationHolder getNavigationHolder() {
066: return navigationHolder;
067: }
068:
069: @Override
070: protected ViewAdapter executeAction(Object actionBean,
071: ActionContext context) {
072:
073: NavigableSubmitAction action = (NavigableSubmitAction) actionBean;
074:
075: ActionForm form = context.getForm();
076:
077: HttpServletRequest request = context.getRequest();
078:
079: boolean cancelled = false;
080: if (request.getAttribute(Globals.CANCEL_KEY) != null) {
081: cancelled = true;
082: }
083:
084: if (form instanceof BindingForm && !cancelled) {
085:
086: action.preBind();
087: BindingForm validBindingForm = (BindingForm) form;
088: validBindingForm.bindInwards(actionBean);
089:
090: }
091:
092: ActionMapping mapping = context.getMapping();
093:
094: if (cancelled) {
095: action.cancel();
096: } else {
097:
098: // now figure out what method to execute and do so
099:
100: String parameterName = mapping.getParameter();
101:
102: getHelper().checkParameterName(mapping, parameterName);
103:
104: String methodName = getMethodName(context, parameterName);
105:
106: if (methodName == null) {
107: action.execute();
108: } else {
109: Method method = getHelper().getMethod(actionBean,
110: methodName);
111: ReflectHelper.invokeMethod(actionBean, method,
112: String.class);
113: }
114:
115: }
116:
117: return NavigateUtils.findActionForward(navigationHolder,
118: actionBean, context);
119:
120: }
121:
122: @Override
123: protected Map<String, String> internalGetKeyMethodMap() {
124: return super.internalGetKeyMethodMap();
125: }
126:
127: }
|