public Query decorate ( final Query query ) { // Clear the result to fetch if previously specified (i.e. by getting count) query . setResult ( null ) ; if ( pagination != null && pagination . isPaginated ( ) ) { final long begin = pagination . getOffset ( ) ; final long end = begin + pagination . getLimit ( ) ; query . setRange ( begin , end ) ; } if ( orderBy != null && RegexSequence . Pattern . ALPHA_NUMERIC . matcher ( orderBy ) . matches ( ) && orderDirection != OrderDirection . UNSPECIFIED ) { // Check to see if the specified orderBy field is defined in the class being queried. boolean found = false ; final org . datanucleus . store . query . Query iq = ( ( JDOQuery ) query ) . getInternalQuery ( ) ; for ( final Field field : iq . getCandidateClass ( ) . getDeclaredFields ( ) ) { if ( orderBy . equals ( field . getName ( ) ) ) { found = true ; break ; } } if ( found ) { query . setOrdering ( orderBy + " " + orderDirection . name ( ) . toLowerCase ( ) ) ; } } return query ; }
Given a query this method will decorate that query with pagination ordering and sorting direction . Specific checks are performed to ensure the execution of the query is capable of being paged and that ordering can be securely performed .