From 34e2bb3c4dcac9848633d692858decab142d5b1e Mon Sep 17 00:00:00 2001 From: ashok-conflux Date: Thu, 7 Mar 2013 22:00:07 +0530 Subject: [PATCH] Updated Notes in API doc --- api-docs/apiLive.htm | 409 +++++++++++------- .../portfolio/note/api/NotesApiResource.java | 16 + ...oteResourceNotSupportedFoundException.java | 18 + ...WritePlatformServiceJpaRepositoryImpl.java | 400 ++++++++++++++--- 4 files changed, 608 insertions(+), 235 deletions(-) create mode 100644 mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/exception/NoteResourceNotSupportedFoundException.java diff --git a/api-docs/apiLive.htm b/api-docs/apiLive.htm index 1c3ab088e..062942278 100644 --- a/api-docs/apiLive.htm +++ b/api-docs/apiLive.htm @@ -187,23 +187,6 @@ - - - clients/{clientId}/notes - Add a Client Note - Retrieve a Client's Notes - - - - - - clients/{clientId}/notes/{noteId} - - Retrieve a Client - Note - Update a Client Note - - @@ -803,6 +786,22 @@ Delete Entry in Data Table (One to Many) + + NOTE + {resource}/{resourceId}/notes + Add a Resource Note + Retrieve a Resource's Notes + + + + + + {resource}/{resourceId}/notes/{noteId} + + Retrieve a Resource Note + Update a Resource Note + Delete a Resource Note + @@ -1266,6 +1265,8 @@ + +
@@ -2015,140 +2016,6 @@ GET https://DomainName/api/v1/clients/{clientId}/loans
-   -
-
-

Retrieve a Client's Notes

-

- Note: Notes are returned in descending createOn order. -

-

Example Requests:

-
clients/2/notes
-
-
-
clients/2/notes?fields=note,createdOn,createdByUsername
-
-
- -GET https://DomainName/api/v1/clients/{clientId}/notes - - -[ - { - "id": 2, - "clientId": 1, - "noteType": { - "id": 100, - "code": "noteType.client", - "value": "Client note" - }, - "note": "First note edited", - "createdById": 1, - "createdByUsername": "mifos", - "createdOn": 1342498505000, - "updatedById": 1, - "updatedByUsername": "mifos", - "updatedOn": 1342498517000 - } -] - -
-
- -   -
-
-

Add a Client Note

-
-
- -POST https://DomainName/api/v1/clients/{clientId}/notes - - -POST clients/1/notes -Content-Type: application/json -Request Body: -{ - "note": "a note about the client" -} - - -{ - "officeId": 1, - "clientId": 1, - "resourceId": 76 -} - -
-
- -   -
-
-

Retrieve a Client Note

-

Example Requests:

-
clients/1/notes/76
-
-
-
clients/1/notes/76?fields=note,createdOn,createdByUsername
-
-
- -GET https://DomainName/api/v1/clients/{clientId}/notes/{noteId} - - -{ - "id": 76, - "clientId": 1, - "noteType": { - "id": 100, - "code": "noteType.client", - "value": "Client note" - }, - "note": "a note about the client", - "createdById": 1, - "createdByUsername": "mifos", - "createdOn": 1359463135000, - "updatedById": 1, - "updatedByUsername": "mifos", - "updatedOn": 1359463135000 -} - -
-
- -   -
-
-

Update a Client Note

-
-
- -PUT https://DomainName/api/v1/clients/{clientId}/notes/{noteId} - - -PUT clients/1/notes/76 -Content-Type: application/json -Request Body: -{ - "note": "and here the note is updated nicely." } - - -{ - "officeId": 1, - "clientId": 1, - "resourceId": 76, - "changes": { - "note": "and here the note is updated nicely." - } -} - -
-
-  
@@ -2449,7 +2316,7 @@ DELETE groups/7
- +   @@ -8297,7 +8164,229 @@ Request Body: + + + +   +
+
+

Notes

+

Notes API allows to enter notes for supported resources.

+ + + + + + + + + + +
Field Descriptions
note
A simple text note created for supported resources.
+
+
+ + + + + + + +
Supported Resources
+ Client +
+ Loan +
+ Group +
+ Deposit Account +
+ Saving Account +
+
+
+ +   +
+
+

Add a Resource Note

+

Adds a new note to a supported resource.

