use of io.crnk.core.queryspec.SortSpec in project crnk-framework by crnk-project.
the class TaskResourceRepositoryTest method checkOrderByPriorityDesc.
@Test
public void checkOrderByPriorityDesc() {
addTask("otherTask", 14);
QuerySpec querySpec = new QuerySpec(ApproveTask.class);
querySpec.addSort(new SortSpec(Arrays.asList("priority"), Direction.DESC));
ResourceList<ApproveTask> resources = taskRepository.findAll(querySpec);
Assert.assertEquals(2, resources.size());
Assert.assertEquals("otherTask", resources.get(0).getName());
Assert.assertEquals("testTask", resources.get(1).getName());
}
use of io.crnk.core.queryspec.SortSpec in project crnk-framework by crnk-project.
the class TaskResourceRepositoryTest method checkOrderByPriorityAsc.
@Test
public void checkOrderByPriorityAsc() {
addTask("otherTask", 14);
QuerySpec querySpec = new QuerySpec(ApproveTask.class);
querySpec.addSort(new SortSpec(Arrays.asList("priority"), Direction.ASC));
ResourceList<ApproveTask> resources = taskRepository.findAll(querySpec);
Assert.assertEquals(2, resources.size());
Assert.assertEquals("testTask", resources.get(0).getName());
Assert.assertEquals("otherTask", resources.get(1).getName());
}
use of io.crnk.core.queryspec.SortSpec in project crnk-framework by crnk-project.
the class ActivitiQuerySpecMapper method applySortSpec.
private static void applySortSpec(Query activitiQuery, QuerySpec querySpec) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
for (SortSpec orderSpec : querySpec.getSort()) {
List<String> attributePath = orderSpec.getAttributePath();
PreconditionUtil.assertTrue("nested attribute paths not supported", attributePath.size() == 1);
String attributeName = attributePath.get(0);
String orderbyMethodName = "orderBy" + firstToUpper(addTypePrefix(querySpec.getResourceClass(), attributeName));
Method method = activitiQuery.getClass().getMethod(orderbyMethodName);
method.invoke(activitiQuery);
if (orderSpec.getDirection() == Direction.DESC) {
activitiQuery.desc();
} else {
activitiQuery.asc();
}
}
}
use of io.crnk.core.queryspec.SortSpec in project crnk-framework by crnk-project.
the class DefaultQueryParamsConverter method applySorting.
protected void applySorting(QuerySpec spec, QueryParams queryParams) {
List<SortSpec> sortSpecs = spec.getSort();
Map<String, SortingParams> decodedSortingMap = new LinkedHashMap<>();
if (sortSpecs != null && !sortSpecs.isEmpty()) {
String resourceType = getResourceType(spec.getResourceClass());
for (SortSpec sortSpec : sortSpecs) {
Map<String, RestrictedSortingValues> sortingValues = new HashMap<>();
String joinedPath = joinPath(sortSpec.getAttributePath());
RestrictedSortingValues sortValue = sortSpec.getDirection() == Direction.DESC ? RestrictedSortingValues.desc : RestrictedSortingValues.asc;
SortingParams sortingParams = new SortingParams(sortingValues);
sortingValues.put(joinedPath, sortValue);
decodedSortingMap.put(resourceType, sortingParams);
}
}
queryParams.setSorting(new TypedParams<>(Collections.unmodifiableMap(decodedSortingMap)));
}
use of io.crnk.core.queryspec.SortSpec in project crnk-framework by crnk-project.
the class DefaultQuerySpecDeserializerTestBase method testHyphenIsAllowedInResourceName.
@Test
public void testHyphenIsAllowedInResourceName() {
QuerySpec expectedSpec = new QuerySpec(Task.class);
expectedSpec.addSort(new SortSpec(Arrays.asList("id"), Direction.ASC));
Map<String, Set<String>> params = new HashMap<>();
add(params, "sort[task-with-lookup]", "id");
ResourceInformation taskWithLookUpInformation = resourceRegistry.getEntryForClass(TaskWithLookup.class).getResourceInformation();
QuerySpec actualSpec = deserializer.deserialize(taskWithLookUpInformation, params);
Assert.assertEquals(expectedSpec, actualSpec);
}
Aggregations