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

Taxonomy2: results of /taxonomy/list contain accessionCount

parent d0401c8e
......@@ -77,14 +77,14 @@ public class TaxonomyController extends ApiBaseController {
}
/**
* List taxonomies.
* List of Taxonomy2 entities with included count of accessions.
*
* @param page the page
* @param filter the taxonomy2 filter
* @return the page
*/
@PostMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<Taxonomy2> listTaxonomies(final Pagination page, @RequestBody final TaxonomyFilter filter) {
public FilteredPage<TaxonomyService.Taxonomy2Info> listTaxonomies(final Pagination page, @RequestBody final TaxonomyFilter filter) {
return new FilteredPage<>(filter, taxonomyService.list(filter, page.toPageRequest(100, Sort.Direction.ASC, "genus", "species", "id")));
}
}
......@@ -76,14 +76,14 @@ public class TaxonomyController extends ApiBaseController {
}
/**
* List taxonomies.
* List of Taxonomy2 entities with included count of accessions.
*
* @param page the page
* @param filter the taxonomy2 filter
* @return the page
*/
@PostMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<Taxonomy2> listTaxonomies(final Pagination page, @RequestBody final TaxonomyFilter filter) {
public FilteredPage<TaxonomyService.Taxonomy2Info> listTaxonomies(final Pagination page, @RequestBody final TaxonomyFilter filter) {
return new FilteredPage<>(filter, taxonomyService.list(filter, page.toPageRequest(100, Sort.Direction.ASC, "genus", "species", "id")));
}
}
......@@ -16,8 +16,10 @@
package org.genesys2.server.service;
import java.io.Serializable;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.service.filter.TaxonomyFilter;
import org.springframework.data.domain.Page;
......@@ -45,7 +47,7 @@ public interface TaxonomyService {
List<Taxonomy2> list(TaxonomyFilter filter);
Page<Taxonomy2> list(TaxonomyFilter filter, Pageable page);
Page<Taxonomy2Info> list(TaxonomyFilter filter, Pageable page);
List<String> getAllGenera();
......@@ -65,4 +67,19 @@ public interface TaxonomyService {
List<Long> findGrinGenusId(List<String> genera);
List<Long> findGrinSpeciesId(List<String> speciesNames);
public static class Taxonomy2Info implements Serializable {
private static final long serialVersionUID = 2114693656081783236L;
@JsonUnwrapped
public Taxonomy2 taxonomy;
public Long accessionCount;
public static Taxonomy2Info from(Taxonomy2 taxonomy, Long accessionCount) {
Taxonomy2Info taxonomy2Info = new Taxonomy2Info();
taxonomy2Info.taxonomy = taxonomy;
taxonomy2Info.accessionCount = accessionCount;
return taxonomy2Info;
}
}
}
......@@ -19,6 +19,7 @@ package org.genesys2.server.service.impl;
import java.math.BigInteger;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.model.genesys.QTaxonomy2;
......@@ -26,7 +27,9 @@ import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.persistence.Taxonomy2Repository;
import org.genesys2.server.persistence.grin.TaxonomyGenusRepository;
import org.genesys2.server.persistence.grin.TaxonomySpeciesRepository;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.filter.TaxonomyFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -36,6 +39,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
......@@ -58,6 +62,9 @@ public class TaxonomyServiceImpl implements TaxonomyService {
@Autowired
private TaxonomyGenusRepository grinGenusRepository;
@Autowired
private AccessionService accessionService;
@Override
public Taxonomy2 get(Long id) {
return taxonomy2Repository.findById(id).orElse(null);
......@@ -213,13 +220,26 @@ public class TaxonomyServiceImpl implements TaxonomyService {
}
@Override
public Page<Taxonomy2> list(TaxonomyFilter filter, Pageable page) {
@PreAuthorize("hasRole('ADMINISTRATOR')")
public Page<Taxonomy2Info> list(TaxonomyFilter filter, Pageable page) {
final BooleanBuilder predicate = new BooleanBuilder();
if (filter != null) {
predicate.and(filter.buildPredicate());
}
Page<Taxonomy2> res = taxonomy2Repository.findAll(predicate, page);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
final Page<Taxonomy2> res = taxonomy2Repository.findAll(predicate, page);
final List<Taxonomy2Info> content = res.getContent().stream().map(taxonomy2 -> {
AccessionFilter byTaxonomy2 = new AccessionFilter();
byTaxonomy2.taxa().id().add(taxonomy2.getId());
Long accessionCount = null;
try {
accessionCount = accessionService.countAccessions(byTaxonomy2);
} catch (SearchException e) {
LOG.warn("Error occurred during count", e);
}
return Taxonomy2Info.from(taxonomy2, accessionCount);
}).collect(Collectors.toList());
return new PageImpl<>(content, page, res.getTotalElements());
}
@Override
......
......@@ -58,6 +58,7 @@ import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.model.impl.SubsetCreator;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService;
import org.junit.After;
import org.junit.Before;
......@@ -122,6 +123,8 @@ public abstract class AbstractApiTest extends AbstractTest {
protected TaxonomyService taxonomyService;
@Autowired
protected AccessionRepository accessionRepository;
@Autowired
protected InstituteService instituteService;
private AtomicInteger acceNumb = new AtomicInteger(1);
......@@ -241,6 +244,14 @@ public abstract class AbstractApiTest extends AbstractTest {
return partnerRepository.save(partner);
}
protected FaoInstitute setUpInstitute() {
FaoInstitute institute = new FaoInstitute();
institute.setCode("INS001");
institute.setFullName("An institute");
instituteService.update(Lists.newArrayList(institute));
return instituteService.getInstitute("INS001");
}
protected Descriptor setUpDescriptor() {
final Descriptor descriptor = new Descriptor();
......
......@@ -24,7 +24,6 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import org.genesys.catalog.model.dataset.DatasetAccessionRef;
import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.model.genesys.Accession;
......@@ -32,7 +31,6 @@ import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.service.InstituteService;
import org.junit.After;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
......@@ -44,8 +42,6 @@ public abstract class AbstractDatasetControllerTest extends AbstractApiTest {
@Autowired
protected AccessionRepository accessionRepository;
@Autowired
protected InstituteService instituteService;
@Override
public void beforeTest() throws Exception {
......@@ -71,14 +67,6 @@ public abstract class AbstractDatasetControllerTest extends AbstractApiTest {
return accessionRefs;
}
protected FaoInstitute setUpInstitute() {
FaoInstitute institute = new FaoInstitute();
institute.setCode("INS001");
institute.setFullName("An institute");
instituteService.update(Lists.newArrayList(institute));
return instituteService.getInstitute("INS001");
}
protected Set<DatasetAccessionRef> setUpAccessions(FaoInstitute institute) {
final List<Accession> accessions = new ArrayList<>(2);
accessions.add(setUpAccession(institute));
......
......@@ -16,6 +16,9 @@
package org.genesys.test.server.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.persistence.Taxonomy2Repository;
......@@ -25,9 +28,6 @@ import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Maxym Borodenko
*/
......@@ -55,6 +55,8 @@ public abstract class AbstractTaxonomyControllerTest extends AbstractApiTest {
@Override
@Transactional
public void cleanup() throws Exception {
accessionRepository.deleteAll();
accessionIdRepository.deleteAll();
super.cleanup();
taxonomyRepository.deleteAll();
}
......
......@@ -27,6 +27,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import com.google.common.collect.Sets;
import org.genesys.test.server.api.AbstractTaxonomyControllerTest;
import org.genesys2.server.api.v1.TaxonomyController;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.service.filter.TaxonomyFilter;
import org.junit.Test;
......@@ -64,9 +65,11 @@ public class TaxonomyControllerTest extends AbstractTaxonomyControllerTest {
@Test
public void listByFilterTest() throws Exception {
assertThat(taxonomyRepository.count(), is(2L));
Accession accession = accessionRepository.save(setUpAccession(setUpInstitute()));
assertThat(accessionRepository.count(), is(1L));
TaxonomyFilter filter = new TaxonomyFilter();
filter.genus = Sets.newHashSet(GENUS_1);
filter.genus = Sets.newHashSet(accession.getTaxonomy().getGenus());
/*@formatter:off*/
mockMvc
......@@ -82,9 +85,11 @@ public class TaxonomyControllerTest extends AbstractTaxonomyControllerTest {
.andExpect(jsonPath("$.sort[0].direction", is("ASC")))
.andExpect(jsonPath("$.totalElements", is(1)))
.andExpect(jsonPath("$.totalPages", is(1)))
.andExpect(jsonPath("$.content[0].genus", is(GENUS_1)))
.andExpect(jsonPath("$.content[0].genus", is(accession.getTaxonomy().getGenus())))
.andExpect(jsonPath("$.content[0].grinTaxonomySpecies").hasJsonPath())
.andExpect(jsonPath("$.content[0].currentTaxonomySpecies").hasJsonPath())
.andExpect(jsonPath("$.content[0].accessionCount").hasJsonPath())
.andExpect(jsonPath("$.content[0].accessionCount", is(1)))
;
/*@formatter:on*/
}
......
......@@ -27,6 +27,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import com.google.common.collect.Sets;
import org.genesys.test.server.api.AbstractTaxonomyControllerTest;
import org.genesys2.server.api.v1.TaxonomyController;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.service.filter.TaxonomyFilter;
import org.junit.Test;
......@@ -64,9 +65,11 @@ public class TaxonomyControllerTest extends AbstractTaxonomyControllerTest {
@Test
public void listByFilterTest() throws Exception {
assertThat(taxonomyRepository.count(), is(2L));
Accession accession = accessionRepository.save(setUpAccession(setUpInstitute()));
assertThat(accessionRepository.count(), is(1L));
TaxonomyFilter filter = new TaxonomyFilter();
filter.genus = Sets.newHashSet(GENUS_1);
filter.genus = Sets.newHashSet(accession.getTaxonomy().getGenus());
/*@formatter:off*/
mockMvc
......@@ -82,9 +85,11 @@ public class TaxonomyControllerTest extends AbstractTaxonomyControllerTest {
.andExpect(jsonPath("$.sort[0].direction", is("ASC")))
.andExpect(jsonPath("$.totalElements", is(1)))
.andExpect(jsonPath("$.totalPages", is(1)))
.andExpect(jsonPath("$.content[0].genus", is(GENUS_1)))
.andExpect(jsonPath("$.content[0].genus", is(accession.getTaxonomy().getGenus())))
.andExpect(jsonPath("$.content[0].grinTaxonomySpecies").hasJsonPath())
.andExpect(jsonPath("$.content[0].currentTaxonomySpecies").hasJsonPath())
.andExpect(jsonPath("$.content[0].accessionCount").hasJsonPath())
.andExpect(jsonPath("$.content[0].accessionCount", is(1)))
;
/*@formatter:on*/
}
......
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