JPAUtils.java
package org.genesys.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 PageRequest.of(oldPageRequest.getPageNumber(),oldPageRequest.getPageSize(), new QSort(orders));
}
}