java - How can I sort on the results of a case statement in Spring Data? -
we have ui sortable table backed following query, clicking column order by
ascending or descending on column. problem 1 of columns either renders the users role name or human name dependent on conditional, , need able sort column.
currently repository definition
page<useractivitylog> findbyactivitytargetuserid( long id, pageable pageable );
i'm trying write specification that's looks more or less 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 incomplete. i'm not sure how take (assuming it's right case statement want do) , make return predicate
. may not able use specification
add case statement, though suspect can. i'm unsure of once work, how i'll pass "field name" sort
(i know how pass field name, guess i'm not sure how figure out field name be).
it's worth saying don't understand criteriabuilder
api yet.
update modified example of case statement grabbed an oracle tutorial demonstrates need do, don't need results of case-ed column in return object though.
sql> select job, ename 2 , case 3 when msal <= 2500 4 'cheap' 5 else 'expensive' 6 end class 7 employees 8 bdate < date '1964-01-01' 9 order class;
how can sort results based on case statement generated field while still using spring data repository?
this ended doing. needed fetch each attribute @ time (not obvious tutorials imho) , @ end in order return predicate have created specification delegate has actual clause (in case simple id). it's worth saying .index.name
stuff static string contains property name, haven't decided whether generate 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 ); } }
Comments
Post a Comment