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

org.molgenis.data.support.QueryImpl 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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
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.MolgenisDataException;
import org.molgenis.data.Query;
import org.molgenis.data.QueryRule;
import org.molgenis.data.QueryRule.Operator;
import org.molgenis.data.Repository;
import org.molgenis.data.Sort;

public class QueryImpl implements Query {
  private final List> rules = new ArrayList<>();

  private DataService dataService;
  private String entityTypeId;
  private Class entityClass;

  private int offset;
  private int pageSize;
  private Sort sort;
  /** {@link Fetch} that defines which entity attributes to retrieve. */
  private Fetch fetch;

  @Nullable
  @CheckForNull
  @Override
  public Repository getRepository() {
    return repository;
  }

  public void setRepository(Repository repository) {
    this.repository = repository;
  }

  private Repository repository;

  public static  Query query() {
    return new QueryImpl<>();
  }

  public QueryImpl() {
    this.rules.add(new ArrayList<>());
  }

  public QueryImpl(Repository repository) {
    this();
    this.repository = repository;
  }

  public QueryImpl(DataService dataService, String entityTypeId) {
    this(dataService, entityTypeId, null);
  }

  public QueryImpl(
      DataService dataService, String entityTypeId, @Nullable @CheckForNull Class entityClass) {
    this();
    this.dataService = requireNonNull(dataService);
    this.entityTypeId = requireNonNull(entityTypeId);
    this.entityClass = entityClass;
  }

  public QueryImpl(Query q) {
    this();
    for (QueryRule rule : q.getRules()) {
      addRule(rule);
    }
    this.pageSize = q.getPageSize();
    this.offset = q.getOffset();
    this.sort = q.getSort();
    this.fetch = q.getFetch();
  }

  public QueryImpl(QueryRule queryRule) {
    this();
    addRule(queryRule);
  }

  public QueryImpl(List queryRules) {
    this();
    queryRules.forEach(this::addRule);
  }

  public void addRule(QueryRule rule) {
    this.rules.get(this.rules.size() - 1).add(rule);
  }

