Fix deposit account data object, make it immutable again.

This commit is contained in:
Keith Woodlock
2012-09-11 21:08:26 +01:00
parent 0bfabbe99a
commit 5154e87b23
8 changed files with 95 additions and 110 deletions
@@ -130,10 +130,9 @@ public class DepositAccountsApiResource {
responseParameters.addAll(associationParameters);
}
permissions = this.depositAccountReadPlatformService.retrieveDepositAccountsPermissions(account);
account = new DepositAccountData(account, permissions, account.getTransactions());
}
account.setPermissions(permissions);
return this.apiJsonSerializerService.serializeDepositAccountDataToJson(prettyPrint, responseParameters, account);
}
@@ -45,8 +45,8 @@ public class DepositAccountData {
private final List<EnumOptionData> interestCompoundedEveryPeriodTypeOptions;
private final List<DepositProductLookup> productOptions;
private Collection<DepositAccountTransactionData> transactions;
private DepositPermissionData permissions;
private final Collection<DepositAccountTransactionData> transactions;
private final DepositPermissionData permissions;
/*
* used when returning account template data but only a clientId is passed, no selected product.
@@ -132,6 +132,42 @@ public class DepositAccountData {
this.permissions=account.getPermissions();
}
public DepositAccountData(final DepositAccountData account, final DepositPermissionData permissions, final Collection<DepositAccountTransactionData> transactions) {
this.id = account.getId();
this.externalId = account.getExternalId();
this.status = account.getStatus();
this.clientId = account.getClientId();
this.clientName = account.getClientName();
this.productId = account.getProductId();
this.productName = account.getProductName();
this.currency = account.getCurrency();
this.deposit = account.getDeposit();
this.maturityInterestRate = account.getMaturityInterestRate();
this.tenureInMonths = account.getTenureInMonths();
this.projectedCommencementDate = account.getProjectedCommencementDate();
this.actualCommencementDate = account.getActualCommencementDate();
this.maturedOn = account.getMaturedOn();
this.projectedInterestAccrued = account.getProjectedInterestAccrued();
this.actualInterestAccrued = account.getActualInterestAccrued();
this.projectedMaturityAmount = account.getProjectedMaturityAmount();
this.actualMaturityAmount = account.getActualMaturityAmount();
this.interestCompoundedEvery = account.getInterestCompoundedEvery();
this.interestCompoundedEveryPeriodType = account.getInterestCompoundedEveryPeriodType();
this.renewalAllowed = account.isRenewalAllowed();
this.preClosureAllowed = account.isPreClosureAllowed();
this.preClosureInterestRate = account.getPreClosureInterestRate();
this.interestCompoundedEveryPeriodTypeOptions = account.getInterestCompoundedEveryPeriodTypeOptions();
this.productOptions = account.getProductOptions();
this.withdrawnonDate=account.getWithdrawnonDate();
this.rejectedonDate=account.getRejectedonDate();
this.closedonDate=account.getClosedonDate();
this.transactions=transactions;
this.permissions=permissions;
}
public DepositAccountData(
final Long id,
final String externalId,
@@ -357,16 +393,7 @@ public class DepositAccountData {
return transactions;
}
public void setTransactions(Collection<DepositAccountTransactionData> transactions) {
this.transactions = transactions;
}
public DepositPermissionData getPermissions() {
return permissions;
}
public void setPermissions(DepositPermissionData permissions) {
this.permissions = permissions;
}
}
@@ -4,6 +4,9 @@ import java.math.BigDecimal;
import org.joda.time.LocalDate;
/**
* Immutable data object to represent transactions on a deposit account.
*/
public class DepositAccountTransactionData {
private final Long transactionId;
@@ -12,18 +15,9 @@ public class DepositAccountTransactionData {
private final BigDecimal transactionAmount;
private final Long accountId;
public DepositAccountTransactionData() {
this.transactionId = null;
this.accountId = null;
this.transactionType = null;
this.transactionDate = null;
this.transactionAmount = null;
}
public DepositAccountTransactionData(final Long transactionId, final Long accountId, final EnumOptionData transactionType, final LocalDate transactionDate, final BigDecimal transactionAmount ){
public DepositAccountTransactionData(
final Long transactionId, final Long accountId, final EnumOptionData transactionType,
final LocalDate transactionDate, final BigDecimal transactionAmount ) {
this.transactionId = transactionId;
this.accountId = accountId;
this.transactionType = transactionType;
@@ -50,7 +44,4 @@ public class DepositAccountTransactionData {
public BigDecimal getTransactionAmount() {
return transactionAmount;
}
}
}
@@ -8,7 +8,7 @@ public class DepositPermissionData {
private final boolean pendingApproval;
private final boolean isActive;
private final boolean anyActionOnLoanAllowed;
private final boolean anyActionOnAccountAllowed;
public DepositPermissionData(final boolean rejectAllowed,final boolean withdrawnByApplicantAllowed,final boolean undoApprovalAllowed,final boolean pendingApproval,final boolean isActive) {
this.rejectAllowed = rejectAllowed;
@@ -16,7 +16,7 @@ public class DepositPermissionData {
this.undoApprovalAllowed = undoApprovalAllowed;
this.pendingApproval = pendingApproval;
this.isActive=isActive;
this.anyActionOnLoanAllowed = isRejectAllowed() || isPendingApproval() || isUndoApprovalAllowed() || isWithdrawnByApplicantAllowed();
this.anyActionOnAccountAllowed = isRejectAllowed() || isPendingApproval() || isUndoApprovalAllowed() || isWithdrawnByApplicantAllowed();
}
public boolean isRejectAllowed() {
@@ -34,12 +34,12 @@ public class DepositPermissionData {
public boolean isPendingApproval() {
return pendingApproval;
}
public boolean isAnyActionOnLoanAllowed() {
return anyActionOnLoanAllowed;
public boolean isAnyActionOnAccountAllowed() {
return anyActionOnAccountAllowed;
}
public boolean isActive() {
return isActive;
}
}
}
@@ -916,7 +916,7 @@ public class Loan extends AbstractAuditableCustom<AppUser, Long> {
if (StringUtils.isNotBlank(externalSystemIdentifer)) {
this.externalId = externalSystemIdentifer.trim();
} else {
this.externalId = externalSystemIdentifer;
this.externalId = null;
}
}
@@ -18,6 +18,7 @@ import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.joda.time.LocalDate;
@@ -179,7 +180,8 @@ public class DepositAccount extends AbstractAuditableCustom<AppUser, Long> {
final Integer depositStatus) {
this.client = client;
this.product = product;
this.externalId = externalId;
setExternalId(externalId);
this.currency = deposit.getCurrency();
this.depositAmount = deposit.getAmount();
product.validateDepositInRange(this.depositAmount);
@@ -205,6 +207,14 @@ public class DepositAccount extends AbstractAuditableCustom<AppUser, Long> {
this.depositStatus=depositStatus;
}
private void setExternalId(final String externalId) {
if (StringUtils.isNotBlank(externalId)) {
this.externalId = externalId.trim();
} else {
this.externalId = null;
}
}
public boolean isDeleted() {
return deleted;
}
@@ -19,12 +19,12 @@ import org.joda.time.LocalDate;
import org.mifosng.platform.currency.domain.MonetaryCurrency;
import org.mifosng.platform.currency.domain.Money;
import org.springframework.data.jpa.domain.AbstractPersistable;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name="m_deposit_account_transaction")
public class DepositAccountTransaction extends AbstractPersistable<Long> {
@SuppressWarnings("unused")
@ManyToOne(optional = false)
@JoinColumn(name = "deposit_account_id", nullable=false)
private DepositAccount depositAccount;
@@ -33,6 +33,7 @@ public class DepositAccountTransaction extends AbstractPersistable<Long> {
@Column(name = "transaction_type_enum", nullable = false)
private DepositAccountTransactionType typeOf;
@SuppressWarnings("unused")
@OneToOne(optional=true, cascade={CascadeType.PERSIST})
@JoinColumn(name="contra_id")
private DepositAccountTransaction contra;
@@ -45,20 +46,16 @@ public class DepositAccountTransaction extends AbstractPersistable<Long> {
private final BigDecimal amount;
protected DepositAccountTransaction(){
this.depositAccount=null;
this.typeOf=null;
this.amount=null;
this.dateOf=null;
}
private DepositAccountTransaction(DepositAccountTransactionType type, final BigDecimal amount, final LocalDate date) {
this.typeOf = type;
this.amount = amount;
this.dateOf = date.toDateMidnight().toDate();
}
public Date getDateOf() {
@@ -72,29 +69,17 @@ public class DepositAccountTransaction extends AbstractPersistable<Long> {
public DepositAccountTransactionType getTypeOf() {
return typeOf;
}
public Money getAmount(MonetaryCurrency currency) {
return Money.of(currency, this.amount);
}
@DateTimeFormat(style="-M")
public LocalDate getTransactionDate() {
return new LocalDate(this.dateOf);
}
public DepositAccountTransaction getContra() {
return contra;
}
public boolean isNotContra() {
return this.contra == null;
}
public boolean isDeposit(){
return DepositAccountTransactionType.DEPOSIT.equals(typeOf) && isNotContra();
return DepositAccountTransactionType.DEPOSIT.equals(typeOf);
}
public static DepositAccountTransaction deposit(Money amount, LocalDate paymentDate) {
@@ -105,29 +90,7 @@ public class DepositAccountTransaction extends AbstractPersistable<Long> {
return new DepositAccountTransaction(DepositAccountTransactionType.WITHDRAW, amount.getAmount(), paymentDate);
}
private static DepositAccountTransaction contra(DepositAccountTransaction originalTransaction) {
DepositAccountTransaction contra = new DepositAccountTransaction(DepositAccountTransactionType.REVERSAL, originalTransaction.getAmount().negate(), new LocalDate(originalTransaction.getDateOf()));
contra.updateContra(originalTransaction);
return contra;
}
public void updateContra(DepositAccountTransaction transaction) {
this.contra = transaction;
}
public DepositAccount getDepositAccount() {
return depositAccount;
}
public void setDepositAccount(DepositAccount depositAccount) {
this.depositAccount = depositAccount;
}
public void updateAccount(DepositAccount depositAccount) {
this.depositAccount=depositAccount;
}
}
}
@@ -3,9 +3,7 @@ package org.mifosng.platform.saving.service;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.joda.time.LocalDate;
import org.mifosng.platform.api.data.ClientData;
@@ -54,24 +52,27 @@ public class DepositAccountReadPlatformServiceImpl implements DepositAccountRead
String sql = "select " + mapper.schema() + " where da.is_deleted=0";
Collection<DepositAccountData> depositAccountDatas = this.jdbcTemplate.query(sql,mapper, new Object[]{});
return this.jdbcTemplate.query(sql,mapper, new Object[]{});
DepositAccountTransactionMapper transactionMapper = new DepositAccountTransactionMapper();
String transactionSchema = "Select "+transactionMapper.schema();
Collection<DepositAccountTransactionData> depositAccountTransactionDatas = this.jdbcTemplate.query(transactionSchema, transactionMapper, new Object[]{});
// NOTE: when retrieving a list of deposit accounts, its unlikey we want to much detail so at that point in time,
// not likely that returning what transactions have is useful so removing it.
for(DepositAccountData depositAccountData : depositAccountDatas){
List<DepositAccountTransactionData> myAccountTransactionDatas = new ArrayList<DepositAccountTransactionData>();
for(DepositAccountTransactionData depositAccountTransactionData : depositAccountTransactionDatas){
if(depositAccountData.getId().equals(depositAccountTransactionData.getAccountId())){
myAccountTransactionDatas.add(depositAccountTransactionData);
}
depositAccountData.setTransactions(myAccountTransactionDatas);
}
}
// DepositAccountTransactionMapper transactionMapper = new DepositAccountTransactionMapper();
// String transactionSchema = "Select " + transactionMapper.schema();
// Collection<DepositAccountTransactionData> depositAccountTransactionDatas = this.jdbcTemplate.query(transactionSchema, transactionMapper, new Object[]{});
return depositAccountDatas;
// for (DepositAccountData depositAccountData : depositAccountDatas) {
// List<DepositAccountTransactionData> myAccountTransactionDatas = new ArrayList<DepositAccountTransactionData>();
//
// for (DepositAccountTransactionData depositAccountTransactionData : depositAccountTransactionDatas) {
// if (depositAccountData.getId().equals(depositAccountTransactionData.getAccountId())) {
// myAccountTransactionDatas.add(depositAccountTransactionData);
// }
// depositAccountData.setTransactions(myAccountTransactionDatas);
// }
// }
// return depositAccountDatas;
}
@Override
@@ -79,26 +80,20 @@ public class DepositAccountReadPlatformServiceImpl implements DepositAccountRead
try{
DepositAccountMapper mapper = new DepositAccountMapper();
String sql = "select " + mapper.schema()
+ " where da.id = ? and da.is_deleted=0";
String sql = "select " + mapper.schema() + " where da.id = ? and da.is_deleted=0";
DepositAccountData productData = this.jdbcTemplate.queryForObject(sql, mapper, new Object[] { accountId });
DepositAccountData depositAccountData = this.jdbcTemplate.queryForObject(sql, mapper, new Object[] { accountId });
DepositAccountTransactionMapper transactionMapper = new DepositAccountTransactionMapper();
String transactionSchema = "Select "+transactionMapper.schema() +" where txn.deposit_account_id = ? ";
Collection<DepositAccountTransactionData> depositAccountTransactionDatas = this.jdbcTemplate.query(transactionSchema, transactionMapper, new Object[]{accountId});
String transactionSchema = "select " + transactionMapper.schema() + " where txn.deposit_account_id = ? ";
List<DepositAccountTransactionData> myAccountTransactionDatas = new ArrayList<DepositAccountTransactionData>();
for(DepositAccountTransactionData depositAccountTransactionData : depositAccountTransactionDatas){
if(productData.getId().equals(depositAccountTransactionData.getAccountId()))
myAccountTransactionDatas.add(depositAccountTransactionData);
}
Collection<DepositAccountTransactionData> depositAccountTransactions = this.jdbcTemplate.query(transactionSchema, transactionMapper, new Object[]{accountId});
if (!depositAccountTransactions.isEmpty()) {
DepositPermissionData permissions = null;
depositAccountData = new DepositAccountData(depositAccountData, permissions, depositAccountTransactions);
}
productData.setTransactions(myAccountTransactionDatas);
return productData;
return depositAccountData;
} catch (EmptyResultDataAccessException e) {
throw new LoanProductNotFoundException(accountId);
}