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.TemporalType;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate;
@@ -26,7 +27,7 @@ import org.mifosplatform.organisation.office.domain.Office;
import org.springframework.data.jpa.domain.AbstractPersistable;
@Entity
@Table(name = "m_client")
@Table(name = "m_client", uniqueConstraints = { @UniqueConstraint(columnNames = { "account_no" }, name = "account_no_UNIQUE")})
public class Client extends AbstractPersistable<Long> {
@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,
final LocalDate openingDate, final String externalId) {
private Client(final Office office, final String accountNo, final String firstname, final String middlename, final String lastname,
final String fullname, final LocalDate openingDate, final String externalId) {
if (StringUtils.isBlank(accountNo)) {
this.accountNumber = new RandomPasswordGenerator(19).generate();
this.accountNumberRequiresAutoGeneration = true;
@@ -123,15 +124,15 @@ public class Client extends AbstractPersistable<Long> {
} else {
this.fullname = null;
}
deriveDisplayName();
validateNameParts();
}
public boolean isAccountNumberRequiresAutoGeneration() {
return this.accountNumberRequiresAutoGeneration;
}
public void setAccountNumberRequiresAutoGeneration(boolean accountNumberRequiresAutoGeneration) {
this.accountNumberRequiresAutoGeneration = accountNumberRequiresAutoGeneration;
}
@@ -107,6 +107,10 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
final String externalId = command.stringValueOfParameterNamed("externalId");
throw new PlatformDataIntegrityException("error.msg.client.duplicate.externalId", "Client with 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);
@@ -173,11 +177,16 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
}
if (!changes.isEmpty()) {
this.clientRepository.save(clientForUpdate);
this.clientRepository.saveAndFlush(clientForUpdate);
}
return new CommandProcessingResultBuilder().withCommandId(command.commandId())
.withOfficeId(clientForUpdate.getOffice().getId()).withClientId(clientId).withEntityId(clientId).with(changes).build();
return new CommandProcessingResultBuilder() //
.withCommandId(command.commandId()) //
.withOfficeId(clientForUpdate.getOffice().getId()) //
.withClientId(clientId) //
.withEntityId(clientId) //
.with(changes) //
.build();
} catch (DataIntegrityViolationException dve) {
handleDataIntegrityIssues(command, dve);
return new CommandProcessingResult(Long.valueOf(-1));