Skip to content

Commit

Permalink
Merge pull request #243 from ncats/default_sort_order
Browse files Browse the repository at this point in the history
Default sort order
  • Loading branch information
blueSwordfish authored Jan 3, 2024
2 parents b2cf7bb + 8e1e9e5 commit 6e73ba0
Showing 1 changed file with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.net.URLDecoder;
import java.security.Principal;
import java.util.ArrayList;
Expand All @@ -15,8 +17,12 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.metamodel.Metamodel;
import javax.servlet.http.HttpServletRequest;

import org.hibernate.metadata.ClassMetadata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -75,8 +81,6 @@ public abstract class AbstractGsrsEntityController<C extends AbstractGsrsEntityC
private EntityLinks entityLinks;




// /**
// * Create a new GSRS Controller with the given context.
// * @param context the context for the routes of this controller.
Expand Down Expand Up @@ -407,8 +411,7 @@ public ResponseEntity<Object> page(@RequestParam(value = "top", defaultValue = "
@RequestParam(value = "skip", defaultValue = "0") long skip,
@RequestParam(value = "order", required = false) String order,
@RequestParam Map<String, String> queryParameters){



Page<T> page = getEntityService().page(new OffsetBasedPageRequest(skip, top,parseSortFromOrderParam(order)));

String view=queryParameters.get("view");
Expand All @@ -419,10 +422,27 @@ public ResponseEntity<Object> page(@RequestParam(value = "top", defaultValue = "
return new ResponseEntity<>(new PagedResult(page, queryParameters), HttpStatus.OK);
}

private Sort parseSortFromOrderParam(String order){
//match Gsrs Play API
if(order ==null || order.trim().isEmpty()){
return Sort.sort(getEntityService().getEntityClass());
private Sort parseSortFromOrderParam(String order){
if(order == null || order.trim().isEmpty()){
Field[] fields = getEntityService().getEntityClass().getFields();

boolean found = false;
String name = "";
for(Field field: fields) {
if(found)
break;
name = field.getName();
Annotation[] annotations = field.getAnnotations();
if(annotations.length > 0) {
for(Annotation annotation : annotations) {
if(annotation.annotationType().equals(Id.class)) {
found = true;
break;
}
}
}
}
return Sort.by(Sort.Direction.ASC, name);
}
char firstChar = order.charAt(0);
if('$'==firstChar){
Expand Down

0 comments on commit 6e73ba0

Please sign in to comment.