Commit d053a29f authored by Matija Obreza's avatar Matija Obreza

New Accession filter options: hasDoi, hasSubset, hasDataset

parent 4a7d8b6d
......@@ -119,17 +119,26 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
/** accession aliases */
public StringFilter aliases;
/** accessions with images */
public Boolean images;
/** has DOI assigned */
public Boolean hasDoi;
/** is in a Dataset */
public Boolean hasDataset;
/** is in a Subset */
public Boolean hasSubset;
/** The duplSite. */
public Set<String> duplSite;
/** Subset. */
@JsonDeserialize(contentUsing = UUIDDeserializer.class)
public Set<UUID> subsets;
/** Dataset. */
@JsonDeserialize(contentUsing = UUIDDeserializer.class)
public Set<UUID> datasets;
......@@ -140,14 +149,14 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
/** The partner. */
public Set<UUID> partner;
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.genesys.blocks.model.filters.BasicModelFilter#buildQuery()
*/
public List<Predicate> collectPredicates() {
return collectPredicates(QAccession.accession);
}
public <T> JPQLQuery<T> buildJpaQuery(JPQLQuery<T> query, QAccession accession) {
QAccessionId qAccessionId = accession.accessionId;
query.innerJoin(qAccessionId);
......@@ -193,7 +202,6 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
predicates.add(qDataset.uuid.in(datasets));
}
// Fields
if (historic != null) {
predicates.add(accession.historic.eq(historic));
......@@ -262,11 +270,34 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
predicates.add(accession.institute.id.in(partnerInstitutes));
}
if (hasDoi != null) {
if (hasDoi) {
predicates.add(accession.doi.isNotNull());
} else {
predicates.add(accession.doi.isNull());
}
}
if (hasSubset != null) {
if (hasSubset) {
predicates.add(qAccessionId.subsetCount.gt(0));
} else {
predicates.add(qAccessionId.subsetCount.eq(0l));
}
}
if (hasDataset != null) {
if (hasDataset) {
predicates.add(qAccessionId.datasetCount.gt(0));
} else {
predicates.add(qAccessionId.datasetCount.eq(0l));
}
}
query.where(ExpressionUtils.allOf(predicates));
return query;
}
/**
* Builds the query.
*
......@@ -373,6 +404,31 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
Set<Long> partnerInstitutes = getPartnerInstituteIds(partner);
predicates.add(accession.institute.id.in(partnerInstitutes));
}
if (hasDoi != null) {
if (hasDoi) {
predicates.add(accession.doi.isNotNull());
} else {
predicates.add(accession.doi.isNull());
}
}
if (hasSubset != null) {
if (hasSubset) {
predicates.add(accession.accessionId.subsetCount.gt(0));
} else {
predicates.add(accession.accessionId.subsetCount.eq(0l));
}
}
if (hasDataset != null) {
if (hasDataset) {
predicates.add(accession.accessionId.datasetCount.gt(0));
} else {
predicates.add(accession.accessionId.datasetCount.eq(0l));
}
}
return predicates;
}
......@@ -403,7 +459,7 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
* @return the institute filter
*/
public synchronized InstituteFilter holder() {
if (this.institute ==null) {
if (this.institute == null) {
return this.institute = new InstituteFilter();
} else {
return this.institute;
......
......@@ -63,12 +63,10 @@ public class AccessionGeoFilter extends BasicModelFilter<AccessionGeoFilter, Acc
final List<Predicate> predicates = super.collectPredicates(accessiongeo, accessiongeo._super);
if (referenced != null) {
if (referenced.booleanValue()) {
predicates.add(accessiongeo.longitude.isNotNull());
predicates.add(accessiongeo.latitude.isNotNull());
if (referenced) {
predicates.add(accessiongeo.longitude.isNotNull().and(accessiongeo.latitude.isNotNull()));
} else {
predicates.add(accessiongeo.longitude.isNull());
predicates.add(accessiongeo.latitude.isNull());
predicates.add(accessiongeo.isNull().or(accessiongeo.longitude.isNull()).or(accessiongeo.latitude.isNull()));
}
}
if (longitude != null) {
......
......@@ -50,10 +50,10 @@ import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Sets;
......@@ -1338,4 +1338,144 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
/*@formatter:on*/
}
@Test
public void filterAccessionsHasDoi() throws Exception {
ArrayNode accessions = upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, null);
String complexDoi = "10.25642/AAA1/GRAC/0005324";
accessions.add(upsertAccession(institute.getCode(), ACCENUMB_2, GENUS_2, complexDoi));
accessionUploader.upsertAccessions(institute, accessions);
// ensure we have 2 accessions in DB
assertEquals(2, accessionRepository.count());
AccessionFilter af = new AccessionFilter();
af.hasDoi = null;
/*@formatter:off*/
mockMvc
.perform(post(AccessionController.CONTROLLER_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(af)))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.totalElements", is(2)))
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content[0].accessionNumber", is(ACCENUMB_1)))
;
/*@formatter:on*/
af.hasDoi = false;
/*@formatter:off*/
mockMvc
.perform(post(AccessionController.CONTROLLER_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(af)))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.totalElements", is(1)))
.andExpect(jsonPath("$.content[0].accessionNumber", is(ACCENUMB_1)))
.andExpect(jsonPath("$.content[0].doi", is(nullValue())))
;
/*@formatter:on*/
af.hasDoi = true;
/*@formatter:off*/
mockMvc
.perform(post(AccessionController.CONTROLLER_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(af)))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.totalElements", is(1)))
.andExpect(jsonPath("$.content[0].accessionNumber", is(ACCENUMB_2)))
.andExpect(jsonPath("$.content[0].doi", is(complexDoi)))
;
/*@formatter:on*/
}
@Test
public void filterAccessionsGeoreferenced() throws Exception {
ArrayNode accessions = upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, null);
ObjectNode acceWithGeo = upsertAccession(institute.getCode(), ACCENUMB_2, GENUS_2, null);
acceWithGeo.set("geo", accessionGeo(13.4, 35.1, null));
accessions.add(acceWithGeo);
acceWithGeo = upsertAccession(institute.getCode(), ACCENUMB_2 + "a", GENUS_2, null);
acceWithGeo.set("geo", accessionGeo(null, 35.1, 300.0));
accessions.add(acceWithGeo);
accessionUploader.upsertAccessions(institute, accessions);
// ensure we have 2 accessions in DB
assertEquals(3, accessionRepository.count());
AccessionFilter af = new AccessionFilter();
/*@formatter:off*/
mockMvc
.perform(post(AccessionController.CONTROLLER_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(af)))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.totalElements", is(3)))
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content[0].accessionNumber", is(ACCENUMB_1)))
;
/*@formatter:on*/
af.geo().referenced = false;
/*@formatter:off*/
mockMvc
.perform(post(AccessionController.CONTROLLER_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(af)))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.totalElements", is(2)))
.andExpect(jsonPath("$.content[0].accessionNumber", is(ACCENUMB_1)))
.andExpect(jsonPath("$.content[0].geo", nullValue()))
.andExpect(jsonPath("$.content[1].geo").isMap())
.andExpect(jsonPath("$.content[1].geo.elevation", is(300.0)))
;
/*@formatter:on*/
af.geo().referenced = true;
/*@formatter:off*/
mockMvc
.perform(post(AccessionController.CONTROLLER_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(af)))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.totalElements", is(1)))
.andExpect(jsonPath("$.content[0].accessionNumber", is(ACCENUMB_2)))
.andExpect(jsonPath("$.content[0].geo").isMap())
.andExpect(jsonPath("$.content[0].geo.latitude", is(13.4)))
.andExpect(jsonPath("$.content[0].geo.longitude", is(35.1)))
;
/*@formatter:on*/
}
private JsonNode accessionGeo(Double latitude, Double longitude, Double elevation) {
ObjectNode geo = objectMapper.createObjectNode();
geo.put("latitude", latitude);
geo.put("longitude", longitude);
geo.put("elevation", elevation);
return geo;
}
}
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