public Directory buildLdapDirectory(LdapDirectoryConfiguration configuration) {
    ImmutableDirectory.Builder builder = createBuilder();
    builder.setActive(configuration.isActive());
    builder.setAllowedOperations(configuration.getLdapPermissionOption().getAllowedOperations());
    builder.setEncryptionType(configuration.getLdapUserEncryption());
    builder.setId(configuration.getDirectoryId());
    builder.setImplementationClass(configuration.getType());
    builder.setName(configuration.getName());
    builder.setType(DirectoryType.CONNECTOR);

    LdapDirectoryAttributes attributes = new LdapDirectoryAttributes();
    BeanUtils.copyProperties(
        configuration, attributes); // ignores attributes that don't exist in the target - perfect!
    attributes.setIncrementalSyncEnabled(configuration.isCrowdSyncIncrementalEnabled());
    if (configuration.getLdapPermissionOption() == PermissionOption.READ_ONLY_LOCAL_GROUPS) {
      attributes.setLdapAutoAddGroups(
          commaWhitespaceSeparatedGroupsToPipeSeparatedGroups(
              configuration.getLdapAutoAddGroups()));
    } else {
      attributes.setLdapAutoAddGroups("");
    }
    builder.setAttributes(attributes.toAttributesMap());

    return builder.toDirectory();
  }
 private ImmutableDirectory.Builder createBuilder() {
   ImmutableDirectory.Builder builder = ImmutableDirectory.newBuilder();
   Date now = new Date();
   builder.setCreatedDate(now);
   builder.setUpdatedDate(now);
   return builder;
 }
  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 Directory buildJiraJdbcDirectory(JiraJdbcDirectoryConfiguration configuration) {
    ImmutableDirectory.Builder builder = createBuilder();
    builder.setAllowedOperations(EnumSet.of(OperationType.UPDATE_USER));
    builder.setActive(configuration.isActive());
    builder.setId(configuration.getDirectoryId());
    builder.setImplementationClass(JiraJdbcDirectoryConfiguration.DIRECTORY_CLASS);
    builder.setName(configuration.getName());
    builder.setType(DirectoryType.CUSTOM);

    builder.setAttributes(
        Collections.singletonMap(
            JiraJdbcDirectoryConfiguration.JNDI_NAME_ATTRIBUTE_KEY,
            configuration.getDatasourceJndiName()));

    return builder.toDirectory();
  }
  public Directory buildCrowdDirectory(CrowdDirectoryConfiguration configuration) {
    ImmutableDirectory.Builder builder = createBuilder();
    builder.setAllowedOperations(configuration.getCrowdPermissionOption().getAllowedOperations());
    builder.setActive(configuration.isActive());
    builder.setId(configuration.getDirectoryId());
    builder.setImplementationClass(RemoteCrowdDirectory.class.getName());
    builder.setName(configuration.getName());
    builder.setType(DirectoryType.CROWD);

    CrowdDirectoryAttributes attributes = new CrowdDirectoryAttributes();
    BeanUtils.copyProperties(
        configuration, attributes); // ignores attributes that don't exist in the target - perfect!
    // Convert polling interval from minutes to seconds to store in crowd
    attributes.setCrowdServerSynchroniseIntervalInSeconds(
        Long.toString(configuration.getCrowdServerSynchroniseIntervalInMin() * 60));
    builder.setAttributes(attributes.toAttributesMap());

    return builder.toDirectory();
  }
  public Directory buildInternalDirectory(InternalDirectoryConfiguration configuration) {
    ImmutableDirectory.Builder builder = createBuilder();
    builder.setAllowedOperations(EnumSet.allOf(OperationType.class));
    builder.setActive(configuration.isActive());
    builder.setId(configuration.getDirectoryId());
    builder.setImplementationClass(InternalDirectory.class.getName());
    builder.setName(configuration.getName());
    builder.setType(DirectoryType.INTERNAL);

    HashMap<String, String> map = new HashMap<String, String>();
    map.put(
        InternalDirectory.ATTRIBUTE_USER_ENCRYPTION_METHOD,
        PasswordEncoderFactory.ATLASSIAN_SECURITY_ENCODER);
    map.put(
        DirectoryImpl.ATTRIBUTE_KEY_USE_NESTED_GROUPS,
        Boolean.valueOf(configuration.isNestedGroupsEnabled()).toString());
    builder.setAttributes(map);

    return builder.toDirectory();
  }