Commit 2c685b04 authored by Matija Obreza's avatar Matija Obreza Committed by Maxym Borodenko
Browse files

AccessionFilter: prevent cross-joins with taxonomy tables

parent 95b33002
......@@ -34,6 +34,7 @@ import org.genesys.catalog.service.PartnerService;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.genesys.QAccessionId;
import org.genesys2.server.model.genesys.QTaxonomy2;
import org.genesys2.server.model.impl.DiversityTreeAccessionRef;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.QDiversityTree;
......@@ -175,8 +176,14 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
final List<Predicate> predicates = super.collectPredicates(accession, accession._super._super);
if (taxonomy != null) {
query.innerJoin(accession.taxonomy);
predicates.addAll(taxonomy.collectPredicates(accession.taxonomy));
QTaxonomy2 taxonomyJoin = new QTaxonomy2("T");
query.innerJoin(accession.taxonomy, taxonomyJoin);
// Prevent cross-joins
query.leftJoin(taxonomyJoin.grinTaxonomySpecies);
query.leftJoin(taxonomyJoin.currentTaxonomySpecies);
predicates.addAll(taxonomy.collectPredicates(taxonomyJoin));
}
if (crop != null && !crop.isEmpty()) {
query.leftJoin(accession.crop);
......
......@@ -29,6 +29,7 @@ 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.
......@@ -133,4 +134,73 @@ 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;
// }
}
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