Commit ec50cbc1 authored by Matija Obreza's avatar Matija Obreza

Accession filtering by subset and dataset UUIDs

parent e0847780
......@@ -15,8 +15,6 @@
*/
package org.genesys.catalog.model.filters;
import static org.genesys.catalog.model.dataset.QDataset.dataset;
import java.util.HashSet;
import java.util.Set;
......@@ -24,6 +22,7 @@ import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.QDataset;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.service.filter.IFullTextFilter;
......@@ -72,6 +71,7 @@ public class DatasetFilter extends UuidModelFilter<DatasetFilter, Dataset> imple
/** Is current version */
public Boolean current;
/**
* Builds the query.
......@@ -79,6 +79,15 @@ public class DatasetFilter extends UuidModelFilter<DatasetFilter, Dataset> imple
* @return the predicate
*/
public Predicate buildPredicate() {
return buildQuery(QDataset.dataset);
}
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildQuery(QDataset dataset) {
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(dataset, dataset._super, and);
......
......@@ -69,6 +69,12 @@ public class ElasticQueryBuilder implements Visitor<Void, Void> {
if (path.equals("accessionId.lists.uuid")) {
// Example of @JsonIdentityReference(alwaysAsId = true)
return "lists";
} else if (path.equals("datasets.uuid")) {
// Example of @JsonIdentityReference(alwaysAsId = true)
return "datasets";
} else if (path.equals("subsets.uuid")) {
// Example of @JsonIdentityReference(alwaysAsId = true)
return "subsets";
} else if (path.startsWith("accessionId.")) {
// Example of @JsonUnwrapped
return path.replace("accessionId.", "");
......
......@@ -16,13 +16,30 @@
package org.genesys2.server.model.genesys;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys2.server.model.impl.Subset;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
import org.springframework.data.elasticsearch.annotations.FieldType;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
/**
* Active accession records with all possible constraints.
......@@ -53,4 +70,19 @@ public class Accession extends AccessionData {
*/
private static final long serialVersionUID = -4847875217506974494L;
@ManyToMany(fetch = FetchType.LAZY, cascade = {})
@JoinTable(name = "dataset_accessions", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "datasetId", referencedColumnName = "id"))
@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
@JsonView({ JsonViews.Indexed.class })
@JsonIdentityReference(alwaysAsId = true)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid", scope = Dataset.class)
private List<Dataset> datasets;
@ManyToMany(fetch = FetchType.LAZY, cascade = {})
@JoinTable(name = "subset_accessions", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "subsetId", referencedColumnName = "id"))
@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
@JsonView({ JsonViews.Indexed.class })
@JsonIdentityReference(alwaysAsId = true)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid", scope = Subset.class)
private List<Subset> subsets;
}
......@@ -115,7 +115,13 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
/** accessions with images */
public Boolean images;
/** Subset. */
public Set<UUID> subsets;
/** Dataset. */
public Set<UUID> datasets;
/* (non-Javadoc)
* @see org.genesys.blocks.model.filters.BasicModelFilter#buildQuery()
*/
......@@ -225,6 +231,13 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
and.and(qAccessionId.imageCount.eq(0));
}
}
if (CollectionUtil.isNotEmpty(subsets)) {
and.and(accession.subsets.any().uuid.in(subsets));
}
if (CollectionUtil.isNotEmpty(datasets)) {
and.and(accession.datasets.any().uuid.in(datasets));
}
query.where(and);
return query;
......@@ -322,6 +335,12 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
and.and(accession.accessionId.imageCount.eq(0));
}
}
if (subsets != null) {
and.and(accession.subsets.any().uuid.in(subsets));
}
if (datasets != null) {
and.and(accession.datasets.any().uuid.in(datasets));
}
return and;
}
......
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.service.filter;
import org.elasticsearch.index.query.QueryBuilder;
/**
* Filters able to generate Elasticsearch queries.
*/
public interface ElasticsearchFilter {
/**
* Elastic query.
*
* @return the query builder
*/
public QueryBuilder elasticQuery();
}
......@@ -15,8 +15,6 @@
*/
package org.genesys2.server.service.filter;
import static org.genesys2.server.model.impl.QSubset.subset;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
......@@ -26,6 +24,7 @@ import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys.catalog.model.filters.PartnerFilter;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.impl.QSubset;
import org.genesys2.server.model.impl.Subset;
import com.querydsl.core.BooleanBuilder;
......@@ -74,6 +73,14 @@ public class SubsetFilter extends UuidModelFilter<SubsetFilter, Subset> implemen
* @return the predicate
*/
public Predicate buildPredicate() {
return buildQuery(QSubset.subset);
}
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildQuery(QSubset subset) {
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(subset, subset._super, and);
......
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