@Transactional
  @Override
  public CommandProcessingResult submitApplication(final JsonCommand command) {
    try {
      this.savingsAccountDataValidator.validateForSubmit(command.json());
      final AppUser submittedBy = this.context.authenticatedUser();

      final SavingsAccount account = this.savingAccountAssembler.assembleFrom(command, submittedBy);
      this.savingAccountRepository.save(account);

      if (account.isAccountNumberRequiresAutoGeneration()) {
        final AccountNumberGenerator accountNoGenerator =
            this.accountIdentifierGeneratorFactory.determineSavingsAccountNoGenerator(
                account.getId());
        account.updateAccountNo(accountNoGenerator.generate());

        this.savingAccountRepository.save(account);
      }

      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();
    }
  }
  @Transactional
  @Override
  public CommandProcessingResult rejectApplication(
      final Long savingsId, final JsonCommand command) {

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

    this.savingsAccountApplicationTransitionApiJsonValidator.validateRejection(command.json());

    final SavingsAccount savingsAccount = this.savingAccountAssembler.assembleFrom(savingsId);
    checkClientOrGroupActive(savingsAccount);

    final Map<String, Object> changes =
        savingsAccount.rejectApplication(currentUser, command, DateUtils.getLocalDateOfTenant());
    if (!changes.isEmpty()) {
      this.savingAccountRepository.save(savingsAccount);

      final String noteText = command.stringValueOfParameterNamed("note");
      if (StringUtils.isNotBlank(noteText)) {
        final Note note = Note.savingNote(savingsAccount, noteText);
        changes.put("note", noteText);
        this.noteRepository.save(note);
      }
    }

    return new CommandProcessingResultBuilder() //
        .withCommandId(command.commandId()) //
        .withEntityId(savingsId) //
        .withOfficeId(savingsAccount.officeId()) //
        .withClientId(savingsAccount.clientId()) //
        .withGroupId(savingsAccount.groupId()) //
        .withSavingsId(savingsId) //
        .with(changes) //
        .build();
  }
  @Transactional
  @Override
  public CommandProcessingResult activate(final Long savingsId, final JsonCommand command) {

    this.context.authenticatedUser();

    this.savingsAccountTransactionDataValidator.validateActivation(command);

    final SavingsAccount account =
        this.savingAccountRepository.findOneWithNotFoundDetection(savingsId);

    final Locale locale = command.extractLocale();
    final DateTimeFormatter fmt =
        DateTimeFormat.forPattern(command.dateFormat()).withLocale(locale);
    final LocalDate activationDate = command.localDateValueOfParameterNamed("activationDate");
    final List<Long> existingTransactionIds = new ArrayList<Long>();
    final List<Long> existingReversedTransactionIds = new ArrayList<Long>();

    account.activate(
        fmt, activationDate, existingReversedTransactionIds, existingReversedTransactionIds);

    this.savingAccountRepository.save(account);

    postJournalEntries(account, existingTransactionIds, existingReversedTransactionIds);

    return new CommandProcessingResultBuilder() //
        .withEntityId(savingsId) //
        .withOfficeId(account.officeId()) //
        .withClientId(account.clientId()) //
        .withGroupId(account.groupId()) //
        .withSavingsId(savingsId) //
        .build();
  }
  @Transactional
  @Override
  public CommandProcessingResult deposit(final Long savingsId, final JsonCommand command) {

    this.context.authenticatedUser();

    this.savingsAccountTransactionDataValidator.validate(command);

    final SavingsAccount account =
        this.savingAccountRepository.findOneWithNotFoundDetection(savingsId);

    final Locale locale = command.extractLocale();
    final DateTimeFormatter fmt =
        DateTimeFormat.forPattern(command.dateFormat()).withLocale(locale);

    final LocalDate transactionDate = command.localDateValueOfParameterNamed("transactionDate");
    final BigDecimal transactionAmount =
        command.bigDecimalValueOfParameterNamed("transactionAmount");

    final List<Long> existingTransactionIds = new ArrayList<Long>();
    final List<Long> existingReversedTransactionIds = new ArrayList<Long>();
    final Map<String, Object> changes = new LinkedHashMap<String, Object>();
    PaymentDetail paymentDetail =
        paymentDetailWritePlatformService.createAndPersistPaymentDetail(command, changes);

    final SavingsAccountTransaction deposit =
        account.deposit(
            fmt,
            transactionDate,
            transactionAmount,
            existingTransactionIds,
            existingReversedTransactionIds,
            paymentDetail);
    final Long transactionId = saveTransactionToGenerateTransactionId(deposit);

    this.savingAccountRepository.save(account);

    postJournalEntries(account, existingTransactionIds, existingReversedTransactionIds);

    return new CommandProcessingResultBuilder() //
        .withEntityId(transactionId) //
        .withOfficeId(account.officeId()) //
        .withClientId(account.clientId()) //
        .withGroupId(account.groupId()) //
        .withSavingsId(savingsId) //
        .with(changes) //
        .build();
  }
  @Transactional
  @Override
  public CommandProcessingResult deleteSavingAccount(final Long savingsId) {

    this.context.authenticatedUser();

    final SavingsAccount account =
        this.savingAccountRepository.findOneWithNotFoundDetection(savingsId);

    this.savingAccountRepository.delete(account);

    return new CommandProcessingResultBuilder() //
        .withEntityId(savingsId) //
        .withOfficeId(account.officeId()) //
        .withClientId(account.clientId()) //
        .withGroupId(account.groupId()) //
        .withSavingsId(savingsId) //
        .build();
  }
  @Override
  public CommandProcessingResult calculateInterest(
      final Long savingsId, final JsonCommand command) {
    this.context.authenticatedUser();

    final SavingsAccount account =
        this.savingAccountRepository.findOneWithNotFoundDetection(savingsId);

    final LocalDate today = DateUtils.getLocalDateOfTenant();
    account.calculateInterest(today);
    this.savingAccountRepository.save(account);

    return new CommandProcessingResultBuilder() //
        .withEntityId(savingsId) //
        .withOfficeId(account.officeId()) //
        .withClientId(account.clientId()) //
        .withGroupId(account.groupId()) //
        .withSavingsId(savingsId) //
        .build();
  }
  @Transactional
  @Override
  public CommandProcessingResult createSavingAccount(final JsonCommand command) {
    try {
      this.context.authenticatedUser();
      this.savingsAccountDataValidator.validateForCreate(command.json());

      final List<Long> existingTransactionIds = new ArrayList<Long>();
      final List<Long> existingReversedTransactionIds = new ArrayList<Long>();
      final SavingsAccount account =
          this.savingAccountAssembler.assembleFrom(
              command, existingTransactionIds, existingReversedTransactionIds);
      this.savingAccountRepository.save(account);

      if (account.isAccountNumberRequiresAutoGeneration()) {
        final AccountNumberGenerator accountNoGenerator =
            this.accountIdentifierGeneratorFactory.determineLoanAccountNoGenerator(account.getId());
        account.updateAccountNo(accountNoGenerator.generate());

        this.savingAccountRepository.save(account);
      }

      postJournalEntries(account, existingTransactionIds, existingReversedTransactionIds);

      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 (DataAccessException dve) {
      handleDataIntegrityIssues(command, dve);
      return CommandProcessingResult.empty();
    }
  }
  @Transactional
  @Override
  public CommandProcessingResult deleteApplication(final Long savingsId) {

    final SavingsAccount account = this.savingAccountAssembler.assembleFrom(savingsId);
    checkClientOrGroupActive(account);

    if (account.isNotSubmittedAndPendingApproval()) {
      final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
      final DataValidatorBuilder baseDataValidator =
          new DataValidatorBuilder(dataValidationErrors)
              .resource(
                  SAVINGS_ACCOUNT_RESOURCE_NAME + SavingsApiConstants.deleteApplicationAction);

      baseDataValidator
          .reset()
          .parameter(SavingsApiConstants.activatedOnDateParamName)
          .failWithCodeNoParameterAddedToErrorCode("not.in.submittedandpendingapproval.state");

      if (!dataValidationErrors.isEmpty()) {
        throw new PlatformApiDataValidationException(dataValidationErrors);
      }
    }

    final List<Note> relatedNotes = this.noteRepository.findBySavingsAccountId(savingsId);
    this.noteRepository.deleteInBatch(relatedNotes);

    this.savingAccountRepository.delete(account);

    return new CommandProcessingResultBuilder() //
        .withEntityId(savingsId) //
        .withOfficeId(account.officeId()) //
        .withClientId(account.clientId()) //
        .withGroupId(account.groupId()) //
        .withSavingsId(savingsId) //
        .build();
  }
  @Transactional
  @Override
  public CommandProcessingResult postInterest(final Long savingsId, final JsonCommand command) {
    this.context.authenticatedUser();
    final List<Long> existingTransactionIds = new ArrayList<Long>();
    final List<Long> existingReversedTransactionIds = new ArrayList<Long>();

    final SavingsAccount account =
        this.savingAccountRepository.findOneWithNotFoundDetection(savingsId);

    final LocalDate today = DateUtils.getLocalDateOfTenant();
    account.postInterest(today, existingTransactionIds, existingReversedTransactionIds);
    this.savingAccountRepository.save(account);

    postJournalEntries(account, existingTransactionIds, existingReversedTransactionIds);

    return new CommandProcessingResultBuilder() //
        .withEntityId(savingsId) //
        .withOfficeId(account.officeId()) //
        .withClientId(account.clientId()) //
        .withGroupId(account.groupId()) //
        .withSavingsId(savingsId) //
        .build();
  }
  @Transactional
  @Override
  public CommandProcessingResult modifyApplication(
      final Long savingsId, final JsonCommand command) {
    try {
      this.savingsAccountDataValidator.validateForUpdate(command.json());

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

      final SavingsAccount account = this.savingAccountAssembler.assembleFrom(savingsId);
      checkClientOrGroupActive(account);
      account.modifyApplication(command, changes);
      account.validateNewApplicationState(DateUtils.getLocalDateOfTenant());
      account.validateAccountValuesWithProduct();

      if (!changes.isEmpty()) {

        if (changes.containsKey(SavingsApiConstants.clientIdParamName)) {
          final Long clientId =
              command.longValueOfParameterNamed(SavingsApiConstants.clientIdParamName);
          if (clientId != null) {
            final Client client = this.clientRepository.findOneWithNotFoundDetection(clientId);
            if (client.isNotActive()) {
              throw new ClientNotActiveException(clientId);
            }
            account.update(client);
          } else {
            final Client client = null;
            account.update(client);
          }
        }

        if (changes.containsKey(SavingsApiConstants.groupIdParamName)) {
          final Long groupId =
              command.longValueOfParameterNamed(SavingsApiConstants.groupIdParamName);
          if (groupId != null) {
            final Group group = this.groupRepository.findOne(groupId);
            if (group == null) {
              throw new GroupNotFoundException(groupId);
            }
            if (group.isNotActive()) {
              if (group.isCenter()) {
                throw new CenterNotActiveException(groupId);
              }
              throw new GroupNotActiveException(groupId);
            }
            account.update(group);
          } else {
            final Group group = null;
            account.update(group);
          }
        }

        if (changes.containsKey(SavingsApiConstants.productIdParamName)) {
          final Long productId =
              command.longValueOfParameterNamed(SavingsApiConstants.productIdParamName);
          final SavingsProduct product = this.savingsProductRepository.findOne(productId);
          if (product == null) {
            throw new SavingsProductNotFoundException(productId);
          }

          account.update(product);
        }

        if (changes.containsKey(SavingsApiConstants.fieldOfficerIdParamName)) {
          final Long fieldOfficerId =
              command.longValueOfParameterNamed(SavingsApiConstants.fieldOfficerIdParamName);
          Staff fieldOfficer = null;
          if (fieldOfficerId != null) {
            fieldOfficer = this.staffRepository.findOneWithNotFoundDetection(fieldOfficerId);
          } else {
            changes.put(SavingsApiConstants.fieldOfficerIdParamName, "");
          }
          account.update(fieldOfficer);
        }

        if (changes.containsKey("charges")) {
          final Set<SavingsAccountCharge> charges =
              this.savingsAccountChargeAssembler.fromParsedJson(
                  command.parsedJson(), account.getCurrency().getCode());
          final boolean updated = account.update(charges);
          if (!updated) {
            changes.remove("charges");
          }
        }

        this.savingAccountRepository.saveAndFlush(account);
      }

      return new CommandProcessingResultBuilder() //
          .withCommandId(command.commandId()) //
          .withEntityId(savingsId) //
          .withOfficeId(account.officeId()) //
          .withClientId(account.clientId()) //
          .withGroupId(account.groupId()) //
          .withSavingsId(savingsId) //
          .with(changes) //
          .build();
    } catch (final DataAccessException dve) {
      handleDataIntegrityIssues(command, dve);
      return new CommandProcessingResult(Long.valueOf(-1));
    }
  }
  @Transactional
  @Override
  public CommandProcessingResult updateSavingAccount(
      final Long savingsId, final JsonCommand command) {
    try {
      this.context.authenticatedUser();
      this.savingsAccountDataValidator.validateForUpdate(command.json());

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

      final SavingsAccount account =
          this.savingAccountRepository.findOneWithNotFoundDetection(savingsId);

      account.update(command, changes);

      if (!changes.isEmpty()) {

        if (changes.containsKey(SavingsApiConstants.clientIdParamName)) {
          final Long clientId =
              command.longValueOfParameterNamed(SavingsApiConstants.clientIdParamName);
          if (clientId != null) {
            final Client client = this.clientRepository.findOneWithNotFoundDetection(clientId);
            account.update(client);
          } else {
            final Client client = null;
            account.update(client);
          }
        }

        if (changes.containsKey(SavingsApiConstants.groupIdParamName)) {
          final Long groupId =
              command.longValueOfParameterNamed(SavingsApiConstants.groupIdParamName);
          if (groupId != null) {
            final Group group = this.groupRepository.findOne(groupId);
            if (group == null) {
              throw new GroupNotFoundException(groupId);
            }
            account.update(group);
          } else {
            final Group group = null;
            account.update(group);
          }
        }

        if (changes.containsKey(SavingsApiConstants.productIdParamName)) {
          final Long productId =
              command.longValueOfParameterNamed(SavingsApiConstants.productIdParamName);
          final SavingsProduct product = this.savingsProductRepository.findOne(productId);
          if (product == null) {
            throw new SavingsProductNotFoundException(productId);
          }

          account.update(product);
        }

        this.savingAccountRepository.saveAndFlush(account);
      }

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