Package org.strecks.action.basic

Source Code of org.strecks.action.basic.BasicLookupDispatchController

/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package org.strecks.action.basic;

import java.lang.reflect.Method;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.actions.LookupDispatchAction;
import org.strecks.action.BasicSubmitAction;
import org.strecks.context.ActionContext;
import org.strecks.controller.LookupDispatchActionController;
import org.strecks.controller.annotation.ActionInterface;
import org.strecks.dispatch.annotation.ReadDispatchLookups;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.form.controller.BindingForm;
import org.strecks.util.ReflectHelper;
import org.strecks.view.ViewAdapter;

/**
* Form-submit action controller which mimics the behaviour of <code>LookupDispatchAction</code>. Internally,
* actually uses an instance of <code>LookupDispatchAction</code> to do the complex stuff of finding the method name
* to execute for a possibly locale variant key. Action beans which use this controller must implement the
* <code>BasicSubmitAction</code> interface
* @see org.strecks.action.BasicSubmitAction
* @author Phil Zoio
*/
@ActionInterface(name = BasicSubmitAction.class)
@ReadDispatchLookups
public class BasicLookupDispatchController extends BasicDispatchController implements LookupDispatchActionController
{

  private Map<String, String> keyMethodMap;

  private LookupDispatchActionDelegate delegate;

  public BasicLookupDispatchController()
  {
    super();
    setDispatchActionDelegate(new LookupDispatchActionDelegate());
  }

  @Override
  protected ViewAdapter executeAction(Object actionBean, ActionContext context)
  {

    BasicSubmitAction action = (BasicSubmitAction) actionBean;

    ActionForm form = context.getForm();

    HttpServletRequest request = context.getRequest();

    boolean cancelled = false;
    if (request.getAttribute(Globals.CANCEL_KEY) != null)
    {
      cancelled = true;
    }

    if (form instanceof BindingForm && !cancelled)
    {

      action.preBind();
      BindingForm validBindingForm = (BindingForm) form;
      validBindingForm.bindInwards(actionBean);

    }

    ActionMapping mapping = context.getMapping();
    String result = null;

    if (cancelled)
    {
      result = action.cancel();
    }
    else
    {

      // now figure out what method to execute and do so

      String parameterName = mapping.getParameter();

      getHelper().checkParameterName(mapping, parameterName);

      String methodName = getMethodName(context, parameterName);

      if (methodName == null)
      {
        result = action.execute();
      }
      else
      {
        Method method = getHelper().getMethod(actionBean, methodName);
        result = ReflectHelper.invokeMethod(actionBean, method, String.class);
      }

    }

    return getActionForward(context, result);

  }

  public void setKeyMethodMap(Map<String, String> keyMethodMap)
  {
    this.keyMethodMap = keyMethodMap;
  }

  /*
   * ************************************* implementation details ***********************************
   */

  protected void setDispatchActionDelegate(LookupDispatchActionDelegate delegate)
  {
    this.delegate = delegate;
  }

  protected String getMethodName(ActionContext context, String parameterName)
  {
    try
    {
      return delegate.getMethodName(context.getMapping(), context.getForm(), context.getRequest(), context
          .getResponse(), parameterName);
    }
    catch (Exception e)
    {
      throw new ApplicationRuntimeException(e);
    }
  }

  protected Map<String, String> internalGetKeyMethodMap()
  {
    return keyMethodMap;
  }

  protected class LookupDispatchActionDelegate extends LookupDispatchAction
  {

    @Override
    protected Map getKeyMethodMap()
    {
      return internalGetKeyMethodMap();
    }

    @Override
    public String getMethodName(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response, String parameter) throws Exception
    {
      return super.getMethodName(mapping, form, request, response, parameter);
    }

  };

  @Override
  public void setServlet(ActionServlet servlet)
  {
    super.setServlet(servlet);
    delegate.setServlet(servlet);
  }

}
TOP

Related Classes of org.strecks.action.basic.BasicLookupDispatchController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.
ts)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-20639858-1', 'auto'); ga('send', 'pageview');