fix client update to support data integrity errors on accountNo and externalId.

This commit is contained in:
Keith Woodlock
2013-01-16 17:01:00 +00:00
parent 11e4a38796
commit 0b9d76582f
2 changed files with 19 additions and 9 deletions
@@ -14,6 +14,7 @@ import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import javax.persistence.Transient; import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
@@ -26,7 +27,7 @@ import org.mifosplatform.organisation.office.domain.Office;
import org.springframework.data.jpa.domain.AbstractPersistable; import org.springframework.data.jpa.domain.AbstractPersistable;
@Entity @Entity
@Table(name = "m_client") @Table(name = "m_client", uniqueConstraints = { @UniqueConstraint(columnNames = { "account_no" }, name = "account_no_UNIQUE")})
public class Client extends AbstractPersistable<Long> { public class Client extends AbstractPersistable<Long> {
@Column(name = "account_no", length = 20, unique = true, nullable = false) @Column(name = "account_no", length = 20, unique = true, nullable = false)
@@ -85,8 +86,8 @@ public class Client extends AbstractPersistable<Long> {
// //
} }
private Client(final Office office, final String accountNo, final String firstname, final String middlename, final String lastname, final String fullname, private Client(final Office office, final String accountNo, final String firstname, final String middlename, final String lastname,
final LocalDate openingDate, final String externalId) { final String fullname, final LocalDate openingDate, final String externalId) {
if (StringUtils.isBlank(accountNo)) { if (StringUtils.isBlank(accountNo)) {
this.accountNumber = new RandomPasswordGenerator(19).generate(); this.accountNumber = new RandomPasswordGenerator(19).generate();
this.accountNumberRequiresAutoGeneration = true; this.accountNumberRequiresAutoGeneration = true;
@@ -107,6 +107,10 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
final String externalId = command.stringValueOfParameterNamed("externalId"); final String externalId = command.stringValueOfParameterNamed("externalId");
throw new PlatformDataIntegrityException("error.msg.client.duplicate.externalId", "Client with externalId `" + externalId throw new PlatformDataIntegrityException("error.msg.client.duplicate.externalId", "Client with externalId `" + externalId
+ "` already exists", "externalId", externalId); + "` already exists", "externalId", externalId);
} else if (realCause.getMessage().contains("account_no_UNIQUE")) {
final String accountNo = command.stringValueOfParameterNamed("accountNo");
throw new PlatformDataIntegrityException("error.msg.client.duplicate.accountNo", "Client with accountNo `" + accountNo
+ "` already exists", "accountNo", accountNo);
} }
logAsErrorUnexpectedDataIntegrityException(dve); logAsErrorUnexpectedDataIntegrityException(dve);
@@ -173,11 +177,16 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
} }
if (!changes.isEmpty()) { if (!changes.isEmpty()) {
this.clientRepository.save(clientForUpdate); this.clientRepository.saveAndFlush(clientForUpdate);
} }
return new CommandProcessingResultBuilder().withCommandId(command.commandId()) return new CommandProcessingResultBuilder() //
.withOfficeId(clientForUpdate.getOffice().getId()).withClientId(clientId).withEntityId(clientId).with(changes).build(); .withCommandId(command.commandId()) //
.withOfficeId(clientForUpdate.getOffice().getId()) //
.withClientId(clientId) //
.withEntityId(clientId) //
.with(changes) //
.build();
} catch (DataIntegrityViolationException dve) { } catch (DataIntegrityViolationException dve) {
handleDataIntegrityIssues(command, dve); handleDataIntegrityIssues(command, dve);
return new CommandProcessingResult(Long.valueOf(-1)); return new CommandProcessingResult(Long.valueOf(-1));