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

ca.gc.aafc.dina.repository.ReadOnlyDinaRepositoryV2 Maven / Gradle / Ivy

The newest version!
package ca.gc.aafc.dina.repository;

import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;

import org.apache.commons.collections.CollectionUtils;

import ca.gc.aafc.dina.filter.FilterComponent;
import ca.gc.aafc.dina.filter.QueryComponent;
import ca.gc.aafc.dina.filter.QueryStringParser;
import ca.gc.aafc.dina.filter.SimpleObjectFilterHandlerV2;
import ca.gc.aafc.dina.service.PredicateBasedReadOnlyDinaService;

/**
 * Based repository for accessing read-only data.
 */
public class ReadOnlyDinaRepositoryV2 {

  private final PredicateBasedReadOnlyDinaService service;

  public ReadOnlyDinaRepositoryV2(PredicateBasedReadOnlyDinaService service) {
    this.service = service;
  }

  public D findOne(K key) {
    return service.findOne(key);
  }

  public List findAll(String queryString) {
    QueryComponent queryComponents = QueryStringParser.parse(queryString);

    FilterComponent fc = queryComponents.getFilters();

    Predicate predicate = SimpleObjectFilterHandlerV2.createPredicate(fc);

    Comparator comparator = null;
    if (CollectionUtils.isNotEmpty(queryComponents.getSorts())) {
      for (String sort : queryComponents.getSorts()) {
        if (comparator == null) {
          comparator = SimpleObjectFilterHandlerV2.generateComparator(sort);
        } else {
          comparator =
            comparator.thenComparing(SimpleObjectFilterHandlerV2.generateComparator(sort));
        }
      }
    }

    return service.findAll(predicate, comparator, queryComponents.getPageOffset(), queryComponents.getPageLimit());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy