/**
   * Retrieves post metadata for the quickfinder component with the given id and if a callback
   * method has been configured, invokes that callback method.
   *
   * @param form form instance containing the model data
   * @param request http request object being handled
   * @param quickfinderId id for the quickfinder component that triggered the lookup we are
   *     returning from
   */
  protected void invokeQuickfinderCallback(
      UifFormBase form, final HttpServletRequest request, final String quickfinderId) {
    String callbackMethodToCall =
        (String)
            form.getViewPostMetadata()
                .getComponentPostData(
                    quickfinderId, UifConstants.PostMetadata.QUICKFINDER_CALLBACK_METHOD_TO_CALL);
    MethodInvokerConfig callbackMethod =
        (MethodInvokerConfig)
            form.getViewPostMetadata()
                .getComponentPostData(
                    quickfinderId, UifConstants.PostMetadata.QUICKFINDER_CALLBACK_METHOD);

    if (StringUtils.isBlank(callbackMethodToCall) && (callbackMethod == null)) {
      return;
    }

    if (callbackMethod == null) {
      callbackMethod = new MethodInvokerConfig();
    }

    // get additional parameters to be passed to the callback method
    Map<String, String> callbackContext =
        (Map<String, String>)
            form.getViewPostMetadata()
                .getComponentPostData(
                    quickfinderId, UifConstants.PostMetadata.QUICKFINDER_CALLBACK_CONTEXT);

    // if target class or object not set, use view helper service
    if ((callbackMethod.getTargetClass() == null) && (callbackMethod.getTargetObject() == null)) {
      callbackMethod.setTargetObject(form.getViewHelperService());
    }

    callbackMethod.setTargetMethod(callbackMethodToCall);

    Object[] arguments = new Object[3];
    arguments[0] = form;
    arguments[1] = quickfinderId;
    arguments[2] = callbackContext;
    callbackMethod.setArguments(arguments);

    final MethodInvokerConfig methodToInvoke = callbackMethod;

    Runnable runnable =
        new Runnable() {
          @Override
          public void run() {
            try {
              methodToInvoke.prepare();
              methodToInvoke.invoke();
            } catch (Exception e) {
              throw new RuntimeException(
                  "Error invoking callback method for quickfinder: " + quickfinderId, e);
            }
          }
        };

    ViewLifecycle.encapsulateLifecycle(
        form.getView(), form, form.getViewPostMetadata(), null, request, runnable);
  }
  /**
   * Handles the refresh call by checking the request parameters and delegating out to helper
   * methods.
   *
   * <p>{@inheritDoc}
   */
  @Override
  public ModelAndView refresh(UifFormBase form) {
    HttpServletRequest request = form.getRequest();

    if (request.getParameterMap().containsKey(UifParameters.MESSAGE_TO_DISPLAY)) {
      String messageToDisplay = request.getParameter(UifParameters.MESSAGE_TO_DISPLAY);

      if (StringUtils.isNotBlank(messageToDisplay)) {
        GlobalVariables.getMessageMap()
            .putErrorForSectionId(KRADConstants.GLOBAL_ERRORS, messageToDisplay);
      }
    }

    if (request.getParameterMap().containsKey(UifParameters.REFRESH_STATUS)) {
      String refreshStatus = request.getParameter(UifParameters.REFRESH_STATUS);

      // if the return URL reported an error, do not continue with the refresh call
      if (UifConstants.RefreshStatus.ERROR.equals(refreshStatus)) {
        return getModelAndViewService().getModelAndView(form);
      }
    }

    String refreshCallerType = "";
    if (request.getParameterMap().containsKey(KRADConstants.REFRESH_CALLER_TYPE)) {
      refreshCallerType = request.getParameter(KRADConstants.REFRESH_CALLER_TYPE);
    }

    if (StringUtils.equals(refreshCallerType, UifConstants.RefreshCallerTypes.MULTI_VALUE_LOOKUP)) {
      processMultiValueReturn(form, request);
    }

    if (request.getParameterMap().containsKey(KRADConstants.REFERENCES_TO_REFRESH)) {
      final String referencesToRefresh = request.getParameter(KRADConstants.REFERENCES_TO_REFRESH);

      Runnable runnable =
          new Runnable() {
            @Override
            public void run() {
              ViewLifecycle.getHelper().refreshReferences(referencesToRefresh);
            }
          };

      ViewLifecycle.encapsulateLifecycle(
          form.getView(), form, form.getViewPostMetadata(), null, request, runnable);
    }

    if (request.getParameterMap().containsKey(UifParameters.QUICKFINDER_ID)) {
      String quickfinderId = request.getParameter(UifParameters.QUICKFINDER_ID);

      setFocusJumpFromQuickfinder(form, quickfinderId);

      invokeQuickfinderCallback(form, request, quickfinderId);
    }

    return getModelAndViewService().getModelAndView(form);
  }
