add audit 'id' field to api get

This commit is contained in:
John Woodlock
2013-03-28 21:52:09 +00:00
parent e37f3cebfa
commit 2662b692bf
2 changed files with 212 additions and 144 deletions
@@ -12,43 +12,67 @@ import org.joda.time.DateTime;
*/ */
public final class AuditData { public final class AuditData {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final Long id; private final Long id;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final String actionName; private final String actionName;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final String entityName; private final String entityName;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final Long resourceId; private final Long resourceId;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final Long subresourceId; private final Long subresourceId;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final String maker; private final String maker;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final DateTime madeOnDate; private final DateTime madeOnDate;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final String checker; private final String checker;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final DateTime checkedOnDate; private final DateTime checkedOnDate;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final String processingResult; private final String processingResult;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final String commandAsJson; private final String commandAsJson;
@SuppressWarnings("unused")
private final String officeName;
@SuppressWarnings("unused")
private final String groupLevelName;
@SuppressWarnings("unused")
private final String groupName;
@SuppressWarnings("unused")
private final String clientName;
@SuppressWarnings("unused")
private final String loanAccountNo;
@SuppressWarnings("unused")
private final String savingsAccountNo;
public AuditData(final Long id, final String actionName, final String entityName, final Long resourceId, final Long subresourceId, final String maker, public AuditData(final Long id, final String actionName,
final DateTime madeOnDate, final String checker, final DateTime checkedOnDate, final String processingResult, final String entityName, final Long resourceId,
final String commandAsJson) { final Long subresourceId, final String maker,
final DateTime madeOnDate, final String checker,
final DateTime checkedOnDate, final String processingResult,
final String commandAsJson, final String officeName,
final String groupLevelName, final String groupName,
final String clientName, final String loanAccountNo,
final String savingsAccountNo) {
this.id = id; this.id = id;
this.actionName = actionName; this.actionName = actionName;
this.entityName = entityName; this.entityName = entityName;
this.resourceId = resourceId; this.resourceId = resourceId;
this.subresourceId = subresourceId; this.subresourceId = subresourceId;
this.maker = maker; this.maker = maker;
this.madeOnDate = madeOnDate; this.madeOnDate = madeOnDate;
this.checker = checker; this.checker = checker;
this.checkedOnDate = checkedOnDate; this.checkedOnDate = checkedOnDate;
this.commandAsJson = commandAsJson; this.commandAsJson = commandAsJson;
this.processingResult = processingResult; this.processingResult = processingResult;
} this.officeName = officeName;
this.groupLevelName = groupLevelName;
this.groupName = groupName;
this.clientName = clientName;
this.loanAccountNo = loanAccountNo;
this.savingsAccountNo = savingsAccountNo;
}
} }
@@ -30,143 +30,187 @@ import org.springframework.stereotype.Service;
@Service @Service
public class AuditReadPlatformServiceImpl implements AuditReadPlatformService { public class AuditReadPlatformServiceImpl implements AuditReadPlatformService {
private final static Logger logger = LoggerFactory.getLogger(AuditReadPlatformServiceImpl.class); private final static Logger logger = LoggerFactory
private final JdbcTemplate jdbcTemplate; .getLogger(AuditReadPlatformServiceImpl.class);
private final PlatformSecurityContext context; private final JdbcTemplate jdbcTemplate;
private final AppUserReadPlatformService appUserReadPlatformService; private final PlatformSecurityContext context;
private final AppUserReadPlatformService appUserReadPlatformService;
@Autowired @Autowired
public AuditReadPlatformServiceImpl(final PlatformSecurityContext context, final TenantAwareRoutingDataSource dataSource, public AuditReadPlatformServiceImpl(final PlatformSecurityContext context,
final AppUserReadPlatformService appUserReadPlatformService) { final TenantAwareRoutingDataSource dataSource,
this.context = context; final AppUserReadPlatformService appUserReadPlatformService) {
this.jdbcTemplate = new JdbcTemplate(dataSource); this.context = context;
this.appUserReadPlatformService = appUserReadPlatformService; this.jdbcTemplate = new JdbcTemplate(dataSource);
} this.appUserReadPlatformService = appUserReadPlatformService;
}
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 = ", aud.command_as_json as commandAsJson "; if (includeJson)
commandAsJsonString = ", aud.command_as_json as commandAsJson ";
return " aud.id as id, aud.action_name as actionName, aud.entity_name as entityName," + " aud.resource_id as resourceId, aud.subresource_id as subresourceId," return " aud.id as id, aud.action_name as actionName, aud.entity_name as entityName,"
+ " mk.username as maker, aud.made_on_date as madeOnDate, " + " aud.resource_id as resourceId, aud.subresource_id as subresourceId,"
+ "ck.username as checker, aud.checked_on_date as checkedOnDate, ev.enum_message_property as processingResult " + " mk.username as maker, aud.made_on_date as madeOnDate, "
+ commandAsJsonString + " from m_portfolio_command_source aud " + " left join m_appuser mk on mk.id = aud.maker_id" + "ck.username as checker, aud.checked_on_date as checkedOnDate, ev.enum_message_property as processingResult "
+ " left join m_appuser ck on ck.id = aud.checker_id" + commandAsJsonString
+ " left join r_enum_value ev on ev.enum_name = 'processing_result_enum' and ev.enum_id = aud.processing_result_enum"; + ", "
} + " o.name as officeName, gl.level_name as groupLevelName, g.name as groupName, c.display_name as clientName, "
+ " l.account_no as loanAccountNo, s.account_no as savingsAccountNo "
+ " 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"
+ " left join m_office o on o.id = aud.office_id"
+ " left join m_group g on g.id = aud.group_id"
+ " left join m_group_level gl on gl.id = g.level_id"
+ " left join m_client c on c.id = aud.client_id"
+ " left join m_loan l on l.id = aud.loan_id"
+ " left join m_savings_account s on s.id = aud.savings_account_id"
+ " left join r_enum_value ev on ev.enum_name = 'processing_result_enum' and ev.enum_id = aud.processing_result_enum";
}
@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 actionName = rs.getString("actionName"); final String actionName = rs.getString("actionName");
final String entityName = rs.getString("entityName"); final String entityName = rs.getString("entityName");
final Long resourceId = JdbcSupport.getLong(rs, "resourceId"); final Long resourceId = JdbcSupport.getLong(rs, "resourceId");
final Long subresourceId = JdbcSupport.getLong(rs, "subresourceId"); final Long subresourceId = JdbcSupport.getLong(rs, "subresourceId");
final String maker = rs.getString("maker"); final String maker = rs.getString("maker");
final DateTime madeOnDate = JdbcSupport.getDateTime(rs, "madeOnDate"); final DateTime madeOnDate = JdbcSupport.getDateTime(rs,
final String checker = rs.getString("checker"); "madeOnDate");
final DateTime checkedOnDate = JdbcSupport.getDateTime(rs, "checkedOnDate"); final String checker = rs.getString("checker");
final String processingResult = rs.getString("processingResult"); final DateTime checkedOnDate = JdbcSupport.getDateTime(rs,
String commandAsJson; "checkedOnDate");
// commandAsJson might not be on the select list of columns final String processingResult = rs.getString("processingResult");
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, actionName, entityName, resourceId, subresourceId, maker, madeOnDate, checker, checkedOnDate, processingResult, commandAsJson = null;
commandAsJson); }
}
}
@Override String officeName = rs.getString("officeName");
public Collection<AuditData> retrieveAuditEntries(String extraCriteria, boolean includeJson) { String groupLevelName = rs.getString("groupLevelName");
context.authenticatedUser(); String groupName = rs.getString("groupName");
String clientName = rs.getString("clientName");
String loanAccountNo = rs.getString("loanAccountNo");
String savingsAccountNo = rs.getString("savingsAccountNo");
final AuditMapper rm = new AuditMapper(); return new AuditData(id, actionName, entityName, resourceId,
subresourceId, maker, madeOnDate, checker, checkedOnDate,
processingResult, commandAsJson, officeName,
groupLevelName, groupName, clientName, loanAccountNo,
savingsAccountNo);
}
}
String sql = "select " + rm.schema(includeJson); @Override
if (StringUtils.isNotBlank(extraCriteria)) sql += " where (" + extraCriteria + ")"; public Collection<AuditData> retrieveAuditEntries(String extraCriteria,
sql += " order by aud.id DESC"; boolean includeJson) {
logger.info("sql: " + sql); context.authenticatedUser();
return this.jdbcTemplate.query(sql, rm, new Object[] {});
}
@Override final AuditMapper rm = new AuditMapper();
public AuditData retrieveAuditEntry(Long auditId) {
context.authenticatedUser();
final AuditMapper rm = new AuditMapper(); String sql = "select " + rm.schema(includeJson);
if (StringUtils.isNotBlank(extraCriteria))
sql += " where (" + extraCriteria + ")";
sql += " order by aud.id DESC";
logger.info("sql: " + sql);
return this.jdbcTemplate.query(sql, rm, new Object[] {});
}
String sql = "select " + rm.schema(true); @Override
sql += " where aud.id = " + auditId; public AuditData retrieveAuditEntry(Long auditId) {
context.authenticatedUser();
return this.jdbcTemplate.queryForObject(sql, rm, new Object[] {}); final AuditMapper rm = new AuditMapper();
}
@Override String sql = "select " + rm.schema(true);
public AuditSearchData retrieveSearchTemplate() { sql += " where aud.id = " + auditId;
final Collection<AppUserData> appUsers = appUserReadPlatformService.retrieveSearchTemplate();
ActionNamesMapper mapper = new ActionNamesMapper(); return this.jdbcTemplate.queryForObject(sql, rm, new Object[] {});
List<String> actionNames = this.jdbcTemplate.query(mapper.schema(), mapper, new Object[] {}); }
EntityNamesMapper mapper2 = new EntityNamesMapper(); @Override
List<String> entityNames = this.jdbcTemplate.query(mapper2.schema(), mapper2, new Object[] {}); public AuditSearchData retrieveSearchTemplate() {
final Collection<AppUserData> appUsers = appUserReadPlatformService
.retrieveSearchTemplate();
ProcessingResultsMapper mapper3 = new ProcessingResultsMapper(); ActionNamesMapper mapper = new ActionNamesMapper();
Collection<ProcessingResultLookup> processingResults = this.jdbcTemplate.query(mapper3.schema(), mapper3, new Object[] {}); List<String> actionNames = this.jdbcTemplate.query(mapper.schema(),
mapper, new Object[] {});
return new AuditSearchData(appUsers, actionNames, entityNames, processingResults); EntityNamesMapper mapper2 = new EntityNamesMapper();
} List<String> entityNames = this.jdbcTemplate.query(mapper2.schema(),
mapper2, new Object[] {});
private static final class ActionNamesMapper implements RowMapper<String> { ProcessingResultsMapper mapper3 = new ProcessingResultsMapper();
Collection<ProcessingResultLookup> processingResults = this.jdbcTemplate
.query(mapper3.schema(), mapper3, new Object[] {});
@Override return new AuditSearchData(appUsers, actionNames, entityNames,
public String mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException { processingResults);
}
return rs.getString("actionName"); private static final class ActionNamesMapper implements RowMapper<String> {
}
public String schema() { @Override
public String mapRow(final ResultSet rs,
@SuppressWarnings("unused") final int rowNum)
throws SQLException {
return " SELECT distinct(action_name) as actionName FROM m_permission " return rs.getString("actionName");
+ " where action_name is not null and action_name <> 'READ' " }
+ " order by if(action_name in ('CREATE', 'DELETE', 'UPDATE'), action_name, 'ZZZ'), action_name";
}
} public String schema() {
private static final class EntityNamesMapper implements RowMapper<String> { return " SELECT distinct(action_name) as actionName FROM m_permission "
+ " where action_name is not null and action_name <> 'READ' "
+ " order by if(action_name in ('CREATE', 'DELETE', 'UPDATE'), action_name, 'ZZZ'), action_name";
}
@Override }
public String mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
return rs.getString("entityName");
}
public String schema() { private static final class EntityNamesMapper implements RowMapper<String> {
return " select distinct(entity_name) as entityName from m_permission where action_name is not null and action_name <> 'READ' "
+ " order by if(grouping = 'datatable', 'ZZZ', entity_name), entity_name";
}
}
private static final class ProcessingResultsMapper implements RowMapper<ProcessingResultLookup> { @Override
public String mapRow(final ResultSet rs,
@SuppressWarnings("unused") final int rowNum)
throws SQLException {
return rs.getString("entityName");
}
@Override public String schema() {
public ProcessingResultLookup mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException { return " select distinct(entity_name) as entityName from m_permission where action_name is not null and action_name <> 'READ' "
Long id = JdbcSupport.getLong(rs, "id"); + " order by if(grouping = 'datatable', 'ZZZ', entity_name), entity_name";
String processingResult = rs.getString("processingResult"); }
}
return new ProcessingResultLookup(id, processingResult); private static final class ProcessingResultsMapper implements
} RowMapper<ProcessingResultLookup> {
public String schema() { @Override
return " select enum_id as id, enum_message_property as processingResult from r_enum_value where enum_name = 'processing_result_enum' " public ProcessingResultLookup mapRow(final ResultSet rs,
+ " order by enum_id"; @SuppressWarnings("unused") final int rowNum)
} throws SQLException {
} Long id = JdbcSupport.getLong(rs, "id");
String processingResult = rs.getString("processingResult");
return new ProcessingResultLookup(id, processingResult);
}
public String schema() {
return " select enum_id as id, enum_message_property as processingResult from r_enum_value where enum_name = 'processing_result_enum' "
+ " order by enum_id";
}
}
} }