private static String getWindowStateOrDefault(UpdateNavigationalStateResponse navResponse) {
   org.gatein.pc.api.WindowState state = navResponse.getWindowState();
   if (state == null) {
     state = org.gatein.pc.api.WindowState.NORMAL;
   }
   return state.toString();
 }
 private static String getPortletModeOrDefault(UpdateNavigationalStateResponse navResponse) {
   Mode mode = navResponse.getMode();
   if (mode == null) {
     mode = Mode.VIEW;
   }
   return mode.toString();
 }
  /**
   * This method is called when the javax.portlet.Event is supported by the current portlet stored
   * in the Portlet Caontainer
   *
   * <p>The processEvent() method can also generates IPC events and hence the portal itself will
   * call the ProcessEventsActionListener once again
   */
  public static <S, C extends Serializable, I> List<javax.portlet.Event> processEvent(
      UIPortlet<S, C> uiPortlet, javax.portlet.Event event) {
    log.trace("Process Event: " + event.getName() + " for portlet: " + uiPortlet.getState());
    try {
      PortalRequestContext context =
          (PortalRequestContext) WebuiRequestContext.getCurrentInstance();

      //
      EventInvocation eventInvocation = uiPortlet.create(EventInvocation.class, context);

      //
      eventInvocation.setName(event.getQName());
      eventInvocation.setPayload(event.getValue());

      //
      PortletInvocationResponse piResponse = uiPortlet.invoke(eventInvocation);

      //
      ExoPortletInstanceContext instanceCtx =
          (ExoPortletInstanceContext) eventInvocation.getInstanceContext();
      if (instanceCtx.getModifiedContext() != null) {
        StatefulPortletContext<C> updatedCtx =
            (StatefulPortletContext<C>) instanceCtx.getModifiedContext();
        C portletState = updatedCtx.getState();
        uiPortlet.update(portletState);
      }

      // todo: handle the error response better than this.
      if (!(piResponse instanceof UpdateNavigationalStateResponse)) {
        if (piResponse instanceof ErrorResponse) {
          ErrorResponse errorResponse = (ErrorResponse) piResponse;
          throw (Exception) errorResponse.getCause();
        } else {
          throw new Exception(
              "Unexpected response type ["
                  + piResponse
                  + "]. Expected a UpdateNavigationResponse or an ErrorResponse.");
        }
      }

      UpdateNavigationalStateResponse navResponse = (UpdateNavigationalStateResponse) piResponse;

      //

      /*
       * Update the portlet window state according to the action output
       * information
       *
       * If the current node is displaying a usual layout page, also tells the
       * page which portlet to render or not when the state is maximized
       */
      WindowState state = new WindowState(getWindowStateOrDefault(navResponse));
      setNextState(uiPortlet, state);

      // update the portlet with the next mode to display
      PortletMode mode = new PortletMode(getPortletModeOrDefault(navResponse));
      setNextMode(uiPortlet, mode);

      StateString navState = navResponse.getNavigationalState();
      if (navState != null) {
        uiPortlet.setNavigationalState(navResponse.getNavigationalState());
      }
      setupPublicRenderParams(uiPortlet, navResponse.getPublicNavigationalStateUpdates());

      // TODO: (mwringe) add this to the UpdateNavigationStateResponse.Event class instead of here
      class PortletEvent implements javax.portlet.Event {
        QName qName;

        Serializable value;

        public PortletEvent(QName qName, Serializable value) {
          this.qName = qName;
          this.value = value;
        }

        public String getName() {
          return qName.getLocalPart();
        }

        public QName getQName() {
          return qName;
        }

        public Serializable getValue() {
          return value;
        }
      }

      List<UpdateNavigationalStateResponse.Event> nsEvents = navResponse.getEvents();
      List<javax.portlet.Event> events = new ArrayList<javax.portlet.Event>(nsEvents.size());
      if (nsEvents != null && !nsEvents.isEmpty()) {
        for (UpdateNavigationalStateResponse.Event nsEvent : nsEvents) {
          javax.portlet.Event portletEvent =
              new PortletEvent(nsEvent.getName(), nsEvent.getPayload());
          events.add(portletEvent);
        }
      }

      return events;
    } catch (Exception e) {
      log.error("Problem while processesing event for the portlet: " + uiPortlet.getState(), e);
    }
    return null;
  }
    private void handleUpdateNavigationalStateResponse(
        UpdateNavigationalStateResponse navStateResponse,
        UIPortlet<S, C> uiPortlet,
        PortalRequestContext prcontext)
        throws Exception {
      /*
       * Update the portlet window state according to the action output
       * information
       *
       * If the current node is displaying a usual layout page, also tells the
       * page which portlet to render or not when the state is maximized
       */
      // Note: we should only update the WindowState if the
      // UpdateNavigationalStateResponse.getWindowState is not null,
      // otherwise it means the WindowState has not changed and we should use the current value.
      if (navStateResponse.getWindowState() != null) {
        WindowState state = new WindowState(getWindowStateOrDefault(navStateResponse));
        setNextState(uiPortlet, state);
      }

      // update the portlet with the next mode to display
      // Note: we should only update the Mode if the UpdateNavigationalStateResponse.getMode is not
      // null,
      // otherwise it means the mode has not changed and we should use the current value.
      if (navStateResponse.getMode() != null) {
        PortletMode mode = new PortletMode(getPortletModeOrDefault(navStateResponse));
        setNextMode(uiPortlet, mode);
      }

      /*
       * Cache the render parameters in the UI portlet component to handle the
       * navigational state. Each time a portlet is rendered (except using
       * directly a RenderURL) those parameters are added to the portlet request
       * to preserve the portlet state among all the portal clicks
       */

      //
      StateString navigationalState = navStateResponse.getNavigationalState();
      if (navigationalState != null) {
        uiPortlet.setNavigationalState(navigationalState);
      }

      // update the public render parameters with the changes from the invocation
      setupPublicRenderParams(uiPortlet, navStateResponse.getPublicNavigationalStateUpdates());

      /*
       * Handle the events returned by the action output and broadcast a new UI
       * event to the ProcessEventsActionListener that will then target the
       * portlet container service directly
       */

      // TODO: (mwringe) add this to the UpdateNavigationStateResponse.Event class instead of here
      class PortletEvent implements javax.portlet.Event {
        QName qName;

        Serializable value;

        public PortletEvent(QName qName, Serializable value) {
          this.qName = qName;
          this.value = value;
        }

        public String getName() {
          return qName.getLocalPart();
        }

        public QName getQName() {
          return qName;
        }

        public Serializable getValue() {
          return value;
        }
      }

      List<UpdateNavigationalStateResponse.Event> nsEvents = navStateResponse.getEvents();
      List<javax.portlet.Event> events = new ArrayList<javax.portlet.Event>(nsEvents.size());
      if (nsEvents != null && !nsEvents.isEmpty()) {
        for (UpdateNavigationalStateResponse.Event nsEvent : nsEvents) {
          if (uiPortlet.supportsPublishingEvent(nsEvent.getName())) {
            javax.portlet.Event portletEvent =
                new PortletEvent(nsEvent.getName(), nsEvent.getPayload());
            events.add(portletEvent);
          }
        }
      }

      if (events != null) {
        prcontext.setAttribute(PORTLET_EVENTS, new EventsWrapper(events));
        uiPortlet.createEvent("ProcessEvents", Phase.PROCESS, prcontext).broadcast();
      }
    }