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(); else 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. Phew!
4 Comments
J-Tricks
1/22/2011 08:02:41 pm
Thanks Matt :) Some days we would have looked at SearchService class first but it was fun in the end. Thought I would share.
Reply
Ashwini .
2/7/2012 10:22:07 pm
Thanks for post.
Reply
thomas
8/10/2012 09:31:33 am
thx man. took me some days to find your solution ;-)
Reply
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorJobin Kuruvilla - Works in Adaptavist as Head of DevOps Professional Services. Categories
All
Archives
October 2016
|