@Override
  public Collection<LoanProductLookup> retrieveAllLoanProductsForLookup() {

    AppUser currentUser = this.context.authenticatedUser();

    LoanProductLookupMapper rm = new LoanProductLookupMapper();

    String sql = "select " + rm.loanProductLookupSchema() + " where lp.org_id = ?";

    return this.jdbcTemplate.query(sql, rm, new Object[] {currentUser.getOrganisation().getId()});
  }
  @Override
  public Collection<LoanProductData> retrieveAllLoanProducts() {

    AppUser currentUser = this.context.authenticatedUser();
    // TODO - include currency read in the sql
    List<CurrencyData> allowedCurrencies =
        currencyReadPlatformService.retrieveAllPlatformCurrencies();

    LoanProductMapper rm = new LoanProductMapper(allowedCurrencies);

    String sql = "select " + rm.loanProductSchema() + " where lp.org_id = ?";

    return this.jdbcTemplate.query(sql, rm, new Object[] {currentUser.getOrganisation().getId()});
  }
  @Transactional
  @Override
  public Long updateRole(RoleCommand command) {

    AppUser currentUser = context.authenticatedUser();

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

    List<Permission> selectedPermissions =
        assembleListOfSelectedPermissions(command.getPermissions());

    Role role =
        this.roleRepository.findOne(rolesThatMatch(currentUser.getOrganisation(), command.getId()));
    if (role == null) {
      throw new RoleNotFoundException(command.getId());
    }
    role.update(command.getName(), command.getDescription(), selectedPermissions);

    this.roleRepository.save(role);

    return role.getId();
  }
  @Transactional
  @Override
  public Long createRole(final RoleCommand command) {

    AppUser currentUser = context.authenticatedUser();

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

    List<Permission> selectedPermissions =
        assembleListOfSelectedPermissions(command.getPermissions());

    Role entity =
        new Role(
            currentUser.getOrganisation(),
            command.getName(),
            command.getDescription(),
            selectedPermissions);

    this.roleRepository.save(entity);

    return entity.getId();
  }