From 1c49268e77638863475b4a209c87c9e098499322 Mon Sep 17 00:00:00 2001 From: Madhukar Date: Wed, 2 Apr 2014 12:49:25 +0530 Subject: [PATCH] MIFOSX-1027 Fixing issues with maker checker --- ...n_id_column_m_portfolio_command_source.sql | 2 + .../commands/domain/CommandSource.java | 20 +++- .../commands/domain/CommandWrapper.java | 43 +------ .../service/CommandWrapperBuilder.java | 111 +++++++++++------- ...CommandSourceWritePlatformServiceImpl.java | 9 +- .../SynchronousCommandProcessingService.java | 18 ++- .../DeleteCodeValueCommandHandler.java | 2 +- .../UpdateCodeValueCommandHandler.java | 2 +- ...WritePlatformServiceJpaRepositoryImpl.java | 2 +- .../infrastructure/core/api/JsonCommand.java | 38 ++---- .../calendar/api/CalendarsApiResource.java | 28 ++++- .../calendar/domain/CalendarEntityType.java | 17 ++- .../calendar/domain/CalendarInstance.java | 5 +- ...lendarEntityTypeNotSupportedException.java | 11 ++ ...WritePlatformServiceJpaRepositoryImpl.java | 45 ++++--- ...WritePlatformServiceJpaRepositoryImpl.java | 2 +- .../meeting/api/MeetingsApiResource.java | 29 ++++- ...WritePlatformServiceJpaRepositoryImpl.java | 28 +++-- .../portfolio/note/api/NotesApiResource.java | 32 +++-- .../portfolio/note/domain/NoteType.java | 4 + ...WritePlatformServiceJpaRepositoryImpl.java | 64 ++++++---- 21 files changed, 322 insertions(+), 190 deletions(-) create mode 100644 mifosng-db/migrations/core_db/V159__add_transaction_id_column_m_portfolio_command_source.sql create mode 100644 mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/exception/CalendarEntityTypeNotSupportedException.java diff --git a/mifosng-db/migrations/core_db/V159__add_transaction_id_column_m_portfolio_command_source.sql b/mifosng-db/migrations/core_db/V159__add_transaction_id_column_m_portfolio_command_source.sql new file mode 100644 index 000000000..728bee53b --- /dev/null +++ b/mifosng-db/migrations/core_db/V159__add_transaction_id_column_m_portfolio_command_source.sql @@ -0,0 +1,2 @@ +ALTER TABLE `m_portfolio_command_source` + ADD COLUMN `transaction_id` VARCHAR(100) NULL DEFAULT NULL AFTER `product_id`; \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandSource.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandSource.java index d9b43b307..747c7d8e9 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandSource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandSource.java @@ -79,6 +79,9 @@ public class CommandSource extends AbstractPersistable { @Column(name = "product_id") private Long productId; + + @Column(name = "transaction_id", length = 100) + private String transactionId; public static CommandSource fullEntryFrom(final CommandWrapper wrapper, final JsonCommand command, final AppUser maker) { return new CommandSource(wrapper.actionName(), wrapper.entityName(), wrapper.getHref(), command.entityId(), command.subentityId(), @@ -167,13 +170,14 @@ public class CommandSource extends AbstractPersistable { } public void updateForAudit(final Long officeId, final Long groupId, final Long clientId, final Long loanId, final Long savingsId, - final Long productId) { + final Long productId, final String transactionId) { this.officeId = officeId; this.groupId = groupId; this.clientId = clientId; this.loanId = loanId; this.savingsId = savingsId; this.productId = productId; + this.transactionId = transactionId; } public String getResourceGetUrl() { @@ -218,6 +222,16 @@ public class CommandSource extends AbstractPersistable { public Long getSavingsId() { return savingsId; } - - + + /** + * @return the transactionId + */ + public String getTransactionId() { + return this.transactionId; + } + + public void updateTransaction(final String transactionId) { + this.transactionId = transactionId; + } + } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java index eab81a155..94df268d7 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/domain/CommandWrapper.java @@ -21,12 +21,8 @@ public class CommandWrapper { private final Long subentityId; private final String href; private final String json; - private final Long codeId; private final String transactionId; - private final String supportedEntityType; - private final Long supportedEntityId; private final Long productId; - private Long templateId; public static CommandWrapper wrap(final String actionName, final String entityName, final Long resourceId, final Long subresourceId) { return new CommandWrapper(null, actionName, entityName, resourceId, subresourceId, null, null); @@ -39,9 +35,9 @@ public class CommandWrapper { public static CommandWrapper fromExistingCommand(final Long commandId, final String actionName, final String entityName, final Long resourceId, final Long subresourceId, final String resourceGetUrl, final Long productId, final Long officeId, - final Long groupId, final Long clientId, final Long loanId, final Long savingsId) { + final Long groupId, final Long clientId, final Long loanId, final Long savingsId, final String transactionId) { return new CommandWrapper(commandId, actionName, entityName, resourceId, subresourceId, resourceGetUrl, productId, officeId, - groupId, clientId, loanId, savingsId); + groupId, clientId, loanId, savingsId, transactionId); } private CommandWrapper(final Long commandId, final String actionName, final String entityName, final Long resourceId, @@ -57,9 +53,6 @@ public class CommandWrapper { this.taskPermissionName = actionName + "_" + entityName; this.entityId = resourceId; this.subentityId = subresourceId; - this.codeId = null; - this.supportedEntityType = null; - this.supportedEntityId = null; this.href = resourceGetUrl; this.json = null; this.transactionId = null; @@ -67,9 +60,8 @@ public class CommandWrapper { } 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 Long productId, final Long templateId) { + final String actionName, final String entityName, final Long entityId, final Long subentityId, final String href, + final String json, final String transactionId, final Long productId) { this.commandId = null; this.officeId = officeId; this.groupId = groupId; @@ -81,19 +73,15 @@ public class CommandWrapper { this.taskPermissionName = actionName + "_" + entityName; this.entityId = entityId; this.subentityId = subentityId; - this.codeId = codeId; - this.supportedEntityType = supportedEntityType; - this.supportedEntityId = supportedEntityId; this.href = href; this.json = json; this.transactionId = transactionId; this.productId = productId; - this.templateId = templateId; } private CommandWrapper(final Long commandId, final String actionName, final String entityName, final Long resourceId, final Long subresourceId, final String resourceGetUrl, final Long productId, final Long officeId, final Long groupId, - final Long clientId, final Long loanId, final Long savingsId) { + final Long clientId, final Long loanId, final Long savingsId, final String transactionId) { this.commandId = commandId; this.officeId = officeId; @@ -106,12 +94,9 @@ public class CommandWrapper { this.taskPermissionName = actionName + "_" + entityName; this.entityId = resourceId; this.subentityId = subresourceId; - this.codeId = null; - this.supportedEntityType = null; - this.supportedEntityId = null; this.href = resourceGetUrl; this.json = null; - this.transactionId = null; + this.transactionId = transactionId; this.productId = productId; } @@ -159,10 +144,6 @@ public class CommandWrapper { return this.taskPermissionName; } - public Long getCodeId() { - return this.codeId; - } - public String getHref() { return this.href; } @@ -175,10 +156,6 @@ public class CommandWrapper { return this.transactionId; } - public Long getTemplateId() { - return this.templateId; - } - public String getEntityName() { return this.entityName; } @@ -207,14 +184,6 @@ public class CommandWrapper { return this.savingsId; } - public Long getSupportedEntityId() { - return this.supportedEntityId; - } - - public String getSupportedEntityType() { - return this.supportedEntityType; - } - public Long getProductId() { return this.productId; } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java index deead4c67..01411be1b 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/CommandWrapperBuilder.java @@ -6,7 +6,6 @@ package org.mifosplatform.commands.service; import org.mifosplatform.commands.domain.CommandWrapper; -import org.mifosplatform.portfolio.calendar.domain.CalendarEntityType; public class CommandWrapperBuilder { @@ -21,28 +20,43 @@ public class CommandWrapperBuilder { private Long subentityId; private String href; private String json = "{}"; - private Long codeId; private String transactionId; - private String supportedEntityType; - private Long supportedEntityId; private Long productId; - private Long templateId; 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.productId, this.templateId); + this.entityName, this.entityId, this.subentityId, this.href, this.json, this.transactionId, this.productId); } public CommandWrapperBuilder withLoanId(final Long withLoanId) { this.loanId = withLoanId; return this; } + + public CommandWrapperBuilder withSavingsId(final Long withSavingsId) { + this.savingsId = withSavingsId; + return this; + } public CommandWrapperBuilder withClientId(final Long withClientId) { this.clientId = withClientId; return this; } + + public CommandWrapperBuilder withGroupId(final Long withGroupId) { + this.groupId = withGroupId; + return this; + } + + public CommandWrapperBuilder withEntityName(final String withEntityName) { + this.entityName = withEntityName; + return this; + } + + public CommandWrapperBuilder withSubEntityId(final Long withSubEntityId) { + this.subentityId = withSubEntityId; + return this; + } public CommandWrapperBuilder withJson(final String withJson) { this.json = withJson; @@ -729,7 +743,7 @@ public class CommandWrapperBuilder { public CommandWrapperBuilder createCodeValue(final Long codeId) { this.actionName = "CREATE"; this.entityName = "CODEVALUE"; - this.codeId = codeId; + this.entityId = codeId; this.href = "/codes/" + codeId + "/codevalues/template"; return this; } @@ -737,8 +751,8 @@ public class CommandWrapperBuilder { public CommandWrapperBuilder updateCodeValue(final Long codeId, final Long codeValueId) { this.actionName = "UPDATE"; this.entityName = "CODEVALUE"; - this.entityId = codeValueId; - this.codeId = codeId; + this.subentityId = codeValueId; + this.entityId = codeId; this.href = "/codes/" + codeId + "/codevalues/" + codeValueId; return this; } @@ -746,8 +760,8 @@ public class CommandWrapperBuilder { public CommandWrapperBuilder deleteCodeValue(final Long codeId, final Long codeValueId) { this.actionName = "DELETE"; this.entityName = "CODEVALUE"; - this.entityId = codeValueId; - this.codeId = codeId; + this.subentityId = codeValueId; + this.entityId = codeId; this.href = "/codes/" + codeId + "/codevalues/" + codeValueId; return this; } @@ -1048,11 +1062,13 @@ public class CommandWrapperBuilder { return this; } - public CommandWrapperBuilder createCalendar(final String supportedEntityType, final Long supportedEntityId) { + public CommandWrapperBuilder createCalendar(final CommandWrapper resourceDetails, final String supportedEntityType, + final Long supportedEntityId) { this.actionName = "CREATE"; this.entityName = "CALENDAR"; - this.supportedEntityType = supportedEntityType; - this.supportedEntityId = supportedEntityId; + this.clientId = resourceDetails.getClientId(); + this.loanId = resourceDetails.getLoanId(); + this.groupId = resourceDetails.getGroupId(); this.href = "/" + supportedEntityType + "/" + supportedEntityId + "/calendars/template"; return this; } @@ -1073,34 +1089,51 @@ public class CommandWrapperBuilder { return this; } - public CommandWrapperBuilder createNote(final String entityName, final String resourceType, final Long resourceId) { + public CommandWrapperBuilder createNote(final CommandWrapper resourceDetails, final String resourceType, final Long resourceId) { this.actionName = "CREATE"; - this.entityName = entityName;// Note supports multiple resources. Note - // Permissions are set for each resource. - this.supportedEntityType = resourceType; - this.supportedEntityId = resourceId; + this.entityName = resourceDetails.entityName();// Note supports multiple + // resources. Note + // Permissions are set + // for each resource. + this.clientId = resourceDetails.getClientId(); + this.loanId = resourceDetails.getLoanId(); + this.savingsId = resourceDetails.getSavingsId(); + this.groupId = resourceDetails.getGroupId(); + this.subentityId = resourceDetails.subresourceId(); this.href = "/" + resourceType + "/" + resourceId + "/notes/template"; return this; } - public CommandWrapperBuilder updateNote(final String entityName, final String resourceType, final Long resourceId, final Long noteId) { + public CommandWrapperBuilder updateNote(final CommandWrapper resourceDetails, final String resourceType, final Long resourceId, + final Long noteId) { this.actionName = "UPDATE"; - this.entityName = entityName;// Note supports multiple resources. Note - // Permissions are set for each resource. + this.entityName = resourceDetails.entityName();// Note supports multiple + // resources. Note + // Permissions are set + // for each resource. this.entityId = noteId; - this.supportedEntityType = resourceType; - this.supportedEntityId = resourceId; + this.clientId = resourceDetails.getClientId(); + this.loanId = resourceDetails.getLoanId(); + this.savingsId = resourceDetails.getSavingsId(); + this.groupId = resourceDetails.getGroupId(); + this.subentityId = resourceDetails.subresourceId(); this.href = "/" + resourceType + "/" + resourceId + "/notes"; return this; } - public CommandWrapperBuilder deleteNote(final String entityName, final String resourceType, final Long resourceId, final Long noteId) { + public CommandWrapperBuilder deleteNote(final CommandWrapper resourceDetails, final String resourceType, final Long resourceId, + final Long noteId) { this.actionName = "DELETE"; - this.entityName = entityName;// Note supports multiple resources. Note - // Permissions are set for each resource. + this.entityName = resourceDetails.entityName();// Note supports multiple + // resources. Note + // Permissions are set + // for each resource. this.entityId = noteId; - this.supportedEntityType = resourceType; - this.supportedEntityId = resourceId; + this.clientId = resourceDetails.getClientId(); + this.loanId = resourceDetails.getLoanId(); + this.savingsId = resourceDetails.getSavingsId(); + this.groupId = resourceDetails.getGroupId(); + this.subentityId = resourceDetails.subresourceId(); this.href = "/" + resourceType + "/" + resourceId + "/calendars/" + noteId; return this; } @@ -1135,8 +1168,6 @@ public class CommandWrapperBuilder { this.entityName = "GROUP"; this.entityId = groupId; this.groupId = groupId; - this.supportedEntityId = groupId; - this.supportedEntityType = CalendarEntityType.GROUPS.name(); this.href = "/groups/" + groupId + "?command=saveCollectionSheet"; return this; } @@ -1275,8 +1306,7 @@ public class CommandWrapperBuilder { this.actionName = "SAVECOLLECTIONSHEET"; this.entityName = "CENTER"; this.entityId = centerId; - this.supportedEntityId = centerId; - this.supportedEntityType = CalendarEntityType.CENTERS.name(); + this.groupId = centerId; this.href = "/centers/" + centerId + "?command=saveCollectionSheet"; return this; } @@ -1394,7 +1424,6 @@ public class CommandWrapperBuilder { this.actionName = "CREATE"; this.entityName = "TEMPLATE"; this.entityId = null; - this.templateId = null; this.href = "/templates"; return this; } @@ -1403,7 +1432,6 @@ public class CommandWrapperBuilder { this.actionName = "UPDATE"; this.entityName = "TEMPLATE"; this.entityId = templateId; - this.templateId = templateId; this.href = "/templates/" + templateId; return this; } @@ -1412,7 +1440,6 @@ public class CommandWrapperBuilder { this.actionName = "DELETE"; this.entityName = "TEMPLATE"; this.entityId = templateId; - this.templateId = templateId; this.href = "/templates/" + templateId; return this; } @@ -1476,11 +1503,13 @@ public class CommandWrapperBuilder { return this; } - public CommandWrapperBuilder createMeeting(final String supportedEntityType, final Long supportedEntityId) { + public CommandWrapperBuilder createMeeting(final CommandWrapper resourceDetails, final String supportedEntityType, + final Long supportedEntityId) { this.actionName = "CREATE"; this.entityName = "MEETING"; - this.supportedEntityType = supportedEntityType; - this.supportedEntityId = supportedEntityId; + this.clientId = resourceDetails.getClientId(); + this.loanId = resourceDetails.getLoanId(); + this.groupId = resourceDetails.getGroupId(); this.href = "/" + supportedEntityType + "/" + supportedEntityId + "/meetings"; return this; } @@ -1505,8 +1534,6 @@ public class CommandWrapperBuilder { this.actionName = "SAVEORUPDATEATTENDANCE"; this.entityName = "MEETING"; this.entityId = entityId; - this.supportedEntityType = supportedEntityType; - this.supportedEntityId = supportedEntityId; this.href = "/" + supportedEntityType + "/" + supportedEntityId + "/meetings/" + entityId + "?command=saveOrUpdateAttendance"; return this; } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java index a4cc883bf..c49666056 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/PortfolioCommandSourceWritePlatformServiceImpl.java @@ -66,8 +66,7 @@ public class PortfolioCommandSourceWritePlatformServiceImpl implements Portfolio 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(), wrapper.getHref(), wrapper.getProductId()); + wrapper.getSavingsId(), wrapper.getTransactionId(), wrapper.getHref(), wrapper.getProductId()); result = this.processAndLogCommandService.processAndLogCommand(wrapper, command, isApprovedByChecker); } catch (final RollbackTransactionAsCommandIsNotApprovedByCheckerException e) { @@ -88,13 +87,13 @@ public class PortfolioCommandSourceWritePlatformServiceImpl implements Portfolio commandSourceInput.getEntityName(), commandSourceInput.resourceId(), commandSourceInput.subresourceId(), commandSourceInput.getResourceGetUrl(), commandSourceInput.getProductId(), commandSourceInput.getOfficeId(), commandSourceInput.getGroupId(), commandSourceInput.getClientId(), commandSourceInput.getLoanId(), - commandSourceInput.getSavingsId()); + commandSourceInput.getSavingsId(), commandSourceInput.getTransactionId()); 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(), commandSourceInput.getGroupId(), commandSourceInput.getClientId(), - commandSourceInput.getLoanId(), commandSourceInput.getSavingsId(), commandSourceInput.getResourceGetUrl(), - commandSourceInput.getProductId()); + commandSourceInput.getLoanId(), commandSourceInput.getSavingsId(), commandSourceInput.getTransactionId(), + commandSourceInput.getResourceGetUrl(), commandSourceInput.getProductId()); final boolean makerCheckerApproval = true; return this.processAndLogCommandService.processAndLogCommand(wrapper, command, makerCheckerApproval); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java index fdcef6809..962ca9682 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/commands/service/SynchronousCommandProcessingService.java @@ -69,7 +69,7 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer } commandSourceResult.updateResourceId(result.resourceId()); commandSourceResult.updateForAudit(result.getOfficeId(), result.getGroupId(), result.getClientId(), result.getLoanId(), - result.getSavingsId(), result.getProductId()); + result.getSavingsId(), result.getProductId(), result.getTransactionId()); String changesOnlyJson = null; if (result.hasChanges()) { @@ -85,7 +85,21 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer this.commandSourceRepository.save(commandSourceResult); } - if ((rollbackTransaction || result.isRollbackTransaction()) && !isApprovedByChecker) { throw new RollbackTransactionAsCommandIsNotApprovedByCheckerException(commandSourceResult); } + if ((rollbackTransaction || result.isRollbackTransaction()) && !isApprovedByChecker) { + /* + * JournalEntry will generate a new transactionId every time. + * Updating the transactionId with old transactionId, because as + * there are no entries are created with new transactionId, will + * throw an error when checker approves the transaction + */ + commandSourceResult.updateTransaction(command.getTransactionId()); + /* + * Update CommandSource json data with JsonCommand json data, line + * 77 and 81 may update the json data + */ + commandSourceResult.updateJsonTo(command.json()); + throw new RollbackTransactionAsCommandIsNotApprovedByCheckerException(commandSourceResult); + } result.setRollbackTransaction(null); return result; } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/handler/DeleteCodeValueCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/handler/DeleteCodeValueCommandHandler.java index c1ac9b79a..d767898b3 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/handler/DeleteCodeValueCommandHandler.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/handler/DeleteCodeValueCommandHandler.java @@ -27,6 +27,6 @@ public class DeleteCodeValueCommandHandler implements NewCommandSourceHandler { @Override public CommandProcessingResult processCommand(final JsonCommand command) { - return this.writePlatformService.deleteCodeValue(command.getCodeId(), command.entityId()); + return this.writePlatformService.deleteCodeValue(command.entityId(), command.subentityId()); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/handler/UpdateCodeValueCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/handler/UpdateCodeValueCommandHandler.java index 2714af546..5f0c7dd96 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/handler/UpdateCodeValueCommandHandler.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/handler/UpdateCodeValueCommandHandler.java @@ -27,6 +27,6 @@ public class UpdateCodeValueCommandHandler implements NewCommandSourceHandler { @Override public CommandProcessingResult processCommand(final JsonCommand command) { - return this.writePlatformService.updateCodeValue(command.entityId(), command); + return this.writePlatformService.updateCodeValue(command.subentityId(), command); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeValueWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeValueWritePlatformServiceJpaRepositoryImpl.java index 348ae7cfa..a702cf894 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeValueWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeValueWritePlatformServiceJpaRepositoryImpl.java @@ -59,7 +59,7 @@ public class CodeValueWritePlatformServiceJpaRepositoryImpl implements CodeValue this.fromApiJsonDeserializer.validateForCreate(command.json()); - final Long codeId = command.getCodeId(); + final Long codeId = command.entityId(); final Code code = this.codeRepository.findOne(codeId); final CodeValue codeValue = CodeValue.fromJson(code, command); this.codeValueRepository.save(codeValue); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/JsonCommand.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/JsonCommand.java index aa45ab37e..0bc3c86da 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/JsonCommand.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/api/JsonCommand.java @@ -45,44 +45,35 @@ public final class JsonCommand { private final Long loanId; private final Long savingsId; private final String entityName; - private final Long codeId; - private final String supportedEntityType; - private final Long supportedEntityId; private final String transactionId; private final String url; private final Long productId; public static JsonCommand from(final String jsonCommand, final JsonElement parsedCommand, final FromJsonHelper fromApiJsonHelper, final String entityName, final Long resourceId, final Long subresourceId, final Long groupId, final Long clientId, - final Long loanId, final Long savingsId, final Long codeId, final String supportedEntityType, final Long supportedEntityId, - final String transactionId, final String url, final Long productId) { + final Long loanId, final Long savingsId, final String transactionId, final String url, final Long productId) { return new JsonCommand(null, jsonCommand, parsedCommand, fromApiJsonHelper, entityName, resourceId, subresourceId, groupId, - clientId, loanId, savingsId, codeId, supportedEntityType, supportedEntityId, transactionId, url, productId); + clientId, loanId, savingsId, transactionId, url, productId); } public static JsonCommand fromExistingCommand(final Long commandId, final String jsonCommand, final JsonElement parsedCommand, final FromJsonHelper fromApiJsonHelper, final String entityName, final Long resourceId, final Long subresourceId, final String url, final Long productId) { return new JsonCommand(commandId, jsonCommand, parsedCommand, fromApiJsonHelper, entityName, resourceId, subresourceId, null, null, - null, null, null, null, null, null, url, productId); + null, null, null, url, productId); } - /*** - * Vishwas TODO: If we are recreating this command from the database, all - * properties should be set. Presence of NULL properties means we can expect - * errors! - ***/ public static JsonCommand fromExistingCommand(final Long commandId, final String jsonCommand, final JsonElement parsedCommand, final FromJsonHelper fromApiJsonHelper, final String entityName, final Long resourceId, final Long subresourceId, - final Long groupId, final Long clientId, final Long loanId, final Long savingsId, final String url, final Long productId) { + final Long groupId, final Long clientId, final Long loanId, final Long savingsId, final String transactionId, final String url, + final Long productId) { return new JsonCommand(commandId, jsonCommand, parsedCommand, fromApiJsonHelper, entityName, resourceId, subresourceId, groupId, - clientId, loanId, savingsId, null, null, null, null, url, productId); + clientId, loanId, savingsId, transactionId, url, productId); } public JsonCommand(final Long commandId, final String jsonCommand, final JsonElement parsedCommand, final FromJsonHelper fromApiJsonHelper, final String entityName, final Long resourceId, final Long subresourceId, - final Long groupId, final Long clientId, final Long loanId, final Long savingsId, final Long codeId, - final String supportedEntityType, final Long supportedEntityId, final String transactionId, final String url, + final Long groupId, final Long clientId, final Long loanId, final Long savingsId, final String transactionId, final String url, final Long productId) { this.commandId = commandId; this.jsonCommand = jsonCommand; @@ -95,9 +86,6 @@ public final class JsonCommand { this.clientId = clientId; this.loanId = loanId; this.savingsId = savingsId; - this.codeId = codeId; - this.supportedEntityType = supportedEntityType; - this.supportedEntityId = supportedEntityId; this.transactionId = transactionId; this.url = url; this.productId = productId; @@ -152,18 +140,6 @@ public final class JsonCommand { return this.savingsId; } - public Long getCodeId() { - return this.codeId; - } - - public Long getSupportedEntityId() { - return this.supportedEntityId; - } - - public String getSupportedEntityType() { - return this.supportedEntityType; - } - public String getTransactionId() { return this.transactionId; } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/api/CalendarsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/api/CalendarsApiResource.java index 7d9cda283..8b994a7f5 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/api/CalendarsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/api/CalendarsApiResource.java @@ -40,6 +40,7 @@ import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext import org.mifosplatform.portfolio.calendar.data.CalendarData; import org.mifosplatform.portfolio.calendar.domain.Calendar; import org.mifosplatform.portfolio.calendar.domain.CalendarEntityType; +import org.mifosplatform.portfolio.calendar.exception.CalendarEntityTypeNotSupportedException; import org.mifosplatform.portfolio.calendar.service.CalendarDropdownReadPlatformService; import org.mifosplatform.portfolio.calendar.service.CalendarReadPlatformService; import org.mifosplatform.portfolio.calendar.service.CalendarUtils; @@ -167,8 +168,12 @@ public class CalendarsApiResource { @Produces({ MediaType.APPLICATION_JSON }) public String createCalendar(@PathParam("entityType") final String entityType, @PathParam("entityId") final Long entityId, final String apiRequestBodyAsJson) { + + final CalendarEntityType calendarEntityType = CalendarEntityType.getEntityType(entityType); + if (calendarEntityType == null) { throw new CalendarEntityTypeNotSupportedException(entityType); } - final CommandWrapper commandRequest = new CommandWrapperBuilder().createCalendar(entityType, entityId) + final CommandWrapper resourceDetails = getResourceDetails(calendarEntityType, entityId); + final CommandWrapper commandRequest = new CommandWrapperBuilder().createCalendar(resourceDetails, entityType, entityId) .withJson(apiRequestBodyAsJson).build(); final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); @@ -216,4 +221,25 @@ public class CalendarsApiResource { repeatsOnDayOptions); } + private CommandWrapper getResourceDetails(final CalendarEntityType type, final Long entityId) { + CommandWrapperBuilder resourceDetails = new CommandWrapperBuilder(); + switch (type) { + case CENTERS: + resourceDetails.withGroupId(entityId); + break; + case CLIENTS: + resourceDetails.withClientId(entityId); + break; + case GROUPS: + resourceDetails.withGroupId(entityId); + break; + case LOANS: + resourceDetails.withLoanId(entityId); + break; + case INVALID: + break; + } + return resourceDetails.build(); + } + } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarEntityType.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarEntityType.java index bf65b55c1..d3458b74b 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarEntityType.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarEntityType.java @@ -10,8 +10,8 @@ import java.util.Map; public enum CalendarEntityType { - CLIENTS(1, "calendarEntityType.clients"), GROUPS(2, "calendarEntityType.groups"), LOANS(3, "calendarEntityType.loans"), CENTERS(4, - "calendarEntityType.centers"); + INVALID(0, "calendarEntityType.invalid"), CLIENTS(1, "calendarEntityType.clients"), GROUPS(2, "calendarEntityType.groups"), LOANS(3, + "calendarEntityType.loans"), CENTERS(4, "calendarEntityType.centers"); private final Integer value; private final String code; @@ -114,4 +114,17 @@ public enum CalendarEntityType { public boolean isClient() { return this.value.equals(CalendarEntityType.CLIENTS.getValue()); } + + private static final Map entityNameToEnumMap = new HashMap(); + + static { + for (final CalendarEntityType entityType : CalendarEntityType.values()) { + entityNameToEnumMap.put(entityType.name().toLowerCase(), entityType); + } + } + + public static CalendarEntityType getEntityType(String entityType) { + return entityNameToEnumMap.get(entityType.toLowerCase()); + } + } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarInstance.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarInstance.java index ac9cacbb8..fba630456 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarInstance.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarInstance.java @@ -11,7 +11,6 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; -import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.springframework.data.jpa.domain.AbstractPersistable; @Entity @@ -38,9 +37,7 @@ public class CalendarInstance extends AbstractPersistable { } - public static CalendarInstance fromJson(final Calendar calendar, final JsonCommand command) { - final Long entityId = command.getSupportedEntityId(); - final Integer entityTypeId = CalendarEntityType.valueOf(command.getSupportedEntityType().toUpperCase()).getValue(); + public static CalendarInstance fromJson(final Calendar calendar, final Long entityId, final Integer entityTypeId) { return new CalendarInstance(calendar, entityId, entityTypeId); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/exception/CalendarEntityTypeNotSupportedException.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/exception/CalendarEntityTypeNotSupportedException.java new file mode 100644 index 000000000..0fa7f2718 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/exception/CalendarEntityTypeNotSupportedException.java @@ -0,0 +1,11 @@ +package org.mifosplatform.portfolio.calendar.exception; + +import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceNotFoundException; + +public class CalendarEntityTypeNotSupportedException extends AbstractPlatformResourceNotFoundException { + + public CalendarEntityTypeNotSupportedException(final String resource) { + super("calendar.entitytype.not.supported", "Calendar does not support resource "+ resource); + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarWritePlatformServiceJpaRepositoryImpl.java index c4f713117..a86f50a66 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarWritePlatformServiceJpaRepositoryImpl.java @@ -79,21 +79,28 @@ public class CalendarWritePlatformServiceJpaRepositoryImpl implements CalendarWr public CommandProcessingResult createCalendar(final JsonCommand command) { this.fromApiJsonDeserializer.validateForCreate(command.json()); - final Long entityId = command.getSupportedEntityId(); - final CalendarEntityType entityType = CalendarEntityType.valueOf(command.getSupportedEntityType().toUpperCase()); + Long entityId = null; + CalendarEntityType entityType = CalendarEntityType.INVALID; LocalDate entityActivationDate = null; Group centerOrGroup = null; - if (entityType.isCenter() || entityType.isGroup()) { - centerOrGroup = this.groupRepository.findOneWithNotFoundDetection(entityId); + if (command.getGroupId() != null) { + centerOrGroup = this.groupRepository.findOneWithNotFoundDetection(command.getGroupId()); entityActivationDate = centerOrGroup.getActivationLocalDate(); - } else if (entityType.isLoan()) { - final Loan loan = this.loanRepository.findOne(entityId); + entityType = centerOrGroup.isCenter() ? CalendarEntityType.CENTERS : CalendarEntityType.GROUPS; + entityId = command.getGroupId(); + } else if (command.getLoanId() != null) { + final Loan loan = this.loanRepository.findOne(command.getLoanId()); entityActivationDate = (loan.getApprovedOnDate() == null) ? loan.getSubmittedOnDate() : loan.getApprovedOnDate(); - } else if (entityType.isLoan()) { - final Client client = this.clientRepository.findOneWithNotFoundDetection(entityId); + entityType = CalendarEntityType.LOANS; + entityId = command.getLoanId(); + } else if (command.getClientId() != null) { + final Client client = this.clientRepository.findOneWithNotFoundDetection(command.getClientId()); entityActivationDate = client.getActivationLocalDate(); + entityType = CalendarEntityType.CLIENTS; + entityId = command.getClientId(); } - + + final Integer entityTypeId = entityType.getValue(); final Calendar newCalendar = Calendar.fromJson(command); final List dataValidationErrors = new ArrayList(); @@ -108,17 +115,14 @@ public class CalendarWritePlatformServiceJpaRepositoryImpl implements CalendarWr .failWithCodeNoParameterAddedToErrorCode(errorMessage); } - if (entityType.isCenter() || entityType.isGroup()) { - Long centerOrGroupId = entityId; + if (centerOrGroup != null) { + Long centerOrGroupId = centerOrGroup.getId(); Integer centerOrGroupEntityTypeId = entityType.getValue(); - if (entityType.isGroup()) { - @SuppressWarnings("null") - final Group parent = centerOrGroup.getParent(); - if (parent != null) { - centerOrGroupId = parent.getId(); - centerOrGroupEntityTypeId = CalendarEntityType.CENTERS.getValue(); - } + final Group parent = centerOrGroup.getParent(); + if (parent != null) { + centerOrGroupId = parent.getId(); + centerOrGroupEntityTypeId = CalendarEntityType.CENTERS.getValue(); } final CalendarInstance collectionCalendarInstance = this.calendarInstanceRepository @@ -134,12 +138,15 @@ public class CalendarWritePlatformServiceJpaRepositoryImpl implements CalendarWr this.calendarRepository.save(newCalendar); - final CalendarInstance newCalendarInstance = CalendarInstance.fromJson(newCalendar, command); + final CalendarInstance newCalendarInstance = CalendarInstance.fromJson(newCalendar, entityId, entityTypeId); this.calendarInstanceRepository.save(newCalendarInstance); return new CommandProcessingResultBuilder() // .withCommandId(command.commandId()) // .withEntityId(newCalendar.getId()) // + .withClientId(command.getClientId()) // + .withGroupId(command.getGroupId()) // + .withLoanId(command.getLoanId()) // .build(); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanWritePlatformServiceJpaRepositoryImpl.java index 2b1258749..2966dfb31 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanWritePlatformServiceJpaRepositoryImpl.java @@ -1739,7 +1739,7 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf final JsonElement parsedCommand = this.fromApiJsonHelper.parse(overdueInstallment.toString()); final JsonCommand command = JsonCommand.from(overdueInstallment.toString(), parsedCommand, this.fromApiJsonHelper, - null, null, null, null, null, overdueInstallment.getLoanId(), null, null, null, null, null, null, null); + null, null, null, null, null, overdueInstallment.getLoanId(), null, null, null, null); applyChargeToOverdueLoanInstallment(overdueInstallment.getLoanId(), overdueInstallment.getChargeId(), overdueInstallment.getPeriodNumber(), command); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/meeting/api/MeetingsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/meeting/api/MeetingsApiResource.java index 3559e486b..a69de16a8 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/meeting/api/MeetingsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/meeting/api/MeetingsApiResource.java @@ -37,6 +37,7 @@ import org.mifosplatform.infrastructure.core.service.DateUtils; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.portfolio.calendar.data.CalendarData; import org.mifosplatform.portfolio.calendar.domain.CalendarEntityType; +import org.mifosplatform.portfolio.calendar.exception.CalendarEntityTypeNotSupportedException; import org.mifosplatform.portfolio.calendar.service.CalendarReadPlatformService; import org.mifosplatform.portfolio.client.data.ClientData; import org.mifosplatform.portfolio.client.service.ClientReadPlatformService; @@ -163,7 +164,11 @@ public class MeetingsApiResource { public String createMeeting(@PathParam("entityType") final String entityType, @PathParam("entityId") final Long entityId, final String apiRequestBodyAsJson) { - final CommandWrapper commandRequest = new CommandWrapperBuilder().createMeeting(entityType, entityId) + final CalendarEntityType calendarEntityType = CalendarEntityType.getEntityType(entityType); + if (calendarEntityType == null) { throw new CalendarEntityTypeNotSupportedException(entityType); } + + final CommandWrapper resourceDetails = getResourceDetails(calendarEntityType, entityId); + final CommandWrapper commandRequest = new CommandWrapperBuilder().createMeeting(resourceDetails, entityType, entityId) .withJson(apiRequestBodyAsJson).build(); final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); @@ -226,4 +231,26 @@ public class MeetingsApiResource { private boolean is(final String commandParam, final String commandValue) { return StringUtils.isNotBlank(commandParam) && commandParam.trim().equalsIgnoreCase(commandValue); } + + private CommandWrapper getResourceDetails(final CalendarEntityType type, final Long entityId) { + CommandWrapperBuilder resourceDetails = new CommandWrapperBuilder(); + switch (type) { + case CENTERS: + resourceDetails.withGroupId(entityId); + break; + case CLIENTS: + resourceDetails.withClientId(entityId); + break; + case GROUPS: + resourceDetails.withGroupId(entityId); + break; + case LOANS: + resourceDetails.withLoanId(entityId); + break; + case INVALID: + break; + } + return resourceDetails.build(); + } + } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/meeting/service/MeetingWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/meeting/service/MeetingWritePlatformServiceJpaRepositoryImpl.java index 2d422ba0c..6106545fc 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/meeting/service/MeetingWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/meeting/service/MeetingWritePlatformServiceJpaRepositoryImpl.java @@ -106,16 +106,30 @@ public class MeetingWritePlatformServiceJpaRepositoryImpl implements MeetingWrit } private CalendarInstance getCalendarInstance(final JsonCommand command) { + final Long calendarId = command.longValueOfParameterNamed(calendarIdParamName); - Long entityId = command.getSupportedEntityId(); final Calendar calendarForUpdate = this.calendarRepository.findOne(calendarId); if (calendarForUpdate == null) { throw new CalendarNotFoundException(calendarId); } - CalendarEntityType entityType = CalendarEntityType.valueOf(command.getSupportedEntityType().toUpperCase()); - // If group is within a center then center entityType should be passed - // for retrieving CalendarInstance. - if (CalendarEntityType.isGroup(command.getSupportedEntityType().toUpperCase())) { + + Long entityId = null;// command.getSupportedEntityId(); + CalendarEntityType entityType = CalendarEntityType.INVALID;// CalendarEntityType.valueOf(command.getSupportedEntityType().toUpperCase()); + if (command.getLoanId() != null) { + entityId = command.getLoanId(); + entityType = CalendarEntityType.LOANS; + } else if (command.getClientId() != null) { + entityId = command.getClientId(); + entityType = CalendarEntityType.CLIENTS; + } else if (command.getGroupId() != null) { + entityId = command.getGroupId(); + entityType = CalendarEntityType.GROUPS; + /* + * If group is within a center then center entityType should be + * passed for retrieving CalendarInstance. + */ final Group group = this.groupRepository.findOne(entityId); - if (group.isChildGroup()) { + if (group.isCenter()) { + entityType = CalendarEntityType.CENTERS; + } else if (group.isChildGroup()) { entityType = CalendarEntityType.CENTERS; entityId = group.getParent().getId(); } @@ -124,7 +138,7 @@ public class MeetingWritePlatformServiceJpaRepositoryImpl implements MeetingWrit final CalendarInstance calendarInstance = this.calendarInstanceRepository.findByCalendarIdAndEntityIdAndEntityTypeId( calendarForUpdate.getId(), entityId, entityType.getValue()); if (calendarInstance == null) { - final String postFix = "for." + command.getSupportedEntityType() + "not.found"; + final String postFix = "for." + entityType.name().toLowerCase() + "not.found"; final String defaultUserMessage = "No Calendar Instance details found for group with identifier " + entityId + " and calendar with identifier " + calendarId; throw new CalendarInstanceNotFoundException(postFix, defaultUserMessage, entityId, calendarId); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/api/NotesApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/api/NotesApiResource.java index 65780aa82..4c4c7230a 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/api/NotesApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/api/NotesApiResource.java @@ -74,7 +74,7 @@ public class NotesApiResource { if (noteType == null) { throw new NoteResourceNotSupportedException(resourceType); } - this.context.authenticatedUser().validateHasReadPermission(getResourceNameForPermissions(noteType)); + this.context.authenticatedUser().validateHasReadPermission(getResourceDetails(noteType, resourceId).entityName()); final Integer noteTypeId = noteType.getValue(); @@ -95,7 +95,7 @@ public class NotesApiResource { if (noteType == null) { throw new NoteResourceNotSupportedException(resourceType); } - this.context.authenticatedUser().validateHasReadPermission(getResourceNameForPermissions(noteType)); + this.context.authenticatedUser().validateHasReadPermission(getResourceDetails(noteType, resourceId).entityName()); final Integer noteTypeId = noteType.getValue(); @@ -115,8 +115,8 @@ public class NotesApiResource { if (noteType == null) { throw new NoteResourceNotSupportedException(resourceType); } - final String resourceNameForPermissions = getResourceNameForPermissions(noteType); - final CommandWrapper commandRequest = new CommandWrapperBuilder().createNote(resourceNameForPermissions, resourceType, resourceId) + final CommandWrapper resourceDetails = getResourceDetails(noteType, resourceId); + final CommandWrapper commandRequest = new CommandWrapperBuilder().createNote(resourceDetails, resourceType, resourceId) .withJson(apiRequestBodyAsJson).build(); final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); @@ -135,10 +135,10 @@ public class NotesApiResource { if (noteType == null) { throw new NoteResourceNotSupportedException(resourceType); } - final String resourceNameForPermissions = getResourceNameForPermissions(noteType); + final CommandWrapper resourceDetails = getResourceDetails(noteType, resourceId); - final CommandWrapper commandRequest = new CommandWrapperBuilder() - .updateNote(resourceNameForPermissions, resourceType, resourceId, noteId).withJson(apiRequestBodyAsJson).build(); + final CommandWrapper commandRequest = new CommandWrapperBuilder().updateNote(resourceDetails, resourceType, resourceId, noteId) + .withJson(apiRequestBodyAsJson).build(); final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); @@ -156,38 +156,46 @@ public class NotesApiResource { if (noteType == null) { throw new NoteResourceNotSupportedException(resourceType); } - final String resourceNameForPermissions = getResourceNameForPermissions(noteType); + final CommandWrapper resourceDetails = getResourceDetails(noteType, resourceId); - final CommandWrapper commandRequest = new CommandWrapperBuilder().deleteNote(resourceNameForPermissions, resourceType, resourceId, - noteId).build(); + final CommandWrapper commandRequest = new CommandWrapperBuilder().deleteNote(resourceDetails, resourceType, resourceId, noteId) + .build(); final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); return this.toApiJsonSerializer.serialize(result); } - private String getResourceNameForPermissions(final NoteType type) { + private CommandWrapper getResourceDetails(final NoteType type, final Long resourceId) { + CommandWrapperBuilder resourceDetails = new CommandWrapperBuilder(); String resourceNameForPermissions = "INVALIDNOTE"; switch (type) { case CLIENT: resourceNameForPermissions = "CLIENTNOTE"; + resourceDetails.withClientId(resourceId); break; case LOAN: resourceNameForPermissions = "LOANNOTE"; + resourceDetails.withLoanId(resourceId); break; case LOAN_TRANSACTION: resourceNameForPermissions = "LOANTRANSACTIONNOTE"; + // updating loanId, to distinguish saving transaction note and loan transaction note as we are using subEntityId for both. + resourceDetails.withLoanId(resourceId); + resourceDetails.withSubEntityId(resourceId); break; case SAVING_ACCOUNT: resourceNameForPermissions = "SAVINGNOTE"; + resourceDetails.withSavingsId(resourceId); break; case GROUP: resourceNameForPermissions = "GROUPNOTE"; + resourceDetails.withGroupId(resourceId); break; } - return resourceNameForPermissions; + return resourceDetails.withEntityName(resourceNameForPermissions).build(); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/domain/NoteType.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/domain/NoteType.java index 24b2030b8..f3ad9cc10 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/domain/NoteType.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/domain/NoteType.java @@ -34,6 +34,10 @@ public enum NoteType { return this.code; } + public String getApiUrl() { + return this.apiUrl; + } + private static final Map intToEnumMap = new HashMap(); private static int minValue; private static int maxValue; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/service/NoteWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/service/NoteWritePlatformServiceJpaRepositoryImpl.java index 818872e62..ceaae75e8 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/service/NoteWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/service/NoteWritePlatformServiceJpaRepositoryImpl.java @@ -57,7 +57,7 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf private CommandProcessingResult createClientNote(final JsonCommand command) { - final Long resourceId = command.getSupportedEntityId(); + final Long resourceId = command.getClientId(); final Client client = this.clientRepository.findOneWithNotFoundDetection(resourceId); if (client == null) { throw new ClientNotFoundException(resourceId); } @@ -85,7 +85,7 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf private CommandProcessingResult createGroupNote(final JsonCommand command) { - final Long resourceId = command.getSupportedEntityId(); + final Long resourceId = command.getGroupId(); final Group group = this.groupRepository.findOne(resourceId); if (group == null) { throw new GroupNotFoundException(resourceId); } @@ -103,7 +103,7 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf private CommandProcessingResult createLoanNote(final JsonCommand command) { - final Long resourceId = command.getSupportedEntityId(); + final Long resourceId = command.getLoanId(); final Loan loan = this.loanRepository.findOne(resourceId); if (loan == null) { throw new LoanNotFoundException(resourceId); } @@ -125,7 +125,7 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf private CommandProcessingResult createLoanTransactionNote(final JsonCommand command) { - final Long resourceId = command.getSupportedEntityId(); + final Long resourceId = command.subentityId(); final LoanTransaction loanTransaction = this.loanTransactionRepository.findOne(resourceId); if (loanTransaction == null) { throw new LoanTransactionNotFoundException(resourceId); } @@ -173,7 +173,7 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf this.fromApiJsonDeserializer.validateNote(command.json()); - final String resourceUrl = command.getSupportedEntityType(); + final String resourceUrl = getResourceUrlFromCommand(command); //command.getSupportedEntityType(); final NoteType type = NoteType.fromApiUrl(resourceUrl); switch (type) { case CLIENT: { @@ -197,13 +197,36 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf } + private String getResourceUrlFromCommand(JsonCommand command) { + + final String resourceUrl; + + if (command.getClientId() != null) { + resourceUrl = NoteType.CLIENT.getApiUrl(); + } else if (command.getGroupId() != null) { + resourceUrl = NoteType.GROUP.getApiUrl(); + } else if (command.getLoanId() != null) { + if (command.subentityId() != null) { + resourceUrl = NoteType.LOAN_TRANSACTION.getApiUrl(); + } else { + resourceUrl = NoteType.LOAN.getApiUrl(); + } + } else if (command.getSavingsId() != null) { + //TODO: SAVING_TRANSACTION type need to be add. + resourceUrl = NoteType.SAVING_ACCOUNT.getApiUrl(); + } else { + resourceUrl = ""; + } + + return resourceUrl; + } + private CommandProcessingResult updateClientNote(final JsonCommand command) { - final Long resourceId = command.getSupportedEntityId(); + final Long resourceId = command.getClientId(); final Long noteId = command.entityId(); - final String resourceUrl = command.getSupportedEntityType(); - final NoteType type = NoteType.fromApiUrl(resourceUrl); + final NoteType type = NoteType.CLIENT; final Client client = this.clientRepository.findOneWithNotFoundDetection(resourceId); @@ -227,11 +250,10 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf private CommandProcessingResult updateGroupNote(final JsonCommand command) { - final Long resourceId = command.getSupportedEntityId(); + final Long resourceId = command.getGroupId(); final Long noteId = command.entityId(); - final String resourceUrl = command.getSupportedEntityType(); - final NoteType type = NoteType.fromApiUrl(resourceUrl); + final NoteType type = NoteType.GROUP; final Group group = this.groupRepository.findOne(resourceId); if (group == null) { throw new GroupNotFoundException(resourceId); } @@ -255,11 +277,10 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf private CommandProcessingResult updateLoanNote(final JsonCommand command) { - final Long resourceId = command.getSupportedEntityId(); + final Long resourceId = command.getLoanId(); final Long noteId = command.entityId(); - final String resourceUrl = command.getSupportedEntityType(); - final NoteType type = NoteType.fromApiUrl(resourceUrl); + final NoteType type = NoteType.LOAN; final Loan loan = this.loanRepository.findOne(resourceId); if (loan == null) { throw new LoanNotFoundException(resourceId); } @@ -280,11 +301,10 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf private CommandProcessingResult updateLoanTransactionNote(final JsonCommand command) { - final Long resourceId = command.getSupportedEntityId(); + final Long resourceId = command.subentityId(); final Long noteId = command.entityId(); - final String resourceUrl = command.getSupportedEntityType(); - final NoteType type = NoteType.fromApiUrl(resourceUrl); + final NoteType type = NoteType.LOAN_TRANSACTION; final LoanTransaction loanTransaction = this.loanTransactionRepository.findOne(resourceId); if (loanTransaction == null) { throw new LoanTransactionNotFoundException(resourceId); } @@ -346,7 +366,7 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf this.fromApiJsonDeserializer.validateNote(command.json()); - final String resourceUrl = command.getSupportedEntityType(); + final String resourceUrl = getResourceUrlFromCommand(command); //command.getSupportedEntityType(); final NoteType type = NoteType.fromApiUrl(resourceUrl); switch (type) { @@ -383,25 +403,29 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf } private Note getNoteForDelete(final JsonCommand command) { - final String resourceUrl = command.getSupportedEntityType(); - final Long resourceId = command.getSupportedEntityId(); + final String resourceUrl = getResourceUrlFromCommand(command);// command.getSupportedEntityType(); final Long noteId = command.entityId(); final NoteType type = NoteType.fromApiUrl(resourceUrl); + Long resourceId = null; Note noteForUpdate = null; switch (type) { case CLIENT: { + resourceId = command.getClientId(); noteForUpdate = this.noteRepository.findByClientIdAndId(resourceId, noteId); } break; case GROUP: { + resourceId = command.getGroupId(); noteForUpdate = this.noteRepository.findByGroupIdAndId(resourceId, noteId); } break; case LOAN: { + resourceId = command.getLoanId(); noteForUpdate = this.noteRepository.findByLoanIdAndId(resourceId, noteId); } break; case LOAN_TRANSACTION: { + resourceId = command.subentityId(); noteForUpdate = this.noteRepository.findByLoanTransactionIdAndId(resourceId, noteId); } break;