/**
   * Creates a {@link JPQLException} indicating the problems with the JPQL query.
   *
   * @param queryContext The {@link JPQLQueryContext} containing the information about the JPQL
   *     query
   * @param problems The {@link JPQLQueryProblem problems} found in the JPQL query that are
   *     translated into an exception
   * @param messageKey The key used to retrieve the localized message
   * @return The {@link JPQLException} indicating the problems with the JPQL query
   */
  private JPQLException buildException(
      JPQLQueryContext queryContext, Collection<JPQLQueryProblem> problems, String messageKey) {

    ResourceBundle bundle = resourceBundle();
    StringBuilder sb = new StringBuilder();

    for (JPQLQueryProblem problem : problems) {

      // Retrieve the localized message
      String message;

      try {
        message = bundle.getString(problem.getMessageKey());
      } catch (NullPointerException e) {
        // In case the resource bundle was not updated
        message = problem.getMessageKey();
      }

      // Now format the localized message
      String[] arguments = problem.getMessageArguments();

      if (arguments.length > 0) {
        message = MessageFormat.format(message, (Object[]) arguments);
      }

      // Append the description
      sb.append("\n");
      sb.append("[");
      sb.append(problem.getStartPosition());
      sb.append(", ");
      sb.append(problem.getEndPosition());
      sb.append("] ");
      sb.append(message);
    }

    String errorMessage = bundle.getString(messageKey);
    errorMessage = MessageFormat.format(errorMessage, queryContext.getJPQLQuery(), sb);
    return new JPQLException(errorMessage);
  }