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

org.molgenis.data.support.DataServiceImpl Maven / Gradle / Ivy

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

import static java.util.Objects.requireNonNull;

import java.util.Iterator;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.molgenis.data.DataService;
import org.molgenis.data.Entity;
import org.molgenis.data.Fetch;
import org.molgenis.data.Query;
import org.molgenis.data.Repository;
import org.molgenis.data.RepositoryCapability;
import org.molgenis.data.UnknownEntityTypeException;
import org.molgenis.data.UnknownRepositoryException;
import org.molgenis.data.aggregation.AggregateQuery;
import org.molgenis.data.aggregation.AggregateResult;
import org.molgenis.data.meta.MetaDataService;
import org.molgenis.data.meta.model.EntityType;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/** Implementation of the DataService interface */
@Component
public class DataServiceImpl implements DataService {
  private MetaDataService metaDataService;

  public void setMetaDataService(MetaDataService metaDataService) {
    this.metaDataService = requireNonNull(metaDataService);
  }

  @Override
  public boolean hasEntityType(String entityTypeId) {
    return metaDataService.hasEntityType(entityTypeId);
  }

  @Override
  public EntityType getEntityType(String entityTypeId) {
    return metaDataService
        .getEntityType(entityTypeId)
        .orElseThrow(() -> new UnknownEntityTypeException(entityTypeId));
  }

  @Override
  public synchronized Stream getEntityTypeIds() {
    return metaDataService.getEntityTypes().map(EntityType::getId);
  }

  @Transactional(readOnly = true)
  @Override
  public long count(String entityTypeId) {
    return getRepository(entityTypeId).count();
  }

  @Transactional(readOnly = true)
  @Override
  public long count(String entityTypeId, Query q) {
    return getRepository(entityTypeId).count(q);
  }

  @Transactional(readOnly = true)
  @Override
  public Stream findAll(String entityTypeId) {
    return findAll(entityTypeId, query(entityTypeId));
  }

  @Transactional(readOnly = true)
  @Override
  public Stream findAll(String entityTypeId, Query q) {
    return getRepository(entityTypeId).findAll(q);
  }

  @Transactional(readOnly = true)
  @Nullable
  @CheckForNull
  @Override
  public Entity findOneById(String entityTypeId, Object id) {
    return getRepository(entityTypeId).findOneById(id);
  }

  @Transactional(readOnly = true)
  @Nullable
  @CheckForNull
  @Override
  public Entity findOne(String entityTypeId, Query q) {
    return getRepository(entityTypeId).findOne(q);
  }

  @Override
  @Transactional
  public void add(String entityTypeId, Entity entity) {
    getRepository(entityTypeId).add(entity);
  }

  @Override
  @Transactional
  @SuppressWarnings("unchecked")
  public  void add(String entityTypeId, Stream entities) {
    getRepository(entityTypeId).add((Stream) entities);
  }

  @Override
  @Transactional
  public void update(String entityTypeId, Entity entity) {
    getRepository(entityTypeId).update(entity);
  }

  @Override
  @Transactional
  @SuppressWarnings("unchecked")
  public  void update(String entityTypeId, Stream entities) {
    getRepository(entityTypeId).update((Stream) entities);
  }

  @Override
  @Transactional
  public void delete(String entityTypeId, Entity entity) {
    getRepository(entityTypeId).delete(entity);
  }

  @Override
  @Transactional
  @SuppressWarnings("unchecked")
  public  void delete(String entityTypeId, Stream entities) {
    getRepository(entityTypeId).delete((Stream) entities);
  }

  @Override
  @Transactional
  public void deleteById(String entityTypeId, Object id) {
    getRepository(entityTypeId).deleteById(id);
  }

  @Override
  @Transactional
  public void deleteAll(String entityTypeId, Stream ids) {
    getRepository(entityTypeId).deleteAll(ids);
  }

  @Override
  @Transactional
  public void deleteAll(String entityTypeId) {
    getRepository(entityTypeId).deleteAll();
  }

  @Override
  public boolean hasRepository(String entityTypeId) {
    return metaDataService.hasRepository(entityTypeId);
  }

  @Override
  public Repository getRepository(String entityTypeId) {
    return metaDataService
        .getRepository(entityTypeId)
        .orElseThrow(() -> new UnknownRepositoryException(entityTypeId));
  }

  @Override
  public  Repository getRepository(String entityTypeId, Class entityClass) {
    return metaDataService
        .getRepository(entityTypeId, entityClass)
        .orElseThrow(() -> new UnknownRepositoryException(entityTypeId));
  }

  @Override
  public Query query(String entityTypeId) {
    return new QueryImpl<>(this, entityTypeId);
  }

  @Override
  public  Query query(String entityTypeId, Class entityClass) {
    return new QueryImpl<>(this, entityTypeId, entityClass);
  }

  @Transactional(readOnly = true)
  @Override
  public  Stream findAll(String entityTypeId, Query q, Class clazz) {
    return getRepository(entityTypeId, clazz).findAll(q);
  }

  @Transactional(readOnly = true)
  @Nullable
  @CheckForNull
  @Override
  public  E findOneById(String entityTypeId, Object id, Class clazz) {
    return getRepository(entityTypeId, clazz).findOneById(id);
  }

  @Transactional(readOnly = true)
  @Nullable
  @CheckForNull
  @Override
  public  E findOne(String entityTypeId, Query q, Class clazz) {
    return getRepository(entityTypeId, clazz).findOne(q);
  }

  @Transactional(readOnly = true)
  @Override
  public  Stream findAll(String entityTypeId, Class clazz) {
    return findAll(entityTypeId, query(entityTypeId, clazz), clazz);
  }

  @Transactional(readOnly = true)
  @Override
  public AggregateResult aggregate(String entityTypeId, AggregateQuery aggregateQuery) {
    return getRepository(entityTypeId).aggregate(aggregateQuery);
  }

  @Override
  public MetaDataService getMeta() {
    return metaDataService;
  }

  @Override
  public synchronized Iterator> iterator() {
    return metaDataService.getRepositories().iterator();
  }

  @Override
  public Set getCapabilities(String repositoryName) {
    return getRepository(repositoryName).getCapabilities();
  }

  @Transactional(readOnly = true)
  @Nullable
  @CheckForNull
  @Override
  public Entity findOneById(String entityTypeId, Object id, Fetch fetch) {
    return getRepository(entityTypeId).findOneById(id, fetch);
  }

  @Transactional(readOnly = true)
  @Nullable
  @CheckForNull
  @Override
  public  E findOneById(
      String entityTypeId, Object id, Fetch fetch, Class clazz) {
    return getRepository(entityTypeId, clazz).findOneById(id, fetch);
  }

  @Transactional(readOnly = true)
  @Override
  public Stream findAll(String entityTypeId, Stream ids) {
    return getRepository(entityTypeId).findAll(ids);
  }

  @Transactional(readOnly = true)
  @Override
  public  Stream findAll(
      String entityTypeId, Stream ids, Class clazz) {
    return getRepository(entityTypeId, clazz).findAll(ids);
  }

  @Transactional(readOnly = true)
  @Override
  public Stream findAll(String entityTypeId, Stream ids, Fetch fetch) {
    return getRepository(entityTypeId).findAll(ids, fetch);
  }

  @Transactional(readOnly = true)
  @Override
  public  Stream findAll(
      String entityTypeId, Stream ids, Fetch fetch, Class clazz) {
    return getRepository(entityTypeId, clazz).findAll(ids, fetch);
  }
}