From 485d6f10e3e864e6260a41de75d31d08106ff6ba Mon Sep 17 00:00:00 2001 From: Keith Woodlock Date: Thu, 9 May 2013 15:29:23 +0100 Subject: [PATCH] fix null pointer when creating a group with clients. --- ...WritePlatformServiceJpaRepositoryImpl.java | 88 ++++++++++--------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java index 57e420cdf..9c6c44b14 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java @@ -110,7 +110,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group staff = this.staffRepository.findByOfficeWithNotFoundDetection(staffId, officeId); } - final Set clientMembers = assembleSetOfClients(parentGroup, command); + final Set clientMembers = assembleSetOfClients(officeId, command); final Set groupMembers = assembleSetOfChildGroups(officeId, command); @@ -278,11 +278,14 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group } } - /*final Set clientMembers = assembleSetOfClients(officeId, command); - List changes = groupForUpdate.updateClientMembersIfDifferent(clientMembers); - if (!changes.isEmpty()) { - actualChanges.put(GroupingTypesApiConstants.clientMembersParamName, changes); - }*/ + /* + * final Set clientMembers = assembleSetOfClients(officeId, + * command); List changes = + * groupForUpdate.updateClientMembersIfDifferent(clientMembers); if + * (!changes.isEmpty()) { + * actualChanges.put(GroupingTypesApiConstants + * .clientMembersParamName, changes); } + */ this.groupRepository.saveAndFlush(groupForUpdate); @@ -354,7 +357,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group .build(); } - private Set assembleSetOfClients(final Group group, final JsonCommand command) { + private Set assembleSetOfClients(final Long groupOfficeId, final JsonCommand command) { final Set clientMembers = new HashSet(); final String[] clientMembersArray = command.arrayValueOfParameterNamed(GroupingTypesApiConstants.clientMembersParamName); @@ -363,9 +366,9 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group for (final String clientId : clientMembersArray) { final Long id = Long.valueOf(clientId); final Client client = this.clientRepository.findOneWithNotFoundDetection(id); - if (!client.isOfficeIdentifiedBy(group.officeId())) { - final String errorMessage = "Group with identifier " + group.getId() + " and Client with identifier " + clientId + " must have the same office."; - throw new InvalidOfficeException("client", "attach.to.group", errorMessage, group.getId(), clientId); + if (!client.isOfficeIdentifiedBy(groupOfficeId)) { + final String errorMessage = "Client with identifier " + clientId + " must have the same office as group."; + throw new InvalidOfficeException("client", "attach.to.group", errorMessage, clientId, groupOfficeId); } clientMembers.add(client); } @@ -440,67 +443,68 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group } @Override - public CommandProcessingResult associateClientsToGroup(Long groupId, JsonCommand command) { - + public CommandProcessingResult associateClientsToGroup(final Long groupId, final JsonCommand command) { + this.fromApiJsonDeserializer.validateForAssociateClients(command.json()); - + final Group groupForUpdate = this.groupRepository.findOneWithNotFoundDetection(groupId); - final Set clientMembers = assembleSetOfClients(groupForUpdate, command); + final Set clientMembers = assembleSetOfClients(groupForUpdate.officeId(), command); Map actualChanges = new HashMap(); - + List changes = groupForUpdate.associateClients(clientMembers); if (!changes.isEmpty()) { actualChanges.put(GroupingTypesApiConstants.clientMembersParamName, changes); } - + this.groupRepository.saveAndFlush(groupForUpdate); - + return new CommandProcessingResultBuilder() // - .withCommandId(command.commandId()) // - .withOfficeId(groupForUpdate.officeId()) // - .withGroupId(groupForUpdate.getId()) // - .withEntityId(groupForUpdate.getId()) // - .with(actualChanges) // - .build(); + .withCommandId(command.commandId()) // + .withOfficeId(groupForUpdate.officeId()) // + .withGroupId(groupForUpdate.getId()) // + .withEntityId(groupForUpdate.getId()) // + .with(actualChanges) // + .build(); } @Transactional @Override - public CommandProcessingResult disassociateClientsFromGroup(Long groupId, JsonCommand command) { - this.fromApiJsonDeserializer.validateForDisassociateClients(command.json()); - + public CommandProcessingResult disassociateClientsFromGroup(final Long groupId, final JsonCommand command) { + this.fromApiJsonDeserializer.validateForDisassociateClients(command.json()); + final Group groupForUpdate = this.groupRepository.findOneWithNotFoundDetection(groupId); - final Set clientMembers = assembleSetOfClients(groupForUpdate, command); - - //check if any client has got group loans + final Set clientMembers = assembleSetOfClients(groupForUpdate.officeId(), command); + + // check if any client has got group loans validateForJLGLoan(groupForUpdate.getId(), clientMembers); Map actualChanges = new HashMap(); - + List changes = groupForUpdate.disassociateClients(clientMembers); if (!changes.isEmpty()) { actualChanges.put(GroupingTypesApiConstants.clientMembersParamName, changes); } - + this.groupRepository.saveAndFlush(groupForUpdate); - + return new CommandProcessingResultBuilder() // - .withCommandId(command.commandId()) // - .withOfficeId(groupForUpdate.officeId()) // - .withGroupId(groupForUpdate.getId()) // - .withEntityId(groupForUpdate.getId()) // - .with(actualChanges) // - .build(); + .withCommandId(command.commandId()) // + .withOfficeId(groupForUpdate.officeId()) // + .withGroupId(groupForUpdate.getId()) // + .withEntityId(groupForUpdate.getId()) // + .with(actualChanges) // + .build(); } @Transactional - private void validateForJLGLoan(Long groupId, Set clientMembers) { + private void validateForJLGLoan(final Long groupId, final Set clientMembers) { for (Client client : clientMembers) { Collection loans = this.loanRepository.findByClientIdAndGroupId(client.getId(), groupId); - if(!CollectionUtils.isEmpty(loans)){ - final String defaultUserMessage = "Client with identifier " + client.getId() + " cannot be disassociated it has group loans."; + if (!CollectionUtils.isEmpty(loans)) { + final String defaultUserMessage = "Client with identifier " + client.getId() + + " cannot be disassociated it has group loans."; throw new GroupLoanExistsException("disassociate", "client.has.group.loan", defaultUserMessage, client.getId(), groupId); } } } - + } \ No newline at end of file