WIP Audit views
This commit is contained in:
@@ -8,6 +8,7 @@ import java.util.Set;
|
|||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
@@ -74,18 +75,39 @@ public class AuditApiResource {
|
|||||||
RESPONSE_DATA_PARAMETERS);
|
RESPONSE_DATA_PARAMETERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("{auditId}")
|
||||||
|
@Consumes({ MediaType.APPLICATION_JSON })
|
||||||
|
@Produces({ MediaType.APPLICATION_JSON })
|
||||||
|
public String retrieveAuditEntry(@PathParam("auditId") final Long auditId, @Context final UriInfo uriInfo) {
|
||||||
|
|
||||||
|
context.authenticatedUser().validateHasReadPermission(
|
||||||
|
resourceNameForPermissions);
|
||||||
|
|
||||||
|
|
||||||
|
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper
|
||||||
|
.process(uriInfo.getQueryParameters());
|
||||||
|
|
||||||
|
final AuditData auditEntry = this.auditReadPlatformService
|
||||||
|
.retrieveAuditEntry(auditId);
|
||||||
|
|
||||||
|
return this.toApiJsonSerializer.serialize(settings, auditEntry,
|
||||||
|
RESPONSE_DATA_PARAMETERS);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private String getExtraCriteria(String apiOperation, String resource, Long resourceId) {
|
private String getExtraCriteria(String apiOperation, String resource, Long resourceId) {
|
||||||
|
|
||||||
String extraCriteria = "";
|
String extraCriteria = "";
|
||||||
|
|
||||||
if (apiOperation != null) {
|
if (apiOperation != null) {
|
||||||
extraCriteria += " and api_operation like " + ApiParameterHelper.sqlEncodeString(apiOperation);
|
extraCriteria += " and aud.api_operation like " + ApiParameterHelper.sqlEncodeString(apiOperation);
|
||||||
}
|
}
|
||||||
if (resource != null) {
|
if (resource != null) {
|
||||||
extraCriteria += " and api_resource like " + ApiParameterHelper.sqlEncodeString(resource);
|
extraCriteria += " and aud.api_resource like " + ApiParameterHelper.sqlEncodeString(resource);
|
||||||
}
|
}
|
||||||
if (resourceId != null) {
|
if (resourceId != null) {
|
||||||
extraCriteria += " and resource_id = " + resourceId;
|
extraCriteria += " and aud.resource_id = " + resourceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+4
-1
@@ -4,6 +4,9 @@ import java.util.Collection;
|
|||||||
|
|
||||||
public interface AuditReadPlatformService {
|
public interface AuditReadPlatformService {
|
||||||
|
|
||||||
Collection<AuditData> retrieveAuditEntries(String extraCriteria, boolean includeJson);
|
Collection<AuditData> retrieveAuditEntries(String extraCriteria,
|
||||||
|
boolean includeJson);
|
||||||
|
|
||||||
|
AuditData retrieveAuditEntry(Long auditId);
|
||||||
|
|
||||||
}
|
}
|
||||||
+64
-47
@@ -17,67 +17,84 @@ import org.springframework.stereotype.Service;
|
|||||||
@Service
|
@Service
|
||||||
public class AuditReadPlatformServiceImpl implements AuditReadPlatformService {
|
public class AuditReadPlatformServiceImpl implements AuditReadPlatformService {
|
||||||
|
|
||||||
private final JdbcTemplate jdbcTemplate;
|
private final JdbcTemplate jdbcTemplate;
|
||||||
private final PlatformSecurityContext context;
|
private final PlatformSecurityContext context;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public AuditReadPlatformServiceImpl(final PlatformSecurityContext context, final TenantAwareRoutingDataSource dataSource) {
|
public AuditReadPlatformServiceImpl(final PlatformSecurityContext context,
|
||||||
this.context = context;
|
final TenantAwareRoutingDataSource dataSource) {
|
||||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
this.context = context;
|
||||||
}
|
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
private static final class AuditMapper implements RowMapper<AuditData> {
|
private static final class AuditMapper implements RowMapper<AuditData> {
|
||||||
|
|
||||||
public String schema(boolean includeJson) {
|
public String schema(boolean includeJson) {
|
||||||
|
|
||||||
String commandAsJsonString = "";
|
String commandAsJsonString = "";
|
||||||
if (includeJson) commandAsJsonString = ", mc.command_as_json as commandAsJson ";
|
if (includeJson)
|
||||||
|
commandAsJsonString = ", aud.command_as_json as commandAsJson ";
|
||||||
|
|
||||||
return " mc.id as id, mc.api_operation as apiOperation, mc.api_resource as resource, mc.resource_id as resourceId,"
|
return " aud.id as id, aud.api_operation as apiOperation, aud.api_resource as resource, aud.resource_id as resourceId,"
|
||||||
+ " mk.username as maker, mc.made_on_date as madeOnDate, ck.username as checker, mc.checked_on_date as checkedOnDate"
|
+ " mk.username as maker, aud.made_on_date as madeOnDate, ck.username as checker, aud.checked_on_date as checkedOnDate"
|
||||||
+ commandAsJsonString
|
+ commandAsJsonString
|
||||||
+ " from m_portfolio_command_source mc " + " left join m_appuser mk on mk.id = mc.maker_id"
|
+ " from m_portfolio_command_source aud "
|
||||||
+ " left join m_appuser ck on ck.id = mc.checker_id";
|
+ " left join m_appuser mk on mk.id = aud.maker_id"
|
||||||
}
|
+ " left join m_appuser ck on ck.id = aud.checker_id";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AuditData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
|
public AuditData mapRow(final ResultSet rs,
|
||||||
|
@SuppressWarnings("unused") final int rowNum)
|
||||||
|
throws SQLException {
|
||||||
|
|
||||||
final Long id = rs.getLong("id");
|
final Long id = rs.getLong("id");
|
||||||
final String apiOperation = rs.getString("apiOperation");
|
final String apiOperation = rs.getString("apiOperation");
|
||||||
final String resource = rs.getString("resource");
|
final String resource = rs.getString("resource");
|
||||||
final Long resourceId = JdbcSupport.getLong(rs, "resourceId");
|
final Long resourceId = JdbcSupport.getLong(rs, "resourceId");
|
||||||
final String maker = rs.getString("maker");
|
final String maker = rs.getString("maker");
|
||||||
final LocalDate madeOnDate = JdbcSupport.getLocalDate(rs, "madeOnDate");
|
final LocalDate madeOnDate = JdbcSupport.getLocalDate(rs,
|
||||||
final String checker = rs.getString("checker");
|
"madeOnDate");
|
||||||
final LocalDate checkedOnDate = JdbcSupport.getLocalDate(rs, "checkedOnDate");
|
final String checker = rs.getString("checker");
|
||||||
String commandAsJson;
|
final LocalDate checkedOnDate = JdbcSupport.getLocalDate(rs,
|
||||||
//commandAsJson might not be on the select list of columns
|
"checkedOnDate");
|
||||||
try {
|
String commandAsJson;
|
||||||
commandAsJson = rs.getString("commandAsJson");
|
// commandAsJson might not be on the select list of columns
|
||||||
} catch (SQLException e) {
|
try {
|
||||||
commandAsJson = null;
|
commandAsJson = rs.getString("commandAsJson");
|
||||||
}
|
} catch (SQLException e) {
|
||||||
return new AuditData(id, apiOperation, resource, resourceId, maker, madeOnDate, checker, checkedOnDate, commandAsJson);
|
commandAsJson = null;
|
||||||
}
|
}
|
||||||
}
|
return new AuditData(id, apiOperation, resource, resourceId, maker,
|
||||||
|
madeOnDate, checker, checkedOnDate, commandAsJson);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<AuditData> retrieveAuditEntries(String extraCriteria, boolean includeJson) {
|
public Collection<AuditData> retrieveAuditEntries(String extraCriteria,
|
||||||
context.authenticatedUser();
|
boolean includeJson) {
|
||||||
|
context.authenticatedUser();
|
||||||
|
|
||||||
final AuditMapper rm = new AuditMapper();
|
final AuditMapper rm = new AuditMapper();
|
||||||
|
|
||||||
|
String sql = "select " + rm.schema(includeJson);
|
||||||
|
if (StringUtils.isNotBlank(extraCriteria))
|
||||||
|
sql += " where (" + extraCriteria + ")";
|
||||||
|
sql += " order by aud.id DESC";
|
||||||
|
|
||||||
|
return this.jdbcTemplate.query(sql, rm, new Object[] {});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AuditData retrieveAuditEntry(Long auditId) {
|
||||||
|
context.authenticatedUser();
|
||||||
|
|
||||||
|
final AuditMapper rm = new AuditMapper();
|
||||||
|
|
||||||
String sql = "select " + rm.schema(includeJson);
|
String sql = "select " + rm.schema(true);
|
||||||
if (StringUtils.isNotBlank(extraCriteria)) sql += " where (" + extraCriteria + ")";
|
sql += " where aud.id = " + auditId;
|
||||||
// + " where mc.checker_id is null order by mc.made_on_date DESC, mc.api_resource ASC, mc.api_operation ASC";
|
|
||||||
sql += " order by id DESC";
|
|
||||||
|
|
||||||
return this.jdbcTemplate.query(sql, rm, new Object[] {});
|
return this.jdbcTemplate.queryForObject(sql, rm, new Object[] {});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user