@Transactional
  @Override
  public EntityIdentifier updateRolePermissions(final Long roleId, final JsonCommand command) {
    context.authenticatedUser();

    final Role role = this.roleRepository.findOne(roleId);
    if (role == null) {
      throw new RoleNotFoundException(roleId);
    }

    final Collection<Permission> allPermissions = this.permissionRepository.findAll();

    final PermissionsCommand permissionsCommand =
        this.permissionsFromApiJsonDeserializer.commandFromApiJson(command.json());

    final Map<String, Boolean> commandPermissions = permissionsCommand.getPermissions();
    final Map<String, Object> changes = new HashMap<String, Object>();
    final Map<String, Boolean> changedPermissions = new HashMap<String, Boolean>();
    for (final String permissionCode : commandPermissions.keySet()) {
      final boolean isSelected = commandPermissions.get(permissionCode).booleanValue();

      final Permission permission = findPermissionByCode(allPermissions, permissionCode);
      boolean changed = role.updatePermission(permission, isSelected);
      if (changed) {
        changedPermissions.put(permissionCode, isSelected);
      }
    }

    if (!changedPermissions.isEmpty()) {
      changes.put("permissions", changedPermissions);
      this.roleRepository.save(role);
    }

    return EntityIdentifier.withChanges(role.getId(), changes);
  }
  @Transactional
  @Override
  public EntityIdentifier updateRole(final Long roleId, final JsonCommand command) {

    context.authenticatedUser();

    final RoleCommand roleCommand =
        this.roleCommandFromApiJsonDeserializer.commandFromApiJson(command.json());
    roleCommand.validateForUpdate();

    final Role role = this.roleRepository.findOne(roleId);
    if (role == null) {
      throw new RoleNotFoundException(roleId);
    }

    final Map<String, Object> changes = role.update(command);
    if (!changes.isEmpty()) {
      this.roleRepository.save(role);
    }

    return EntityIdentifier.withChanges(role.getId(), changes);
  }