public Directory buildDelegatingLdapDirectory(
      DelegatingLdapDirectoryConfiguration configuration) {
    ImmutableDirectory.Builder builder = createBuilder();
    builder.setAllowedOperations(EnumSet.allOf(OperationType.class));
    builder.setActive(configuration.isActive());
    builder.setId(configuration.getDirectoryId());
    builder.setImplementationClass(DelegatedAuthenticationDirectory.class.getName());
    builder.setName(configuration.getName());
    builder.setType(DirectoryType.DELEGATING);

    LdapDelegatingDirectoryAttributes attributes = new LdapDelegatingDirectoryAttributes();
    BeanUtils.copyProperties(
        configuration, attributes); // ignores attributes that don't exist in the target - perfect!
    attributes.setDelegatedToClass(configuration.getType());
    if (configuration.isCreateUserOnAuth()) {
      attributes.setUpdateUserOnAuth(true); // create-on-auth implies update-on-auth
      attributes.setLdapAutoAddGroups(
          commaWhitespaceSeparatedGroupsToPipeSeparatedGroups(
              configuration.getLdapAutoAddGroups()));
    } else {
      attributes.setLdapAutoAddGroups("");
    }
    builder.setAttributes(attributes.toAttributesMap());

    return builder.toDirectory();
  }
  public DelegatingLdapDirectoryConfiguration toDelegatingLdapConfiguration(
      final Directory directory) {
    DelegatingLdapDirectoryConfiguration configuration = new DelegatingLdapDirectoryConfiguration();
    configuration.setDirectoryId(directory.getId() != null ? directory.getId() : 0);
    configuration.setActive(directory.isActive());
    configuration.setName(directory.getName());

    LdapDelegatingDirectoryAttributes attributes =
        LdapDelegatingDirectoryAttributes.fromAttributesMap(directory.getAttributes());
    BeanUtils.copyProperties(attributes, configuration);
    configuration.setType(attributes.getDelegatedToClass());
    final String ldapAutoAddGroups = attributes.getLdapAutoAddGroups();
    if (ldapAutoAddGroups != null) {
      configuration.setLdapAutoAddGroups(
          pipeSeparatedGroupsToCommaSeparatedGroups(ldapAutoAddGroups));
    } else {
      configuration.setLdapAutoAddGroups("");
    }

    return configuration;
  }