@Override
 public void setDefaultGroup(String defaultGroup) {
   String groupId = roleService.getReaderRoleObjectId();
   try {
     groupId = roleService.load(defaultGroup).getId();
   } catch (NotFoundException e) {
     LOG.error("Unable to load role mapping");
   }
   fields.put(DEFAULT_GROUP, groupId);
 }
 @Override
 public String getDefaultGroup() {
   final String defaultGroupId = getDefaultGroupId();
   if (defaultGroupId.equals(roleService.getReaderRoleObjectId())) {
     return "Reader";
   }
   try {
     final Map<String, Role> idToRole = roleService.loadAllIdMap();
     return idToRole.get(defaultGroupId).getName();
   } catch (Exception e) {
     LOG.error("Unable to load role mapping");
     return "Reader";
   }
 }
  @Override
  public void setAdditionalDefaultGroups(Set<String> groupNames) {
    try {
      if (groupNames == null) return;

      final Map<String, Role> nameToRole =
          Maps.uniqueIndex(roleService.loadAll(), Roles.roleToNameFunction());
      final List<String> groupIds =
          Lists.newArrayList(
              Collections2.transform(
                  groupNames,
                  new Function<String, String>() {
                    @Nullable
                    @Override
                    public String apply(@Nullable String groupName) {
                      if (groupName == null || !nameToRole.containsKey(groupName)) {
                        return null;
                      }
                      return nameToRole.get(groupName).getId();
                    }
                  }));
      fields.put(ADDITIONAL_DEFAULT_GROUPS, groupIds);
    } catch (NotFoundException e) {
      LOG.error("Unable to convert group names to ids", e);
      throw new IllegalStateException("Unable to convert group names to ids", e);
    }
  }
  @Override
  public void setGroupMapping(Map<String, String> mapping) {
    Map<String, String> internal;
    if (mapping == null) {
      internal = Collections.emptyMap();
    } else {
      // we store ids internally but external users use the group names
      try {
        final Map<String, Role> nameToRole =
            Maps.uniqueIndex(roleService.loadAll(), Roles.roleToNameFunction());

        internal =
            Maps.newHashMap(
                Maps.transformValues(
                    mapping,
                    new Function<String, String>() {
                      @Nullable
                      @Override
                      public String apply(@Nullable String groupName) {
                        if (groupName == null || !nameToRole.containsKey(groupName)) {
                          return null;
                        }
                        return nameToRole.get(groupName).getId();
                      }
                    }));
      } catch (NotFoundException e) {
        LOG.error("Unable to convert group names to ids", e);
        throw new IllegalStateException("Unable to convert group names to ids", e);
      }
    }

    fields.put(GROUP_MAPPING, internal);
  }
 @Override
 public Set<String> getAdditionalDefaultGroups() {
   final Set<String> additionalGroups = getAdditionalDefaultGroupIds();
   try {
     final Map<String, Role> idToRole = roleService.loadAllIdMap();
     return Sets.newHashSet(
         Collections2.transform(additionalGroups, Roles.roleIdToNameFunction(idToRole)));
   } catch (NotFoundException e) {
     LOG.error("Unable to load role mapping");
     return Collections.emptySet();
   }
 }
  @Nonnull
  @SuppressWarnings("unchecked")
  @Override
  public Map<String, String> getGroupMapping() {
    final Map<String, String> groupMapping = (Map<String, String>) fields.get(GROUP_MAPPING);

    if (groupMapping == null || groupMapping.isEmpty()) {
      return Collections.emptyMap();
    } else {
      // we store role ids, but the outside world uses role names to identify them
      try {
        final Map<String, Role> idToRole = roleService.loadAllIdMap();
        return Maps.newHashMap(
            Maps.transformValues(groupMapping, Roles.roleIdToNameFunction(idToRole)));
      } catch (NotFoundException e) {
        LOG.error("Unable to load role mapping");
        return Collections.emptyMap();
      }
    }
  }
 @Override
 public String getDefaultGroupId() {
   final Object o = fields.get(DEFAULT_GROUP);
   return o == null ? roleService.getReaderRoleObjectId() : (String) o;
 }