@Override
  public AuditData retrieveAuditEntry(final Long auditId) {

    final AppUser currentUser = this.context.authenticatedUser();
    final String hierarchy = currentUser.getOffice().getHierarchy();

    final AuditMapper rm = new AuditMapper();

    final String sql = "select " + rm.schema(true, hierarchy) + " where aud.id = " + auditId;

    final AuditData auditResult = this.jdbcTemplate.queryForObject(sql, rm, new Object[] {});

    return replaceIdsOnAuditData(auditResult);
  }
  @Override
  public Page<AuditData> retrievePaginatedAuditEntries(
      final String extraCriteria,
      final boolean includeJson,
      final PaginationParameters parameters) {

    this.paginationParametersDataValidator.validateParameterValues(
        parameters, supportedOrderByValues, "audits");
    final AppUser currentUser = this.context.authenticatedUser();
    final String hierarchy = currentUser.getOffice().getHierarchy();

    String updatedExtraCriteria = "";
    if (StringUtils.isNotBlank(extraCriteria)) {
      updatedExtraCriteria = " where (" + extraCriteria + ")";
    }

    final AuditMapper rm = new AuditMapper();
    final StringBuilder sqlBuilder = new StringBuilder(200);
    sqlBuilder.append("select SQL_CALC_FOUND_ROWS ");
    sqlBuilder.append(rm.schema(includeJson, hierarchy));
    sqlBuilder.append(' ').append(updatedExtraCriteria);

    if (parameters.isOrderByRequested()) {
      sqlBuilder.append(' ').append(parameters.orderBySql());
    } else {
      sqlBuilder.append(' ').append(' ').append(" order by aud.id DESC");
    }

    if (parameters.isLimited()) {
      sqlBuilder.append(' ').append(parameters.limitSql());
    }

    logger.info("sql: " + sqlBuilder.toString());

    final String sqlCountRows = "SELECT FOUND_ROWS()";
    return this.paginationHelper.fetchPage(
        this.jdbcTemplate, sqlCountRows, sqlBuilder.toString(), new Object[] {}, rm);
  }
  public Collection<AuditData> retrieveEntries(
      final String useType, final String extraCriteria, final boolean includeJson) {

    if (!(useType.equals("audit") || useType.equals("makerchecker"))) {
      throw new PlatformDataIntegrityException(
          "error.msg.invalid.auditSearchTemplate.useType",
          "Invalid Audit Search Template UseType: " + useType);
    }

    final AppUser currentUser = this.context.authenticatedUser();
    final String hierarchy = currentUser.getOffice().getHierarchy();

    final AuditMapper rm = new AuditMapper();
    String sql = "select " + rm.schema(includeJson, hierarchy);

    Boolean isLimitedChecker = false;
    if (useType.equals("makerchecker")) {
      if (currentUser.hasNotPermissionForAnyOf("ALL_FUNCTIONS", "CHECKER_SUPER_USER")) {
        isLimitedChecker = true;
      }
    }

    if (isLimitedChecker) {
      sql +=
          " join m_permission p on REPLACE(p.action_name, '_CHECKER', '')  = aud.action_name and p.entity_name = aud.entity_name and p.code like '%\\_CHECKER'"
              + " join m_role_permission rp on rp.permission_id = p.id"
              + " join m_role r on r.id = rp.role_id "
              + " join m_appuser_role ur on ur.role_id = r.id and ur.appuser_id = "
              + currentUser.getId();
    }
    sql += extraCriteria;

    logger.info("sql: " + sql);

    return this.jdbcTemplate.query(sql, rm, new Object[] {});
  }