/**
   * This method fixes a problem with {@link UIComponent#findComponent(String)} where sometimes it
   * is unable to find components due to incorrect clientId values. For more info, see the following
   * issues:
   *
   * <ul>
   *   <li>http://issues.liferay.com/browse/FACES-198
   *   <li>http://jira.icesoft.org/browse/ICE-6659
   *   <li>http://jira.icesoft.org/browse/ICE-6667
   * </ul>
   */
  @Override
  public void setId(String id) {

    if (BridgeUtil.isPortletRequest()) {
      super.setId(getContainerClientId(FacesContext.getCurrentInstance()));
    }
  }
  /**
   * This method provides the ability to supply an instance of the bridge API's {@link
   * PortletNamingContainerUIViewRoot} class which properly handles namespacing of "id" attributes
   * for portlets.
   *
   * @see Application#createComponent(String)
   */
  @Override
  public UIComponent createComponent(String componentType) throws FacesException {

    if (componentType.equals(UIViewRoot.COMPONENT_TYPE) && BridgeUtil.isPortletRequest()) {
      return new UIViewRootBridgeImpl();
    } else {
      return wrappedApplication.createComponent(componentType);
    }
  }
  public void beforePhase(PhaseEvent phaseEvent) {

    Bridge.PortletPhase portletRequestPhase = BridgeUtil.getPortletRequestPhase();

    if ((portletRequestPhase == Bridge.PortletPhase.RENDER_PHASE)
        || (portletRequestPhase == Bridge.PortletPhase.RESOURCE_PHASE)) {

      // If about to execute the INVOKE_APPLICATION phase of the JSF lifecycle, then
      if (phaseEvent.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
        beforeInvokeApplicationPhase(phaseEvent);
      } else if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE) {
        beforeRenderResponsePhase(phaseEvent);
      }
    }
  }
  public void beforePhase(PhaseEvent phaseEvent) {

    Bridge.PortletPhase portletRequestPhase =
        BridgeUtil.getPortletRequestPhase(phaseEvent.getFacesContext());

    if ((portletRequestPhase == Bridge.PortletPhase.RESOURCE_PHASE)
        || (portletRequestPhase == Bridge.PortletPhase.RENDER_PHASE)) {

      if (phaseEvent.getPhaseId() == PhaseId.APPLY_REQUEST_VALUES) {
        beforeApplyRequestValuesPhase(phaseEvent);
      } else if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE) {
        FacesContext facesContext = phaseEvent.getFacesContext();

        if (facesContext.getPartialViewContext().isAjaxRequest()) {
          beforeAjaxifiedRenderResponsePhase(phaseEvent);
        }
      }
    }
  }
  @Override
  public Object getValue(ELContext context, Object base, Object property) throws ELException {
    // variable resolution is a special case of property resolution
    // where the base is null.
    if (!BridgeUtil.isPortletRequest() || base != null) {
      return null;
    }
    if (property == null) {
      throw new PropertyNotFoundException("Null property");
    }

    FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
    ExternalContext extCtx = facesContext.getExternalContext();

    // only process if running in a portlet request
    // Bridge.PortletPhase phase =
    // (Bridge.PortletPhase)
    // FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(Bridge.PORTLET_LIFECYCLE_PHASE);
    // if (phase == null) {
    // return null;
    // }

    if (PORTLET_CONFIG.equals(property)) {
      context.setPropertyResolved(true);
      return context.getContext(PortletConfig.class);
    } else if (ACTION_REQUEST.equals(property)
        && (BridgeUtil.getPortletRequestPhase() == Bridge.PortletPhase.ACTION_PHASE)) {
      context.setPropertyResolved(true);
      return extCtx.getRequest();
    } else if (ACTION_RESPONSE.equals(property)
        && (BridgeUtil.getPortletRequestPhase() == Bridge.PortletPhase.ACTION_PHASE)) {
      context.setPropertyResolved(true);
      return extCtx.getResponse();
    } else if (RENDER_REQUEST.equals(property)
        && (BridgeUtil.getPortletRequestPhase() == Bridge.PortletPhase.RENDER_PHASE)) {
      context.setPropertyResolved(true);
      return extCtx.getRequest();
    } else if (RENDER_RESPONSE.equals(property)
        && (BridgeUtil.getPortletRequestPhase() == Bridge.PortletPhase.RENDER_PHASE)) {
      context.setPropertyResolved(true);
      return extCtx.getResponse();
    } else if (EVENT_REQUEST.equals(property)
        && (BridgeUtil.getPortletRequestPhase() == Bridge.PortletPhase.EVENT_PHASE)) {
      context.setPropertyResolved(true);
      return extCtx.getRequest();
    } else if (EVENT_RESPONSE.equals(property)
        && (BridgeUtil.getPortletRequestPhase() == Bridge.PortletPhase.EVENT_PHASE)) {
      context.setPropertyResolved(true);
      return extCtx.getResponse();
    } else if (RESOURCE_REQUEST.equals(property)
        && (BridgeUtil.getPortletRequestPhase() == Bridge.PortletPhase.RESOURCE_PHASE)) {
      context.setPropertyResolved(true);
      return extCtx.getRequest();
    } else if (RESOURCE_RESPONSE.equals(property)
        && (BridgeUtil.getPortletRequestPhase() == Bridge.PortletPhase.RESOURCE_PHASE)) {
      context.setPropertyResolved(true);
      return extCtx.getResponse();
    } else if (SESSION_APPLICATION_SCOPE.equals(property) || HTTP_SESSION_SCOPE.equals(property)) {
      context.setPropertyResolved(true);
      if (mAppScopeSessionMap == null) {
        Object request = extCtx.getRequest();
        // Object portletLifecycleAttr = extCtx.getRequestMap().get(Bridge.PORTLET_LIFECYCLE_PHASE);
        if (BridgeUtil.isPortletRequest()) {
          mAppScopeSessionMap = new PortletApplicationScopeSessionMap((PortletRequest) request);
        }
      }
      return mAppScopeSessionMap;
    } else if (SESSION_PORTLET_SCOPE.equals(property) || PORTLET_SESSION_SCOPE.equals(property)) {
      context.setPropertyResolved(true);
      return extCtx.getSessionMap();
    } else if (PORTLET_SESSION.equals(property)) {
      context.setPropertyResolved(true);
      return extCtx.getSession(false);
    } else if (PORTLET_PREFERENCE_VALUE.equals(property)) {
      context.setPropertyResolved(true);
      return getPreferencesValueMap(facesContext);
    } else if (PORTLET_PREFERENCES.equals(property)) {
      context.setPropertyResolved(true);
      return ((PortletRequest) extCtx.getRequest()).getPreferences();
    } else if (MUTABLE_PORTLET_PREFERENCES_VALUES.equals(property)) {
      context.setPropertyResolved(true);
      return getPreferenceMap(((PortletRequest) extCtx.getRequest()).getPreferences());
    } else {
      return null;
    }
    // }
  }