Example usage for java.math MathContext DECIMAL64

List of usage examples for java.math MathContext DECIMAL64

Introduction

In this page you can find the example usage for java.math MathContext DECIMAL64.

Prototype

MathContext DECIMAL64

To view the source code for java.math MathContext DECIMAL64.

Click Source Link

Document

A MathContext object with a precision setting matching the IEEE 754R Decimal64 format, 16 digits, and a rounding mode of RoundingMode#HALF_EVEN HALF_EVEN , the IEEE 754R default.

Usage

From source file:org.apache.fineract.portfolio.savings.service.DepositAccountWritePlatformServiceJpaRepositoryImpl.java

@Transactional
@Override/*from   w  ww . j a  v a2s.c  o  m*/
public CommandProcessingResult activateRDAccount(final Long savingsId, final JsonCommand command) {
    boolean isRegularTransaction = false;

    final AppUser user = this.context.authenticatedUser();

    final boolean isSavingsInterestPostingAtCurrentPeriodEnd = this.configurationDomainService
            .isSavingsInterestPostingAtCurrentPeriodEnd();
    final Integer financialYearBeginningMonth = this.configurationDomainService
            .retrieveFinancialYearBeginningMonth();

    this.depositAccountTransactionDataValidator.validateActivation(command);

    final RecurringDepositAccount account = (RecurringDepositAccount) this.depositAccountAssembler
            .assembleFrom(savingsId, DepositAccountType.RECURRING_DEPOSIT);
    checkClientOrGroupActive(account);

    final Set<Long> existingTransactionIds = new HashSet<>();
    final Set<Long> existingReversedTransactionIds = new HashSet<>();
    updateExistingTransactionsDetails(account, existingTransactionIds, existingReversedTransactionIds);

    final Map<String, Object> changes = account.activate(user, command, DateUtils.getLocalDateOfTenant());

    if (!changes.isEmpty()) {
        final Locale locale = command.extractLocale();
        final DateTimeFormatter fmt = DateTimeFormat.forPattern(command.dateFormat()).withLocale(locale);
        Money amountForDeposit = account.activateWithBalance();
        if (amountForDeposit.isGreaterThanZero()) {
            final PortfolioAccountData portfolioAccountData = this.accountAssociationsReadPlatformService
                    .retriveSavingsLinkedAssociation(savingsId);
            if (portfolioAccountData == null) {
                this.depositAccountDomainService.handleRDDeposit(account, fmt, account.getActivationLocalDate(),
                        amountForDeposit.getAmount(), null, isRegularTransaction);
            } else {
                final boolean isExceptionForBalanceCheck = false;
                final SavingsAccount fromSavingsAccount = null;
                final AccountTransferDTO accountTransferDTO = new AccountTransferDTO(
                        account.getActivationLocalDate(), amountForDeposit.getAmount(),
                        PortfolioAccountType.SAVINGS, PortfolioAccountType.SAVINGS,
                        portfolioAccountData.accountId(), account.getId(), "Account Transfer", locale, fmt,
                        null, null, null, null, null, AccountTransferType.ACCOUNT_TRANSFER.getValue(), null,
                        null, null, null, account, fromSavingsAccount, isRegularTransaction,
                        isExceptionForBalanceCheck);
                this.accountTransfersWritePlatformService.transferFunds(accountTransferDTO);
            }
            updateExistingTransactionsDetails(account, existingTransactionIds, existingReversedTransactionIds);
        }

        final MathContext mc = MathContext.DECIMAL64;

        // submitted and activation date are different then recalculate
        // maturity date and schedule
        if (!account.accountSubmittedAndActivationOnSameDate()) {
            final boolean isPreMatureClosure = false;
            final CalendarInstance calendarInstance = this.calendarInstanceRepository
                    .findByEntityIdAndEntityTypeIdAndCalendarTypeId(savingsId,
                            CalendarEntityType.SAVINGS.getValue(), CalendarType.COLLECTION.getValue());

            final Calendar calendar = calendarInstance.getCalendar();
            final PeriodFrequencyType frequencyType = CalendarFrequencyType
                    .from(CalendarUtils.getFrequency(calendar.getRecurrence()));
            Integer frequency = CalendarUtils.getInterval(calendar.getRecurrence());
            frequency = frequency == -1 ? 1 : frequency;
            account.generateSchedule(frequencyType, frequency, calendar);
            account.updateMaturityDateAndAmount(mc, isPreMatureClosure,
                    isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth);
        }

        final LocalDate overdueUptoDate = DateUtils.getLocalDateOfTenant();
        account.updateOverduePayments(overdueUptoDate);
        final boolean isInterestTransfer = false;
        if (account.isBeforeLastPostingPeriod(account.getActivationLocalDate())) {
            final LocalDate today = DateUtils.getLocalDateOfTenant();
            account.postInterest(mc, today, isInterestTransfer, isSavingsInterestPostingAtCurrentPeriodEnd,
                    financialYearBeginningMonth);
        } else {
            final LocalDate today = DateUtils.getLocalDateOfTenant();
            account.calculateInterestUsing(mc, today, isInterestTransfer,
                    isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth);
        }
        List<DepositAccountOnHoldTransaction> depositAccountOnHoldTransactions = null;
        if (account.getOnHoldFunds().compareTo(BigDecimal.ZERO) == 1) {
            depositAccountOnHoldTransactions = this.depositAccountOnHoldTransactionRepository
                    .findBySavingsAccountAndReversedFalseOrderByCreatedDateAsc(account);
        }

        account.validateAccountBalanceDoesNotBecomeNegative(SavingsAccountTransactionType.PAY_CHARGE.name(),
                depositAccountOnHoldTransactions);

        this.savingAccountRepository.save(account);
    }

    postJournalEntries(account, existingTransactionIds, existingReversedTransactionIds);

    return new CommandProcessingResultBuilder() //
            .withEntityId(savingsId) //
            .withOfficeId(account.officeId()) //
            .withClientId(account.clientId()) //
            .withGroupId(account.groupId()) //
            .withSavingsId(savingsId) //
            .with(changes) //
            .build();
}

