Commit d6cde111 authored by Matija Obreza's avatar Matija Obreza

Permission checks for Subset/Dataset accession lists

parent bbb6c9e9
......@@ -118,13 +118,13 @@ public interface DatasetService {
Dataset loadDataset(UUID uuid, int version);
/**
* Load AccessionRef list by Dataset
* Load AccessionRef list by Dataset.
*
* @param uuid uuid of Dataset
* @param dataset the dataset
* @param page Pageable
* @return PageImpl of AccessionRef
*/
Page<AccessionRef> listAccessions(UUID uuid, Pageable page);
Page<AccessionRef> listAccessions(Dataset dataset, Pageable page);
/**
* List published datasets by accession.
......
......@@ -34,6 +34,7 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys.blocks.security.service.CustomAclService;
......@@ -382,18 +383,20 @@ public class DatasetServiceImpl implements DatasetService {
* {@inheritDoc}
*/
@Override
public Page<AccessionRef> listAccessions(final UUID uuid, final Pageable page) {
final Dataset dataset = datasetRepository.findByUuid(uuid);
if (dataset == null) {
throw new NotFoundElement("Dataset not found by UUID=" + uuid.toString());
}
final Set<AccessionRef> accessionRefs = dataset.getAccessionRefs();
if (accessionRefs != null && !accessionRefs.isEmpty()) {
int fromIndex = Math.multiplyExact(page.getPageNumber(), page.getPageSize());
int toIndex = Math.min(fromIndex + page.getPageSize(), accessionRefs.size());
return new PageImpl<>(new ArrayList<AccessionRef>(accessionRefs).subList(fromIndex, toIndex), page, accessionRefs.size());
@PreAuthorize("hasRole('ADMINISTRATOR') || dataset.published || hasPermission(dataset, 'read')")
public Page<AccessionRef> listAccessions(Dataset dataset, final Pageable page) {
dataset = loadDataset(dataset);
final List<AccessionRef> accessionRefs = new ArrayList<AccessionRef>(dataset.getAccessionRefs());
if (CollectionUtils.isEmpty(accessionRefs)) {
return new PageImpl<>(Collections.emptyList(), page, 0);
}
return new PageImpl<>(Collections.emptyList(), page, 0);
accessionRefs.sort((a, b) -> a.getAcceNumb().compareTo(b.getAcceNumb()));
int fromIndex = Math.multiplyExact(page.getPageNumber(), page.getPageSize());
int toIndex = Math.min(fromIndex + page.getPageSize(), accessionRefs.size());
return new PageImpl<>(accessionRefs.subList(fromIndex, toIndex), page, accessionRefs.size());
}
/**
......
......@@ -387,7 +387,7 @@ public class AccessionController {
public String[] tileServers;
}
private static class AccessionOverview {
public static class AccessionOverview {
public String filterCode;
public AccessionFilter filter;
public Map<String, TermResult> overview;
......
......@@ -46,13 +46,13 @@ public interface SubsetService {
Page<Subset> list(SubsetFilter filter, Pageable page);
/**
* Load AccessionRef list by Subset
* Load AccessionRef list by Subset.
*
* @param uuid uuid of Subset
* @param subset the subset
* @param page Pageable
* @return PageImpl of AccessionRef
*/
Page<AccessionRef> listAccessions(UUID uuid, Pageable page);
Page<AccessionRef> listAccessions(Subset subset, Pageable page);
/**
* Count published subsets.
......
......@@ -27,6 +27,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.service.DatasetService;
......@@ -186,14 +187,23 @@ public class AccessionServiceImpl implements AccessionService {
@Override
public Map<UUID, AccessionIdentifier3> toUUID(List<? extends AccessionIdentifier3> identifiers) {
if (CollectionUtils.isEmpty(identifiers)) {
return Collections.emptyMap();
}
Map<UUID, AccessionIdentifier3> res = new HashMap<>();
List<Accession> foundAccessions = accessionRepository.find(identifiers);
final int chunkSize = 200;
for (Accession accession : foundAccessions) {
Optional<? extends AccessionIdentifier3> toPut = identifiers.stream().filter(id -> id.getAccessionNumber().equals(accession.getAccessionNumber()) && id.getGenus()
.equals(accession.getGenus()) && id.getHoldingInstitute().equals(accession.getInstCode())).findFirst();
for (int fromIndex = 0; fromIndex < identifiers.size(); fromIndex += chunkSize) {
List<Accession> foundAccessions = accessionRepository.find(identifiers.subList(fromIndex, Math.min(identifiers.size(), fromIndex + chunkSize)));
for (Accession accession : foundAccessions) {
Optional<? extends AccessionIdentifier3> toPut = identifiers.stream().filter(id -> id.getAccessionNumber().equals(accession.getAccessionNumber()) && id.getGenus()
.equals(accession.getGenus()) && id.getHoldingInstitute().equals(accession.getInstCode())).findFirst();
toPut.ifPresent(accessionIdentifier3 -> res.put(accession.getUuid(), accessionIdentifier3));
toPut.ifPresent(accessionIdentifier3 -> res.put(accession.getUuid(), accessionIdentifier3));
}
}
return res;
......@@ -201,13 +211,17 @@ public class AccessionServiceImpl implements AccessionService {
@Override
public List<Accession> forUuids(Set<UUID> uuids) {
final int chunkSize = 100;
List<UUID> uuidList = new ArrayList<>(uuids);
List<Accession> accessions = new ArrayList<>(uuidList.size());
if (CollectionUtils.isEmpty(uuids)) {
return Collections.emptyList();
}
List<UUID> uuidList = new ArrayList<>(uuids);
List<Accession> accessions = new ArrayList<>(uuids.size());
for (int i = 0; i < uuids.size(); i+= chunkSize){
List<UUID> chunk = uuidList.subList(i, Math.min(uuids.size(), i+chunkSize));
final int chunkSize = 200;
for (int fromIndex = 0; fromIndex < uuids.size(); fromIndex+= chunkSize){
List<UUID> chunk = uuidList.subList(fromIndex, Math.min(uuids.size(), fromIndex+chunkSize));
accessions.addAll((List<Accession>) accessionRepository.findAll(QAccession.accession.accessionId.uuid.in(chunk)));
}
......
......@@ -28,6 +28,7 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
......@@ -107,18 +108,20 @@ public class SubsetServiceImpl implements SubsetService {
}
@Override
public Page<AccessionRef> listAccessions(final UUID uuid, final Pageable page) {
final Subset subset = subsetRepository.getByUuid(uuid);
if (subset == null) {
throw new NotFoundElement("Subset not found by UUID=" + uuid.toString());
}
final Set<AccessionRef> accessionRefs = subset.getAccessionRefs();
if (accessionRefs != null && !accessionRefs.isEmpty()) {
int fromIndex = Math.multiplyExact(page.getPageNumber(), page.getPageSize());
int toIndex = Math.min(fromIndex + page.getPageSize(), accessionRefs.size());
return new PageImpl<>(new ArrayList<AccessionRef>(accessionRefs).subList(fromIndex, toIndex), page, accessionRefs.size());
@PreAuthorize("hasRole('ADMINISTRATOR') || subset.published || hasPermission(subset, 'read')")
public Page<AccessionRef> listAccessions(Subset subset, final Pageable page) {
subset = loadSubset(subset);
final List<AccessionRef> accessionRefs = new ArrayList<AccessionRef>(subset.getAccessionRefs());
if (CollectionUtils.isEmpty(accessionRefs)) {
return new PageImpl<>(Collections.emptyList(), page, 0);
}
return new PageImpl<>(Collections.emptyList(), page, 0);
accessionRefs.sort((a, b) -> a.getAcceNumb().compareTo(b.getAcceNumb()));
int fromIndex = Math.multiplyExact(page.getPageNumber(), page.getPageSize());
int toIndex = Math.min(fromIndex + page.getPageSize(), accessionRefs.size());
return new PageImpl<>(accessionRefs.subList(fromIndex, toIndex), page, accessionRefs.size());
}
@Override
......@@ -158,28 +161,6 @@ public class SubsetServiceImpl implements SubsetService {
return deepLoad(subsetRepository.save(subset));
}
/**
* Copy subset accessions.
*
* @param target the target
* @param list the subset accessions
*/
private void copyAccessions(final Subset target, final Set<AccessionRef> list) {
if ((list == null) || (list.size() == 0)) {
if (target.getAccessionRefs() != null)
target.getAccessionRefs().clear();
return;
}
if (target.getAccessionRefs() != null && !target.getAccessionRefs().isEmpty()) {
target.getAccessionRefs().clear();
} else {
target.setAccessionRefs(new HashSet<>());
}
target.getAccessionRefs().addAll(list);
}
/**
* Copy values.
*
......
......@@ -860,9 +860,9 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
dataset1 = datasetService.loadDataset(dataset1.getUuid());
dataset2 = datasetService.loadDataset(dataset2.getUuid());
AccessionRef acceRef1 = datasetService.listAccessions(dataset1.getUuid(), new PageRequest(0, 4)).getContent()
AccessionRef acceRef1 = datasetService.listAccessions(dataset1, new PageRequest(0, 4)).getContent()
.stream().filter(acceRef -> acceRef.getAcceNumb().equals("A8") && acceRef.getAccession() != null).findFirst().orElse(null);
AccessionRef acceRef2 = datasetService.listAccessions(dataset1.getUuid(), new PageRequest(0, 4)).getContent()
AccessionRef acceRef2 = datasetService.listAccessions(dataset1, new PageRequest(0, 4)).getContent()
.stream().filter(acceRef -> acceRef.getAccession() != null).findFirst().orElse(null);
assertThat(acceRef1, notNullValue());
......@@ -876,13 +876,13 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
deleteAccession(TEST_INSTCODE, "A2", "Musa");
dataset2 = datasetService.loadDataset(dataset2.getUuid());
assertThat(dataset2.getAccessionCount(), is(3));
datasetService.listAccessions(dataset2.getUuid(), new PageRequest(0, 10)).getContent()
datasetService.listAccessions(dataset2, new PageRequest(0, 10)).getContent()
.forEach(acceRef -> assertThat(acceRef.getAccession(), nullValue()));
deleteAccession(TEST_INSTCODE, "A8", "Musa");
dataset1 = datasetService.loadDataset(dataset1.getUuid());
assertThat(dataset1.getAccessionCount(), is(3));
datasetService.listAccessions(dataset1.getUuid(), new PageRequest(0, 10)).getContent()
datasetService.listAccessions(dataset1, new PageRequest(0, 10)).getContent()
.forEach(acceRef -> assertThat(acceRef.getAccession(), nullValue()));
}
......
......@@ -113,20 +113,20 @@ public class SubsetServiceTest extends CatalogServiceTest {
final Set<AccessionRef> refs = new HashSet<>(5);
// fetch first page
Page<AccessionRef> page1 = subsetService.listAccessions(subset.getUuid(), new PageRequest(0, PAGE_SIZE));
Page<AccessionRef> page1 = subsetService.listAccessions(subset, new PageRequest(0, PAGE_SIZE));
assertThat(page1.getTotalElements(), is((long) subset.getAccessionCount()));
assertThat(page1.getContent().size(), is(PAGE_SIZE));
// add page content to the HashSet to be sure that we avoid duplicates
refs.addAll(page1.getContent());
// fetch second page
Page<AccessionRef> page2 = subsetService.listAccessions(subset.getUuid(), new PageRequest(1, PAGE_SIZE));
Page<AccessionRef> page2 = subsetService.listAccessions(subset, new PageRequest(1, PAGE_SIZE));
assertThat(page2.getTotalElements(), is((long) subset.getAccessionCount()));
assertThat(page2.getContent().size(), is(PAGE_SIZE));
refs.addAll(page2.getContent());
// fetch third page
Page<AccessionRef> page3 = subsetService.listAccessions(subset.getUuid(), new PageRequest(2, PAGE_SIZE));
Page<AccessionRef> page3 = subsetService.listAccessions(subset, new PageRequest(2, PAGE_SIZE));
assertThat(page3.getTotalElements(), is((long) subset.getAccessionCount()));
// it's a last page, so content size should be 1
......
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