throw exception with details in InteropServiceImpl (FINERACT-860) (#728)
instead of the NoSuchElementException from Optional.get(), we're now throwing an UnsupportedOperationException which includes the details of the missing object. (This kind of detailed exception was already thrown in two existing places in the same code after a null check, but it never reached there, because the Optional.get() failed earlier. Another option would have been to return null for an absent Optional, but this is better.) The other unrelated changes are automatic clean up actions (in Eclipse).
This commit is contained in:
+53
-42
@@ -108,21 +108,19 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
private final SavingsAccountTransactionSummaryWrapper savingsAccountTransactionSummaryWrapper;
|
private final SavingsAccountTransactionSummaryWrapper savingsAccountTransactionSummaryWrapper;
|
||||||
|
|
||||||
private final SavingsAccountDomainService savingsAccountService;
|
private final SavingsAccountDomainService savingsAccountService;
|
||||||
private final PaymentDetailWritePlatformService paymentDetailService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public InteropServiceImpl(PlatformSecurityContext securityContext,
|
public InteropServiceImpl(PlatformSecurityContext securityContext,
|
||||||
InteropDataValidator interopDataValidator,
|
InteropDataValidator interopDataValidator,
|
||||||
SavingsAccountRepository savingsAccountRepository,
|
SavingsAccountRepository savingsAccountRepository,
|
||||||
SavingsAccountTransactionRepository savingsAccountTransactionRepository,
|
SavingsAccountTransactionRepository savingsAccountTransactionRepository,
|
||||||
ApplicationCurrencyRepository applicationCurrencyRepository,
|
ApplicationCurrencyRepository applicationCurrencyRepository,
|
||||||
NoteRepository noteRepository,
|
NoteRepository noteRepository,
|
||||||
PaymentTypeRepository paymentTypeRepository,
|
PaymentTypeRepository paymentTypeRepository,
|
||||||
InteropIdentifierRepository identifierRepository,
|
InteropIdentifierRepository identifierRepository,
|
||||||
SavingsHelper savingsHelper,
|
SavingsHelper savingsHelper,
|
||||||
SavingsAccountTransactionSummaryWrapper savingsAccountTransactionSummaryWrapper,
|
SavingsAccountTransactionSummaryWrapper savingsAccountTransactionSummaryWrapper,
|
||||||
SavingsAccountDomainService savingsAccountService,
|
SavingsAccountDomainService savingsAccountService,
|
||||||
PaymentDetailWritePlatformService paymentDetailWritePlatformService) {
|
PaymentDetailWritePlatformService paymentDetailWritePlatformService) {
|
||||||
this.securityContext = securityContext;
|
this.securityContext = securityContext;
|
||||||
this.dataValidator = interopDataValidator;
|
this.dataValidator = interopDataValidator;
|
||||||
this.savingsAccountRepository = savingsAccountRepository;
|
this.savingsAccountRepository = savingsAccountRepository;
|
||||||
@@ -134,7 +132,6 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
this.savingsHelper = savingsHelper;
|
this.savingsHelper = savingsHelper;
|
||||||
this.savingsAccountTransactionSummaryWrapper = savingsAccountTransactionSummaryWrapper;
|
this.savingsAccountTransactionSummaryWrapper = savingsAccountTransactionSummaryWrapper;
|
||||||
this.savingsAccountService = savingsAccountService;
|
this.savingsAccountService = savingsAccountService;
|
||||||
this.paymentDetailService = paymentDetailWritePlatformService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -153,11 +150,13 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
ZoneId zoneId = ZoneId.of(ThreadLocalContextUtil.getTenant().getTimezoneId());
|
ZoneId zoneId = ZoneId.of(ThreadLocalContextUtil.getTenant().getTimezoneId());
|
||||||
Predicate<SavingsAccountTransaction> transFilter = t -> {
|
Predicate<SavingsAccountTransaction> transFilter = t -> {
|
||||||
SavingsAccountTransactionType transactionType = SavingsAccountTransactionType.fromInt(t.getTypeOf());
|
SavingsAccountTransactionType transactionType = SavingsAccountTransactionType.fromInt(t.getTypeOf());
|
||||||
if (debit != transactionType.isDebit() && credit != transactionType.isCredit())
|
if (debit != transactionType.isDebit() && credit != transactionType.isCredit()) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (transactionsFrom == null && transactionsTo == null)
|
if (transactionsFrom == null && transactionsTo == null) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
java.time.LocalDateTime transactionDate = t.getTransactionLocalDate().toDateTimeAtStartOfDay().toDate().toInstant().atZone(zoneId).toLocalDateTime();
|
java.time.LocalDateTime transactionDate = t.getTransactionLocalDate().toDateTimeAtStartOfDay().toDate().toInstant().atZone(zoneId).toLocalDateTime();
|
||||||
return (transactionsTo == null || transactionsTo.compareTo(transactionDate) > 0)
|
return (transactionsTo == null || transactionsTo.compareTo(transactionDate) > 0)
|
||||||
@@ -175,19 +174,18 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public InteropIdentifierAccountResponseData getAccountByIdentifier(@NotNull InteropIdentifierType idType, @NotNull String idValue, String subIdOrType) {
|
public InteropIdentifierAccountResponseData getAccountByIdentifier(@NotNull InteropIdentifierType idType, @NotNull String idValue, String subIdOrType) {
|
||||||
InteropIdentifier identifier = findIdentifier(idType, idValue, subIdOrType);
|
InteropIdentifier identifier = findIdentifier(idType, idValue, subIdOrType);
|
||||||
if (identifier == null)
|
|
||||||
throw new UnsupportedOperationException("Account not found for identifier " + idType + "/" + idValue + (subIdOrType == null ? "" : ("/" + subIdOrType)));
|
|
||||||
|
|
||||||
return InteropIdentifierAccountResponseData.build(identifier.getAccount().getExternalId());
|
return InteropIdentifierAccountResponseData.build(identifier.getAccount().getExternalId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@Override
|
||||||
@Transactional(propagation = Propagation.MANDATORY)
|
@Transactional(propagation = Propagation.MANDATORY)
|
||||||
public InteropIdentifierAccountResponseData registerAccountIdentifier(@NotNull InteropIdentifierType idType, @NotNull String idValue,
|
public InteropIdentifierAccountResponseData registerAccountIdentifier(@NotNull InteropIdentifierType idType, @NotNull String idValue,
|
||||||
String subIdOrType, @NotNull JsonCommand command) {
|
String subIdOrType, @NotNull JsonCommand command) {
|
||||||
InteropIdentifierRequestData request = dataValidator.validateAndParseCreateIdentifier(idType, idValue, subIdOrType, command);
|
InteropIdentifierRequestData request = dataValidator.validateAndParseCreateIdentifier(idType, idValue, subIdOrType, command);
|
||||||
//TODO: error handling
|
//TODO: error handling
|
||||||
SavingsAccount savingsAccount = validateAndGetSavingAccount(request.getAccountId());
|
SavingsAccount savingsAccount = validateAndGetSavingAccount(request.getAccountId());
|
||||||
@@ -202,13 +200,12 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
return InteropIdentifierAccountResponseData.build(savingsAccount.getExternalId());
|
return InteropIdentifierAccountResponseData.build(savingsAccount.getExternalId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
@Transactional(propagation = Propagation.MANDATORY)
|
@Transactional(propagation = Propagation.MANDATORY)
|
||||||
public InteropIdentifierAccountResponseData deleteAccountIdentifier(@NotNull InteropIdentifierType idType, @NotNull String idValue,
|
public InteropIdentifierAccountResponseData deleteAccountIdentifier(@NotNull InteropIdentifierType idType, @NotNull String idValue,
|
||||||
String subIdOrType) {
|
String subIdOrType) {
|
||||||
InteropIdentifier identifier = findIdentifier(idType, idValue, subIdOrType);
|
InteropIdentifier identifier = findIdentifier(idType, idValue, subIdOrType);
|
||||||
if (identifier == null)
|
|
||||||
throw new UnsupportedOperationException("Account not found for identifier " + idType + "/" + idValue + (subIdOrType == null ? "" : ("/" + subIdOrType)));
|
|
||||||
|
|
||||||
String accountId = identifier.getAccount().getExternalId();
|
String accountId = identifier.getAccount().getExternalId();
|
||||||
|
|
||||||
@@ -230,8 +227,7 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
// only when Payee request transaction from Payer, so here role must be always Payer
|
// only when Payee request transaction from Payer, so here role must be always Payer
|
||||||
InteropTransactionRequestData request = dataValidator.validateAndParseCreateRequest(command);
|
InteropTransactionRequestData request = dataValidator.validateAndParseCreateRequest(command);
|
||||||
|
|
||||||
//TODO: error handling
|
validateAndGetSavingAccount(request);
|
||||||
SavingsAccount savingsAccount = validateAndGetSavingAccount(request);
|
|
||||||
|
|
||||||
return InteropTransactionRequestResponseData.build(command.commandId(), request.getTransactionCode(), InteropActionState.ACCEPTED,
|
return InteropTransactionRequestResponseData.build(command.commandId(), request.getTransactionCode(), InteropActionState.ACCEPTED,
|
||||||
request.getExpiration(), request.getExtensionList(), request.getRequestCode());
|
request.getExpiration(), request.getExtensionList(), request.getRequestCode());
|
||||||
@@ -282,8 +278,9 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
if (MathUtil.isLessThan(savingsAccount.getWithdrawableBalance(), total)) {
|
if (MathUtil.isLessThan(savingsAccount.getWithdrawableBalance(), total)) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
if (findTransaction(savingsAccount, transferCode, SavingsAccountTransactionType.AMOUNT_HOLD) != null)
|
if (findTransaction(savingsAccount, transferCode, SavingsAccountTransactionType.AMOUNT_HOLD) != null) {
|
||||||
throw new UnsupportedOperationException("Transfer amount was already put on hold " + transferCode);
|
throw new UnsupportedOperationException("Transfer amount was already put on hold " + transferCode);
|
||||||
|
}
|
||||||
|
|
||||||
PaymentDetail paymentDetail = PaymentDetail.instance(findPaymentType(), savingsAccount.getExternalId(), null, getRoutingCode(), transferCode, null);
|
PaymentDetail paymentDetail = PaymentDetail.instance(findPaymentType(), savingsAccount.getExternalId(), null, getRoutingCode(), transferCode, null);
|
||||||
AppUser appUser = getLoginUser();
|
AppUser appUser = getLoginUser();
|
||||||
@@ -316,8 +313,9 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
validateTransfer(request, savingsAccount);
|
validateTransfer(request, savingsAccount);
|
||||||
|
|
||||||
String transferCode = request.getTransferCode();
|
String transferCode = request.getTransferCode();
|
||||||
if (findTransaction(savingsAccount, transferCode, debit ? SavingsAccountTransactionType.WITHDRAWAL : SavingsAccountTransactionType.DEPOSIT) != null)
|
if (findTransaction(savingsAccount, transferCode, debit ? SavingsAccountTransactionType.WITHDRAWAL : SavingsAccountTransactionType.DEPOSIT) != null) {
|
||||||
throw new UnsupportedOperationException("Transfer was already committed " + transferCode);
|
throw new UnsupportedOperationException("Transfer was already committed " + transferCode);
|
||||||
|
}
|
||||||
|
|
||||||
PaymentDetail paymentDetail = PaymentDetail.instance(findPaymentType(), savingsAccount.getExternalId(), null, getRoutingCode(), transferCode, null);
|
PaymentDetail paymentDetail = PaymentDetail.instance(findPaymentType(), savingsAccount.getExternalId(), null, getRoutingCode(), transferCode, null);
|
||||||
|
|
||||||
@@ -366,8 +364,9 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
|
|
||||||
private SavingsAccount validateAndGetSavingAccount(String accountId) {
|
private SavingsAccount validateAndGetSavingAccount(String accountId) {
|
||||||
SavingsAccount savingsAccount = savingsAccountRepository.findByExternalId(accountId);
|
SavingsAccount savingsAccount = savingsAccountRepository.findByExternalId(accountId);
|
||||||
if (savingsAccount == null)
|
if (savingsAccount == null) {
|
||||||
throw new SavingsAccountNotFoundException(accountId);
|
throw new SavingsAccountNotFoundException(accountId);
|
||||||
|
}
|
||||||
return savingsAccount;
|
return savingsAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -377,16 +376,19 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
savingsAccount.setHelpers(savingsAccountTransactionSummaryWrapper, savingsHelper);
|
savingsAccount.setHelpers(savingsAccountTransactionSummaryWrapper, savingsHelper);
|
||||||
|
|
||||||
ApplicationCurrency currency = currencyRepository.findOneByCode(request.getAmount().getCurrency());
|
ApplicationCurrency currency = currencyRepository.findOneByCode(request.getAmount().getCurrency());
|
||||||
if (!savingsAccount.getCurrency().getCode().equals(currency.getCode()))
|
if (!savingsAccount.getCurrency().getCode().equals(currency.getCode())) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
SavingsAccountTransactionType transactionType = request.getTransactionRole().getTransactionType();
|
SavingsAccountTransactionType transactionType = request.getTransactionRole().getTransactionType();
|
||||||
if (!savingsAccount.isTransactionAllowed(transactionType, request.getExpirationLocalDate()))
|
if (!savingsAccount.isTransactionAllowed(transactionType, request.getExpirationLocalDate())) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
request.normalizeAmounts(savingsAccount.getCurrency());
|
request.normalizeAmounts(savingsAccount.getCurrency());
|
||||||
if (transactionType.isDebit() && MathUtil.isLessThan(savingsAccount.getWithdrawableBalance(), request.getAmount().getAmount()))
|
if (transactionType.isDebit() && MathUtil.isLessThan(savingsAccount.getWithdrawableBalance(), request.getAmount().getAmount())) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
return savingsAccount;
|
return savingsAccount;
|
||||||
}
|
}
|
||||||
@@ -398,15 +400,17 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
BigDecimal total = transactionType.isDebit() ? amount : MathUtil.negate(amount);
|
BigDecimal total = transactionType.isDebit() ? amount : MathUtil.negate(amount);
|
||||||
MoneyData fspFee = request.getFspFee();
|
MoneyData fspFee = request.getFspFee();
|
||||||
if (fspFee != null) {
|
if (fspFee != null) {
|
||||||
if (!savingsAccount.getCurrency().getCode().equals(fspFee.getCurrency()))
|
if (!savingsAccount.getCurrency().getCode().equals(fspFee.getCurrency())) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
//TODO: compare with calculated quote fee
|
//TODO: compare with calculated quote fee
|
||||||
total = MathUtil.add(total, fspFee.getAmount());
|
total = MathUtil.add(total, fspFee.getAmount());
|
||||||
}
|
}
|
||||||
MoneyData fspCommission = request.getFspCommission();
|
MoneyData fspCommission = request.getFspCommission();
|
||||||
if (fspCommission != null) {
|
if (fspCommission != null) {
|
||||||
if (!savingsAccount.getCurrency().getCode().equals(fspCommission.getCurrency()))
|
if (!savingsAccount.getCurrency().getCode().equals(fspCommission.getCurrency())) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
//TODO: compare with calculated quote commission
|
//TODO: compare with calculated quote commission
|
||||||
total = MathUtil.subtractToZero(total, fspCommission.getAmount());
|
total = MathUtil.subtractToZero(total, fspCommission.getAmount());
|
||||||
}
|
}
|
||||||
@@ -415,11 +419,13 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
|
|
||||||
private DateTimeFormatter getDateTimeFormatter(@NotNull JsonCommand command) {
|
private DateTimeFormatter getDateTimeFormatter(@NotNull JsonCommand command) {
|
||||||
Locale locale = command.extractLocale();
|
Locale locale = command.extractLocale();
|
||||||
if (locale == null)
|
if (locale == null) {
|
||||||
locale = DEFAULT_LOCALE;
|
locale = DEFAULT_LOCALE;
|
||||||
|
}
|
||||||
String dateFormat = command.dateFormat();
|
String dateFormat = command.dateFormat();
|
||||||
if (StringUtils.isEmpty(dateFormat))
|
if (StringUtils.isEmpty(dateFormat)) {
|
||||||
dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
|
dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
|
||||||
|
}
|
||||||
|
|
||||||
return DateTimeFormat.forPattern(dateFormat).withLocale(locale);
|
return DateTimeFormat.forPattern(dateFormat).withLocale(locale);
|
||||||
}
|
}
|
||||||
@@ -427,11 +433,12 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
PaymentType findPaymentType() {
|
PaymentType findPaymentType() {
|
||||||
List<PaymentType> paymentTypes = paymentTypeRepository.findAll();
|
List<PaymentType> paymentTypes = paymentTypeRepository.findAll();
|
||||||
for (PaymentType paymentType : paymentTypes) {
|
for (PaymentType paymentType : paymentTypes) {
|
||||||
if (!paymentType.isCashPayment())
|
if (!paymentType.isCashPayment()) {
|
||||||
return paymentType;
|
return paymentType;
|
||||||
//TODO: for now first not cash is retured:
|
//TODO: for now first not cash is retured:
|
||||||
// 1. must be added as initial setup,
|
// 1. must be added as initial setup,
|
||||||
// 2. if more than one non-cashe type added then update this code
|
// 2. if more than one non-cashe type added then update this code
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -439,18 +446,22 @@ public class InteropServiceImpl implements InteropService {
|
|||||||
SavingsAccountTransaction findTransaction(@NotNull SavingsAccount savingsAccount, @NotNull String transactionCode, SavingsAccountTransactionType transactionType) {
|
SavingsAccountTransaction findTransaction(@NotNull SavingsAccount savingsAccount, @NotNull String transactionCode, SavingsAccountTransactionType transactionType) {
|
||||||
String routingCode = getRoutingCode();
|
String routingCode = getRoutingCode();
|
||||||
for (SavingsAccountTransaction transaction : savingsAccount.getTransactions()) {
|
for (SavingsAccountTransaction transaction : savingsAccount.getTransactions()) {
|
||||||
if (transactionType != null && !transactionType.getValue().equals(transaction.getTypeOf()))
|
if (transactionType != null && !transactionType.getValue().equals(transaction.getTypeOf())) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
PaymentDetail detail = transaction.getPaymentDetail();
|
PaymentDetail detail = transaction.getPaymentDetail();
|
||||||
if (detail != null && routingCode.equals(detail.getRoutingCode()) && transactionCode.equals(detail.getReceiptNumber()))
|
if (detail != null && routingCode.equals(detail.getRoutingCode()) && transactionCode.equals(detail.getReceiptNumber())) {
|
||||||
return transaction;
|
return transaction;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InteropIdentifier findIdentifier(@NotNull InteropIdentifierType idType, @NotNull String idValue, String subIdOrType) {
|
public InteropIdentifier findIdentifier(@NotNull InteropIdentifierType idType, @NotNull String idValue, String subIdOrType) {
|
||||||
return identifierRepository.findOne(where(idTypeEqual(idType)).and(idValueEqual(idValue)).and(subIdOrTypeEqual(subIdOrType))).get();
|
return identifierRepository.findOne(where(idTypeEqual(idType)).and(idValueEqual(idValue)).and(subIdOrTypeEqual(subIdOrType)))
|
||||||
|
.orElseThrow(() -> new UnsupportedOperationException(
|
||||||
|
"Account not found for identifier " + idType + "/" + idValue + (subIdOrType == null ? "" : ("/" + subIdOrType))));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Specification<InteropIdentifier> idTypeEqual(@NotNull InteropIdentifierType idType) {
|
public static Specification<InteropIdentifier> idTypeEqual(@NotNull InteropIdentifierType idType) {
|
||||||
|
|||||||
Reference in New Issue
Block a user