J Tricks - Little JIRA Tricks
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)
    return query.getQueryString();
    return getGeneratedJqlString(query);

So what is happening in getGeneratedJqlString(query)?

return jqlStringSupport.generateJqlString(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.



01/22/2011 6:18pm

Yes, I got all tangled up that code when I was rewriting the Timecharts plugin for JIRA 4.x. As you say, a little documentation could go a long way there. Thanks for the post.


01/23/2011 1:02am

Thanks Matt :) Some days we would have looked at SearchService class first but it was fun in the end. Thought I would share.

Ashwini .
02/08/2012 3:22am

Thanks for post.

08/10/2012 1:31pm

thx man. took me some days to find your solution ;-)


Your comment will be posted after it is approved.

Leave a Reply

J tricks - Little JIRA Tricks