diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/client/domain/Client.java b/mifosng-provider/src/main/java/org/mifosng/platform/client/domain/Client.java index 4cc2c60f8..3af0588c2 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/client/domain/Client.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/client/domain/Client.java @@ -118,7 +118,7 @@ public class Client extends AbstractAuditableCustom { } if (command.isExternalIdChanged()) { - this.externalId = StringUtils.defaultIfEmpty(externalId.trim(), null); + this.externalId = StringUtils.defaultIfEmpty(command.getExternalId().trim(), null); } } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java index e6f35cd64..ff0e2c4c6 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java @@ -11,16 +11,22 @@ import org.mifosng.platform.client.domain.NoteRepository; import org.mifosng.platform.exceptions.ClientNotFoundException; import org.mifosng.platform.exceptions.NoteNotFoundException; import org.mifosng.platform.exceptions.OfficeNotFoundException; +import org.mifosng.platform.exceptions.PlatformDataIntegrityException; import org.mifosng.platform.organisation.domain.Office; import org.mifosng.platform.organisation.domain.OfficeRepository; import org.mifosng.platform.security.PlatformSecurityContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWritePlatformService { + private final static Logger logger = LoggerFactory.getLogger(ClientWritePlatformServiceJpaRepositoryImpl.class); + private final PlatformSecurityContext context; private final ClientRepository clientRepository; private final OfficeRepository officeRepository; @@ -50,61 +56,85 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP return new EntityIdentifier(client.getId()); } + /* + * Guaranteed to throw an exception no matter what the data integrity issue is. + */ + private void handleDataIntegrityIssues(final ClientCommand command, final DataIntegrityViolationException dve) { + + Throwable realCause = dve.getMostSpecificCause(); + if (realCause.getMessage().contains("external_id")) { + throw new PlatformDataIntegrityException("error.msg.client.duplicate.externalId", "Client with externalId `"+ command.getExternalId() +"` already exists", "externalId", command.getExternalId()); + } + + logger.error(dve.getMessage(), dve); + throw new PlatformDataIntegrityException("error.msg.client.unknown.data.integrity.issue", "Unknown data integrity issue with resource."); + } + @Transactional @Override public Long enrollClient(final ClientCommand command) { - context.authenticatedUser(); - - ClientCommandValidator validator = new ClientCommandValidator(command); - validator.validateForCreate(); - - Office clientOffice = this.officeRepository.findOne(command.getOfficeId()); - if (clientOffice == null) { - throw new OfficeNotFoundException(command.getOfficeId()); + try{ + context.authenticatedUser(); + + ClientCommandValidator validator = new ClientCommandValidator(command); + validator.validateForCreate(); + + Office clientOffice = this.officeRepository.findOne(command.getOfficeId()); + if (clientOffice == null) { + throw new OfficeNotFoundException(command.getOfficeId()); + } + + String firstname = command.getFirstname(); + String lastname = command.getLastname(); + if (StringUtils.isNotBlank(command.getClientOrBusinessName())) { + lastname = command.getClientOrBusinessName(); + firstname = null; + } + + Client newClient = Client.newClient(clientOffice, firstname, lastname, command.getJoiningDate(), command.getExternalId()); + + this.clientRepository.save(newClient); + + return newClient.getId(); + } catch (DataIntegrityViolationException dve) { + handleDataIntegrityIssues(command, dve); + return Long.valueOf(-1); } - - String firstname = command.getFirstname(); - String lastname = command.getLastname(); - if (StringUtils.isNotBlank(command.getClientOrBusinessName())) { - lastname = command.getClientOrBusinessName(); - firstname = null; - } - - Client newClient = Client.newClient(clientOffice, firstname, lastname, command.getJoiningDate(), command.getExternalId()); - - this.clientRepository.save(newClient); - - return newClient.getId(); } @Transactional @Override public EntityIdentifier updateClientDetails(final ClientCommand command) { - context.authenticatedUser(); - - ClientCommandValidator validator = new ClientCommandValidator(command); - validator.validateForUpdate(); - - Office clientOffice = null; - Long officeId = command.getOfficeId(); - if (command.isOfficeChanged() && officeId != null) { - clientOffice = this.officeRepository.findOne(officeId); - if (clientOffice == null) { - throw new OfficeNotFoundException(command.getOfficeId()); - } + try { + context.authenticatedUser(); + + ClientCommandValidator validator = new ClientCommandValidator(command); + validator.validateForUpdate(); + + Office clientOffice = null; + Long officeId = command.getOfficeId(); + if (command.isOfficeChanged() && officeId != null) { + clientOffice = this.officeRepository.findOne(officeId); + if (clientOffice == null) { + throw new OfficeNotFoundException(command.getOfficeId()); + } + } + + Client clientForUpdate = this.clientRepository.findOne(command.getId()); + if (clientForUpdate == null || clientForUpdate.isDeleted()) { + throw new ClientNotFoundException(command.getId()); + } + clientForUpdate.update(clientOffice, command); + + this.clientRepository.saveAndFlush(clientForUpdate); + + return new EntityIdentifier(clientForUpdate.getId()); + } catch (DataIntegrityViolationException dve) { + handleDataIntegrityIssues(command, dve); + return new EntityIdentifier(Long.valueOf(-1)); } - - Client clientForUpdate = this.clientRepository.findOne(command.getId()); - if (clientForUpdate == null || clientForUpdate.isDeleted()) { - throw new ClientNotFoundException(command.getId()); - } - clientForUpdate.update(clientOffice, command); - - this.clientRepository.save(clientForUpdate); - - return new EntityIdentifier(clientForUpdate.getId()); } @Transactional