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