Here is the scenario: I have a SearchRequest object from which I need to get the JQL query String. That's it. Simple, straight forward. Or so I thought!
As it appeared so straight forward, I started with the JAVA docs for SearchRequest. The method getQuery() returns a Query object - half way there!
All I need now is the query String or a String representation of the Query object. Over to the JAVA Docs - Query. There it is - getQueryString(). Job done. Or so I thought until I saw the output as null. Time to scratch the head!! Sure I haven't done anything wrong?
Time to look for alternatives. That is when I came across JqlQueryParser. There we have a useful method, parseQuery - to convert a JQL Query String back to a Query object. Nice but exactly opposite to the need of the hour. Bad day?
To give it one last shot before digging deep into the source code or crying out in the forums, I decided to have a look at SearchService - one that does the actual search operation. And boy, there it is:
return query.getQueryString();
else
return getGeneratedJqlString(query);
In Summary, if you want to convert a Query object to Jql String, use SearhService.getJqlString(Query query). If you want to convert a JQL String back to a Query object, use JqlQueryParser.parseQuery(String Query). In the latter case, you can also use SearchService.parseQuery(User searcher, String query) which returns SearchService.ParseResult from which Query object can be retrieved.
Phew!