Package org.apache.beehive.netui.script.common

Source Code of org.apache.beehive.netui.script.common.ImplicitObjectUtil

/*
* Copyright 2004 The Apache Software Foundation.
*
* 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.
*
* $Header:$
*/
package org.apache.beehive.netui.script.common;

import java.util.Map;
import java.util.Collections;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.VariableResolver;

import org.apache.beehive.netui.pageflow.FacesBackingBean;
import org.apache.beehive.netui.pageflow.GlobalApp;
import org.apache.beehive.netui.pageflow.PageFlowController;
import org.apache.beehive.netui.pageflow.PageFlowUtils;
import org.apache.beehive.netui.pageflow.SharedFlowController;
import org.apache.beehive.netui.pageflow.internal.AnyBeanActionForm;
import org.apache.beehive.netui.pageflow.internal.InternalUtils;
import org.apache.beehive.netui.pageflow.internal.FacesBackingBeanFactory;
import org.apache.beehive.netui.script.el.NetUIUpdateVariableResolver;
import org.apache.beehive.netui.util.logging.Logger;

/**
*
*/
public class ImplicitObjectUtil {

    private static final Logger LOGGER = Logger.getInstance(ImplicitObjectUtil.class);

    private static final String PAGE_FLOW_IMPLICIT_OBJECT_KEY = "pageFlow";
    private static final String SHARED_FLOW_IMPLICIT_OBJECT_KEY = "sharedFlow";
    private static final String GLOBAL_APP_IMPLICIT_OBJECT_KEY = "globalApp";
    private static final String BUNDLE_IMPLICIT_OBJECT_KEY = "bundle";
    private static final String BACKING_IMPLICIT_OBJECT_KEY = "backing";
    private static final String PAGE_INPUT_IMPLICIT_OBJECT_KEY = "pageInput";
    private static final String ACTION_FORM_IMPLICIT_OBJECT_KEY = "actionForm";
    private static final String OUTPUT_FORM_BEAN_OBJECT_KEY = "outputFormBean";

    /* do not construct */
    private ImplicitObjectUtil() {}

    public static final void loadActionForm(JspContext jspContext, Object form) {
        jspContext.setAttribute(ACTION_FORM_IMPLICIT_OBJECT_KEY, unwrapForm(form));
    }

    public static final void unloadActionForm(JspContext jspContext) {
        jspContext.removeAttribute(ACTION_FORM_IMPLICIT_OBJECT_KEY);
    }

    public static final void loadPageFlow(ServletRequest request, PageFlowController pageFlow, FacesBackingBean fbb) {
        if(pageFlow != null)
            request.setAttribute(PAGE_FLOW_IMPLICIT_OBJECT_KEY, pageFlow);
        if(fbb != null)
            request.setAttribute(BACKING_IMPLICIT_OBJECT_KEY, fbb);

        Map map = InternalUtils.getPageInputMap(request);
        request.setAttribute(PAGE_INPUT_IMPLICIT_OBJECT_KEY, map != null ? map : Collections.EMPTY_MAP);
    }

    public static final void loadSharedFlow(ServletRequest request, Map/*<String, SharedFlowController>*/ sharedFlows) {
        if(sharedFlows != null)
            request.setAttribute(SHARED_FLOW_IMPLICIT_OBJECT_KEY, sharedFlows);
    }

    public static final void loadGlobalApp(ServletRequest request, GlobalApp globalApp) {
        if(globalApp != null)
            request.setAttribute(GLOBAL_APP_IMPLICIT_OBJECT_KEY, globalApp);
    }

    public static final void loadBundleMap(ServletRequest servletRequest, BundleMap bundleMap) {
        servletRequest.setAttribute(BUNDLE_IMPLICIT_OBJECT_KEY, bundleMap);
    }

    public static final Object unwrapForm(Object form) {
        if(LOGGER.isDebugEnabled() && form instanceof AnyBeanActionForm)
            LOGGER.debug("using form of type: " + ((AnyBeanActionForm)form != null ? ((AnyBeanActionForm)form).getClass().getName() : "null"));

        if(form instanceof AnyBeanActionForm)
            return ((AnyBeanActionForm)form).getBean();
        else
            return form;
    }

    public static final Map/*<String, SharedFlowController>*/ getSharedFlow(ServletRequest request) {
        return (Map/*<String, SharedFlowController>*/)request.getAttribute(SHARED_FLOW_IMPLICIT_OBJECT_KEY);
    }

    public static final PageFlowController getPageFlow(ServletRequest request, ServletResponse response) {
        assert request instanceof HttpServletRequest;

        PageFlowController jpf = PageFlowUtils.getCurrentPageFlow((HttpServletRequest)request);
        if(jpf != null)
            return jpf;
        else {
            // @todo: i18n
            RuntimeException re = new RuntimeException("There is no current PageFlow for the expression.");
            if(LOGGER.isErrorEnabled()) LOGGER.error("", re);
            throw re;
        }
    }

    public static final GlobalApp getGlobalApp(ServletRequest request, ServletResponse response) {
        assert request instanceof HttpServletRequest;

        GlobalApp ga = PageFlowUtils.getGlobalApp((HttpServletRequest)request);

        if(ga == null) {
            // @todo: i18n
            RuntimeException re = new RuntimeException("Can not create the globalApp binding context; the GlobalApp object is null.");
            if(LOGGER.isErrorEnabled()) LOGGER.error("", re);
            throw re;
        }

        return ga;
    }

    /**
     *
     */
    public static final VariableResolver getUpdateVariableResolver(ServletRequest request, ServletResponse response, boolean isHandlingPost) {
        Object form = ImplicitObjectUtil.unwrapForm(request.getAttribute(org.apache.struts.taglib.html.Constants.BEAN_KEY));

        /* todo: need to provide get(Read|Update)VariableResolver methods on the ExpressionEngineFactory */
        return new NetUIUpdateVariableResolver(form, request, response, isHandlingPost);
    }

    public static final VariableResolver getUpdateVariableResolver(Object form, ServletRequest request, ServletResponse response, boolean isHandlingPost) {
        Object realForm = ImplicitObjectUtil.unwrapForm(form);

        /* todo: need to provide get(Read|Update)VariableResolver methods on the ExpressionEngineFactory */
        return new NetUIUpdateVariableResolver(realForm, request, response, isHandlingPost);
    }

    public static final VariableResolver getReadVariableResolver(PageContext pageContext) {
        assert pageContext != null;
        return pageContext.getVariableResolver();
    }

    public static final void loadImplicitObjects(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, PageFlowController curJpf) {
        // @todo: need to wrap this in checks for JSP 1.2
        // @todo: feature: need to add support for chaining in user-code to run when setting implicit objects on the request
        FacesBackingBean fbb =
            FacesBackingBeanFactory.getFacesBackingBeanForRequest(request, response, servletContext);
        loadPageFlow(request, curJpf, fbb);
       
        // @todo: need to move bundleMap creation to a BundleMapFactory
        BundleMap bundleMap = new BundleMap(request, servletContext);
        loadBundleMap(request, bundleMap);
    }

    public static final void loadOutputFormBean(ServletRequest request, Object bean) {
        if(bean != null)
            request.setAttribute(OUTPUT_FORM_BEAN_OBJECT_KEY, bean);
    }
}
TOP

Related Classes of org.apache.beehive.netui.script.common.ImplicitObjectUtil

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.