It's all about the answers!

Ask a question

How to use ParameterVariable using plain java API to run parameterized query?


Andy Jewell (24236374) | asked Apr 07 '14, 8:06 p.m.
edited Apr 07 '14, 8:07 p.m.
 Hi. . . I'm trying to run a shared query that has a run-time variable - i.e., when the user runs it from the UI RTC prompts them for a value for the variable expression.  I've search this site as well as Ralph Schoon's blog but didn't find anything.

I've found the query.setParameterValues(arg0) method and it seems promising but I'm not sure how to set up an Expression:

ParameterVariableContext ctx = new ParameterVariableContext();
addVariableValue("value to match?", <Expression> );
query.setParameterValues(ctx);

Any suggestions or examples would be greatly appreciated!

Andy

One answer



permanent link
Andy Jewell (24236374) | answered Apr 08 '14, 12:04 p.m.
 I didn't find out exactly how to replace parameterized expressions in a saved query, I guess in my case it wasn't strictly required.  I used some code from Ralph (link above) to just create a query on the fly that served my purpose:

public List<String> findWorkitemsByIssue(String issuenumber) throws TeamRepositoryException{
log.trace("entered");
String propname = "Summary";
        List<String> itemids = new ArrayList<String>();
IQueryClient queryClient = client.getQueryClient();
IQueryableAttribute idAttribute = findAttribute(propname);
log.debug("for custom attribute {}, found attr (displayname) {}",propname,idAttribute.getDisplayName());

AttributeExpression summaryContains = new AttributeExpression(idAttribute,AttributeOperation.CONTAINS, issuenumber);
IQueryableAttribute projectAreaAttribute = findAttribute(IWorkItem.PROJECT_AREA_PROPERTY);
	AttributeExpression projectAreaExpression = new AttributeExpression(
projectAreaAttribute, AttributeOperation.EQUALS, project_area);
Term term = new Term(Operator.AND);
term.add(projectAreaExpression);
term.add(summaryContains);
IQueryResult<IResolvedResult<IWorkItem>> results = queryClient.getResolvedExpressionResults(project_area, term, IWorkItem.FULL_PROFILE);
results.setLimit(Integer.MIN_VALUE);
while (results.hasNext(monitor)) {
IResolvedResult<IWorkItem> resresult = (IResolvedResult<IWorkItem>) results
.next(monitor);
IWorkItem item = resresult.getItem();
String strid = Integer.toString(item.getId()); 
log.trace("Found ID {} - {}",strid,item.getHTMLSummary());
itemids.add(strid);
}

        return itemids;
}

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.