Пример #3
0
  @Test
  /**
   * Tests rendering on required messages
   *
   * @see KULRICE-7130
   */
  public void testRequiredMessageDisplay() throws Exception {

    // create mock component
    //        View mockView =  mock(View.class);
    //        ViewHelperService mockViewHelperService = mock(ViewHelperService.class);
    //        when(mockView.getViewHelperService()).thenReturn(mockViewHelperService);
    //        when(mockView.copy()).thenReturn(mockView);
    //        when(mockView.clone()).thenReturn(mockView);

    ViewLifecycle.encapsulateLifecycle(
        view,
        null,
        null,
        new Runnable() {
          @Override
          public void run() {
            Object nullModel = null;
            Component mockComponent = mock(Component.class);

            // build asterisk required message and mock label to test rendering
            Label mockLabel = new Label();
            Message message = new Message();
            message.setMessageText("*");
            message.setRender(true);

            FieldBase fieldBase = new FieldBase();
            fieldBase.setFieldLabel(mockLabel);
            fieldBase.setRequired(true);
            fieldBase.setReadOnly(false);

            FieldBase fieldBaseCopy = CopyUtils.copy(fieldBase);
            fieldBaseCopy.performFinalize(nullModel, mockComponent);
            assertTrue(fieldBaseCopy.getFieldLabel().isRenderRequiredIndicator());

            // required and readonly -  do not render
            fieldBaseCopy = CopyUtils.copy(fieldBase);
            fieldBaseCopy.setReadOnly(true);
            fieldBaseCopy.performFinalize(nullModel, mockComponent);
            assertFalse(fieldBaseCopy.getFieldLabel().isRenderRequiredIndicator());
          }
        });
  }
  /**
   * Handles the return from a multi-value lookup, processing any select line values and invoking
   * the configured view helper service to create the lines for those values in the model
   * collection.
   *
   * <p>There are two supported strategies for returning the selected lines. One, if the lookup view
   * and the caller are within the same application container, Springs input flash map is used. If
   * however, the lookup view is outside the caller, then just a standard request parameter is used.
   *
   * @param form form instance containing the model data
   * @param request http request object being handled
   */
  protected void processMultiValueReturn(final UifFormBase form, HttpServletRequest request) {
    final String lookupCollectionId = request.getParameter(UifParameters.LOOKUP_COLLECTION_ID);

    final String lookupCollectionName = request.getParameter(UifParameters.LOOKUP_COLLECTION_NAME);
    if (StringUtils.isBlank(lookupCollectionName)) {
      throw new RuntimeException(
          "Lookup collection name is required for processing multi-value lookup results");
    }

    final String multiValueReturnFields =
        request.getParameter(UifParameters.MULIT_VALUE_RETURN_FILEDS);
    String selectedLineValuesParam = request.getParameter(UifParameters.SELECTED_LINE_VALUES);

    String flashMapSelectedLineValues = "";
    if (RequestContextUtils.getInputFlashMap(request) != null) {
      flashMapSelectedLineValues =
          (String)
              RequestContextUtils.getInputFlashMap(request).get(UifParameters.SELECTED_LINE_VALUES);
    }

    if (!StringUtils.isBlank(flashMapSelectedLineValues)) {
      selectedLineValuesParam = flashMapSelectedLineValues;
    }

    final String selectedLineValues = selectedLineValuesParam;

    Runnable runnable =
        new Runnable() {
          @Override
          public void run() {
            // invoked view helper to populate the collection from lookup results
            ViewLifecycle.getHelper()
                .processMultipleValueLookupResults(
                    form,
                    lookupCollectionId,
                    lookupCollectionName,
                    multiValueReturnFields,
                    selectedLineValues);
          }
        };

    ViewLifecycle.encapsulateLifecycle(
        form.getView(), form, form.getViewPostMetadata(), null, request, runnable);
  }