Updated Notes in API doc

This commit is contained in:
ashok-conflux
2013-03-07 22:11:06 +05:30
parent 80c3850e00
commit 34e2bb3c4d
4 changed files with 608 additions and 235 deletions
@@ -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();
@@ -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);
}
}
@@ -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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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;
}
}