+ + + + + + + +
Mandatory Fields
note
+

Example Requests:

+
clients/1/notes
+
+
+
groups/1/notes
+
+
+ +POST https://DomainName/api/v1/{resource}/{resourceId}/notes + + +POST clients/1/notes +Content-Type: application/json +Request Body: +{ + "note": "a note about the client" +} + + +{ + "officeId": 1, + "clientId": 1, + "resourceId": 76 +} + +
+
+ +   +
+
+

Retrieve a Resource's Notes

+

+ Note: Notes are returned in descending createOn order. +

+

Example Requests:

+
clients/2/notes
+
+
+
groups/2/notes?fields=note,createdOn,createdByUsername
+
+
+ +GET https://DomainName/api/v1/{resource}/{resourceId}/notes + + +[ + { + "id": 2, + "clientId": 1, + "noteType": { + "id": 100, + "code": "noteType.client", + "value": "Client note" + }, + "note": "First note edited", + "createdById": 1, + "createdByUsername": "mifos", + "createdOn": 1342498505000, + "updatedById": 1, + "updatedByUsername": "mifos", + "updatedOn": 1342498517000 + } +] + +
+
+ +   +
+
+

Retrieve a Resource Note

+

Example Requests:

+
clients/1/notes/76
+
+
+
groups/1/notes/20
+
+
+
clients/1/notes/76?fields=note,createdOn,createdByUsername
+
+
+
groups/1/notes/20?fields=note,createdOn,createdByUsername
+
+
+ +GET https://DomainName/api/v1/{resource}/{resourceId}/notes/{noteId} + + +{ + "id": 76, + "clientId": 1, + "noteType": { + "id": 100, + "code": "noteType.client", + "value": "Client note" + }, + "note": "a note about the client", + "createdById": 1, + "createdByUsername": "mifos", + "createdOn": 1359463135000, + "updatedById": 1, + "updatedByUsername": "mifos", + "updatedOn": 1359463135000 +} + +
+
+ +   +
+
+

Update a Resource Note

+
+
+ +PUT https://DomainName/api/v1/{resource}/{resourceId}/notes/{noteId} + + +PUT clients/1/notes/76 +Content-Type: application/json +Request Body: +{ + "note": "and here the note is updated nicely." +} + + +{ + "officeId": 1, + "clientId": 1, + "resourceId": 76, + "changes": { + "note": "and here the note is updated nicely." + } +} + +
+
+ +   +
+
+

Delete a Resource Note

