<commit_before>package org.openlmis.referencedata.web;

import org.apache.commons.lang3.StringUtils;
import org.javers.core.Javers;
import org.javers.core.changelog.SimpleTextChangeLog;
import org.javers.core.diff.Change;
import org.javers.core.json.JsonConverter;
import org.javers.repository.jql.QueryBuilder;
import org.openlmis.referencedata.i18n.ExposedMessageSource;
import org.openlmis.referencedata.service.RightService;
import org.openlmis.referencedata.util.Pagination;
import org.openlmis.util.ErrorResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@RequestMapping("/api")
public abstract class BaseController {

  @Autowired
  RightService rightService;

  @Autowired
  private ExposedMessageSource messageSource;

  @Autowired
  private Javers javers;


  protected Map<String, String> getErrors(Errors errors) {
    return errors
        .getFieldErrors()
        .stream()
        .collect(Collectors.toMap(FieldError::getField,f -> messageSource.getMessage(
            f.getDefaultMessage(), f.getArguments(), LocaleContextHolder.getLocale()))
        );
  }

  protected ErrorResponse buildErrorResponse(String messageKey) {
    return buildErrorResponse(messageKey, null);
  }

  protected ErrorResponse buildErrorResponse(String messageKey, Object[] errorArgs) {
    return new ErrorResponse(messageKey,
        messageSource.getMessage(messageKey, errorArgs, LocaleContextHolder.getLocale()));
  }


  /**
   * <p>
   * Convenience method intended to return audit log information via either JSON or raw text,
   * based on the value of the returnJson argument.
   * </p><p>
   * For testing and development, it’s useful to set this to false in order to retrieve a
   * human-readable response. For production, however, JSON should exclusively be returned.
   * </p>
   * See getAuditedChanges() for a list and explanation of the available parameters.
   */
  protected String getAuditLog(Class type, UUID id, String author, String changedPropertyName,
                               Pageable page, boolean returnJson) {
    if (returnJson) {
      return getAuditLogJson(type, id, author, changedPropertyName, page);
    } else {
      return getAuditLogText(type, id, author, changedPropertyName, page);
    }
  }

  /**
   * Return a list of audited changes via JSON.
   * @param type The type of class for which we wish to retrieve historical changes.
   */
  protected String getAuditLogJson(Class type) {
    return getAuditLogJson(type, null, null, null, null);
  }

  protected String getAuditLogJson(Class type, UUID id) {
    return getAuditLogJson(type, id, null, null, null);
  }<commit_msg>OLMIS-1649: remove MessageSource from base controller, remove redundant methods<commit_after>package org.openlmis.referencedata.web;

import org.apache.commons.lang3.StringUtils;
import org.javers.core.Javers;
import org.javers.core.changelog.SimpleTextChangeLog;
import org.javers.core.diff.Change;
import org.javers.core.json.JsonConverter;
import org.javers.repository.jql.QueryBuilder;
import org.openlmis.referencedata.service.RightService;
import org.openlmis.referencedata.util.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.UUID;

@RequestMapping("/api")
public abstract class BaseController {

  @Autowired
  RightService rightService;

  @Autowired
  private Javers javers;

  /**
   * <p>
   * Convenience method intended to return audit log information via either JSON or raw text,
   * based on the value of the returnJson argument.
   * </p><p>
   * For testing and development, it’s useful to set this to false in order to retrieve a
   * human-readable response. For production, however, JSON should exclusively be returned.
   * </p>
   * See getAuditedChanges() for a list and explanation of the available parameters.
   */
  protected String getAuditLog(Class type, UUID id, String author, String changedPropertyName,
                               Pageable page, boolean returnJson) {
    if (returnJson) {
      return getAuditLogJson(type, id, author, changedPropertyName, page);
    } else {
      return getAuditLogText(type, id, author, changedPropertyName, page);
    }
  }

  /**
   * Return a list of audited changes via JSON.
   * @param type The type of class for which we wish to retrieve historical changes.
   */
  protected String getAuditLogJson(Class type) {
    return getAuditLogJson(type, null, null, null, null);
  }

  protected String getAuditLogJson(Class type, UUID id) {
    return getAuditLogJson(type, id, null, null, null);
  }
{'fraction_non_alphanumeric': 0.0894228875320071, 'fraction_numerical': 0.0019696671262556626, 'mean_word_length': 4.181632653061224, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1889752', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1442, 'n_words': 409}