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

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

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

import static com.google.common.collect.Streams.stream;
import static java.util.Objects.requireNonNull;

import org.molgenis.data.meta.model.EntityType;
import org.molgenis.data.support.LazyEntity;
import org.springframework.stereotype.Component;

@Component
public class EntityReferenceCreatorImpl implements EntityReferenceCreator {
  private final DataService dataService;
  private final EntityFactoryRegistry entityFactoryRegistry;

  public EntityReferenceCreatorImpl(
      DataService dataService, EntityFactoryRegistry entityFactoryRegistry) {
    this.dataService = requireNonNull(dataService);
    this.entityFactoryRegistry = requireNonNull(entityFactoryRegistry);
  }

  @Override
  public Entity getReference(EntityType entityType, Object id) {
    Entity lazyEntity = new LazyEntity(entityType, dataService, id);

    EntityFactory entityFactory =
        entityFactoryRegistry.getEntityFactory(entityType);
    if (entityFactory != null) {
      // create static entity (e.g. Tag, Language, Package) that wraps the constructed dynamic or
      // partial entity.
      lazyEntity = entityFactory.create(lazyEntity);
    }

    return lazyEntity;
  }

  @Override
  public Iterable getReferences(EntityType entityType, Iterable ids) {
    EntityFactory entityFactory =
        entityFactoryRegistry.getEntityFactory(entityType);
    return () ->
        stream(ids)
            .map(
                id -> {
                  Entity lazyEntity = getReference(entityType, id);
                  if (entityFactory != null) {
                    // create static entity (e.g. Tag, Language, Package) that wraps the constructed
                    // dynamic or partial entity.
                    lazyEntity = entityFactory.create(lazyEntity);
                  }
                  return lazyEntity;
                })
            .iterator();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy