Commit e9f6c56b authored by Maxym Borodenko's avatar Maxym Borodenko
Browse files

AccessionFilter: add data provider

parent baf97745
......@@ -16,6 +16,7 @@
package org.genesys.catalog.persistence;
import org.genesys.catalog.model.Partner;
import org.genesys2.server.model.impl.FaoInstitute;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
......@@ -23,6 +24,7 @@ import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Set;
import java.util.UUID;
/**
......@@ -64,4 +66,13 @@ public interface PartnerRepository extends JpaRepository<Partner, Long>, QueryDs
*/
@Query("select distinct p from Partner p where p.id in :ids")
List<Partner> loadListByIds(@Param("ids") List<Long> ids);
/**
* Load all institutes by partners uuids.
*
* @param uuids the uuids
* @return the list of institutes
*/
@Query("select distinct i from Partner p join p.institutes i where p.uuid in :uuids")
Set<FaoInstitute> findInstitutesInPartners(@Param("uuids") Set<UUID> uuids);
}
......@@ -95,6 +95,14 @@ public interface PartnerService {
*/
Page<FaoInstitute> loadPartnerInstitutes(UUID uuid, InstituteFilter filter, Pageable page);
/**
* Load partners institutes by uuids of partners.
*
* @param partnersUUIDs the list of partners UUIDs
* @return list of institutes
*/
Set<FaoInstitute> loadInstitutes(Set<UUID> partnersUUIDs);
/**
* Load partner.
*
......@@ -113,7 +121,7 @@ public interface PartnerService {
Partner loadPartner(Partner input);
/**
* /** Load page for Partners by current user.
* Load page for Partners by current user.
*
* @param filter the filter
* @param page page with parameters
......
......@@ -285,6 +285,11 @@ public class PartnerServiceImpl implements PartnerService, InitializingBean {
return instituteRepository.findAll(QFaoInstitute.faoInstitute.owner.uuid.eq(uuid).and(filter.buildPredicate()), page);
}
@Override
public Set<FaoInstitute> loadInstitutes(Set<UUID> partnersUUIDs) {
return partnerRepository.findInstitutesInPartners(partnersUUIDs);
}
/**
* {@inheritDoc}
*/
......
......@@ -28,9 +28,11 @@ import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.genesys.catalog.model.dataset.QDataset;
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.impl.FaoInstitute;
import org.genesys2.server.model.impl.QSubset;
import org.genesys2.server.service.PGRFANetworkService;
......@@ -131,6 +133,9 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
/** The networks. */
public Set<String> networks;
/** The partner. */
public Set<UUID> partner;
/* (non-Javadoc)
* @see org.genesys.blocks.model.filters.BasicModelFilter#buildQuery()
......@@ -245,6 +250,10 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
Set<Long> networkMembers = getNetworkMemberIds(networks);
predicates.add(accession.institute.id.in(networkMembers));
}
if (CollectionUtils.isNotEmpty(partner)) {
Set<Long> partnerInstitutes = getPartnerInstituteIds(partner);
predicates.add(accession.institute.id.in(partnerInstitutes));
}
query.where(ExpressionUtils.allOf(predicates));
return query;
......@@ -350,12 +359,21 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
Set<Long> networkMembers = getNetworkMemberIds(networks);
predicates.add(accession.institute.id.in(networkMembers));
}
if (CollectionUtils.isNotEmpty(partner)) {
Set<Long> partnerInstitutes = getPartnerInstituteIds(partner);
predicates.add(accession.institute.id.in(partnerInstitutes));
}
return predicates;
}
private Set<Long> getNetworkMemberIds(Set<String> slugs) {
PGRFANetworkService networkService = CurrentApplicationContext.getContext().getBean(PGRFANetworkService.class);
return networkService.getMembers(slugs).stream().map(faoInstitute -> faoInstitute.getId()).collect(Collectors.toSet());
return networkService.getMembers(slugs).stream().map(FaoInstitute::getId).collect(Collectors.toSet());
}
private Set<Long> getPartnerInstituteIds(Set<UUID> uuids) {
PartnerService partnerService = CurrentApplicationContext.getContext().getBean(PartnerService.class);
return partnerService.loadInstitutes(uuids).stream().map(FaoInstitute::getId).collect(Collectors.toSet());
}
/**
......
......@@ -24,6 +24,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.RandomUtils;
import org.genesys.catalog.service.PartnerService;
import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionGeo;
......@@ -77,6 +78,8 @@ public abstract class AbstractAccessionControllerTest extends AbstractApiTest {
protected TaxonomyService taxonomyService;
@Autowired
protected AccessionUploader accessionUploader;
@Autowired
protected PartnerService partnerService;
protected FaoInstitute institute;
protected AtomicInteger acceNumb = new AtomicInteger(1);
......
......@@ -34,6 +34,7 @@ import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.commons.lang3.RandomUtils;
import org.genesys.catalog.model.Partner;
import org.genesys.test.server.api.AbstractAccessionControllerTest;
import org.genesys2.server.api.v1.AccessionController;
import org.genesys2.server.api.v1.AccessionUploadController;
......@@ -117,6 +118,36 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
assertNotNull(accessionRepository.findByInstituteCodeAndAccessionNumber(institute.getCode(), ACCENUMB_2));
}
@Test
public void filterAccessionsByPartnerTest() throws Exception {
Partner partner = setUpPartner("Partner", true, "Partner", UUID.randomUUID());
partner = partnerService.addInstitutes(partner, Sets.newHashSet(institute.getCode()));
ArrayNode arrayNode = verboseMapper.createArrayNode();
arrayNode.add(upsertAccession(institute.getCode(), ACCENUMB_1, GENUS_1, null));
accessionUploader.upsertAccessions(institute, arrayNode);
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_2, GENUS_2, null));
AccessionFilter filter = new AccessionFilter();
filter.partner = Sets.newHashSet(partner.getUuid());
/*@formatter:off*/
mockMvc
.perform(post(AccessionController.CONTROLLER_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(verboseMapper.writeValueAsString(filter)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.totalElements", is(2)))
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content[0].instituteCode", is(institute.getCode())))
.andExpect(jsonPath("$.content[1].instituteCode", is(institute.getCode())))
;
/*@formatter:on*/
}
@Test
public void taxonomyFilterTest() throws Exception {
ObjectNode accessionJson = setUpAccession();
......
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