Search in sources :

Example 1 with SortSpec

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());
}
Also used : QuerySpec(io.crnk.core.queryspec.QuerySpec) ApproveTask(io.crnk.activiti.example.model.ApproveTask) SortSpec(io.crnk.core.queryspec.SortSpec) Test(org.junit.Test)

Example 2 with SortSpec

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());
}
Also used : QuerySpec(io.crnk.core.queryspec.QuerySpec) ApproveTask(io.crnk.activiti.example.model.ApproveTask) SortSpec(io.crnk.core.queryspec.SortSpec) Test(org.junit.Test)

Example 3 with SortSpec

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();
        }
    }
}
Also used : Method(java.lang.reflect.Method) SortSpec(io.crnk.core.queryspec.SortSpec)

Example 4 with SortSpec

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)));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SortSpec(io.crnk.core.queryspec.SortSpec) SortingParams(io.crnk.legacy.queryParams.params.SortingParams) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with SortSpec

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);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) TaskWithLookup(io.crnk.core.mock.models.TaskWithLookup) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) QuerySpec(io.crnk.core.queryspec.QuerySpec) SortSpec(io.crnk.core.queryspec.SortSpec) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Aggregations

SortSpec (io.crnk.core.queryspec.SortSpec)15 QuerySpec (io.crnk.core.queryspec.QuerySpec)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)6 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 ApproveTask (io.crnk.activiti.example.model.ApproveTask)4 Task (io.crnk.test.mock.models.Task)3 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 TaskWithLookup (io.crnk.core.mock.models.TaskWithLookup)1 FilterSpec (io.crnk.core.queryspec.FilterSpec)1 SortingParams (io.crnk.legacy.queryParams.params.SortingParams)1 MetaAttribute (io.crnk.meta.model.MetaAttribute)1 MetaElement (io.crnk.meta.model.MetaElement)1 MetaResource (io.crnk.meta.model.resource.MetaResource)1 MetaResourceField (io.crnk.meta.model.resource.MetaResourceField)1 Method (java.lang.reflect.Method)1 LinkedHashMap (java.util.LinkedHashMap)1