From source file:org.apache.fineract.portfolio.savings.service.DepositAccountWritePlatformServiceJpaRepositoryImpl.java

@Override
public CommandProcessingResult undoRDTransaction(final Long savingsId, final Long transactionId,
        final boolean allowAccountTransferModification) {

    final boolean isSavingsInterestPostingAtCurrentPeriodEnd = this.configurationDomainService
            .isSavingsInterestPostingAtCurrentPeriodEnd();
    final Integer financialYearBeginningMonth = this.configurationDomainService
            .retrieveFinancialYearBeginningMonth();

    final RecurringDepositAccount account = (RecurringDepositAccount) this.depositAccountAssembler
            .assembleFrom(savingsId, DepositAccountType.RECURRING_DEPOSIT);
    final Set<Long> existingTransactionIds = new HashSet<>();
    final Set<Long> existingReversedTransactionIds = new HashSet<>();
    updateExistingTransactionsDetails(account, existingTransactionIds, existingReversedTransactionIds);

    final SavingsAccountTransaction savingsAccountTransaction = this.savingsAccountTransactionRepository
            .findOneByIdAndSavingsAccountId(transactionId, savingsId);
    if (savingsAccountTransaction == null) {
        throw new SavingsAccountTransactionNotFoundException(savingsId, transactionId);
    }/*  ww w .  ja va  2 s  .c om*/

    if (!allowAccountTransferModification && this.accountTransfersReadPlatformService
            .isAccountTransfer(transactionId, PortfolioAccountType.SAVINGS)) {
        throw new PlatformServiceUnavailableException(
                "error.msg.recurring.deposit.account.transfer.transaction.update.not.allowed",
                "Recurring deposit account transaction:" + transactionId
                        + " update not allowed as it involves in account transfer",
                transactionId);
    }

    final LocalDate today = DateUtils.getLocalDateOfTenant();
    final MathContext mc = MathContext.DECIMAL64;

    if (account.isNotActive()) {
        throwValidationForActiveStatus(SavingsApiConstants.undoTransactionAction);
    }
    account.undoTransaction(transactionId);
    boolean isInterestTransfer = false;
    checkClientOrGroupActive(account);
    if (savingsAccountTransaction.isPostInterestCalculationRequired()
            && account.isBeforeLastPostingPeriod(savingsAccountTransaction.transactionLocalDate())) {
        account.postInterest(mc, today, isInterestTransfer, isSavingsInterestPostingAtCurrentPeriodEnd,
                financialYearBeginningMonth);
    } else {
        account.calculateInterestUsing(mc, today, isInterestTransfer,
                isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth);
    }
    List<DepositAccountOnHoldTransaction> depositAccountOnHoldTransactions = null;
    if (account.getOnHoldFunds().compareTo(BigDecimal.ZERO) == 1) {
        depositAccountOnHoldTransactions = this.depositAccountOnHoldTransactionRepository
                .findBySavingsAccountAndReversedFalseOrderByCreatedDateAsc(account);
    }

    account.validateAccountBalanceDoesNotBecomeNegative(SavingsApiConstants.undoTransactionAction,
            depositAccountOnHoldTransactions);
    // account.activateAccountBasedOnBalance();
    final boolean isPreMatureClosure = false;
    account.updateMaturityDateAndAmount(mc, isPreMatureClosure, isSavingsInterestPostingAtCurrentPeriodEnd,
            financialYearBeginningMonth);

    final LocalDate overdueUptoDate = DateUtils.getLocalDateOfTenant();

    if (savingsAccountTransaction.isDeposit()) {
        account.updateScheduleInstallments();
    }

    account.updateOverduePayments(overdueUptoDate);

    postJournalEntries(account, existingTransactionIds, existingReversedTransactionIds);

    return new CommandProcessingResultBuilder() //
            .withEntityId(savingsId) //
            .withOfficeId(account.officeId()) //
            .withClientId(account.clientId()) //
            .withGroupId(account.groupId()) //
            .withSavingsId(savingsId) //
            .build();
}

From source file:org.apache.fineract.portfolio.savings.service.DepositAccountWritePlatformServiceJpaRepositoryImpl.java

@Override
public SavingsAccountTransaction initiateSavingsTransfer(final Long accountId, final LocalDate transferDate,
        final DepositAccountType depositAccountType) {

    AppUser user = getAppUserIfPresent();
    final boolean isSavingsInterestPostingAtCurrentPeriodEnd = this.configurationDomainService
            .isSavingsInterestPostingAtCurrentPeriodEnd();
    final Integer financialYearBeginningMonth = this.configurationDomainService
            .retrieveFinancialYearBeginningMonth();

    final SavingsAccount savingsAccount = this.depositAccountAssembler.assembleFrom(accountId,
            depositAccountType);/*from  w w  w.  j  a  v  a2  s .  c om*/

    final Set<Long> existingTransactionIds = new HashSet<>();
    final Set<Long> existingReversedTransactionIds = new HashSet<>();
    updateExistingTransactionsDetails(savingsAccount, existingTransactionIds, existingReversedTransactionIds);

    final SavingsAccountTransaction newTransferTransaction = SavingsAccountTransaction
            .initiateTransfer(savingsAccount, savingsAccount.office(), transferDate, user);
    savingsAccount.getTransactions().add(newTransferTransaction);
    savingsAccount.setStatus(SavingsAccountStatusType.TRANSFER_IN_PROGRESS.getValue());
    final MathContext mc = MathContext.DECIMAL64;
    boolean isInterestTransfer = false;
    savingsAccount.calculateInterestUsing(mc, transferDate, isInterestTransfer,
            isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth);

    this.savingsAccountTransactionRepository.save(newTransferTransaction);
    this.savingAccountRepository.save(savingsAccount);

    postJournalEntries(savingsAccount, existingTransactionIds, existingReversedTransactionIds);

    return newTransferTransaction;
}

From source file:org.apache.fineract.portfolio.savings.service.DepositAccountWritePlatformServiceJpaRepositoryImpl.java

@Override
public SavingsAccountTransaction withdrawSavingsTransfer(final Long accountId, final LocalDate transferDate,
        final DepositAccountType depositAccountType) {

    AppUser user = getAppUserIfPresent();

    final boolean isSavingsInterestPostingAtCurrentPeriodEnd = this.configurationDomainService
            .isSavingsInterestPostingAtCurrentPeriodEnd();
    final Integer financialYearBeginningMonth = this.configurationDomainService
            .retrieveFinancialYearBeginningMonth();

    final SavingsAccount savingsAccount = this.depositAccountAssembler.assembleFrom(accountId,
            depositAccountType);//from  w  ww  .j  av  a2 s  .c o  m

    final Set<Long> existingTransactionIds = new HashSet<>();
    final Set<Long> existingReversedTransactionIds = new HashSet<>();
    updateExistingTransactionsDetails(savingsAccount, existingTransactionIds, existingReversedTransactionIds);

    final SavingsAccountTransaction withdrawtransferTransaction = SavingsAccountTransaction
            .withdrawTransfer(savingsAccount, savingsAccount.office(), transferDate, user);
    savingsAccount.getTransactions().add(withdrawtransferTransaction);
    savingsAccount.setStatus(SavingsAccountStatusType.ACTIVE.getValue());
    final MathContext mc = MathContext.DECIMAL64;
    boolean isInterestTransfer = false;
    savingsAccount.calculateInterestUsing(mc, transferDate, isInterestTransfer,
            isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth);

    this.savingsAccountTransactionRepository.save(withdrawtransferTransaction);
    this.savingAccountRepository.save(savingsAccount);

    postJournalEntries(savingsAccount, existingTransactionIds, existingReversedTransactionIds);

    return withdrawtransferTransaction;
}

From source file:org.apache.fineract.portfolio.savings.service.DepositAccountWritePlatformServiceJpaRepositoryImpl.java

@Override
public SavingsAccountTransaction acceptSavingsTransfer(final Long accountId, final LocalDate transferDate,
        final Office acceptedInOffice, final Staff fieldOfficer, final DepositAccountType depositAccountType) {

    AppUser user = getAppUserIfPresent();

    final boolean isSavingsInterestPostingAtCurrentPeriodEnd = this.configurationDomainService
            .isSavingsInterestPostingAtCurrentPeriodEnd();
    final Integer financialYearBeginningMonth = this.configurationDomainService
            .retrieveFinancialYearBeginningMonth();

    final SavingsAccount savingsAccount = this.depositAccountAssembler.assembleFrom(accountId,
            depositAccountType);/*from  ww  w .  ja  v a2s  .  co m*/

    final Set<Long> existingTransactionIds = new HashSet<>();
    final Set<Long> existingReversedTransactionIds = new HashSet<>();
    updateExistingTransactionsDetails(savingsAccount, existingTransactionIds, existingReversedTransactionIds);

    final SavingsAccountTransaction acceptTransferTransaction = SavingsAccountTransaction
            .approveTransfer(savingsAccount, acceptedInOffice, transferDate, user);
    savingsAccount.getTransactions().add(acceptTransferTransaction);
    savingsAccount.setStatus(SavingsAccountStatusType.ACTIVE.getValue());
    if (fieldOfficer != null) {
        savingsAccount.reassignSavingsOfficer(fieldOfficer, transferDate);
    }
    boolean isInterestTransfer = false;
    final MathContext mc = MathContext.DECIMAL64;
    savingsAccount.calculateInterestUsing(mc, transferDate, isInterestTransfer,
            isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth);

    this.savingsAccountTransactionRepository.save(acceptTransferTransaction);
    this.savingAccountRepository.save(savingsAccount);

    postJournalEntries(savingsAccount, existingTransactionIds, existingReversedTransactionIds);

    return acceptTransferTransaction;
}

From source file:org.apache.fineract.portfolio.savings.service.DepositAccountWritePlatformServiceJpaRepositoryImpl.java

@Transactional
@Override/*from  w  w w.  j a v a  2s.  com*/
public CommandProcessingResult waiveCharge(final Long savingsAccountId, final Long savingsAccountChargeId,
        @SuppressWarnings("unused") final DepositAccountType depositAccountType) {

    AppUser user = getAppUserIfPresent();

    final boolean isSavingsInterestPostingAtCurrentPeriodEnd = this.configurationDomainService
            .isSavingsInterestPostingAtCurrentPeriodEnd();
    final Integer financialYearBeginningMonth = this.configurationDomainService
            .retrieveFinancialYearBeginningMonth();

    final SavingsAccountCharge savingsAccountCharge = this.savingsAccountChargeRepository
            .findOneWithNotFoundDetection(savingsAccountChargeId, savingsAccountId);

    // Get Savings account from savings charge
    final SavingsAccount account = savingsAccountCharge.savingsAccount();
    this.depositAccountAssembler.assignSavingAccountHelpers(account);

    final Set<Long> existingTransactionIds = new HashSet<>();
    final Set<Long> existingReversedTransactionIds = new HashSet<>();
    updateExistingTransactionsDetails(account, existingTransactionIds, existingReversedTransactionIds);

    account.waiveCharge(savingsAccountChargeId, user);
    boolean isInterestTransfer = false;
    final MathContext mc = MathContext.DECIMAL64;
    if (account.isBeforeLastPostingPeriod(savingsAccountCharge.getDueLocalDate())) {
        final LocalDate today = DateUtils.getLocalDateOfTenant();
        account.postInterest(mc, today, isInterestTransfer, isSavingsInterestPostingAtCurrentPeriodEnd,
                financialYearBeginningMonth);
    } else {
        final LocalDate today = DateUtils.getLocalDateOfTenant();
        account.calculateInterestUsing(mc, today, isInterestTransfer,
                isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth);
    }
    List<DepositAccountOnHoldTransaction> depositAccountOnHoldTransactions = null;
    if (account.getOnHoldFunds().compareTo(BigDecimal.ZERO) == 1) {
        depositAccountOnHoldTransactions = this.depositAccountOnHoldTransactionRepository
                .findBySavingsAccountAndReversedFalseOrderByCreatedDateAsc(account);
    }

    account.validateAccountBalanceDoesNotBecomeNegative(SavingsApiConstants.waiveChargeTransactionAction,
            depositAccountOnHoldTransactions);

    this.savingAccountRepository.saveAndFlush(account);

    postJournalEntries(account, existingTransactionIds, existingReversedTransactionIds);

    return new CommandProcessingResultBuilder() //
            .withEntityId(savingsAccountChargeId) //
            .withOfficeId(account.officeId()) //
            .withClientId(account.clientId()) //
            .withGroupId(account.groupId()) //
            .withSavingsId(savingsAccountId) //
            .build();
}

From source file:org.apache.fineract.portfolio.savings.service.DepositAccountWritePlatformServiceJpaRepositoryImpl.java

@Transactional
private void payCharge(final SavingsAccountCharge savingsAccountCharge, final LocalDate transactionDate,
        final BigDecimal amountPaid, final DateTimeFormatter formatter) {

    AppUser user = getAppUserIfPresent();

    final boolean isSavingsInterestPostingAtCurrentPeriodEnd = this.configurationDomainService
            .isSavingsInterestPostingAtCurrentPeriodEnd();
    final Integer financialYearBeginningMonth = this.configurationDomainService
            .retrieveFinancialYearBeginningMonth();

    // Get Savings account from savings charge
    final SavingsAccount account = savingsAccountCharge.savingsAccount();
    this.depositAccountAssembler.assignSavingAccountHelpers(account);
    final Set<Long> existingTransactionIds = new HashSet<>();
    final Set<Long> existingReversedTransactionIds = new HashSet<>();
    updateExistingTransactionsDetails(account, existingTransactionIds, existingReversedTransactionIds);
    account.payCharge(savingsAccountCharge, amountPaid, transactionDate, formatter, user);
    boolean isInterestTransfer = false;
    final MathContext mc = MathContext.DECIMAL64;
    if (account.isBeforeLastPostingPeriod(transactionDate)) {
        final LocalDate today = DateUtils.getLocalDateOfTenant();
        account.postInterest(mc, today, isInterestTransfer, isSavingsInterestPostingAtCurrentPeriodEnd,
                financialYearBeginningMonth);
    } else {//  w w w . j a v  a 2s.  c o  m
        final LocalDate today = DateUtils.getLocalDateOfTenant();
        account.calculateInterestUsing(mc, today, isInterestTransfer,
                isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth);
    }
    List<DepositAccountOnHoldTransaction> depositAccountOnHoldTransactions = null;
    if (account.getOnHoldFunds().compareTo(BigDecimal.ZERO) == 1) {
        depositAccountOnHoldTransactions = this.depositAccountOnHoldTransactionRepository
                .findBySavingsAccountAndReversedFalseOrderByCreatedDateAsc(account);
    }

    account.validateAccountBalanceDoesNotBecomeNegative(
            "." + SavingsAccountTransactionType.PAY_CHARGE.getCode(), depositAccountOnHoldTransactions);

    this.savingAccountRepository.save(account);

    postJournalEntries(account, existingTransactionIds, existingReversedTransactionIds);
}

From source file:org.apache.fineract.portfolio.savings.service.DepositApplicationProcessWritePlatformServiceJpaRepositoryImpl.java

@Transactional
@Override// w ww . j  av  a  2 s  .  com
public CommandProcessingResult submitFDApplication(final JsonCommand command) {
    try {
        this.depositAccountDataValidator.validateFixedDepositForSubmit(command.json());
        final AppUser submittedBy = this.context.authenticatedUser();

        final boolean isSavingsInterestPostingAtCurrentPeriodEnd = this.configurationDomainService
                .isSavingsInterestPostingAtCurrentPeriodEnd();
        final Integer financialYearBeginningMonth = this.configurationDomainService
                .retrieveFinancialYearBeginningMonth();

        final FixedDepositAccount account = (FixedDepositAccount) this.depositAccountAssembler
                .assembleFrom(command, submittedBy, DepositAccountType.FIXED_DEPOSIT);

        final MathContext mc = MathContext.DECIMAL64;
        final boolean isPreMatureClosure = false;

        account.updateMaturityDateAndAmountBeforeAccountActivation(mc, isPreMatureClosure,
                isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth);
        this.fixedDepositAccountRepository.save(account);

        if (account.isAccountNumberRequiresAutoGeneration()) {
            AccountNumberFormat accountNumberFormat = this.accountNumberFormatRepository
                    .findByAccountType(EntityAccountType.CLIENT);
            account.updateAccountNo(this.accountNumberGenerator.generate(account, accountNumberFormat));

            this.savingAccountRepository.save(account);
        }

        // Save linked account information
        final Long savingsAccountId = command
                .longValueOfParameterNamed(DepositsApiConstants.linkedAccountParamName);
        if (savingsAccountId != null) {
            final SavingsAccount savingsAccount = this.depositAccountAssembler.assembleFrom(savingsAccountId,
                    DepositAccountType.SAVINGS_DEPOSIT);
            this.depositAccountDataValidator.validatelinkedSavingsAccount(savingsAccount, account);
            boolean isActive = true;
            final AccountAssociations accountAssociations = AccountAssociations.associateSavingsAccount(account,
                    savingsAccount, AccountAssociationType.LINKED_ACCOUNT_ASSOCIATION.getValue(), isActive);
            this.accountAssociationsRepository.save(accountAssociations);
        }

        final Long savingsId = account.getId();

        return new CommandProcessingResultBuilder() //
                .withCommandId(command.commandId()) //
                .withEntityId(savingsId) //
                .withOfficeId(account.officeId()) //
                .withClientId(account.clientId()) //
                .withGroupId(account.groupId()) //
                .withSavingsId(savingsId) //
                .build();
    } catch (final DataAccessException dve) {
        handleDataIntegrityIssues(command, dve);
        return CommandProcessingResult.empty();
    }
}

From source file:org.apache.fineract.portfolio.savings.service.DepositApplicationProcessWritePlatformServiceJpaRepositoryImpl.java

@Transactional
@Override//from w  ww . ja  va 2s.  c  o m
public CommandProcessingResult submitRDApplication(final JsonCommand command) {
    try {
        this.depositAccountDataValidator.validateRecurringDepositForSubmit(command.json());
        final AppUser submittedBy = this.context.authenticatedUser();

        final boolean isSavingsInterestPostingAtCurrentPeriodEnd = this.configurationDomainService
                .isSavingsInterestPostingAtCurrentPeriodEnd();
        final Integer financialYearBeginningMonth = this.configurationDomainService
                .retrieveFinancialYearBeginningMonth();

        final RecurringDepositAccount account = (RecurringDepositAccount) this.depositAccountAssembler
                .assembleFrom(command, submittedBy, DepositAccountType.RECURRING_DEPOSIT);

        this.recurringDepositAccountRepository.save(account);

        if (account.isAccountNumberRequiresAutoGeneration()) {
            final AccountNumberFormat accountNumberFormat = this.accountNumberFormatRepository
                    .findByAccountType(EntityAccountType.SAVINGS);
            account.updateAccountNo(this.accountNumberGenerator.generate(account, accountNumberFormat));
        }

        final Long savingsId = account.getId();
        final CalendarInstance calendarInstance = getCalendarInstance(command, account);
        this.calendarInstanceRepository.save(calendarInstance);

        // FIXME: Avoid save separately (Calendar instance requires account
        // details)
        final MathContext mc = MathContext.DECIMAL64;
        final Calendar calendar = calendarInstance.getCalendar();
        final PeriodFrequencyType frequencyType = CalendarFrequencyType
                .from(CalendarUtils.getFrequency(calendar.getRecurrence()));
        Integer frequency = CalendarUtils.getInterval(calendar.getRecurrence());
        frequency = frequency == -1 ? 1 : frequency;
        account.generateSchedule(frequencyType, frequency, calendar);
        final boolean isPreMatureClosure = false;
        account.updateMaturityDateAndAmount(mc, isPreMatureClosure, isSavingsInterestPostingAtCurrentPeriodEnd,
                financialYearBeginningMonth);
        account.validateApplicableInterestRate();
        this.savingAccountRepository.save(account);

        return new CommandProcessingResultBuilder() //
                .withCommandId(command.commandId()) //
                .withEntityId(savingsId) //
                .withOfficeId(account.officeId()) //
                .withClientId(account.clientId()) //
                .withGroupId(account.groupId()) //
                .withSavingsId(savingsId) //
                .build();
    } catch (final DataAccessException dve) {
        handleDataIntegrityIssues(command, dve);
        return CommandProcessingResult.empty();
    }
}

From source file:org.apache.fineract.portfolio.savings.service.DepositApplicationProcessWritePlatformServiceJpaRepositoryImpl.java

@Transactional
@Override//from www . j  a  v  a 2 s . c  o  m
public CommandProcessingResult modifyFDApplication(final Long accountId, final JsonCommand command) {
    try {
        this.depositAccountDataValidator.validateFixedDepositForUpdate(command.json());

        final boolean isSavingsInterestPostingAtCurrentPeriodEnd = this.configurationDomainService
                .isSavingsInterestPostingAtCurrentPeriodEnd();
        final Integer financialYearBeginningMonth = this.configurationDomainService
                .retrieveFinancialYearBeginningMonth();

        final Map<String, Object> changes = new LinkedHashMap<>(20);

        final FixedDepositAccount account = (FixedDepositAccount) this.depositAccountAssembler
                .assembleFrom(accountId, DepositAccountType.FIXED_DEPOSIT);
        checkClientOrGroupActive(account);
        account.modifyApplication(command, changes);
        account.validateNewApplicationState(DateUtils.getLocalDateOfTenant(),
                DepositAccountType.FIXED_DEPOSIT.resourceName());

        if (!changes.isEmpty()) {
            updateFDAndRDCommonChanges(changes, command, account);
            final MathContext mc = MathContext.DECIMAL64;
            final boolean isPreMatureClosure = false;
            account.updateMaturityDateAndAmountBeforeAccountActivation(mc, isPreMatureClosure,
                    isSavingsInterestPostingAtCurrentPeriodEnd, financialYearBeginningMonth);
            this.savingAccountRepository.save(account);
        }

        boolean isLinkedAccRequired = command
                .booleanPrimitiveValueOfParameterNamed(transferInterestToSavingsParamName);

        // Save linked account information
        final Long savingsAccountId = command
                .longValueOfParameterNamed(DepositsApiConstants.linkedAccountParamName);
        AccountAssociations accountAssociations = this.accountAssociationsRepository.findBySavingsIdAndType(
                accountId, AccountAssociationType.LINKED_ACCOUNT_ASSOCIATION.getValue());
        if (savingsAccountId == null) {
            if (accountAssociations != null) {
                if (this.fromJsonHelper.parameterExists(DepositsApiConstants.linkedAccountParamName,
                        command.parsedJson())) {
                    this.accountAssociationsRepository.delete(accountAssociations);
                    changes.put(DepositsApiConstants.linkedAccountParamName, null);
                    if (isLinkedAccRequired) {
                        this.depositAccountDataValidator.throwLinkedAccountRequiredError();
                    }
                }
            } else if (isLinkedAccRequired) {
                this.depositAccountDataValidator.throwLinkedAccountRequiredError();
            }
        } else {
            boolean isModified = false;
            if (accountAssociations == null) {
                isModified = true;
            } else {
                final SavingsAccount savingsAccount = accountAssociations.linkedSavingsAccount();
                if (savingsAccount == null || savingsAccount.getId() != savingsAccountId) {
                    isModified = true;
                }
            }
            if (isModified) {
                final SavingsAccount savingsAccount = this.depositAccountAssembler
                        .assembleFrom(savingsAccountId, DepositAccountType.SAVINGS_DEPOSIT);
                this.depositAccountDataValidator.validatelinkedSavingsAccount(savingsAccount, account);
                if (accountAssociations == null) {
                    boolean isActive = true;
                    accountAssociations = AccountAssociations.associateSavingsAccount(account, savingsAccount,
                            AccountAssociationType.LINKED_ACCOUNT_ASSOCIATION.getValue(), isActive);
                } else {
                    accountAssociations.updateLinkedSavingsAccount(savingsAccount);
                }
                changes.put(DepositsApiConstants.linkedAccountParamName, savingsAccountId);
                this.accountAssociationsRepository.save(accountAssociations);
            }
        }

        return new CommandProcessingResultBuilder() //
                .withCommandId(command.commandId()) //
                .withEntityId(accountId) //
                .withOfficeId(account.officeId()) //
                .withClientId(account.clientId()) //
                .withGroupId(account.groupId()) //
                .withSavingsId(accountId) //
                .with(changes) //
                .build();
    } catch (final DataAccessException dve) {
        handleDataIntegrityIssues(command, dve);
        return new CommandProcessingResult(Long.valueOf(-1));
    }
}