How to query in server plug-in?
2 answers
I can find useful information here: https://jazz.net/wiki/bin/view/Main/QueryDevGuide
Here is an example that I made to find a workitem with summary starting with given string
Here is an example that I made to find a workitem with summary starting with given string
public List<IWorkItem> findWorkItemBySummary(String name, IProjectAreaHandle projectHandler) {
try {
IWorkItemServer workItemServer = getService(IWorkItemServer.class);
IQueryCommon queryService= getService(IQueryCommon.class);
IQueryableAttributeFactory factory = QueryableAttributes.getFactory(IWorkItem.ITEM_TYPE);
IQueryableAttribute projectAreaAttribute = factory.findAttribute(projectHandler, IWorkItem.PROJECT_AREA_PROPERTY, workItemServer.getAuditableServer(), null);
IQueryableAttribute workItemNameAttribute = factory.findAttribute(projectHandler, IWorkItem.SUMMARY_PROPERTY, workItemServer.getAuditableServer(), null);
AttributeExpression projectExpression = new AttributeExpression(projectAreaAttribute, AttributeOperation.EQUALS, projectHandler);
AttributeExpression summaryExpression = new AttributeExpression(workItemNameAttribute, AttributeOperation.STARTS_WITH, XMLString.createFromPlainText(name));
Term term= new Term(Operator.AND);
term.add(projectExpression);
term.add(summaryExpression);
SortCriteria[] sortCriteria= new SortCriteria[] { new SortCriteria(IWorkItem.SUMMARY_PROPERTY, true) };
Statement statement = new Statement(new SelectClause(), (Expression) term, sortCriteria);
IQueryResult<IResolvedResult<IWorkItem>> qryResult = queryService.getResolvedExpressionResults( projectHandler, (Expression) statement, IWorkItem.FULL_PROFILE);
List<IWorkItem> matchingWorkItems = new ArrayList<IWorkItem>(qryResult.getResultSize(null).getTotalAvailable());
while (qryResult.hasNext(null)) {
matchingWorkItems.add(qryResult.next(null).getItem());
}
return matchingWorkItems;
} catch (IllegalArgumentException e) {
return null;
} catch (TeamRepositoryException e) {
return null;
}
}
Thanks for your help.
I'll try to implement it.
I'll try to implement it.
I can find useful information here: https://jazz.net/wiki/bin/view/Main/QueryDevGuide
Here is an example that I made to find a workitem with summary starting with given string
public List<IWorkItem> findWorkItemBySummary(String name, IProjectAreaHandle projectHandler) {
try {
IWorkItemServer workItemServer = getService(IWorkItemServer.class);
IQueryCommon queryService= getService(IQueryCommon.class);
IQueryableAttributeFactory factory = QueryableAttributes.getFactory(IWorkItem.ITEM_TYPE);
IQueryableAttribute projectAreaAttribute = factory.findAttribute(projectHandler, IWorkItem.PROJECT_AREA_PROPERTY, workItemServer.getAuditableServer(), null);
IQueryableAttribute workItemNameAttribute = factory.findAttribute(projectHandler, IWorkItem.SUMMARY_PROPERTY, workItemServer.getAuditableServer(), null);
AttributeExpression projectExpression = new AttributeExpression(projectAreaAttribute, AttributeOperation.EQUALS, projectHandler);
AttributeExpression summaryExpression = new AttributeExpression(workItemNameAttribute, AttributeOperation.STARTS_WITH, XMLString.createFromPlainText(name));
Term term= new Term(Operator.AND);
term.add(projectExpression);
term.add(summaryExpression);
SortCriteria[] sortCriteria= new SortCriteria[] { new SortCriteria(IWorkItem.SUMMARY_PROPERTY, true) };
Statement statement = new Statement(new SelectClause(), (Expression) term, sortCriteria);
IQueryResult<IResolvedResult<IWorkItem>> qryResult = queryService.getResolvedExpressionResults( projectHandler, (Expression) statement, IWorkItem.FULL_PROFILE);
List<IWorkItem> matchingWorkItems = new ArrayList<IWorkItem>(qryResult.getResultSize(null).getTotalAvailable());
while (qryResult.hasNext(null)) {
matchingWorkItems.add(qryResult.next(null).getItem());
}
return matchingWorkItems;
} catch (IllegalArgumentException e) {
return null;
} catch (TeamRepositoryException e) {
return null;
}
}