How can I sort on the results of a case statement in Spring Data?

xenoterracide

we have a UI sortable table that is backed by the following query, clicking a column does order by ascending or descending on that column. Problem is that one of the columns either renders the the users role name or their human name dependent on a conditional, and we need to be able to sort that column.

currently this the repository definition

Page<UserActivityLog> findByActivityTargetUserId( Long id, Pageable pageable );

I'm trying to write a specification that's looks more or less like this.

private static class UserSpecification implements Specification<UserActivityLog> {

    @Override
    public Predicate toPredicate(
            final Root<UserRelatedEntity> root,
            final CriteriaQuery<?> query,
            final CriteriaBuilder cb )

     cb.selectCase()
     .when( root.<Boolean>get( "user.setting.fieldBoolean" ), 
         root.<RoleType>get( "user.role.roleTypeEnum" ).toString() )
     .otherwise( root.<String>get("user.humanNameString") )
... //magic 

this is obviously incomplete. I'm not sure how to take this (assuming it's right for the case statement I want to do) and make it return the Predicate. I may not even be able to use the Specification to add the case statement, though I suspect I can. I'm also unsure of once I get it to work, how I'll pass the "field name" to a Sort (I know how to pass a field name, I guess I'm just not sure how to figure out what the field name will be).

It's worth saying I don't really understand the CriteriaBuilder api yet.

Update slightly modified example of a case statement that I grabbed from a an oracle tutorial which demonstrates what I need to do, I don't need the results of the case-ed column in my return object though.

SQL> select job, ename
2  ,      case
3           when msal <= 2500
4           then 'cheap'
5           else 'expensive'
6         end         as class
7  from   employees
8  where  bdate < date '1964-01-01'
9  order  by class;

How can sort my results based on a case statement generated field while still using a Spring Data repository?

xenoterracide

This is what I ended up doing. I needed to fetch each attribute at a time (not obvious by tutorials imho) and then at the end in order to return a predicate I have created another specification which I delegate to which has the actual where clause (in this case a simple by id). It's worth saying that the .Index.NAME stuff is just a static string that contains the property name, we haven't decided whether to generate the metamodel yet.

public class FilterByIdForUserSpecification<AL extends AbstractActivityLogWithSiteUser> implements Specification<AL>
{
private final Sort.Direction direction;
private final Specification<AL> specification;


public FilterByIdForUserSpecification(
        final Specification<AL> specification,
        final Sort.Direction direction )
{
    this.specification = specification;
    this.direction = direction;
}

@Override
public Predicate toPredicate(
        final Root<AL> root,
        final CriteriaQuery<?> query,
        final CriteriaBuilder cb )
{
    Path<Object> siteUser = root.get( AbstractActivityLogWithSiteUser.Index.USER );
    Path<Object> ownerOrg = siteUser.get( SiteUser.Index.PRI_ORG ).get( Organization.Index.OWNER );
    Path<Object> fullName = siteUser.get( SiteUser.Index.CONTACT ).get( Contact.Index.FULL_NAME );
    Path<Object> roleDisp = siteUser.get( SiteUser.Index.ROLE ).get( Role.Index.DISPLAY );

    Expression<Object> queryCase = cb.selectCase()
            .when( cb.equal( ownerOrg, true ), roleDisp )
            .otherwise( fullName );

    query.orderBy( direction( cb, queryCase, direction ) );
    return specification.toPredicate( root, query, cb );
}

static Order direction( final CriteriaBuilder cb, final Expression<?> e, final Sort.Direction direction )
{
    if ( direction == Direction.ASC )
    {
        return cb.asc( e );
    }
    return cb.desc( e );
}
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i loop through These with A CASE statement?

From Java

How can I sort arrays and data in PHP?

From Dev

How I can sort this data out in R

From Dev

How can I show all the results in case of a draw in Postgres?

From Dev

How can I check results for select statement in MVC?

From Dev

How can I change "join with select statement results to" laravel eloquent?

From Dev

How can I erase q in this case? and sort the file alphabetically?

From Dev

how can i use case statement after using left join?

From Dev

How can I make my switch statement case insensitive?

From Dev

How can I write this ElseIf as a Case statement in Excel-VBA?

From Dev

How can I correct this query that involves a CASE statement for a summary?

From Dev

How can I use a case statement with a Drop Box? Access Database

From Dev

how can i use case statement after using left join?

From Dev

How can I use conditional constructs in a bash case statement?

From Dev

How can I sort one column by ascending and another by descending in Spring Data?

From Java

How can I retrieve sort property and direction from a Spring Data Pageable?

From Dev

How to sort results of Find statement by date?

From Dev

Spring data MongoDB sort spatial results

From Dev

Spring data MongoDB sort spatial results

From Dev

How can I get the no results statement to only show when there are no results instead of all of the time?

From Dev

How can I sort data in python by the specific part of data?

From Dev

Can I use an array in a case statement?

From Dev

How can I sort these?

From Dev

How can I transfer data from html to php in this case

From Dev

How can I transfer data from html to php in this case

From Dev

How can I query data from multiple tables and sort by time?

From Dev

How can I sort the data in a table over four columns?

From Dev

How can I iterate over unordered JSON data and sort into an array

From Dev

LINQ - How can I sort this data in one query

Related Related

  1. 1

    How can i loop through These with A CASE statement?

  2. 2

    How can I sort arrays and data in PHP?

  3. 3

    How I can sort this data out in R

  4. 4

    How can I show all the results in case of a draw in Postgres?

  5. 5

    How can I check results for select statement in MVC?

  6. 6

    How can I change "join with select statement results to" laravel eloquent?

  7. 7

    How can I erase q in this case? and sort the file alphabetically?

  8. 8

    how can i use case statement after using left join?

  9. 9

    How can I make my switch statement case insensitive?

  10. 10

    How can I write this ElseIf as a Case statement in Excel-VBA?

  11. 11

    How can I correct this query that involves a CASE statement for a summary?

  12. 12

    How can I use a case statement with a Drop Box? Access Database

  13. 13

    how can i use case statement after using left join?

  14. 14

    How can I use conditional constructs in a bash case statement?

  15. 15

    How can I sort one column by ascending and another by descending in Spring Data?

  16. 16

    How can I retrieve sort property and direction from a Spring Data Pageable?

  17. 17

    How to sort results of Find statement by date?

  18. 18

    Spring data MongoDB sort spatial results

  19. 19

    Spring data MongoDB sort spatial results

  20. 20

    How can I get the no results statement to only show when there are no results instead of all of the time?

  21. 21

    How can I sort data in python by the specific part of data?

  22. 22

    Can I use an array in a case statement?

  23. 23

    How can I sort these?

  24. 24

    How can I transfer data from html to php in this case

  25. 25

    How can I transfer data from html to php in this case

  26. 26

    How can I query data from multiple tables and sort by time?

  27. 27

    How can I sort the data in a table over four columns?

  28. 28

    How can I iterate over unordered JSON data and sort into an array

  29. 29

    LINQ - How can I sort this data in one query

HotTag

Archive