Commit 279904c4 authored by Matija Obreza's avatar Matija Obreza
Browse files

List Taxonomy2 by number of accessions

parent fa1db31c
......@@ -17,6 +17,7 @@
package org.genesys2.server.model.genesys;
import java.text.MessageFormat;
import java.util.List;
import javax.persistence.Cacheable;
import javax.persistence.Column;
......@@ -25,6 +26,7 @@ import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;
......@@ -39,6 +41,7 @@ import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
......@@ -102,6 +105,9 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
@Field(type = FieldType.Object)
private TaxonomySpecies currentTaxonomySpecies;
@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "taxonomy")
@JsonIgnore
private List<Accession> accessions;
public Taxonomy2() {
}
......
......@@ -168,4 +168,6 @@ public interface AccessionRepository extends JpaRepository<Accession, Long>, Acc
@Query("select count(a.id) - count(distinct a.accessionNumber) from Accession a where a.institute.id = ?1")
public long countNonuniqueAccessionNumbers(long instituteId);
public Long countByTaxonomy(Taxonomy2 taxonomy2);
}
......@@ -29,7 +29,6 @@ import org.genesys2.server.service.TaxonomyService;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.JPQLQuery;
/**
* The Class TaxonomyFilter.
......@@ -134,73 +133,4 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter,
public StringFilter subtaxa() {
return this.subtaxa == null ? this.subtaxa = new StringFilter() : this.subtaxa;
}
//
// /**
// * This is mostly a copy of {@link #collectPredicates()} above, with inner joins where possible.
// *
// * @param taxonomy
// * @return the list of where clauses
// */
// public List<Predicate> buildJpaQuery(JPQLQuery<?> query, QTaxonomy2 taxonomy) {
// final List<Predicate> predicates = super.collectPredicates(taxonomy, taxonomy._super._super);
// final TaxonomyService taxonomyService = CurrentApplicationContext.getContext().getBean(TaxonomyService.class);
//
// if (CollectionUtils.isNotEmpty(genus) && taxonomyService != null) {
// // We need to look up the taxonomy2.id for provided names so that ES queries
// // also work out of the box
// List<Long> taxGenus = taxonomyService.findTaxonomy2GenusId(new ArrayList<>(genus));
// List<Long> grinGenus = taxonomyService.findGrinGenusId(new ArrayList<>(genus));
//
// BooleanBuilder builder = new BooleanBuilder(taxonomy.taxGenus.in(taxGenus));
// if (!grinGenus.isEmpty()) {
// builder.or(taxonomy.grinTaxonomySpecies.taxonomyGenus.id.in(grinGenus));
// if (synonyms == null || synonyms) {
// builder.or(taxonomy.currentTaxonomySpecies.taxonomyGenus.id.in(grinGenus));
// }
// }
// predicates.add(builder);
// }
// if (species != null && !species.isEmpty()) {
// BooleanBuilder builder = new BooleanBuilder(taxonomy.species.in(species));
// builder.or(taxonomy.grinTaxonomySpecies.speciesName.in(species));
// if (synonyms == null || synonyms) {
// builder.or(taxonomy.currentTaxonomySpecies.speciesName.in(species));
// }
// predicates.add(builder);
// }
// if (subtaxa != null) {
// BooleanBuilder builder = new BooleanBuilder(subtaxa.buildQuery(taxonomy.subtaxa));
// builder.or(subtaxa.buildQuery(taxonomy.grinTaxonomySpecies.subtaxa));
// if (synonyms == null || synonyms) {
// builder.or(subtaxa.buildQuery(taxonomy.currentTaxonomySpecies.subtaxa));
// }
// predicates.add(builder);
// }
// if (taxonName != null) {
// BooleanBuilder builder = new BooleanBuilder(taxonName.buildQuery(taxonomy.taxonName));
// builder.or(taxonName.buildQuery(taxonomy.grinTaxonomySpecies.name));
// if (synonyms == null || synonyms) {
// builder.or(taxonName.buildQuery(taxonomy.currentTaxonomySpecies.name));
// }
// predicates.add(builder);
// }
// if (CollectionUtils.isNotEmpty(genusSpecies) && taxonomyService != null) {
// // We need to look up the taxonomy2.id for provided names so that ES queries
// // also work out of the box
// List<Long> taxSpecies = taxonomyService.findTaxonomy2SpeciesId(new ArrayList<>(genusSpecies));
// List<Long> grinSpecies = taxonomyService.findGrinSpeciesId(new ArrayList<>(genusSpecies));
//
// BooleanBuilder builder = new BooleanBuilder(taxonomy.taxSpecies.in(taxSpecies));
// if (!grinSpecies.isEmpty()) {
// builder.or(taxonomy.grinTaxonomySpecies.id.in(grinSpecies));
// if (synonyms == null || synonyms) {
// builder.or(taxonomy.currentTaxonomySpecies.id.in(grinSpecies));
// }
// }
// predicates.add(builder);
// }
//
// return predicates;
// }
}
......@@ -24,12 +24,11 @@ import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.model.genesys.QTaxonomy2;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.Taxonomy2Repository;
import org.genesys2.server.persistence.grin.TaxonomyGenusRepository;
import org.genesys2.server.persistence.grin.TaxonomySpeciesRepository;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.filter.TaxonomyFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -47,6 +46,7 @@ import org.springframework.transaction.annotation.Transactional;
import com.google.api.client.util.Lists;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
@Service
@Transactional(readOnly = true)
......@@ -63,7 +63,10 @@ public class TaxonomyServiceImpl implements TaxonomyService {
private TaxonomyGenusRepository grinGenusRepository;
@Autowired
private AccessionService accessionService;
private AccessionRepository accessionRepository;
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Override
public Taxonomy2 get(Long id) {
......@@ -226,16 +229,13 @@ public class TaxonomyServiceImpl implements TaxonomyService {
if (filter != null) {
predicate.and(filter.buildPredicate());
}
List<Taxonomy2> matches = jpaQueryFactory.selectFrom(QTaxonomy2.taxonomy2).where(predicate).offset(page.getOffset()).limit(page.getPageSize()).orderBy(
QTaxonomy2.taxonomy2.accessions.size().desc()).fetch();
final Page<Taxonomy2> res = taxonomy2Repository.findAll(predicate, page);
final List<Taxonomy2Info> content = res.getContent().stream().map(taxonomy2 -> {
AccessionFilter byTaxonomy2 = new AccessionFilter();
byTaxonomy2.taxa().id().add(taxonomy2.getId());
Long accessionCount = null;
try {
accessionCount = accessionService.countAccessions(byTaxonomy2);
} catch (SearchException e) {
LOG.warn("Error occurred during count", e);
}
final List<Taxonomy2Info> content = matches.stream().map(taxonomy2 -> {
Long accessionCount = accessionRepository.countByTaxonomy(taxonomy2);
return Taxonomy2Info.from(taxonomy2, accessionCount);
}).collect(Collectors.toList());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment