@Transactional
  @Override
  public EntityIdentifier waiveLoanCharge(final LoanChargeCommand command) {

    this.context.authenticatedUser();

    // LoanChargeCommandValidator validator = new
    // LoanChargeCommandValidator(command);
    // validator.validateForUpdate();

    final Long loanId = command.getLoanId();
    final Loan loan = retrieveLoanBy(loanId);

    final Long loanChargeId = command.getId();
    final LoanCharge loanCharge = retrieveLoanChargeBy(loanId, loanChargeId);

    final LoanTransaction waiveTransaction =
        loan.waiveLoanCharge(loanCharge, defaultLoanLifecycleStateMachine());

    this.loanTransactionRepository.save(waiveTransaction);
    this.loanRepository.save(loan);

    final String noteText = ""; // command.getNote();
    if (StringUtils.isNotBlank(noteText)) {
      final Note note = Note.loanTransactionNote(loan, waiveTransaction, noteText);
      this.noteRepository.save(note);
    }

    return new EntityIdentifier(loanCharge.getId());
  }
  @Transactional
  @Override
  public EntityIdentifier addLoanCharge(final LoanChargeCommand command) {
    this.context.authenticatedUser();

    LoanChargeCommandValidator validator = new LoanChargeCommandValidator(command);
    validator.validateForCreate();

    final Loan loan = this.loanRepository.findOne(command.getLoanId());
    if (loan == null) {
      throw new LoanNotFoundException(command.getLoanId());
    }

    final Charge chargeDefinition = this.chargeRepository.findOne(command.getChargeId());
    if (chargeDefinition == null || chargeDefinition.isDeleted()) {
      throw new ChargeNotFoundException(command.getChargeId());
    }

    if (!chargeDefinition.isActive()) {
      throw new ChargeIsNotActiveException(chargeDefinition.getId(), chargeDefinition.getName());
    }

    final LoanCharge loanCharge = LoanCharge.createNew(loan, chargeDefinition, command);

    if (!loan.hasCurrencyCodeOf(chargeDefinition.getCurrencyCode())) {
      String errorMessage = "Charge and Loan must have the same currency.";
      throw new InvalidCurrencyException("charge", "attach.to.loan", errorMessage);
    }

    loan.addLoanCharge(loanCharge);
    this.loanRepository.saveAndFlush(loan);

    return new EntityIdentifier(loanCharge.getId());
  }
  @Transactional
  @Override
  public EntityIdentifier deleteLoanCharge(final Long loanId, final Long loanChargeId) {

    this.context.authenticatedUser();

    final Loan loan = retrieveLoanBy(loanId);

    final LoanCharge loanCharge = retrieveLoanChargeBy(loanId, loanChargeId);

    loan.removeLoanCharge(loanCharge);
    this.loanRepository.save(loan);

    return new EntityIdentifier(loanCharge.getId());
  }
Example #4
0
 @Override
 public boolean equals(final Object obj) {
   if (obj == null) {
     return false;
   }
   if (obj == this) {
     return true;
   }
   if (obj.getClass() != getClass()) {
     return false;
   }
   final LoanCharge rhs = (LoanCharge) obj;
   return new EqualsBuilder()
       .appendSuper(super.equals(obj)) //
       .append(getId(), rhs.getId()) //
       .append(this.charge.getId(), rhs.charge.getId()) //
       .append(this.amount, rhs.amount) //
       .append(getDueLocalDate(), rhs.getDueLocalDate()) //
       .isEquals();
 }
  @Transactional
  @Override
  public EntityIdentifier updateLoanCharge(final LoanChargeCommand command) {

    this.context.authenticatedUser();

    LoanChargeCommandValidator validator = new LoanChargeCommandValidator(command);
    validator.validateForUpdate();

    final Long loanId = command.getLoanId();
    final Loan loan = retrieveLoanBy(loanId);

    final Long loanChargeId = command.getId();
    final LoanCharge loanCharge = retrieveLoanChargeBy(loanId, loanChargeId);

    loan.updateLoanCharge(loanCharge, command);

    this.loanRepository.save(loan);

    return new EntityIdentifier(loanCharge.getId());
  }