It's all about the answers!

Ask a question

Query with complex boolean Expression from code?

Andy Berner (61127) | asked May 02 '08, 9:05 a.m.
How do you construct a query in code using the Client library classes ("api") that has a complex filter? That is, how do you construct an Expression that's a boolean combination of other Expressions?
For example (simple one), what's the code to construct and run a query to find all defects that have a Resolved Date after a particular date?

One answer

permanent link
Pat McCarthy (12152) | answered May 05 '08, 5:43 p.m.
Snippet of code that works for me; not sure if there is a better way:

My code:

// uses my utility class to get repository, project area, and WI type refs
ITeamRepository repo = getUtility().getRepo(true);
IProjectAreaHandle pa = getUtility().getProjectArea();
IWorkItemType wiType = getUtility().getWorkItemType();

// playing with these attributes
IQueryableAttribute paAttr = getAttribute(IWorkItem.PROJECT_AREA_PROPERTY);
IQueryableAttribute typeAttr = getAttribute(IWorkItem.TYPE_PROPERTY);
IQueryableAttribute rdateAttr = getAttribute(IWorkItem.RESOLUTION_DATE_PROPERTY);

// Need to know today for resolved by date use
java.util.Date dateToday = new java.util.Date();
java.sql.Timestamp timeToday = new java.sql.Timestamp(dateToday.getTime());

// Make simple expressions with the attributes - default WI type is Defect

Expression paExpr = new AttributeExpression(
paAttr, AttributeOperation.EQUALS, pa);
Expression typeExpr = new AttributeExpression(
typeAttr, AttributeOperation.EQUALS, wiType.getIdentifier());
Expression rdateExpr = new AttributeExpression(
rdateAttr, AttributeOperation.BEFORE, timeToday);

// Make a complex expression
Term combined= new Term(Term.AND, new Expression[] { rdateExpr, paExpr, typeExpr});

// Setup and run query; check them for returned WI count
IQueryClient qc = (IQueryClient) repo.getClientLibrary(IQueryClient.class);
IQueryResult<IResolvedResult<IWorkItem>> result;

result = qc.getResolvedExpressionResults(pa, combined, IWorkItem.SMALL_PROFILE);

System.out.println("Combined expression query: "
+ "(rdate, defect type, and pa specific) query found:"
+ result.getTotalSize(null) + " WIs");

Given my repository I find two work items.

Your answer

Register or to post your answer.