All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.molgenis.data.RepositoryCollectionBootstrapper Maven / Gradle / Ivy

There is a newer version: 8.4.5
Show newest version
package org.molgenis.data;

import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.toList;

import java.util.List;
import java.util.Map;
import org.molgenis.data.meta.model.EntityTypeMetadata;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * Discovers and registers {@link RepositoryCollection} beans with the {@link
 * RepositoryCollectionRegistry}
 */
@Component
public class RepositoryCollectionBootstrapper {
  private final RepositoryCollectionRegistry repoCollectionRegistry;
  private EntityTypeMetadata entityTypeMetadata;

  public RepositoryCollectionBootstrapper(
      RepositoryCollectionRegistry repoCollectionRegistry, EntityTypeMetadata entityTypeMetadata) {
    this.repoCollectionRegistry = repoCollectionRegistry;
    this.entityTypeMetadata = entityTypeMetadata;
  }

  public void bootstrap(ContextRefreshedEvent event, String defaultRepoCollectionName) {
    // register repository collections
    ApplicationContext ctx = event.getApplicationContext();
    Map repoCollectionMap =
        ctx.getBeansOfType(RepositoryCollection.class);
    repoCollectionMap.values().forEach(this::register);

    // set EntityTypeMetadata backend enum options
    List repoNames =
        repoCollectionMap.values().stream().map(RepositoryCollection::getName).collect(toList());
    repoNames.sort(naturalOrder());
    entityTypeMetadata.setBackendEnumOptions(repoNames);

    // set default repository collection
    RepositoryCollection defaultRepoCollection =
        repoCollectionMap.values().stream()
            .filter(repoCollection -> repoCollection.getName().equals(defaultRepoCollectionName))
            .findFirst()
            .orElse(null);
    if (defaultRepoCollection == null) {
      throw new UnknownRepositoryCollectionException(defaultRepoCollectionName);
    }
    repoCollectionRegistry.setDefaultRepoCollection(defaultRepoCollection);
    entityTypeMetadata.setDefaultBackend(defaultRepoCollection.getName());
  }

  private void register(RepositoryCollection repositoryCollection) {
    repoCollectionRegistry.addRepositoryCollection(repositoryCollection);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy