tidy up client update

This commit is contained in:
Keith Woodlock
2012-07-31 19:27:58 +01:00
parent ef02c8be1f
commit 37a45649b1
9 changed files with 55 additions and 29 deletions
@@ -69,7 +69,7 @@ public class FundsApiResource {
@Path("{fundId}")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public String retreiveOffice(@PathParam("fundId") final Long fundId, @Context UriInfo uriInfo) {
public String retreiveOffice(@PathParam("fundId") final Long fundId, @Context final UriInfo uriInfo) {
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
@@ -56,7 +56,27 @@ public class ClientCommand {
return joiningDate;
}
public boolean isOfficeIdChanged() {
public boolean isFirstnameChanged() {
return this.modifiedParameters.contains("firstname");
}
public boolean isLastnameChanged() {
return this.modifiedParameters.contains("lastname");
}
public boolean isClientOrBusinessNameChanged() {
return this.modifiedParameters.contains("clientOrBusinessName");
}
public boolean isExternalIdChanged() {
return this.modifiedParameters.contains("externalId");
}
public boolean isJoiningDateChanged() {
return this.modifiedParameters.contains("joiningDate");
}
public boolean isOfficeChanged() {
return this.modifiedParameters.contains("officeId");
}
}
@@ -9,12 +9,12 @@ import org.joda.time.LocalDate;
public class ClientData {
private final Long id;
private final Long officeId;
private final String officeName;
private final String firstname;
private final String lastname;
private final String clientOrBusinessName;
private final String displayName;
private final Long officeId;
private final String officeName;
private final String externalId;
private final LocalDate joinedDate;
@@ -13,8 +13,9 @@ public class OfficeData {
private String externalId;
private LocalDate openingDate;
private String hierarchy;
private String parentName;
private Long parentId;
private String parentName;
private List<OfficeLookup> allowedParents = new ArrayList<OfficeLookup>();
public OfficeData() {
@@ -106,5 +107,4 @@ public class OfficeData {
public void setAllowedParents(List<OfficeLookup> allowedParents) {
this.allowedParents = allowedParents;
}
}
@@ -98,21 +98,27 @@ public class Client extends AbstractAuditableCustom<AppUser, Long> {
}
}
public void update(Office clientOffice, String firstname, String lastname, String externalId, LocalDate joiningDate) {
if (clientOffice != null) {
public void update(final Office clientOffice, final ClientCommand command) {
if (command.isOfficeChanged()) {
this.office = clientOffice;
}
if (joiningDate != null) {
this.joiningDate = joiningDate.toDate();
if (command.isJoiningDateChanged()) {
this.joiningDate = command.getJoiningDate().toDate();
}
if (StringUtils.isNotBlank(firstname)) {
this.firstName = firstname;
if (command.isFirstnameChanged()) {
this.firstName = command.getFirstname();
}
if (StringUtils.isNotBlank(lastname)) {
this.lastName = lastname;
if (command.isLastnameChanged()) {
this.lastName = command.getLastname();
}
if (externalId != null) {
this.externalId = externalId.trim();
if (command.isClientOrBusinessNameChanged()) {
this.lastName = command.getClientOrBusinessName();
this.firstName = null;
}
if (command.isExternalIdChanged()) {
this.externalId = StringUtils.defaultIfEmpty(externalId.trim(), null);
}
}
@@ -33,7 +33,7 @@ public class ClientCommandValidator {
baseDataValidator.reset().parameter("joiningDate").value(command.getJoiningDate()).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("externalId").value(command.getExternalId()).ignoreIfNull().notExceedingLengthOf(100);
if (command.isOfficeIdChanged()) {
if (command.isOfficeChanged()) {
baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).notNull().greaterThanZero();
}
@@ -89,25 +89,18 @@ public class ClientWritePlatformServiceJpaRepositoryImpl implements ClientWriteP
Office clientOffice = null;
Long officeId = command.getOfficeId();
if (officeId != null) {
if (command.isOfficeChanged() && officeId != null) {
clientOffice = this.officeRepository.findOne(officeId);
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 clientForUpdate = this.clientRepository.findOne(command.getId());
if (clientForUpdate == null || clientForUpdate.isDeleted()) {
throw new ClientNotFoundException(command.getId());
}
clientForUpdate.update(clientOffice, firstname, lastname, command.getExternalId(), command.getJoiningDate());
clientForUpdate.update(clientOffice, command);
this.clientRepository.save(clientForUpdate);
@@ -43,7 +43,7 @@ public class LoanProductCommandValidator {
baseDataValidator.reset().parameter("interestType").value(command.getInterestType()).ignoreIfNull().notNull().inMinMaxRange(0, 1);
baseDataValidator.reset().parameter("interestCalculationPeriodType").value(command.getInterestCalculationPeriodType()).ignoreIfNull().notNull().inMinMaxRange(0, 1);
baseDataValidator.reset().anyOfNotNull(command.getFundId(), command.getName(), command.getDescription(), command.getCurrencyCode(), command.getDigitsAfterDecimal(),
baseDataValidator.reset().anyOfNotNull(command.getFundId(), command.getTransactionProcessingStrategyId(), command.getName(), command.getDescription(), command.getCurrencyCode(), command.getDigitsAfterDecimal(),
command.getPrincipal(), command.getInArrearsTolerance(), command.getRepaymentFrequencyType(), command.getRepaymentEvery(), command.getNumberOfRepayments(),
command.getInterestRatePerPeriod(), command.getInterestRateFrequencyType(), command.getAmortizationType(), command.getInterestType(), command.getInterestCalculationPeriodType());
@@ -121,8 +121,15 @@ public class LoanProductWritePlatformServiceJpaRepositoryImpl implements LoanPro
}
// associating fund with loan product at creation is optional for now.
Fund fund = findFundByIdIfProvided(command.getFundId());
LoanTransactionProcessingStrategy loanTransactionProcessingStrategy = findStrategyByIdIfProvided(command.getTransactionProcessingStrategyId());
Fund fund = null;
if (command.isFundChanged()) {
fund = findFundByIdIfProvided(command.getFundId());
}
LoanTransactionProcessingStrategy loanTransactionProcessingStrategy = null;
if (command.isTransactionProcessingStrategyChanged()) {
findStrategyByIdIfProvided(command.getTransactionProcessingStrategyId());
}
product.update(command, fund, loanTransactionProcessingStrategy);