It is sometimes amazing to realize how we missed something so simple! To realize how a little documentation could have saved us few hours (or days for that matter!). Presenting one that I came across while playing cat and mouse with JQL (JIRA query language).
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:
String getJqlString(Query query)
Gets the JQL string representation for the passed query. Returns the string from Query.getQueryString() if it exists or generates one if it does not.
if (query.getQueryString() != null)
So what is happening in getGeneratedJqlString(query)?
And that is where the actual conversion happens: JqlStringSupport.generateJqlString(Query query) . Not just that, JqlStringSupport has some other helpful utility methods as well. Good Day?
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.