private static String getChainedHandler(
      FacesContext context,
      UIComponent component,
      List<ClientBehavior> behaviors,
      Collection<ClientBehaviorContext.Parameter> params,
      String behaviorEventName,
      String userHandler) {

    StringBuilder builder = new StringBuilder(100);
    builder.append("jsf.util.chain(this,event,");

    appendScriptToChain(builder, userHandler);

    boolean submitting =
        appendBehaviorsToChain(builder, context, component, behaviors, behaviorEventName, params);

    boolean hasParams = ((null != params) && !params.isEmpty());

    if (!submitting && hasParams) {
      String submitHandler = getSubmitHandler(context, component, params, false);

      appendScriptToChain(builder, submitHandler);

      submitting = true;
    }

    builder.append(")");

    if (submitting && ("action".equals(behaviorEventName) || "click".equals(behaviorEventName))) {
      builder.append(";return false");
    }

    return builder.toString();
  }
  private static String getSubmitHandler(
      FacesContext context,
      UIComponent component,
      Collection<ClientBehaviorContext.Parameter> params,
      boolean preventDefault) {

    StringBuilder builder = new StringBuilder(256);

    String formClientId = getFormClientId(component, context);
    String componentClientId = component.getClientId(context);

    builder.append("qab.sf(document.getElementById('");
    builder.append(formClientId);
    builder.append("'),{");

    appendProperty(builder, componentClientId, componentClientId);

    if ((null != params) && (!params.isEmpty())) {
      for (ClientBehaviorContext.Parameter param : params) {
        appendProperty(builder, param.getName(), param.getValue());
      }
    }

    builder.append("})");

    if (preventDefault) {
      builder.append(";return false");
    }

    return builder.toString();
  }
  private static HandlerType getHandlerType(
      List<ClientBehavior> behaviors,
      Collection<ClientBehaviorContext.Parameter> params,
      String userHandler) {

    if ((behaviors == null) || (behaviors.isEmpty())) {

      if (params.isEmpty()) return HandlerType.USER_HANDLER_ONLY;

      return (userHandler == null) ? HandlerType.SUBMIT_ONLY : HandlerType.CHAIN;
    }

    if ((behaviors.size() == 1) && (userHandler == null)) {
      ClientBehavior behavior = behaviors.get(0);

      if (isSubmitting(behavior) || params.isEmpty()) return HandlerType.SINGLE_BEHAVIOR_ONLY;
    }

    return HandlerType.CHAIN;
  }