+
+
+ +DELETE https://DomainName/api/v1/{resource}/{resourceId}/notes/{noteId} + + +DELETE clients/1/notes/76 +Content-Type: application/json +No Request Body: + + +{ + "resourceId": 76 +} + +
+
+ +  
@@ -10911,22 +11000,6 @@ Content-Type: application/json - - - clients/{clientId}/notes - Add a Client Note - Retrieve a Client's Notes - - - - - - clients/{clientId}/notes/{noteId} - - Retrieve a Client Note - Update a Client Note - - Loans 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 df8054c25..1fc261502 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 @@ -32,6 +32,7 @@ import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSeria import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.portfolio.note.data.NoteData; import org.mifosplatform.portfolio.note.domain.NoteType; +import org.mifosplatform.portfolio.note.exception.NoteResourceNotSupportedFoundException; import org.mifosplatform.portfolio.note.service.NoteReadPlatformService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; @@ -70,6 +71,9 @@ public class NotesApiResource { @PathParam("resourceId") final Long resourceId, @Context final UriInfo uriInfo) { NoteType noteType = NoteType.fromApiUrl(resourceType); + + if(noteType == null){ throw new NoteResourceNotSupportedFoundException(resourceType); }; + this.context.authenticatedUser().validateHasReadPermission(getResourceNameForPermissions(noteType)); final Integer noteTypeId = noteType.getValue(); @@ -88,6 +92,9 @@ public class NotesApiResource { @PathParam("noteId") final Long noteId, @Context final UriInfo uriInfo) { NoteType noteType = NoteType.fromApiUrl(resourceType); + + if(noteType == null){ throw new NoteResourceNotSupportedFoundException(resourceType); }; + this.context.authenticatedUser().validateHasReadPermission(getResourceNameForPermissions(noteType)); final Integer noteTypeId = noteType.getValue(); @@ -105,6 +112,9 @@ public class NotesApiResource { final String apiRequestBodyAsJson) { NoteType noteType = NoteType.fromApiUrl(resourceType); + + if(noteType == null){ throw new NoteResourceNotSupportedFoundException(resourceType); }; + final String resourceNameForPermissions = getResourceNameForPermissions(noteType); final CommandWrapper commandRequest = new CommandWrapperBuilder().createNote(resourceNameForPermissions, resourceType, resourceId) .withJson(apiRequestBodyAsJson).build(); @@ -122,6 +132,9 @@ public class NotesApiResource { @PathParam("noteId") final Long noteId, final String apiRequestBodyAsJson) { NoteType noteType = NoteType.fromApiUrl(resourceType); + + if(noteType == null){ throw new NoteResourceNotSupportedFoundException(resourceType); }; + final String resourceNameForPermissions = getResourceNameForPermissions(noteType); final CommandWrapper commandRequest = new CommandWrapperBuilder().updateNote(resourceNameForPermissions, resourceType, resourceId, noteId).withJson(apiRequestBodyAsJson).build(); @@ -139,6 +152,9 @@ public class NotesApiResource { @PathParam("noteId") final Long noteId) { NoteType noteType = NoteType.fromApiUrl(resourceType); + + if(noteType == null){ throw new NoteResourceNotSupportedFoundException(resourceType); }; + final String resourceNameForPermissions = getResourceNameForPermissions(noteType); final CommandWrapper commandRequest = new CommandWrapperBuilder().deleteNote(resourceNameForPermissions, resourceType, resourceId, noteId).build(); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/exception/NoteResourceNotSupportedFoundException.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/exception/NoteResourceNotSupportedFoundException.java new file mode 100644 index 000000000..5d359cb4f --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/note/exception/NoteResourceNotSupportedFoundException.java @@ -0,0 +1,18 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mifosplatform.portfolio.note.exception; + +import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceNotFoundException; + +/** + * A {@link RuntimeException} thrown when Note does not support a resource. + */ +public class NoteResourceNotSupportedFoundException extends AbstractPlatformResourceNotFoundException { + + public NoteResourceNotSupportedFoundException(final String resource) { + super("error.msg.note.resource.not.supported", "Note does not support resource " + resource); + } +} \ No newline at end of file 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 167158a2f..258f6f440 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 @@ -26,6 +26,7 @@ import org.mifosplatform.portfolio.note.domain.Note; import org.mifosplatform.portfolio.note.domain.NoteRepository; import org.mifosplatform.portfolio.note.domain.NoteType; import org.mifosplatform.portfolio.note.exception.NoteNotFoundException; +import org.mifosplatform.portfolio.note.exception.NoteResourceNotSupportedFoundException; import org.mifosplatform.portfolio.note.serialization.NoteCommandFromApiJsonDeserializer; import org.mifosplatform.portfolio.savingsaccount.domain.SavingAccount; import org.mifosplatform.portfolio.savingsaccount.domain.SavingAccountRepository; @@ -63,28 +64,169 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf this.fromApiJsonDeserializer = fromApiJsonDeserializer; } + private CommandProcessingResult createClientNote(final JsonCommand command) { + + final Long resourceId = command.getSupportedEntityId(); + + final Client client = this.clientRepository.findOne(resourceId); + if (client == null) { throw new ClientNotFoundException(resourceId); } + final Note newNote = Note.clientNoteFromJson(client, command); + + this.noteRepository.save(newNote); + + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(newNote.getId()) // + .withClientId(client.getId()).withOfficeId(client.getOffice().getId()).build(); + + } + + private CommandProcessingResult createGroupNote(final JsonCommand command) { + + final Long resourceId = command.getSupportedEntityId(); + + final Group group = this.groupRepository.findOne(resourceId); + if (group == null) { throw new GroupNotFoundException(resourceId); } + final Note newNote = Note.groupNoteFromJson(group, command); + + this.noteRepository.save(newNote); + + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(newNote.getId()) // + .withClientId(group.getId()).withOfficeId(group.getOfficeId()).build(); + } + + private CommandProcessingResult createLoanNote(final JsonCommand command) { + + final Long resourceId = command.getSupportedEntityId(); + + final Loan loan = this.loanRepository.findOne(resourceId); + if (loan == null) { throw new LoanNotFoundException(resourceId); } + + final String note = command.stringValueOfParameterNamed("note"); + final Note newNote = Note.loanNote(loan, note); + + this.noteRepository.save(newNote); + + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(newNote.getId()) // + .withClientId(loan.getClientId()).withOfficeId(loan.getOfficeId()).withLoanId(loan.getId()).withGroupId(loan.getGroupId())// Loan + // can + // be + // associated + // to + // group + .build(); + } + + private CommandProcessingResult createLoanTransactionNote(final JsonCommand command) { + + final Long resourceId = command.getSupportedEntityId(); + + final LoanTransaction loanTransaction = this.loanTransactionRepository.findOne(resourceId); + if (loanTransaction == null) { throw new LoanTransactionNotFoundException(resourceId); } + + final Loan loan = loanTransaction.getLoan(); + + final String note = command.stringValueOfParameterNamed("note"); + final Note newNote = Note.loanTransactionNote(loan, loanTransaction, note); + + this.noteRepository.save(newNote); + + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(newNote.getId()) // + .withClientId(loan.getClientId()).withOfficeId(loan.getOfficeId()).withLoanId(loan.getId()).withGroupId(loan.getGroupId())// Loan + // can + // be + // associated + .build(); + } + + private CommandProcessingResult createDepositAccountNote(final JsonCommand command) { + + final Long resourceId = command.getSupportedEntityId(); + + final DepositAccount depositAccount = this.depositAccountRepository.findOne(resourceId); + if (depositAccount == null) { throw new DepositAccountNotFoundException(resourceId); } + + final String note = command.stringValueOfParameterNamed("note"); + final Note newNote = Note.depositNote(depositAccount, note); + + this.noteRepository.save(newNote); + + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(newNote.getId()) // + .withClientId(depositAccount.client().getId()).withOfficeId(depositAccount.client().getOffice().getId()).build(); + } + + private CommandProcessingResult createSavingAccountNote(final JsonCommand command) { + + final Long resourceId = command.getSupportedEntityId(); + + final SavingAccount savingAccount = this.savingAccountRepository.findOne(resourceId); + if (savingAccount == null) { throw new SavingAccountNotFoundException(resourceId); } + + final String note = command.stringValueOfParameterNamed("note"); + final Note newNote = Note.savingNote(savingAccount, note); + + this.noteRepository.save(newNote); + + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(newNote.getId()) // + .withClientId(savingAccount.getClient().getId()).withOfficeId(savingAccount.getClient().getOffice().getId()).build(); + } + @Override public CommandProcessingResult createNote(final JsonCommand command) { this.fromApiJsonDeserializer.validateNote(command.json()); - final Note newNote = getNewNote(command); - - this.noteRepository.save(newNote); - return new CommandProcessingResultBuilder() // - .withCommandId(command.commandId()) // - .withEntityId(newNote.getId()) // - .build(); + final String resourceUrl = command.getSupportedEntityType(); + final NoteType type = NoteType.fromApiUrl(resourceUrl); + switch (type) { + case CLIENT: { + return createClientNote(command); + } + case DEPOSIT: { + return createDepositAccountNote(command); + } + case GROUP: { + return createGroupNote(command); + } + case LOAN: { + return createLoanNote(command); + } + case LOAN_TRANSACTION: { + return createLoanTransactionNote(command); + } + case SAVING: { + return createSavingAccountNote(command); + } + default: + throw new NoteResourceNotSupportedFoundException(resourceUrl); + } } - @Override - public CommandProcessingResult updateNote(final JsonCommand command) { + private CommandProcessingResult updateClientNote(final JsonCommand command) { - this.fromApiJsonDeserializer.validateNote(command.json()); + final Long resourceId = command.getSupportedEntityId(); + final Long noteId = command.entityId(); + final String resourceUrl = command.getSupportedEntityType(); + + final NoteType type = NoteType.fromApiUrl(resourceUrl); + + final Client client = this.clientRepository.findOne(resourceId); + if (client == null) { throw new ClientNotFoundException(resourceId); } + + final Note noteForUpdate = this.noteRepository.findByClientIdAndId(resourceId, noteId); + if (noteForUpdate == null) { throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase()); } - final Note noteForUpdate = getNoteForUpdate(command); - final Map changes = noteForUpdate.update(command); if (!changes.isEmpty()) { @@ -94,14 +236,187 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf return new CommandProcessingResultBuilder() // .withCommandId(command.commandId()) // .withEntityId(noteForUpdate.getId()) // - .with(changes) // + .withClientId(client.getId()).withOfficeId(client.getOffice().getId()) + .with(changes) + .build(); + + } + + private CommandProcessingResult updateDepositAccountNote(final JsonCommand command) { + + final Long resourceId = command.getSupportedEntityId(); + final Long noteId = command.entityId(); + final String resourceUrl = command.getSupportedEntityType(); + + final NoteType type = NoteType.fromApiUrl(resourceUrl); + + final DepositAccount depositAccount = this.depositAccountRepository.findOne(resourceId); + if (depositAccount == null) { throw new DepositAccountNotFoundException(resourceId); } + final Note noteForUpdate = this.noteRepository.findByDepositAccountIdAndId(resourceId, noteId); + + if (noteForUpdate == null) { throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase()); } + + final Map changes = noteForUpdate.update(command); + + if (!changes.isEmpty()) { + this.noteRepository.saveAndFlush(noteForUpdate); + } + + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(noteForUpdate.getId()) // + .withClientId(depositAccount.client().getId()).withOfficeId(depositAccount.client().getOffice().getId()) + .with(changes) + .build(); + } + + private CommandProcessingResult updateGroupNote(final JsonCommand command) { + + final Long resourceId = command.getSupportedEntityId(); + final Long noteId = command.entityId(); + final String resourceUrl = command.getSupportedEntityType(); + + final NoteType type = NoteType.fromApiUrl(resourceUrl); + + final Group group = this.groupRepository.findOne(resourceId); + if (group == null) { throw new GroupNotFoundException(resourceId); } + final Note noteForUpdate = this.noteRepository.findByGroupIdAndId(resourceId, noteId); + + if (noteForUpdate == null) { throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase()); } + + final Map changes = noteForUpdate.update(command); + + if (!changes.isEmpty()) { + this.noteRepository.saveAndFlush(noteForUpdate); + } + + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(noteForUpdate.getId()) // + .withGroupId(group.getId()).withOfficeId(group.getOfficeId()) + .with(changes) + .build(); + } + + private CommandProcessingResult updateLoanNote(final JsonCommand command) { + + final Long resourceId = command.getSupportedEntityId(); + final Long noteId = command.entityId(); + final String resourceUrl = command.getSupportedEntityType(); + + final NoteType type = NoteType.fromApiUrl(resourceUrl); + + final Loan loan = this.loanRepository.findOne(resourceId); + if (loan == null) { throw new LoanNotFoundException(resourceId); } + final Note noteForUpdate = this.noteRepository.findByLoanIdAndId(resourceId, noteId); + + if (noteForUpdate == null) { throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase()); } + + final Map changes = noteForUpdate.update(command); + + if (!changes.isEmpty()) { + this.noteRepository.saveAndFlush(noteForUpdate); + } + + return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(noteForUpdate.getId()) + .withClientId(loan.getClientId()).withLoanId(loan.getId()).withGroupId(loan.getGroupId()).withOfficeId(loan.getOfficeId()) + .with(changes) + .build(); + } + + private CommandProcessingResult updateLoanTransactionNote(final JsonCommand command) { + + final Long resourceId = command.getSupportedEntityId(); + final Long noteId = command.entityId(); + final String resourceUrl = command.getSupportedEntityType(); + + final NoteType type = NoteType.fromApiUrl(resourceUrl); + + final LoanTransaction loanTransaction = this.loanTransactionRepository.findOne(resourceId); + if (loanTransaction == null) { throw new LoanTransactionNotFoundException(resourceId); } + final Loan loan = loanTransaction.getLoan(); + + final Note noteForUpdate = this.noteRepository.findByLoanTransactionIdAndId(resourceId, noteId); + + if (noteForUpdate == null) { throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase()); } + + final Map changes = noteForUpdate.update(command); + + if (!changes.isEmpty()) { + this.noteRepository.saveAndFlush(noteForUpdate); + } + + return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(noteForUpdate.getId()) + .withClientId(loan.getClientId()).withLoanId(loan.getId()).withGroupId(loan.getGroupId()).withOfficeId(loan.getOfficeId()) + .with(changes) + .build(); + } + + private CommandProcessingResult updateSavingAccountNote(final JsonCommand command) { + + final Long resourceId = command.getSupportedEntityId(); + final Long noteId = command.entityId(); + final String resourceUrl = command.getSupportedEntityType(); + + final NoteType type = NoteType.fromApiUrl(resourceUrl); + + final SavingAccount savingAccount = this.savingAccountRepository.findOne(resourceId); + if (savingAccount == null) { throw new SavingAccountNotFoundException(resourceId); } + + final Note noteForUpdate = this.noteRepository.findBySavingAccountIdAndId(resourceId, noteId); + + if (noteForUpdate == null) { throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase()); } + + final Map changes = noteForUpdate.update(command); + + if (!changes.isEmpty()) { + this.noteRepository.saveAndFlush(noteForUpdate); + } + + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(noteForUpdate.getId()) // + .withClientId(savingAccount.getClient().getId()).withOfficeId(savingAccount.getClient().getOffice().getId()) + .with(changes) .build(); } @Override - public CommandProcessingResult deleteNote(JsonCommand command) { - - final Note noteForDelete = getNoteForUpdate(command); + public CommandProcessingResult updateNote(final JsonCommand command) { + + this.fromApiJsonDeserializer.validateNote(command.json()); + + final String resourceUrl = command.getSupportedEntityType(); + final NoteType type = NoteType.fromApiUrl(resourceUrl); + + switch (type) { + case CLIENT: { + return updateClientNote(command); + } + case DEPOSIT: { + return updateDepositAccountNote(command); + } + case GROUP: { + return updateGroupNote(command); + } + case LOAN: { + return updateLoanNote(command); + } + case LOAN_TRANSACTION: { + return updateLoanTransactionNote(command); + } + case SAVING: { + return updateSavingAccountNote(command); + } + default: + throw new NoteResourceNotSupportedFoundException(resourceUrl); + } + } + + @Override + public CommandProcessingResult deleteNote(final JsonCommand command) { + + final Note noteForDelete = getNoteForDelete(command); this.noteRepository.delete(noteForDelete); return new CommandProcessingResultBuilder() // @@ -110,56 +425,7 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf .build(); } - private Note getNewNote(final JsonCommand command) { - final String resourceUrl = command.getSupportedEntityType(); - final Long resourceId = command.getSupportedEntityId(); - final NoteType type = NoteType.fromApiUrl(resourceUrl); - Note newNote = null; - final String note = command.stringValueOfParameterNamed("note"); - switch (type) { - case CLIENT: { - final Client client = this.clientRepository.findOne(resourceId); - if (client == null) { throw new ClientNotFoundException(resourceId); } - newNote = Note.clientNoteFromJson(client, command); - } - break; - case DEPOSIT: { - final DepositAccount depositAccount = this.depositAccountRepository.findOne(resourceId); - if (depositAccount == null) { throw new DepositAccountNotFoundException(resourceId); } - newNote = Note.depositNote(depositAccount, note); - } - break; - case GROUP: { - final Group group = this.groupRepository.findOne(resourceId); - if (group == null) { throw new GroupNotFoundException(resourceId); } - newNote = Note.groupNoteFromJson(group, command); - } - break; - case LOAN: { - final Loan loan = this.loanRepository.findOne(resourceId); - if (loan == null) { throw new LoanNotFoundException(resourceId); } - newNote = Note.loanNote(loan, note); - } - break; - case LOAN_TRANSACTION: { - final LoanTransaction loanTransaction = this.loanTransactionRepository.findOne(resourceId); - if (loanTransaction == null) { throw new LoanTransactionNotFoundException(resourceId); } - final Loan loan = loanTransaction.getLoan(); - newNote = Note.loanTransactionNote(loan, loanTransaction, note); - } - break; - case SAVING: { - final SavingAccount savingAccount = this.savingAccountRepository.findOne(resourceId); - if (savingAccount == null) { throw new SavingAccountNotFoundException(resourceId); } - newNote = Note.savingNote(savingAccount, note); - } - break; - } - - return newNote; - } - - private Note getNoteForUpdate(final JsonCommand command) { + private Note getNoteForDelete(final JsonCommand command) { final String resourceUrl = command.getSupportedEntityType(); final Long resourceId = command.getSupportedEntityId(); final Long noteId = command.entityId(); @@ -194,5 +460,5 @@ public class NoteWritePlatformServiceJpaRepositoryImpl implements NoteWritePlatf if (noteForUpdate == null) { throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase()); } return noteForUpdate; } - + }