MIFOSX-329: refactor audit work for replacing ids of offices and clients for better display.

This commit is contained in:
Keith Woodlock
2013-05-07 22:34:15 +01:00
parent c99d2f6114
commit 052eb845c9
15 changed files with 354 additions and 410 deletions
@@ -38,192 +38,151 @@ import org.springframework.stereotype.Component;
@Scope("singleton")
public class AuditsApiResource {
private final Set<String> RESPONSE_DATA_PARAMETERS = new HashSet<String>(
Arrays.asList("id", "actionName", "entityName", "resourceId",
"subresourceId", "maker", "madeOnDate", "checker",
"checkedOnDate", "processingResult", "commandAsJson",
"officeName", "groupLevelName", "groupName", "clientName",
"loanAccountNo", "savingsAccountNo"));
private final Set<String> RESPONSE_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("id", "actionName", "entityName", "resourceId",
"subresourceId", "maker", "madeOnDate", "checker", "checkedOnDate", "processingResult", "commandAsJson", "officeName",
"groupLevelName", "groupName", "clientName", "loanAccountNo", "savingsAccountNo"));
private final String resourceNameForPermissions = "AUDIT";
private final String resourceNameForPermissions = "AUDIT";
private final PlatformSecurityContext context;
private final AuditReadPlatformService auditReadPlatformService;
private final ApiRequestParameterHelper apiRequestParameterHelper;
private final DefaultToApiJsonSerializer<AuditData> toApiJsonSerializer;
private final DefaultToApiJsonSerializer<AuditSearchData> toApiJsonSerializerSearchTemplate;
private final PlatformSecurityContext context;
private final AuditReadPlatformService auditReadPlatformService;
private final ApiRequestParameterHelper apiRequestParameterHelper;
private final DefaultToApiJsonSerializer<AuditData> toApiJsonSerializer;
private final DefaultToApiJsonSerializer<AuditSearchData> toApiJsonSerializerSearchTemplate;
@Autowired
public AuditsApiResource(
final PlatformSecurityContext context,
final AuditReadPlatformService auditReadPlatformService,
final ApiRequestParameterHelper apiRequestParameterHelper,
final DefaultToApiJsonSerializer<AuditData> toApiJsonSerializer,
final DefaultToApiJsonSerializer<AuditSearchData> toApiJsonSerializerSearchTemplate) {
this.context = context;
this.auditReadPlatformService = auditReadPlatformService;
this.apiRequestParameterHelper = apiRequestParameterHelper;
this.toApiJsonSerializer = toApiJsonSerializer;
this.toApiJsonSerializerSearchTemplate = toApiJsonSerializerSearchTemplate;
}
@Autowired
public AuditsApiResource(final PlatformSecurityContext context, final AuditReadPlatformService auditReadPlatformService,
final ApiRequestParameterHelper apiRequestParameterHelper, final DefaultToApiJsonSerializer<AuditData> toApiJsonSerializer,
final DefaultToApiJsonSerializer<AuditSearchData> toApiJsonSerializerSearchTemplate) {
this.context = context;
this.auditReadPlatformService = auditReadPlatformService;
this.apiRequestParameterHelper = apiRequestParameterHelper;
this.toApiJsonSerializer = toApiJsonSerializer;
this.toApiJsonSerializerSearchTemplate = toApiJsonSerializerSearchTemplate;
}
@GET
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String retrieveAuditEntries(
@Context final UriInfo uriInfo,
@QueryParam("actionName") final String actionName,
@QueryParam("entityName") final String entityName,
@QueryParam("resourceId") final Long resourceId,
@QueryParam("makerId") final Long makerId,
@QueryParam("makerDateTimeFrom") final String makerDateTimeFrom,
@QueryParam("makerDateTimeTo") final String makerDateTimeTo,
@QueryParam("checkerId") final Long checkerId,
@QueryParam("checkerDateTimeFrom") final String checkerDateTimeFrom,
@QueryParam("checkerDateTimeTo") final String checkerDateTimeTo,
@QueryParam("processingResult") final Integer processingResult,
@QueryParam("officeId") final Integer officeId,
@QueryParam("groupId") final Integer groupId,
@QueryParam("clientId") final Integer clientId,
@QueryParam("loanid") final Integer loanId,
@QueryParam("savingsAccountId") final Integer savingsAccountId) {
@GET
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String retrieveAuditEntries(@Context final UriInfo uriInfo, @QueryParam("actionName") final String actionName,
@QueryParam("entityName") final String entityName, @QueryParam("resourceId") final Long resourceId,
@QueryParam("makerId") final Long makerId, @QueryParam("makerDateTimeFrom") final String makerDateTimeFrom,
@QueryParam("makerDateTimeTo") final String makerDateTimeTo, @QueryParam("checkerId") final Long checkerId,
@QueryParam("checkerDateTimeFrom") final String checkerDateTimeFrom,
@QueryParam("checkerDateTimeTo") final String checkerDateTimeTo,
@QueryParam("processingResult") final Integer processingResult, @QueryParam("officeId") final Integer officeId,
@QueryParam("groupId") final Integer groupId, @QueryParam("clientId") final Integer clientId,
@QueryParam("loanid") final Integer loanId, @QueryParam("savingsAccountId") final Integer savingsAccountId) {
context.authenticatedUser().validateHasReadPermission(
resourceNameForPermissions);
context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions);
final String extraCriteria = getExtraCriteria(actionName, entityName,
resourceId, makerId, makerDateTimeFrom, makerDateTimeTo,
checkerId, checkerDateTimeFrom, checkerDateTimeTo,
processingResult, officeId, groupId, clientId, loanId,
savingsAccountId);
final String extraCriteria = getExtraCriteria(actionName, entityName, resourceId, makerId, makerDateTimeFrom, makerDateTimeTo,
checkerId, checkerDateTimeFrom, checkerDateTimeTo, processingResult, officeId, groupId, clientId, loanId, savingsAccountId);
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper
.process(uriInfo.getQueryParameters());
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters());
final Collection<AuditData> auditEntries = this.auditReadPlatformService
.retrieveAuditEntries(extraCriteria, settings.isIncludeJson());
final Collection<AuditData> auditEntries = this.auditReadPlatformService.retrieveAuditEntries(extraCriteria,
settings.isIncludeJson());
return this.toApiJsonSerializer.serialize(settings, auditEntries,
RESPONSE_DATA_PARAMETERS);
}
return this.toApiJsonSerializer.serialize(settings, auditEntries, 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) {
@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);
context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions);
final AuditData auditEntry = this.auditReadPlatformService
.retrieveAuditEntry(auditId);
final AuditData auditEntry = this.auditReadPlatformService.retrieveAuditEntry(auditId);
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper
.process(uriInfo.getQueryParameters());
return this.toApiJsonSerializer.serialize(settings, auditEntry,
RESPONSE_DATA_PARAMETERS);
}
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.toApiJsonSerializer.serialize(settings, auditEntry, RESPONSE_DATA_PARAMETERS);
}
@GET
@Path("/searchtemplate")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String retrieveAuditSearchTemplate(@Context final UriInfo uriInfo) {
@GET
@Path("/searchtemplate")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String retrieveAuditSearchTemplate(@Context final UriInfo uriInfo) {
context.authenticatedUser().validateHasReadPermission(
resourceNameForPermissions);
context.authenticatedUser().validateHasReadPermission(resourceNameForPermissions);
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper
.process(uriInfo.getQueryParameters());
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters());
final AuditSearchData auditSearchData = this.auditReadPlatformService
.retrieveSearchTemplate("audit");
final AuditSearchData auditSearchData = this.auditReadPlatformService.retrieveSearchTemplate("audit");
final Set<String> RESPONSE_DATA_PARAMETERS_SEARCH_TEMPLATE = new HashSet<String>(
Arrays.asList("appUsers", "actionNames", "entityNames",
"processingResults"));
final Set<String> RESPONSE_DATA_PARAMETERS_SEARCH_TEMPLATE = new HashSet<String>(Arrays.asList("appUsers", "actionNames",
"entityNames", "processingResults"));
return this.toApiJsonSerializerSearchTemplate.serialize(settings,
auditSearchData, RESPONSE_DATA_PARAMETERS_SEARCH_TEMPLATE);
}
return this.toApiJsonSerializerSearchTemplate.serialize(settings, auditSearchData, RESPONSE_DATA_PARAMETERS_SEARCH_TEMPLATE);
}
private String getExtraCriteria(final String actionName,
final String entityName, final Long resourceId, final Long makerId,
final String makerDateTimeFrom, final String makerDateTimeTo,
final Long checkerId, final String checkerDateTimeFrom,
final String checkerDateTimeTo, final Integer processingResult,
final Integer officeId, final Integer groupId,
final Integer clientId, final Integer loanId,
final Integer savingsAccountId) {
private String getExtraCriteria(final String actionName, final String entityName, final Long resourceId, final Long makerId,
final String makerDateTimeFrom, final String makerDateTimeTo, final Long checkerId, final String checkerDateTimeFrom,
final String checkerDateTimeTo, final Integer processingResult, final Integer officeId, final Integer groupId,
final Integer clientId, final Integer loanId, final Integer savingsAccountId) {
String extraCriteria = "";
String extraCriteria = "";
if (actionName != null) {
extraCriteria += " and aud.action_name = "
+ ApiParameterHelper.sqlEncodeString(actionName);
}
if (entityName != null) {
extraCriteria += " and aud.entity_name like "
+ ApiParameterHelper.sqlEncodeString(entityName + "%");
}
if (actionName != null) {
extraCriteria += " and aud.action_name = " + ApiParameterHelper.sqlEncodeString(actionName);
}
if (entityName != null) {
extraCriteria += " and aud.entity_name like " + ApiParameterHelper.sqlEncodeString(entityName + "%");
}
if (resourceId != null) {
extraCriteria += " and aud.resource_id = " + resourceId;
}
if (makerId != null) {
extraCriteria += " and aud.maker_id = " + makerId;
}
if (checkerId != null) {
extraCriteria += " and aud.checker_id = " + checkerId;
}
if (makerDateTimeFrom != null) {
extraCriteria += " and aud.made_on_date >= "
+ ApiParameterHelper.sqlEncodeString(makerDateTimeFrom);
}
if (makerDateTimeTo != null) {
extraCriteria += " and aud.made_on_date <= "
+ ApiParameterHelper.sqlEncodeString(makerDateTimeTo);
}
if (checkerDateTimeFrom != null) {
extraCriteria += " and aud.checked_on_date >= "
+ ApiParameterHelper.sqlEncodeString(checkerDateTimeFrom);
}
if (checkerDateTimeTo != null) {
extraCriteria += " and aud.checked_on_date <= "
+ ApiParameterHelper.sqlEncodeString(checkerDateTimeTo);
}
if (resourceId != null) {
extraCriteria += " and aud.resource_id = " + resourceId;
}
if (makerId != null) {
extraCriteria += " and aud.maker_id = " + makerId;
}
if (checkerId != null) {
extraCriteria += " and aud.checker_id = " + checkerId;
}
if (makerDateTimeFrom != null) {
extraCriteria += " and aud.made_on_date >= " + ApiParameterHelper.sqlEncodeString(makerDateTimeFrom);
}
if (makerDateTimeTo != null) {
extraCriteria += " and aud.made_on_date <= " + ApiParameterHelper.sqlEncodeString(makerDateTimeTo);
}
if (checkerDateTimeFrom != null) {
extraCriteria += " and aud.checked_on_date >= " + ApiParameterHelper.sqlEncodeString(checkerDateTimeFrom);
}
if (checkerDateTimeTo != null) {
extraCriteria += " and aud.checked_on_date <= " + ApiParameterHelper.sqlEncodeString(checkerDateTimeTo);
}
if (processingResult != null) {
extraCriteria += " and aud.processing_result_enum = "
+ processingResult;
}
if (processingResult != null) {
extraCriteria += " and aud.processing_result_enum = " + processingResult;
}
if (officeId != null) {
extraCriteria += " and aud.office_id = " + officeId;
}
if (officeId != null) {
extraCriteria += " and aud.office_id = " + officeId;
}
if (groupId != null) {
extraCriteria += " and aud.group_id = " + groupId;
}
if (groupId != null) {
extraCriteria += " and aud.group_id = " + groupId;
}
if (clientId != null) {
extraCriteria += " and aud.client_id = " + clientId;
}
if (clientId != null) {
extraCriteria += " and aud.client_id = " + clientId;
}
if (loanId != null) {
extraCriteria += " and aud.loan_id = " + loanId;
}
if (loanId != null) {
extraCriteria += " and aud.loan_id = " + loanId;
}
if (savingsAccountId != null) {
extraCriteria += " and aud.savings_account_id = " + savingsAccountId;
}
if (savingsAccountId != null) {
extraCriteria += " and aud.savings_account_id = " + savingsAccountId;
}
if (StringUtils.isNotBlank(extraCriteria)) {
extraCriteria = extraCriteria.substring(4);
}
if (StringUtils.isNotBlank(extraCriteria)) {
extraCriteria = extraCriteria.substring(4);
}
return extraCriteria;
}
return extraCriteria;
}
}
@@ -12,75 +12,70 @@ import org.joda.time.DateTime;
*/
public final class AuditData {
@SuppressWarnings("unused")
private final Long id;
@SuppressWarnings("unused")
private final String actionName;
@SuppressWarnings("unused")
private final String entityName;
@SuppressWarnings("unused")
private final Long resourceId;
@SuppressWarnings("unused")
private final Long subresourceId;
@SuppressWarnings("unused")
private final String maker;
@SuppressWarnings("unused")
private final DateTime madeOnDate;
@SuppressWarnings("unused")
private final String checker;
@SuppressWarnings("unused")
private final DateTime checkedOnDate;
@SuppressWarnings("unused")
private final String processingResult;
@SuppressWarnings("unused")
private 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;
@SuppressWarnings("unused")
private final Long id;
@SuppressWarnings("unused")
private final String actionName;
@SuppressWarnings("unused")
private final String entityName;
@SuppressWarnings("unused")
private final Long resourceId;
@SuppressWarnings("unused")
private final Long subresourceId;
@SuppressWarnings("unused")
private final String maker;
@SuppressWarnings("unused")
private final DateTime madeOnDate;
@SuppressWarnings("unused")
private final String checker;
@SuppressWarnings("unused")
private final DateTime checkedOnDate;
@SuppressWarnings("unused")
private final String processingResult;
@SuppressWarnings("unused")
private 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,
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) {
public AuditData(final Long id, final String actionName, final String entityName, final Long resourceId, 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.actionName = actionName;
this.entityName = entityName;
this.resourceId = resourceId;
this.subresourceId = subresourceId;
this.maker = maker;
this.madeOnDate = madeOnDate;
this.checker = checker;
this.checkedOnDate = checkedOnDate;
this.commandAsJson = commandAsJson;
this.processingResult = processingResult;
this.officeName = officeName;
this.groupLevelName = groupLevelName;
this.groupName = groupName;
this.clientName = clientName;
this.loanAccountNo = loanAccountNo;
this.savingsAccountNo = savingsAccountNo;
}
this.id = id;
this.actionName = actionName;
this.entityName = entityName;
this.resourceId = resourceId;
this.subresourceId = subresourceId;
this.maker = maker;
this.madeOnDate = madeOnDate;
this.checker = checker;
this.checkedOnDate = checkedOnDate;
this.commandAsJson = commandAsJson;
this.processingResult = processingResult;
this.officeName = officeName;
this.groupLevelName = groupLevelName;
this.groupName = groupName;
this.clientName = clientName;
this.loanAccountNo = loanAccountNo;
this.savingsAccountNo = savingsAccountNo;
}
public void setCommandAsJson(String commandAsJson) {
this.commandAsJson = commandAsJson;
}
public void setCommandAsJson(final String commandAsJson) {
this.commandAsJson = commandAsJson;
}
public String getCommandAsJson() {
return this.commandAsJson;
}
public String getCommandAsJson() {
return this.commandAsJson;
}
}
@@ -54,13 +54,12 @@ public class CommandWrapper {
this.href = resourceGetUrl;
this.json = null;
this.transactionId = null;
}
public CommandWrapper(final Long officeId, final Long groupId, final Long clientId, final Long loanId, final Long savingsId,
final String actionName, final String entityName, final Long entityId, final Long subentityId, final Long codeId,
final String supportedEntityType, final Long supportedEntityId, final String href, final String json, final String transactionId
) {
final String supportedEntityType, final Long supportedEntityId, final String href, final String json, final String transactionId) {
this.commandId = null;
this.officeId = officeId;
this.groupId = groupId;
@@ -78,7 +77,7 @@ public class CommandWrapper {
this.href = href;
this.json = json;
this.transactionId = transactionId;
}
public Long commandId() {
@@ -128,8 +127,7 @@ public class CommandWrapper {
public String getTransactionId() {
return this.transactionId;
}
public String getEntityName() {
return this.entityName;
}
@@ -498,12 +496,12 @@ public class CommandWrapper {
public boolean isReportResource() {
return this.entityName.equalsIgnoreCase("REPORT");
}
public boolean isAssociateClients(){
public boolean isAssociateClients() {
return this.actionName.equalsIgnoreCase("ASSOCIATECLIENTS");
}
public boolean isDisassociateClients(){
public boolean isDisassociateClients() {
return this.actionName.equalsIgnoreCase("DISASSOCIATECLIENTS");
}
}
@@ -12,7 +12,7 @@ import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceN
*/
public class CommandNotAwaitingApprovalException extends AbstractPlatformResourceNotFoundException {
public CommandNotAwaitingApprovalException(Long id) {
super("error.msg.command.id.not.awaiting.approval", "Audit with identifier " + id + " is Not Awaiting Approval", id);
}
public CommandNotAwaitingApprovalException(final Long id) {
super("error.msg.command.id.not.awaiting.approval", "Audit with identifier " + id + " is Not Awaiting Approval", id);
}
}
@@ -12,7 +12,7 @@ import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceN
*/
public class CommandNotFoundException extends AbstractPlatformResourceNotFoundException {
public CommandNotFoundException(Long id) {
super("error.msg.command.id.invalid", "Audit with identifier " + id + " does not exist", id);
}
public CommandNotFoundException(final Long id) {
super("error.msg.command.id.invalid", "Audit with identifier " + id + " does not exist", id);
}
}
@@ -7,28 +7,26 @@ package org.mifosplatform.commands.exception;
import org.mifosplatform.commands.domain.CommandSource;
public class RollbackTransactionAsCommandIsNotApprovedByCheckerException extends
RuntimeException {
public class RollbackTransactionAsCommandIsNotApprovedByCheckerException extends RuntimeException {
/**
* When maker-checker is configured globally and also for the current
* transaction.
*
* An initial save determines if there are any integrity rule or data
* problems.
*
* If there isn't... and the transaction is from a maker... then this roll
* back is issued and the commandSourceResult is used to write the audit
* entry.
*/
private final CommandSource commandSourceResult;
/**
* When maker-checker is configured globally and also for the current
* transaction.
*
* An initial save determines if there are any integrity rule or data
* problems.
*
* If there isn't... and the transaction is from a maker... then this roll
* back is issued and the commandSourceResult is used to write the audit
* entry.
*/
private final CommandSource commandSourceResult;
public RollbackTransactionAsCommandIsNotApprovedByCheckerException(
final CommandSource commandSourceResult) {
this.commandSourceResult = commandSourceResult;
}
public RollbackTransactionAsCommandIsNotApprovedByCheckerException(final CommandSource commandSourceResult) {
this.commandSourceResult = commandSourceResult;
}
public CommandSource getCommandSourceResult() {
return this.commandSourceResult;
}
public CommandSource getCommandSourceResult() {
return this.commandSourceResult;
}
}
@@ -11,4 +11,4 @@ import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
public interface NewCommandSourceHandler {
CommandProcessingResult processCommand(JsonCommand command);
}
}
@@ -19,5 +19,4 @@ public interface AuditReadPlatformService {
AuditData retrieveAuditEntry(Long auditId);
AuditSearchData retrieveSearchTemplate(String useType);
}
@@ -5,10 +5,10 @@
*/
package org.mifosplatform.commands.service;
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -19,8 +19,13 @@ import org.mifosplatform.commands.data.AuditSearchData;
import org.mifosplatform.commands.data.ProcessingResultLookup;
import org.mifosplatform.infrastructure.core.domain.JdbcSupport;
import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException;
import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper;
import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSource;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.organisation.office.data.OfficeData;
import org.mifosplatform.organisation.office.service.OfficeReadPlatformService;
import org.mifosplatform.portfolio.client.data.ClientData;
import org.mifosplatform.portfolio.client.service.ClientReadPlatformService;
import org.mifosplatform.useradministration.data.AppUserData;
import org.mifosplatform.useradministration.domain.AppUser;
import org.mifosplatform.useradministration.service.AppUserReadPlatformService;
@@ -31,22 +36,30 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
@Service
public class AuditReadPlatformServiceImpl implements AuditReadPlatformService {
private final static Logger logger = LoggerFactory.getLogger(AuditReadPlatformServiceImpl.class);
private final JdbcTemplate jdbcTemplate;
private final PlatformSecurityContext context;
private final FromJsonHelper fromApiJsonHelper;
private final AppUserReadPlatformService appUserReadPlatformService;
private final OfficeReadPlatformService officeReadPlatformService;
private final ClientReadPlatformService clientReadPlatformService;
@Autowired
public AuditReadPlatformServiceImpl(final PlatformSecurityContext context, final TenantAwareRoutingDataSource dataSource,
final AppUserReadPlatformService appUserReadPlatformService) {
final FromJsonHelper fromApiJsonHelper, final AppUserReadPlatformService appUserReadPlatformService,
final OfficeReadPlatformService officeReadPlatformService, final ClientReadPlatformService clientReadPlatformService) {
this.context = context;
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.fromApiJsonHelper = fromApiJsonHelper;
this.appUserReadPlatformService = appUserReadPlatformService;
this.officeReadPlatformService = officeReadPlatformService;
this.clientReadPlatformService = clientReadPlatformService;
}
private static final class AuditMapper implements RowMapper<AuditData> {
@@ -166,8 +179,7 @@ public class AuditReadPlatformServiceImpl implements AuditReadPlatformService {
return this.jdbcTemplate.query(sql, rm, new Object[] {});
}
@SuppressWarnings("unchecked")
@Override
@Override
public AuditData retrieveAuditEntry(final Long auditId) {
AppUser currentUser = context.authenticatedUser();
@@ -175,50 +187,47 @@ public class AuditReadPlatformServiceImpl implements AuditReadPlatformService {
final AuditMapper rm = new AuditMapper();
String sql = "select " + rm.schema(true, hierarchy);
sql += " where aud.id = " + auditId;
AuditData auditResult = this.jdbcTemplate.queryForObject(sql, rm, new Object[] {});
String commandAsJson = auditResult.getCommandAsJson();
Map<String,Object> commandAsJsonMap = new HashMap<String,Object>();
commandAsJsonMap = new Gson().fromJson(commandAsJson, commandAsJsonMap.getClass());
String auditAsJson = auditResult.getCommandAsJson();
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
final Map<String, Object> commandAsJsonMap = this.fromApiJsonHelper.extractObjectMap(typeOfMap, auditAsJson);
if (commandAsJsonMap.containsKey("officeId")) {
Object officeIdObj = commandAsJsonMap.get("officeId");
Integer officeId = -1;
if (officeIdObj instanceof Integer) {
officeId = (Integer)officeIdObj;
} else if (officeIdObj instanceof Double) {
officeId = ((Double) officeIdObj).intValue();
}
if (officeId > -1) {
sql = " SELECT name FROM m_office WHERE id = " + officeId;
String officeName = this.jdbcTemplate.queryForObject(sql, String.class);
String regex = "\"officeId\":\\s\\d+";
commandAsJson = commandAsJson.replaceAll(regex, "\"officeName\": \""+officeName+"\"");
}
final Object officeIdObj = commandAsJsonMap.get("officeId");
if (officeIdObj != null && StringUtils.isNotBlank(officeIdObj.toString())) {
final Long officeId = Long.valueOf(officeIdObj.toString());
OfficeData office = this.officeReadPlatformService.retrieveOffice(officeId);
commandAsJsonMap.remove("officeId");
commandAsJsonMap.put("officeName", office.name());
auditAsJson = this.fromApiJsonHelper.toJson(commandAsJsonMap);
}
}
if (commandAsJsonMap.containsKey("clientId")) {
Object clientIdObj = commandAsJsonMap.get("clientId");
Integer clientId = -1;
if (clientIdObj instanceof Integer) {
clientId = (Integer)clientIdObj;
} else if (clientIdObj instanceof Double) {
clientId = ((Double) clientIdObj).intValue();
}
if (clientId > -1) {
sql = " SELECT display_name FROM m_client WHERE id = " + clientId;
String clientName = this.jdbcTemplate.queryForObject(sql, String.class);
String regex = "\"clientId\":\\s\\d+";
commandAsJson = commandAsJson.replaceAll(regex, "\"clientName\": \""+clientName+"\"");
}
final Object clientIdObj = commandAsJsonMap.get("clientId");
if (clientIdObj != null && StringUtils.isNotBlank(clientIdObj.toString())) {
final Long clientId = Long.valueOf(clientIdObj.toString());
ClientData client = this.clientReadPlatformService.retrieveOne(clientId);
commandAsJsonMap.remove("clientId");
commandAsJsonMap.put("clientName", client.displayName());
auditAsJson = this.fromApiJsonHelper.toJson(commandAsJsonMap);
}
}
auditResult.setCommandAsJson(commandAsJson);
auditResult.setCommandAsJson(auditAsJson);
return auditResult;
}
@@ -308,5 +317,5 @@ public class AuditReadPlatformServiceImpl implements AuditReadPlatformService {
+ " order by enum_id";
}
}
}
@@ -12,9 +12,8 @@ import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
public interface CommandProcessingService {
CommandProcessingResult processAndLogCommand(CommandWrapper wrapper,
JsonCommand command, boolean isApprovedByChecker);
CommandProcessingResult processAndLogCommand(CommandWrapper wrapper, JsonCommand command, boolean isApprovedByChecker);
CommandProcessingResult logCommand(CommandSource commandSourceResult);
CommandProcessingResult logCommand(CommandSource commandSourceResult);
}
}
@@ -28,7 +28,7 @@ public class CommandWrapperBuilder {
public CommandWrapper build() {
return new CommandWrapper(this.officeId, this.groupId, this.clientId, this.loanId, this.savingsId, this.actionName,
this.entityName, this.entityId, this.subentityId, this.codeId, this.supportedEntityType, this.supportedEntityId, this.href,
this.json, this.transactionId );
this.json, this.transactionId);
}
public CommandWrapperBuilder withLoanId(final Long withLoanId) {
@@ -865,7 +865,7 @@ public class CommandWrapperBuilder {
this.href = "/groups/" + groupId + "?command=associateClients";
return this;
}
public CommandWrapperBuilder disassociateClientsFromGroup(final Long groupId) {
this.actionName = "DISASSOCIATECLIENTS";
this.entityName = "GROUP";
@@ -874,7 +874,7 @@ public class CommandWrapperBuilder {
this.href = "/groups/" + groupId + "?command=disassociateClients";
return this;
}
public CommandWrapperBuilder unassignStaff(final Long groupId) {
this.actionName = "UNASSIGNSTAFF";
this.entityName = "GROUP";
@@ -22,120 +22,95 @@ import org.springframework.transaction.annotation.Transactional;
import com.google.gson.JsonElement;
@Service
public class PortfolioCommandSourceWritePlatformServiceImpl implements
PortfolioCommandSourceWritePlatformService {
public class PortfolioCommandSourceWritePlatformServiceImpl implements PortfolioCommandSourceWritePlatformService {
private final PlatformSecurityContext context;
private final CommandSourceRepository commandSourceRepository;
private final FromJsonHelper fromApiJsonHelper;
private final CommandProcessingService processAndLogCommandService;
private final PlatformSecurityContext context;
private final CommandSourceRepository commandSourceRepository;
private final FromJsonHelper fromApiJsonHelper;
private final CommandProcessingService processAndLogCommandService;
@Autowired
public PortfolioCommandSourceWritePlatformServiceImpl(
final PlatformSecurityContext context,
final CommandSourceRepository commandSourceRepository,
final FromJsonHelper fromApiJsonHelper,
final CommandProcessingService processAndLogCommandService) {
this.context = context;
this.commandSourceRepository = commandSourceRepository;
this.fromApiJsonHelper = fromApiJsonHelper;
this.processAndLogCommandService = processAndLogCommandService;
}
@Autowired
public PortfolioCommandSourceWritePlatformServiceImpl(final PlatformSecurityContext context,
final CommandSourceRepository commandSourceRepository, final FromJsonHelper fromApiJsonHelper,
final CommandProcessingService processAndLogCommandService) {
this.context = context;
this.commandSourceRepository = commandSourceRepository;
this.fromApiJsonHelper = fromApiJsonHelper;
this.processAndLogCommandService = processAndLogCommandService;
}
@Override
public CommandProcessingResult logCommandSource(final CommandWrapper wrapper) {
@Override
public CommandProcessingResult logCommandSource(final CommandWrapper wrapper) {
boolean isApprovedByChecker = false;
// check if is update of own account details
if (wrapper.isUpdateOfOwnUserDetails(context.authenticatedUser()
.getId())) {
// then allow this operation to proceed.
// maker checker doesnt mean anything here.
isApprovedByChecker = true; // set to true in case permissions have
// been maker-checker enabled by
// accident.
} else {
// if not user changing their own details - check user has
// permission to perform specific task.
context.authenticatedUser().validateHasPermissionTo(
wrapper.getTaskPermissionName());
}
boolean isApprovedByChecker = false;
// check if is update of own account details
if (wrapper.isUpdateOfOwnUserDetails(context.authenticatedUser().getId())) {
// then allow this operation to proceed.
// maker checker doesnt mean anything here.
isApprovedByChecker = true; // set to true in case permissions have
// been maker-checker enabled by
// accident.
} else {
// if not user changing their own details - check user has
// permission to perform specific task.
context.authenticatedUser().validateHasPermissionTo(wrapper.getTaskPermissionName());
}
final String json = wrapper.getJson();
CommandProcessingResult result = null;
try {
final JsonElement parsedCommand = this.fromApiJsonHelper
.parse(json);
final JsonCommand command = JsonCommand.from(json, parsedCommand,
this.fromApiJsonHelper, wrapper.getEntityName(),
wrapper.getEntityId(), wrapper.getSubentityId(),
wrapper.getGroupId(), wrapper.getClientId(),
wrapper.getLoanId(), wrapper.getSavingsId(),
wrapper.getCodeId(), wrapper.getSupportedEntityType(),
wrapper.getSupportedEntityId(), wrapper.getTransactionId());
final String json = wrapper.getJson();
CommandProcessingResult result = null;
try {
final JsonElement parsedCommand = this.fromApiJsonHelper.parse(json);
final JsonCommand command = JsonCommand.from(json, parsedCommand, this.fromApiJsonHelper, wrapper.getEntityName(),
wrapper.getEntityId(), wrapper.getSubentityId(), wrapper.getGroupId(), wrapper.getClientId(), wrapper.getLoanId(),
wrapper.getSavingsId(), wrapper.getCodeId(), wrapper.getSupportedEntityType(), wrapper.getSupportedEntityId(),
wrapper.getTransactionId());
result = this.processAndLogCommandService.processAndLogCommand(
wrapper, command, isApprovedByChecker);
} catch (RollbackTransactionAsCommandIsNotApprovedByCheckerException e) {
result = this.processAndLogCommandService.processAndLogCommand(wrapper, command, isApprovedByChecker);
} catch (RollbackTransactionAsCommandIsNotApprovedByCheckerException e) {
result = this.processAndLogCommandService.logCommand(e
.getCommandSourceResult());
}
result = this.processAndLogCommandService.logCommand(e.getCommandSourceResult());
}
return result;
}
return result;
}
@Override
public CommandProcessingResult approveEntry(final Long makerCheckerId) {
@Override
public CommandProcessingResult approveEntry(final Long makerCheckerId) {
CommandSource commandSourceInput = validateMakerCheckerTransaction(makerCheckerId);
CommandSource commandSourceInput = validateMakerCheckerTransaction(makerCheckerId);
final CommandWrapper wrapper = CommandWrapper.fromExistingCommand(
makerCheckerId, commandSourceInput.getActionName(),
commandSourceInput.getEntityName(),
commandSourceInput.resourceId(),
commandSourceInput.subresourceId(),
commandSourceInput.getResourceGetUrl());
final CommandWrapper wrapper = CommandWrapper.fromExistingCommand(makerCheckerId, commandSourceInput.getActionName(),
commandSourceInput.getEntityName(), commandSourceInput.resourceId(), commandSourceInput.subresourceId(),
commandSourceInput.getResourceGetUrl());
final JsonElement parsedCommand = this.fromApiJsonHelper
.parse(commandSourceInput.json());
final JsonCommand command = JsonCommand.fromExistingCommand(
makerCheckerId, commandSourceInput.json(), parsedCommand,
this.fromApiJsonHelper, commandSourceInput.getEntityName(),
commandSourceInput.resourceId(),
commandSourceInput.subresourceId());
final JsonElement parsedCommand = this.fromApiJsonHelper.parse(commandSourceInput.json());
final JsonCommand command = JsonCommand.fromExistingCommand(makerCheckerId, commandSourceInput.json(), parsedCommand,
this.fromApiJsonHelper, commandSourceInput.getEntityName(), commandSourceInput.resourceId(),
commandSourceInput.subresourceId());
final boolean makerCheckerApproval = true;
return this.processAndLogCommandService.processAndLogCommand(wrapper,
command, makerCheckerApproval);
}
final boolean makerCheckerApproval = true;
return this.processAndLogCommandService.processAndLogCommand(wrapper, command, makerCheckerApproval);
}
@Transactional
@Override
public Long deleteEntry(final Long makerCheckerId) {
@Transactional
@Override
public Long deleteEntry(final Long makerCheckerId) {
validateMakerCheckerTransaction(makerCheckerId);
validateMakerCheckerTransaction(makerCheckerId);
this.commandSourceRepository.delete(makerCheckerId);
this.commandSourceRepository.delete(makerCheckerId);
return makerCheckerId;
}
return makerCheckerId;
}
private CommandSource validateMakerCheckerTransaction(
final Long makerCheckerId) {
private CommandSource validateMakerCheckerTransaction(final Long makerCheckerId) {
final CommandSource commandSourceInput = this.commandSourceRepository
.findOne(makerCheckerId);
if (commandSourceInput == null) {
throw new CommandNotFoundException(makerCheckerId);
}
if (!(commandSourceInput.isMarkedAsAwaitingApproval())) {
throw new CommandNotAwaitingApprovalException(makerCheckerId);
}
final CommandSource commandSourceInput = this.commandSourceRepository.findOne(makerCheckerId);
if (commandSourceInput == null) { throw new CommandNotFoundException(makerCheckerId); }
if (!(commandSourceInput.isMarkedAsAwaitingApproval())) { throw new CommandNotAwaitingApprovalException(makerCheckerId); }
context.authenticatedUser().validateHasCheckerPermissionTo(
commandSourceInput.getPermissionCode());
context.authenticatedUser().validateHasCheckerPermissionTo(commandSourceInput.getPermissionCode());
return commandSourceInput;
}
return commandSourceInput;
}
}
@@ -97,9 +97,9 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
commandSourceResult.markAsAwaitingApproval();
commandSourceRepository.save(commandSourceResult);
return new CommandProcessingResultBuilder().withCommandId(commandSourceResult.getId()).withEntityId(commandSourceResult.getResourceId())
.build();
return new CommandProcessingResultBuilder().withCommandId(commandSourceResult.getId())
.withEntityId(commandSourceResult.getResourceId()).build();
}
private NewCommandSourceHandler findCommandHandler(final CommandWrapper wrapper) {
@@ -48,6 +48,10 @@ public class FromJsonHelper {
return this.gsonConverter.fromJson(json, typeOfMap);
}
public Map<String, Object> extractObjectMap(final Type typeOfMap, final String json) {
return this.gsonConverter.fromJson(json, typeOfMap);
}
public <T> T fromJson(final String json, final Class<T> classOfT) {
return this.gsonConverter.fromJson(json, classOfT);
}
@@ -56,6 +60,10 @@ public class FromJsonHelper {
return this.gsonConverter.toJson(jsonElement);
}
public String toJson(final Object object) {
return this.gsonConverter.toJson(object);
}
public void checkForUnsupportedParameters(final Type typeOfMap, final String json, final Set<String> supportedParams) {
if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
@@ -55,4 +55,8 @@ public class OfficeData {
public boolean hasIdentifyOf(final Long officeId) {
return this.id.equals(officeId);
}
public String name() {
return this.name;
}
}