WIP Audit views

This commit is contained in:
John Woodlock
2012-12-10 13:12:21 +00:00
parent d95a190efb
commit 6f3fcf31b6
3 changed files with 97 additions and 55 deletions
@@ -8,6 +8,7 @@ import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
@@ -74,18 +75,39 @@ public class AuditApiResource {
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) {
String extraCriteria = "";
if (apiOperation != null) {
extraCriteria += " and api_operation like " + ApiParameterHelper.sqlEncodeString(apiOperation);
extraCriteria += " and aud.api_operation like " + ApiParameterHelper.sqlEncodeString(apiOperation);
}
if (resource != null) {
extraCriteria += " and api_resource like " + ApiParameterHelper.sqlEncodeString(resource);
extraCriteria += " and aud.api_resource like " + ApiParameterHelper.sqlEncodeString(resource);
}
if (resourceId != null) {
extraCriteria += " and resource_id = " + resourceId;
extraCriteria += " and aud.resource_id = " + resourceId;
}
@@ -4,6 +4,9 @@ import java.util.Collection;
public interface AuditReadPlatformService {
Collection<AuditData> retrieveAuditEntries(String extraCriteria, boolean includeJson);
Collection<AuditData> retrieveAuditEntries(String extraCriteria,
boolean includeJson);
AuditData retrieveAuditEntry(Long auditId);
}
@@ -17,67 +17,84 @@ import org.springframework.stereotype.Service;
@Service
public class AuditReadPlatformServiceImpl implements AuditReadPlatformService {
private final JdbcTemplate jdbcTemplate;
private final PlatformSecurityContext context;
private final JdbcTemplate jdbcTemplate;
private final PlatformSecurityContext context;
@Autowired
public AuditReadPlatformServiceImpl(final PlatformSecurityContext context, final TenantAwareRoutingDataSource dataSource) {
this.context = context;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Autowired
public AuditReadPlatformServiceImpl(final PlatformSecurityContext context,
final TenantAwareRoutingDataSource 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) {
String commandAsJsonString = "";
if (includeJson) commandAsJsonString = ", mc.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,"
+ " mk.username as maker, mc.made_on_date as madeOnDate, ck.username as checker, mc.checked_on_date as checkedOnDate"
+ commandAsJsonString
+ " from m_portfolio_command_source mc " + " left join m_appuser mk on mk.id = mc.maker_id"
+ " left join m_appuser ck on ck.id = mc.checker_id";
}
public String schema(boolean includeJson) {
@Override
public AuditData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
String commandAsJsonString = "";
if (includeJson)
commandAsJsonString = ", aud.command_as_json as commandAsJson ";
final Long id = rs.getLong("id");
final String apiOperation = rs.getString("apiOperation");
final String resource = rs.getString("resource");
final Long resourceId = JdbcSupport.getLong(rs, "resourceId");
final String maker = rs.getString("maker");
final LocalDate madeOnDate = JdbcSupport.getLocalDate(rs, "madeOnDate");
final String checker = rs.getString("checker");
final LocalDate checkedOnDate = JdbcSupport.getLocalDate(rs, "checkedOnDate");
String commandAsJson;
//commandAsJson might not be on the select list of columns
try {
commandAsJson = rs.getString("commandAsJson");
} catch (SQLException e) {
commandAsJson = null;
}
return new AuditData(id, apiOperation, resource, resourceId, maker, madeOnDate, checker, checkedOnDate, commandAsJson);
}
}
return " aud.id as id, aud.api_operation as apiOperation, aud.api_resource as resource, aud.resource_id as resourceId,"
+ " mk.username as maker, aud.made_on_date as madeOnDate, ck.username as checker, aud.checked_on_date as checkedOnDate"
+ commandAsJsonString
+ " from m_portfolio_command_source aud "
+ " left join m_appuser mk on mk.id = aud.maker_id"
+ " left join m_appuser ck on ck.id = aud.checker_id";
}
@Override
public Collection<AuditData> retrieveAuditEntries(String extraCriteria, boolean includeJson) {
context.authenticatedUser();
@Override
public AuditData mapRow(final ResultSet rs,
@SuppressWarnings("unused") final int rowNum)
throws SQLException {
final AuditMapper rm = new AuditMapper();
final Long id = rs.getLong("id");
final String apiOperation = rs.getString("apiOperation");
final String resource = rs.getString("resource");
final Long resourceId = JdbcSupport.getLong(rs, "resourceId");
final String maker = rs.getString("maker");
final LocalDate madeOnDate = JdbcSupport.getLocalDate(rs,
"madeOnDate");
final String checker = rs.getString("checker");
final LocalDate checkedOnDate = JdbcSupport.getLocalDate(rs,
"checkedOnDate");
String commandAsJson;
// commandAsJson might not be on the select list of columns
try {
commandAsJson = rs.getString("commandAsJson");
} catch (SQLException e) {
commandAsJson = null;
}
return new AuditData(id, apiOperation, resource, resourceId, maker,
madeOnDate, checker, checkedOnDate, commandAsJson);
}
}
@Override
public Collection<AuditData> retrieveAuditEntries(String extraCriteria,
boolean includeJson) {
context.authenticatedUser();
final AuditMapper rm = new AuditMapper();
String sql = "select " + rm.schema(includeJson);
if (StringUtils.isNotBlank(extraCriteria)) sql += " where (" + extraCriteria + ")";
// + " 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";
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[] {});
}
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(true);
sql += " where aud.id = " + auditId;
return this.jdbcTemplate.queryForObject(sql, rm, new Object[] {});
}
}