Commit 74949a31 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '332-sorting-by-title-works-incorrectly-fix' into 'master'

Fix: Sorting by Title works incorrectly

See merge request genesys-pgr/genesys-server!188
parents d67a5185 a3be7df7
......@@ -52,11 +52,13 @@ import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.security.SecurityUtils;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.genesys2.util.JPAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
......@@ -67,6 +69,7 @@ import org.springframework.web.multipart.MultipartFile;
import com.querydsl.core.BooleanBuilder;
/**
* The Class DatasetServiceImpl.
*/
......@@ -262,7 +265,9 @@ public class DatasetServiceImpl implements DatasetService {
*/
@Override
public Page<Dataset> listDatasets(final DatasetFilter filter, final Pageable page) {
return datasetRepository.findAll(new BooleanBuilder().and(filter.buildQuery()).and(QDataset.dataset.state.in(PublishState.PUBLISHED)), page);
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = datasetRepository.findAll(new BooleanBuilder().and(filter.buildQuery()).and(QDataset.dataset.state.in(PublishState.PUBLISHED)), markdownSortPageRequest);
return new PageImpl<Dataset>(res.getContent(), page, res.getTotalElements());
}
/**
......@@ -271,10 +276,14 @@ public class DatasetServiceImpl implements DatasetService {
@Override
public Page<Dataset> listDatasetsForCurrentUser(final DatasetFilter filter, final Pageable page) {
if (securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
return datasetRepository.findAll(filter.buildQuery(), page);
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = datasetRepository.findAll(filter.buildQuery(), markdownSortPageRequest);
return new PageImpl<Dataset>(res.getContent(), page, res.getTotalElements());
} else {
final HashSet<Long> partners = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
return datasetRepository.findAll(dataset.owner.id.in(partners).and(filter.buildQuery()), page);
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = datasetRepository.findAll(dataset.owner.id.in(partners).and(filter.buildQuery()), markdownSortPageRequest);
return new PageImpl<Dataset>(res.getContent(), page, res.getTotalElements());
}
}
......
......@@ -40,16 +40,14 @@ import org.genesys.catalog.service.DescriptorService;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.security.SecurityUtils;
import org.genesys2.util.JPAUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.*;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.acls.domain.BasePermission;
......@@ -306,16 +304,21 @@ public class DescriptorListServiceImpl implements DescriptorListService {
@Override
public Page<DescriptorList> listDescriptorLists(final DescriptorListFilter filters, final Pageable page) {
return descriptorListRepository.findAll(new BooleanBuilder().and(filters.buildQuery()).and(descriptorList.state.in(PublishState.PUBLISHED)), page);
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = descriptorListRepository.findAll(new BooleanBuilder().and(filters.buildQuery()).and(descriptorList.state.in(PublishState.PUBLISHED)), markdownSortPageRequest);
return new PageImpl<DescriptorList>(res.getContent(), page, res.getTotalElements());
}
@Override
public Page<DescriptorList> listDescriptorListsForCurrentUser(final DescriptorListFilter filter, final Pageable page) {
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
if (securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
return descriptorListRepository.findAll(filter.buildQuery(), page);
Page res = descriptorListRepository.findAll(filter.buildQuery(), markdownSortPageRequest);
return new PageImpl<DescriptorList>(res.getContent(), page, res.getTotalElements());
} else {
final HashSet<Long> partners = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
return descriptorListRepository.findAll(descriptorList.owner.id.in(partners).and(filter.buildQuery()), page);
Page res = descriptorListRepository.findAll(descriptorList.owner.id.in(partners).and(filter.buildQuery()), markdownSortPageRequest);
return new PageImpl<DescriptorList>(res.getContent(), page, res.getTotalElements());
}
}
......@@ -421,12 +424,14 @@ public class DescriptorListServiceImpl implements DescriptorListService {
*/
@Override
public List<DescriptorList> autocompleteDescriptorLists(final String text) {
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(new PageRequest(0, 15, new Sort("title")),"title");
if (StringUtils.isBlank(text)) {
final Predicate predicate = descriptorList.state.in(PublishState.PUBLISHED);
return descriptorListRepository.findAll(predicate, new PageRequest(0, 15, new Sort("title"))).getContent();
return descriptorListRepository.findAll(predicate, markdownSortPageRequest).getContent();
} else {
final Predicate predicate = descriptorList.state.in(PublishState.PUBLISHED).and(descriptorList.title.containsIgnoreCase(text));
return descriptorListRepository.findAll(predicate, new PageRequest(0, 15, new Sort("title"))).getContent();
return descriptorListRepository.findAll(predicate, markdownSortPageRequest).getContent();
}
}
}
......@@ -41,6 +41,7 @@ import org.genesys.catalog.service.VersionManager;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.security.SecurityUtils;
import org.genesys2.util.JPAUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -48,6 +49,7 @@ import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
......@@ -217,10 +219,14 @@ public class DescriptorServiceImpl implements DescriptorService {
@PreAuthorize("isAuthenticated()")
public Page<Descriptor> listDescriptorsForCurrentUser(final DescriptorFilter filter, final Pageable page) {
if (securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
return descriptorRepository.findAll(filter.buildQuery(), page);
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = descriptorRepository.findAll(filter.buildQuery(), markdownSortPageRequest);
return new PageImpl<Descriptor>(res.getContent(), page, res.getTotalElements());
} else {
final HashSet<Long> partners = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
return descriptorRepository.findAll(descriptor.owner.id.in(partners).and(filter.buildQuery()), page);
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = descriptorRepository.findAll(descriptor.owner.id.in(partners).and(filter.buildQuery()), markdownSortPageRequest);
return new PageImpl<Descriptor>(res.getContent(), page, res.getTotalElements());
}
}
......@@ -229,7 +235,9 @@ public class DescriptorServiceImpl implements DescriptorService {
*/
@Override
public Page<Descriptor> listDescriptors(final DescriptorFilter descriptorFilter, final Pageable page) {
return descriptorRepository.findAll(new BooleanBuilder().and(descriptorFilter.buildQuery()).and(QDescriptor.descriptor.state.in(PublishState.PUBLISHED)), page);
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page res = descriptorRepository.findAll(new BooleanBuilder().and(descriptorFilter.buildQuery()).and(QDescriptor.descriptor.state.in(PublishState.PUBLISHED)), markdownSortPageRequest);
return new PageImpl<Descriptor>(res.getContent(), page, res.getTotalElements());
}
/**
......@@ -436,4 +444,5 @@ public class DescriptorServiceImpl implements DescriptorService {
copy.setOwner(descriptor.getOwner());
return lazyLoad(descriptorRepository.save(copy));
}
}
......@@ -26,6 +26,7 @@ import org.genesys.catalog.persistence.PartnerRepository;
import org.genesys.catalog.service.PartnerService;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.security.SecurityUtils;
import org.genesys2.util.JPAUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
......@@ -34,6 +35,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.acls.domain.BasePermission;
......@@ -140,10 +142,14 @@ public class PartnerServiceImpl implements PartnerService, InitializingBean {
@Override
public Page<Partner> listPartnersForCurrentUser(final PartnerFilter filter, final Pageable page) {
if (securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
return partnerRepository.findAll(filter.buildQuery(), page);
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "name");
Page res = partnerRepository.findAll(filter.buildQuery(), markdownSortPageRequest);
return new PageImpl<Partner>(res.getContent(), page, res.getTotalElements());
} else {
final HashSet<Long> ids = new HashSet<>(securityUtils.listObjectIdentityIdsForCurrentUser(Partner.class, BasePermission.WRITE));
return partnerRepository.findAll(partner.id.in(ids).and(filter.buildQuery()), page);
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "name");
Page res = partnerRepository.findAll(partner.id.in(ids).and(filter.buildQuery()), markdownSortPageRequest);
return new PageImpl<Partner>(res.getContent(), page, res.getTotalElements());
}
}
......@@ -152,7 +158,9 @@ public class PartnerServiceImpl implements PartnerService, InitializingBean {
*/
@Override
public Page<Partner> listPartners(final PartnerFilter partnerFilter, final Pageable page) {
return partnerRepository.findAll(partnerFilter.buildQuery(), page);
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "name");
Page res = partnerRepository.findAll(partnerFilter.buildQuery(), markdownSortPageRequest);
return new PageImpl<Partner>(res.getContent(), page, res.getTotalElements());
}
/**
......
package org.genesys2.util;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.Expressions;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.querydsl.QSort;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class JPAUtils {
/**
* Returns page request that will sort content ignoring markdown
*
* @param oldPageRequest page to modify
* @param markdownProperties list of properties that can contain markdown symbols
* @return PageRequest with modified sort and oldPageRequest if its sort is null
*/
public static Pageable toMarkdownSort(Pageable oldPageRequest, String... markdownProperties){
List<String> markdownPropertiesList = Arrays.asList(markdownProperties);
List<OrderSpecifier<?>> orders = new ArrayList<>();
if(oldPageRequest.getSort() == null)
return oldPageRequest;
for(Sort.Order order: oldPageRequest.getSort()) {
OrderSpecifier<String> oS;
String prop = order.getProperty();
if (markdownPropertiesList.contains(prop)) {
prop = "replace (" + prop + ", '*', '')";
}
if (order.isAscending()) {
oS = Expressions.stringTemplate(prop).asc();
} else {
oS = Expressions.stringTemplate(prop).desc();
}
orders.add(oS);
}
return new PageRequest(oldPageRequest.getPageNumber(),oldPageRequest.getPageSize(), new QSort(orders));
}
}
......@@ -401,6 +401,22 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
datasetService.addDescriptors(dataset, new Descriptor[] {});
}
@Test
public void testListMarkdownSortDatasets(){
final Dataset dataset1 = buildAndSaveDataset("Dataset 1", DATASET_DESCRIPTION_1, partner, PublishState.PUBLISHED);
final Dataset dataset2 = buildAndSaveDataset("*Dataset* 2", DATASET_DESCRIPTION_1, partner, PublishState.PUBLISHED);
final Dataset dataset3 = buildAndSaveDataset("Dataset 3", DATASET_DESCRIPTION_1, partner, PublishState.PUBLISHED);
final String[] strs = new String[] { "title" };
final Pageable pageable = new PageRequest(0, Integer.min(50, 100), Sort.Direction.ASC, strs);
final Page<?> page = datasetService.listDatasets(new DatasetFilter(), pageable);
assertThat(page.getTotalElements(), is(3L));
assertThat(((Dataset) page.getContent().get(0)).getUuid(), is(dataset1.getUuid()));
assertThat(((Dataset) page.getContent().get(1)).getUuid(), is(dataset2.getUuid()));
assertThat(((Dataset) page.getContent().get(2)).getUuid(), is(dataset3.getUuid()));
}
@Test
public void testLoadList() {
// not listed
......
......@@ -27,6 +27,7 @@ import java.util.UUID;
import org.genesys.catalog.exceptions.InvalidApiUsageException;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.filters.DescriptorListFilter;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.Descriptor.Category;
import org.genesys.catalog.model.traits.DescriptorList;
......@@ -41,6 +42,10 @@ import org.springframework.dao.ConcurrencyFailureException;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
/**
* Test for {@link DescriptorListServiceImpl}
......@@ -173,6 +178,44 @@ public class DescriptorListServiceTest extends CatalogServiceTest {
descriptorListService.deleteDescriptorList(descriptorList);
}
@Test
public void listMarkdownSortDescriptorListsTest(){
final Descriptor descriptorOne = setupDescriptor(partner, DESCRIPTOR_TITLE_1, VERSION_1_0, Descriptor.DataType.NUMERIC);
final List<Descriptor> descriptors = Lists.newArrayList(descriptorOne);
final DescriptorList descriptorList1 = setupDescriptorList("Descriptor list 1", VERSION_1_0, DESCRIPTION_1);
descriptorList1.setDescriptors(descriptors);
final DescriptorList descriptorList2 = setupDescriptorList("*Descriptor list* 2", VERSION_1_0, DESCRIPTION_1);
descriptorList2.setDescriptors(descriptors);
final DescriptorList descriptorList3 = setupDescriptorList("Descriptor list 3", VERSION_1_0, DESCRIPTION_1);
descriptorList3.setDescriptors(descriptors);
DescriptorList result1 = descriptorListService.createDescriptorList(descriptorList1);
result1 = descriptorListService.reviewDescriptorList(result1);
result1 = descriptorListService.approveDescriptorList(result1);
DescriptorList result2 = descriptorListService.createDescriptorList(descriptorList2);
result2 = descriptorListService.reviewDescriptorList(result2);
result2 = descriptorListService.approveDescriptorList(result2);
DescriptorList result3 = descriptorListService.createDescriptorList(descriptorList3);
result3 = descriptorListService.reviewDescriptorList(result3);
result3 = descriptorListService.approveDescriptorList(result3);
final String[] strs = new String[] { "title" };
final Pageable pageable = new PageRequest(0, Integer.min(50, 100), Sort.Direction.ASC, strs);
final Page<DescriptorList> pageResult = descriptorListService.listDescriptorLists(new DescriptorListFilter(), pageable);
assertThat(pageResult.getContent(), not(nullValue()));
assertThat(pageResult.getContent(), hasSize(3));
assertThat(pageResult.getContent().get(0), equalTo(result1));
assertThat(pageResult.getContent().get(1), equalTo(result2));
assertThat(pageResult.getContent().get(2), equalTo(result3));
}
@Test
public void createDescriptorListWithDescriptor() throws Exception {
......
......@@ -365,6 +365,26 @@ public class DescriptorServiceTest extends CatalogServiceTest {
assertThat(result.getVersionTag(), is(VERSION_1_0));
}
@Test
public void listMarkdownSortDescriptorsTest() {
final Descriptor descriptor1 = setupDescriptor(null, "Descriptor 1", Category.PASSPORT, DataType.NUMERIC, VERSION_1_0, PublishState.PUBLISHED);
final Descriptor descriptor2 = setupDescriptor(null, "*Descriptor* 2", Category.PASSPORT, DataType.NUMERIC, VERSION_1_0, PublishState.PUBLISHED);
final Descriptor descriptor3 = setupDescriptor(null, "Descriptor 3", Category.PASSPORT, DataType.NUMERIC, VERSION_1_0, PublishState.PUBLISHED);
final Descriptor result1 = descriptorRepository.save(descriptor1);
final Descriptor result2 = descriptorRepository.save(descriptor2);
final Descriptor result3 = descriptorRepository.save(descriptor3);
final String[] strs = new String[] { "title" };
final Pageable pageable = new PageRequest(0, Integer.min(50, 100), Sort.Direction.ASC, strs);
final Page<Descriptor> pageResult = descriptorService.listDescriptors(new DescriptorFilter(), pageable);
assertThat(pageResult.getContent(), not(nullValue()));
assertThat(pageResult.getContent(), hasSize(3));
assertThat(pageResult.getContent().get(0), equalTo(result1));
assertThat(pageResult.getContent().get(1), equalTo(result2));
assertThat(pageResult.getContent().get(2), equalTo(result3));
}
@Test
public void listDescriptorsTest() {
// unpublished descriptor
......
......@@ -238,6 +238,33 @@ public class PartnerServiceTest extends CatalogServiceTest {
partnerService.updatePartner(result);
}
@Test
public void testListMarkdownSortPartner() {
final Partner partner1 = new Partner();
partner1.setName("partner 1");
partner1.setShortName("Par 1");
final Partner result1 = partnerService.createPartner(partner1);
final Partner partner2 = new Partner();
partner2.setName("*partner* 2");
partner2.setShortName("Par 2");
final Partner result2 = partnerService.createPartner(partner2);
final Partner partner3 = new Partner();
partner3.setName("partner 3");
partner3.setShortName("Par 3");
final Partner result3 = partnerService.createPartner(partner3);
final String[] strs = new String[] { "name" };
final Pageable pageable = new PageRequest(0, Integer.min(50, 100), Sort.Direction.ASC, strs);
final Page<Partner> page = partnerService.listPartners(new PartnerFilter(), pageable);
assertThat(page.getTotalElements(), is(3L));
assertThat(page.getContent().get(0).getUuid(), is(result1.getUuid()));
assertThat(page.getContent().get(1).getUuid(), is(result2.getUuid()));
assertThat(page.getContent().get(2).getUuid(), is(result3.getUuid()));
}
@Test
public void testListPartner() {
final Partner input = new Partner();
......
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