  @SuppressWarnings("unchecked")
  @Override
  public Long count() {
    if (dataService != null) {
      return dataService.count(entityTypeId, (Query) this);
    } else {
      if (repository == null) throw new RuntimeException("Query failed: repository not set");
      return repository.count(this);
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public Stream findAll() {
    if (dataService != null) {
      if (entityClass != null) {
        return dataService.findAll(entityTypeId, this, entityClass);
      } else {
        return (Stream) dataService.findAll(entityTypeId, (Query) this);
      }
    } else {
      if (repository == null) throw new RuntimeException("Query failed: repository not set");
      return repository.findAll(this);
    }
  }

  @SuppressWarnings("unchecked")
  @Nullable
  @CheckForNull
  @Override
  public E findOne() {
    if (dataService != null) {
      if (entityClass != null) {
        return dataService.findOne(entityTypeId, this, entityClass);
      } else {
        return (E) dataService.findOne(entityTypeId, (Query) this);
      }
    } else {
      if (repository == null) throw new RuntimeException("Query failed: repository not set");
      return repository.findOne(this);
    }
  }

  @Override
  public List getRules() {
    if (this.rules.size() > 1)
      throw new MolgenisDataException("Nested query not closed, use unnest() or unnestAll()");

    if (!this.rules.isEmpty()) {
      List queryRules = this.rules.get(this.rules.size() - 1);

      return Collections.unmodifiableList(queryRules);
    } else return Collections.emptyList();
  }

  @Override
  public int getPageSize() {
    return pageSize;
  }

  public QueryImpl setPageSize(int pageSize) {
    this.pageSize = pageSize;
    return this;
  }

  @Override
  public int getOffset() {
    return offset;
  }

  public QueryImpl setOffset(int offset) {
    this.offset = offset;
    return this;
  }

  @Nullable
  @CheckForNull
  @Override
  public Sort getSort() {
    return sort;
  }

  public void setSort(Sort sort) {
    this.sort = sort;
  }

  @Override
  public Query search(String searchTerms) {
    rules.get(this.rules.size() - 1).add(new QueryRule(Operator.SEARCH, searchTerms));
    return this;
  }

  @Override
  public Query search(String field, String searchTerms) {
    rules.get(this.rules.size() - 1).add(new QueryRule(field, Operator.SEARCH, searchTerms));
    return this;
  }

  @Override
  public Query or() {
    rules.get(this.rules.size() - 1).add(new QueryRule(Operator.OR));
    return this;
  }

  @Override
  public Query and() {
    rules.get(this.rules.size() - 1).add(new QueryRule(Operator.AND));
    return this;
  }

  @Override
  public Query not() {
    rules.get(this.rules.size() - 1).add(new QueryRule(Operator.NOT));
    return this;
  }

  @Override
  public Query like(String field, String value) {
    rules.get(this.rules.size() - 1).add(new QueryRule(field, Operator.LIKE, value));
    return this;
  }

  @Override
  public Query eq(String field, Object value) {
    rules.get(this.rules.size() - 1).add(new QueryRule(field, Operator.EQUALS, value));
    return this;
  }

  @Override
  public Query in(String field, Iterable valueIterable) {
    rules.get(this.rules.size() - 1).add(new QueryRule(field, Operator.IN, valueIterable));
    return this;
  }

  @Override
  public Query gt(String field, Object value) {
    rules.get(this.rules.size() - 1).add(new QueryRule(field, Operator.GREATER, value));
    return this;
  }

  @Override
  public Query ge(String field, Object value) {
    rules.get(this.rules.size() - 1).add(new QueryRule(field, Operator.GREATER_EQUAL, value));
    return this;
  }

  @Override
  public Query le(String field, Object value) {
    rules.get(this.rules.size() - 1).add(new QueryRule(field, Operator.LESS_EQUAL, value));
    return this;
  }

  @Override
  public Query lt(String field, Object value) {
    rules.get(this.rules.size() - 1).add(new QueryRule(field, Operator.LESS, value));
    return this;
  }

  @Override
  public Query nest() {
    // add element to our nesting list...
    this.rules.add(new ArrayList<>());
    return this;
  }

  @Override
  public Query unnest() {
    if (this.rules.size() == 1)
      throw new MolgenisDataException("Cannot unnest root element of query");

    // remove last element and add to parent as nested rule
    QueryRule nested = new QueryRule(Operator.NESTED, this.rules.get(this.rules.size() - 1));
    this.rules.remove(this.rules.get(this.rules.size() - 1));
    this.rules.get(this.rules.size() - 1).add(nested);
    return this;
  }

  @Override
  public Query unnestAll() {
    while (this.rules.size() > 1) {
      unnest();
    }
    return this;
  }

  @Override
  public Query rng(String field, Object from, Object to) {
    rules
        .get(this.rules.size() - 1)
        .add(new QueryRule(field, Operator.RANGE, Arrays.asList(from, to)));
    return this;
  }

  @Override
  public Query pageSize(int pageSize) {
    setPageSize(pageSize);
    return this;
  }

  @Override
  public Query offset(int offset) {
    setOffset(offset);
    return this;
  }

  @Override
  public Sort sort() {
    this.sort = new Sort();
    return this.sort;
  }

  @Override
  public Query sort(Sort sort) {
    setSort(sort);
    return this;
  }

  @Override
  public Iterator iterator() {
    if (repository == null) throw new RuntimeException("Query failed: repository not set");
    return repository.findAll(this).iterator();
  }

  @Nullable
  @CheckForNull
  @Override
  public Fetch getFetch() {
    return fetch;
  }

  @Override
  public void setFetch(Fetch fetch) {
    this.fetch = fetch;
  }

  @Override
  public Fetch fetch() {
    this.fetch = new Fetch();
    return getFetch();
  }

  @Override
  public Query fetch(Fetch fetch) {
    setFetch(fetch);
    return this;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    QueryImpl query = (QueryImpl) o;
    return offset == query.offset
        && pageSize == query.pageSize
        && Objects.equals(rules, query.rules)
        && Objects.equals(sort, query.sort);
  }

  @Override
  public int hashCode() {
    return Objects.hash(rules, offset, pageSize, sort);
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    if (!rules.isEmpty()) {
      if (rules.size() == 1) {
        List rule = rules.get(0);
        if (!rule.isEmpty()) {
          builder.append("rules=").append(rule);
        }
      } else {
        builder.append("rules=").append(rules);
      }
    }
    if (offset != 0) {
      if (builder.length() > 0) {
        builder.append(", ");
      }
      builder.append("offset=").append(offset);
    }
    if (pageSize != 0) {
      if (builder.length() > 0) {
        builder.append(", ");
      }
      builder.append("pageSize=").append(pageSize);
    }
    if (sort != null) {
      if (builder.length() > 0) {
        builder.append(", ");
      }
      builder.append("sort=").append(sort);
    }
    if (fetch != null) {
      if (builder.length() > 0) {
        builder.append(", ");
      }
      builder.append("fetch=").append(fetch);
    }
    return builder.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy