MIFOSX-29: allow user to add or edit external id for client.

This commit is contained in:
Keith Woodlock
2012-08-23 12:01:36 +01:00
parent bf4490feb9
commit 47a215ec4f
2 changed files with 74 additions and 44 deletions
@@ -118,7 +118,7 @@ public class Client extends AbstractAuditableCustom<AppUser, Long> {
} }
if (command.isExternalIdChanged()) { if (command.isExternalIdChanged()) {
this.externalId = StringUtils.defaultIfEmpty(externalId.trim(), null); this.externalId = StringUtils.defaultIfEmpty(command.getExternalId().trim(), null);
} }
} }
@@ -11,16 +11,22 @@ import org.mifosng.platform.client.domain.NoteRepository;
import org.mifosng.platform.exceptions.ClientNotFoundException; import org.mifosng.platform.exceptions.ClientNotFoundException;
import org.mifosng.platform.exceptions.NoteNotFoundException; import org.mifosng.platform.exceptions.NoteNotFoundException;
import org.mifosng.platform.exceptions.OfficeNotFoundException; 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.Office;
import org.mifosng.platform.organisation.domain.OfficeRepository; import org.mifosng.platform.organisation.domain.OfficeRepository;
import org.mifosng.platform.security.PlatformSecurityContext; import org.mifosng.platform.security.PlatformSecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@Service @Service
public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWritePlatformService { public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWritePlatformService {
private final static Logger logger = LoggerFactory.getLogger(ClientWritePlatformServiceJpaRepositoryImpl.class);
private final PlatformSecurityContext context; private final PlatformSecurityContext context;
private final ClientRepository clientRepository; private final ClientRepository clientRepository;
private final OfficeRepository officeRepository; private final OfficeRepository officeRepository;
@@ -50,10 +56,25 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
return new EntityIdentifier(client.getId()); 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 @Transactional
@Override @Override
public Long enrollClient(final ClientCommand command) { public Long enrollClient(final ClientCommand command) {
try{
context.authenticatedUser(); context.authenticatedUser();
ClientCommandValidator validator = new ClientCommandValidator(command); ClientCommandValidator validator = new ClientCommandValidator(command);
@@ -76,12 +97,17 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
this.clientRepository.save(newClient); this.clientRepository.save(newClient);
return newClient.getId(); return newClient.getId();
} catch (DataIntegrityViolationException dve) {
handleDataIntegrityIssues(command, dve);
return Long.valueOf(-1);
}
} }
@Transactional @Transactional
@Override @Override
public EntityIdentifier updateClientDetails(final ClientCommand command) { public EntityIdentifier updateClientDetails(final ClientCommand command) {
try {
context.authenticatedUser(); context.authenticatedUser();
ClientCommandValidator validator = new ClientCommandValidator(command); ClientCommandValidator validator = new ClientCommandValidator(command);
@@ -102,9 +128,13 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
} }
clientForUpdate.update(clientOffice, command); clientForUpdate.update(clientOffice, command);
this.clientRepository.save(clientForUpdate); this.clientRepository.saveAndFlush(clientForUpdate);
return new EntityIdentifier(clientForUpdate.getId()); return new EntityIdentifier(clientForUpdate.getId());
} catch (DataIntegrityViolationException dve) {
handleDataIntegrityIssues(command, dve);
return new EntityIdentifier(Long.valueOf(-1));
}
} }
@Transactional @Transactional