Commit 9d1b9f5f authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '296-filtering-by-inventory-group' into 'main'

Resolve "Filtering by inventory group"

Closes #296

See merge request grin-global/grin-global-server!393
parents c2d5be57 09a8331c
...@@ -335,6 +335,10 @@ public class Inventory extends CooperatorOwnedModel implements Copyable<Inventor ...@@ -335,6 +335,10 @@ public class Inventory extends CooperatorOwnedModel implements Copyable<Inventor
@JsonIgnoreProperties({ "ownedBy", "inventory" }) @JsonIgnoreProperties({ "ownedBy", "inventory" })
private List<AccessionInvName> names; private List<AccessionInvName> names;
@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "inventory")
@JsonIgnore
private List<AccessionInvGroupMap> accessionInvGroupMaps;
@Basic @Basic
private Long generation; private Long generation;
......
...@@ -21,10 +21,14 @@ import org.springframework.data.jpa.repository.JpaRepository; ...@@ -21,10 +21,14 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor; import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
@Repository @Repository
public interface AccessionInvGroupMapRepository extends JpaRepository<AccessionInvGroupMap, Long>, QuerydslPredicateExecutor<AccessionInvGroupMap> { public interface AccessionInvGroupMapRepository extends JpaRepository<AccessionInvGroupMap, Long>, QuerydslPredicateExecutor<AccessionInvGroupMap> {
/// Delete members of inventory group /// Delete members of inventory group
int deleteByAccessionInvGroup(AccessionInvGroup entity); int deleteByAccessionInvGroup(AccessionInvGroup entity);
List<AccessionInvGroupMap> findAllByAccessionInvGroup_IdIn(Collection<Long> ids);
} }
...@@ -15,19 +15,23 @@ ...@@ -15,19 +15,23 @@
*/ */
package org.gringlobal.service.filter; package org.gringlobal.service.filter;
import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.DateFilter; import org.genesys.blocks.model.filters.DateFilter;
import org.genesys.blocks.model.filters.NumberFilter; import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter; import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.gringlobal.model.Accession; import org.gringlobal.model.Accession;
import org.gringlobal.model.QAccession; import org.gringlobal.model.QAccession;
import org.gringlobal.model.QAccessionSource; import org.gringlobal.model.QAccessionSource;
import com.querydsl.core.types.Predicate; import com.querydsl.core.types.Predicate;
import org.gringlobal.persistence.AccessionInvGroupMapRepository;
/** /**
* Filters for {@link Accession} * Filters for {@link Accession}
...@@ -108,6 +112,9 @@ public class AccessionFilter extends CooperatorOwnedModelFilter<AccessionFilter, ...@@ -108,6 +112,9 @@ public class AccessionFilter extends CooperatorOwnedModelFilter<AccessionFilter,
/** Site */ /** Site */
public SiteFilter site; public SiteFilter site;
/** The Accession inventory group ids. */
public Set<Long> accessionInvGroup;
/** /**
* Builds the query. * Builds the query.
* *
...@@ -194,10 +201,20 @@ public class AccessionFilter extends CooperatorOwnedModelFilter<AccessionFilter, ...@@ -194,10 +201,20 @@ public class AccessionFilter extends CooperatorOwnedModelFilter<AccessionFilter,
var qAccessionSource = new QAccessionSource(accession.accessionSources.getMetadata().getName()); // alias MUST match accession.accessionSources path var qAccessionSource = new QAccessionSource(accession.accessionSources.getMetadata().getName()); // alias MUST match accession.accessionSources path
predicates.add(nestedFilter(accession.accessionSources, qAccessionSource, qAccessionSource.accession.eq(accession), sources.collectPredicates(qAccessionSource))); predicates.add(nestedFilter(accession.accessionSources, qAccessionSource, qAccessionSource.accession.eq(accession), sources.collectPredicates(qAccessionSource)));
} }
if (accessionInvGroup != null) {
predicates.add(accession.inventories.any().id.in(getInventoryMembersOfGroups(accessionInvGroup)));
}
return predicates; return predicates;
} }
private List<Long> getInventoryMembersOfGroups(Collection<Long> ids) {
var repository = CurrentApplicationContext.getContext().getBean(AccessionInvGroupMapRepository.class);
return repository.findAllByAccessionInvGroup_IdIn(ids).stream()
.map(groupMap -> groupMap.getInventory().getId())
.collect(Collectors.toList());
}
/** /**
* Gets the text. * Gets the text.
* *
......
...@@ -173,6 +173,9 @@ public class InventoryFilter extends CooperatorOwnedModelFilter<InventoryFilter, ...@@ -173,6 +173,9 @@ public class InventoryFilter extends CooperatorOwnedModelFilter<InventoryFilter,
/** The web availability note. */ /** The web availability note. */
public StringFilter webAvailabilityNote; public StringFilter webAvailabilityNote;
/** The accession inventory group id. */
public Set<Long> accessionInvGroup;
/** /**
* Builds the query. * Builds the query.
* *
...@@ -327,6 +330,9 @@ public class InventoryFilter extends CooperatorOwnedModelFilter<InventoryFilter, ...@@ -327,6 +330,9 @@ public class InventoryFilter extends CooperatorOwnedModelFilter<InventoryFilter,
if (webAvailabilityNote != null) { if (webAvailabilityNote != null) {
predicates.add(webAvailabilityNote.buildQuery(inventory.webAvailabilityNote)); predicates.add(webAvailabilityNote.buildQuery(inventory.webAvailabilityNote));
} }
if (CollectionUtils.isNotEmpty(accessionInvGroup)) {
predicates.add(inventory.accessionInvGroupMaps.any().accessionInvGroup.id.in(accessionInvGroup));
}
return predicates; return predicates;
} }
......
...@@ -42,6 +42,7 @@ import org.gringlobal.model.community.CommunityCodeValues; ...@@ -42,6 +42,7 @@ import org.gringlobal.model.community.CommunityCodeValues;
import org.gringlobal.persistence.AccessionInvGroupMapRepository; import org.gringlobal.persistence.AccessionInvGroupMapRepository;
import org.gringlobal.persistence.AccessionInvGroupRepository; import org.gringlobal.persistence.AccessionInvGroupRepository;
import org.gringlobal.persistence.AccessionSourceMapRepository; import org.gringlobal.persistence.AccessionSourceMapRepository;
import org.gringlobal.service.AccessionInvGroupService;
import org.gringlobal.service.AccessionService; import org.gringlobal.service.AccessionService;
import org.gringlobal.service.AccessionService.AcquisitionData; import org.gringlobal.service.AccessionService.AcquisitionData;
import org.gringlobal.service.AccessionSourceMapService; import org.gringlobal.service.AccessionSourceMapService;
...@@ -82,6 +83,9 @@ public class AccessionControllerTest extends AbstractApiV1Test { ...@@ -82,6 +83,9 @@ public class AccessionControllerTest extends AbstractApiV1Test {
@Autowired @Autowired
private AccessionSourceMapService accessionSourceMapService; private AccessionSourceMapService accessionSourceMapService;
@Autowired
private AccessionInvGroupService accessionInvGroupService;
@After @After
@Transactional @Transactional
public void cleanup() throws Exception { public void cleanup() throws Exception {
...@@ -682,6 +686,79 @@ public class AccessionControllerTest extends AbstractApiV1Test { ...@@ -682,6 +686,79 @@ public class AccessionControllerTest extends AbstractApiV1Test {
/*@formatter:on*/ /*@formatter:on*/
} }
@Test
public void testAccessionsListByAccessionInvGroup() throws Exception {
// build accessions
Accession accession = addAccessionToDB();
Accession accession2 = addAccessionToDB();
Inventory inventory = addInventoryToDB(accession);
AccessionInvGroup accessionInvGroup = new AccessionInvGroup();
accessionInvGroup.setGroupName("TestGroup");
accessionInvGroup = accessionInvGroupService.create(accessionInvGroup);
assertThat(accessionInvGroupRepository.count(), Matchers.is(1L));
assertThat(accessionInvGroup.getNumberOfItems(), Matchers.is(nullValue()));
accessionInvGroupService.addMembers(accessionInvGroup, List.of(inventory), "test note");
assertThat(accessionInvGroupMapRepository.count(), Matchers.is(1L));
accessionInvGroup = accessionInvGroupService.reload(accessionInvGroup);
assertThat(accessionInvGroup.getNumberOfItems(), Matchers.is(1));
Inventory inventory2 = addInventoryToDB(accession2);
AccessionInvGroup accessionInvGroup2 = new AccessionInvGroup();
accessionInvGroup2.setGroupName("TestGroup2");
accessionInvGroup2 = accessionInvGroupService.create(accessionInvGroup2);
assertThat(accessionInvGroupRepository.count(), Matchers.is(2L));
assertThat(accessionInvGroup2.getNumberOfItems(), Matchers.is(nullValue()));
accessionInvGroupService.addMembers(accessionInvGroup2, List.of(inventory2), "test note2");
assertThat(accessionInvGroupMapRepository.count(), Matchers.is(2L));
accessionInvGroup2 = accessionInvGroupService.reload(accessionInvGroup2);
assertThat(accessionInvGroup2.getNumberOfItems(), Matchers.is(1));
AccessionFilter filter = new AccessionFilter();
filter.accessionInvGroup = Set.of(accessionInvGroup.getId());
/*@formatter:off*/
mockMvc
.perform(post(AccessionController.API_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(verboseMapper.writeValueAsString(filter))
)
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.totalElements", is(1)))
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content[0].id", is(accession.getId().intValue())))
;
/*@formatter:on*/
filter = new AccessionFilter();
filter.accessionInvGroup = Set.of(accessionInvGroup2.getId());
/*@formatter:off*/
mockMvc
.perform(post(AccessionController.API_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(verboseMapper.writeValueAsString(filter))
)
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.totalElements", is(1)))
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content[0].id", is(accession2.getId().intValue())))
;
/*@formatter:on*/
}
@Test @Test
public void testAccessionsListAsCSV() throws Exception { public void testAccessionsListAsCSV() throws Exception {
......
...@@ -169,6 +169,72 @@ public class InventoryControllerTest extends AbstractApiV1Test { ...@@ -169,6 +169,72 @@ public class InventoryControllerTest extends AbstractApiV1Test {
/*@formatter:on*/ /*@formatter:on*/
} }
@Test
public void filterInventoryByAccessionInvGroup() throws Exception {
Inventory inventory = addInventoryToDB(addAccessionToDB());
AccessionInvGroup accessionInvGroup = new AccessionInvGroup();
accessionInvGroup.setGroupName("TestGroup");
accessionInvGroup = accessionInvGroupService.create(accessionInvGroup);
assertThat(accessionInvGroupRepository.count(), is(1L));
assertThat(accessionInvGroup.getNumberOfItems(), is(nullValue()));
accessionInvGroupService.addMembers(accessionInvGroup, List.of(inventory), "test note");
assertThat(accessionInvGroupMapRepository.count(), is(1L));
accessionInvGroup = accessionInvGroupService.reload(accessionInvGroup);
assertThat(accessionInvGroup.getNumberOfItems(), is(1));
Inventory inventory2 = addInventoryToDB(addAccessionToDB());
AccessionInvGroup accessionInvGroup2 = new AccessionInvGroup();
accessionInvGroup2.setGroupName("TestGroup2");
accessionInvGroup2 = accessionInvGroupService.create(accessionInvGroup2);
assertThat(accessionInvGroupRepository.count(), is(2L));
assertThat(accessionInvGroup2.getNumberOfItems(), is(nullValue()));
accessionInvGroupService.addMembers(accessionInvGroup2, List.of(inventory2), "test note2");
assertThat(accessionInvGroupMapRepository.count(), is(2L));
accessionInvGroup2 = accessionInvGroupService.reload(accessionInvGroup2);
assertThat(accessionInvGroup2.getNumberOfItems(), is(1));
InventoryFilter filter = new InventoryFilter();
filter.accessionInvGroup = Set.of(accessionInvGroup.getId());
/*@formatter:off*/
mockMvc
.perform(post(InventoryController.API_URL + "/filter")
.contentType(MediaType.APPLICATION_JSON)
.characterEncoding("UTF8")
.content(clientMapper.writeValueAsString(filter)))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.content", hasSize(1)))
.andExpect(jsonPath("$.content[0].id", is(inventory.getId().intValue())))
;
/*@formatter:on*/
filter = new InventoryFilter();
filter.accessionInvGroup = Set.of(accessionInvGroup2.getId());
/*@formatter:off*/
mockMvc
.perform(post(InventoryController.API_URL + "/filter")
.contentType(MediaType.APPLICATION_JSON)
.characterEncoding("UTF8")
.content(clientMapper.writeValueAsString(filter)))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.content", hasSize(1)))
.andExpect(jsonPath("$.content[0].id", is(inventory2.getId().intValue())))
;
/*@formatter:on*/
}
@Test @Test
public void filterInventorySingleValueToArrayTest() throws Exception { public void filterInventorySingleValueToArrayTest() throws Exception {
/*@formatter:off*/ /*@formatter:off*/
......
Supports Markdown
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