how can be created a work item query with a team area property using the Java API?
3 answers
You need to determing Category to Team/Project Area. Assigning the work item category implicitly associates the work item to a process area ( Team or Project ). The attribute is exposed as Filed Against, but underneath it is the category.
This little snippet might help you see how the categories can be gotten from project area:
IWorkItemClient service = getWorkItemClient(); // "private" method to my Jazz client class. Your work probably already can do this.
List<ICategory> findCategories=null;
try {
// jazzServer.getProjectArea() returns an IProjectArea, you might have it another way.
findCategories = service.findCategories(
jazzServer.getProjectArea(), ICategory.FULL_PROFILE,
jazzServer.getMonitor());
} catch (TeamRepositoryException e) {
// TODO Auto-generated catch block
logger.error("Exception",e);
}
for (ICategory category : findCategories) {
if (!category_cache.containsKey(category.getHierarchicalName())) {
logger.debug("Adding category name "+category.getHierarchicalName()+" to cache.");
category_cache.put(category.getHierarchicalName(), category);
}
}
The ICategory object has methods to get the associated process area, but the query should use the category.
Comments
Right, the TeamArea Property is not an attribute. as opposed to com.ibm.team.workitem.common.model.IWorkItem.PROJECT_AREA_PROPERTY. It is derived by using the category and you will likely have to get the categories from the team area and then search for the categories that are relevant.
See
Comments
Yes, I have gone through these wonderful articles.
@ Kevin Ramer: Thanks for the useful hint!
private void printQueryResults(IProcessArea fProcessArea,
IProgressMonitor monitor) throws TeamRepositoryException {
IWorkItemCommon workItemCommonService = (IWorkItemCommon) teamRepository
.getClientLibrary(IWorkItemCommon.class);
IQueryCommon queryCommon = (IQueryCommon) teamRepository
.getClientLibrary(IQueryCommon.class);
IQueryableAttributeFactory attrFactory = QueryableAttributes
.getFactory(IWorkItem.ITEM_TYPE);
IQueryableAttribute attribProjectArea = attrFactory.findAttribute(
fProcessArea.getProjectArea(), IWorkItem.PROJECT_AREA_PROPERTY,
queryCommon.getAuditableCommon(), monitor);
Term workItemsInProcessArea = new Term(Term.Operator.AND);
Expression inProjectArea = new AttributeExpression(attribProjectArea,
AttributeOperation.EQUALS, fProcessArea.getProjectArea());
workItemsInProcessArea.add(inProjectArea);
Term term = generateCategoryTerm(fProcessArea, workItemCommonService,
monitor);
workItemsInProcessArea.add(term);
IQueryDescriptor descriptor = queryCommon.createQuery(
fProcessArea.getProjectArea(), "WorkItemsInProcessArea",
"WorkItemsInProcessArea", workItemsInProcessArea);
IQueryResult<IResolvedResult<IWorkItem>> resolvedResults = queryCommon
.getResolvedQueryResults(descriptor, IWorkItem.SMALL_PROFILE);
resolvedResults.setLimit(Integer.MAX_VALUE);
processResolvedResults(resolvedResults, monitor);
}
private static Term generateCategoryTerm(
IProcessArea processArea, IWorkItemCommon workItemCommonService,
IProgressMonitor monitor) throws TeamRepositoryException {
List<ICategory> processAreaCategories = workItemCommonService
.findCategoriesOfProcessArea(processArea,
ICategory.FULL_PROFILE, monitor);
Term processAreaCategoriesTerm = new Term(Operator.OR);
IQueryableAttribute categoryAttribute = QueryableAttributes.getFactory(
IWorkItem.ITEM_TYPE).findAttribute(
processArea.getProjectArea(), IWorkItem.CATEGORY_PROPERTY,
workItemCommonService.getAuditableCommon(), monitor);
for (ICategory category : processAreaCategories) {
Expression categoryExpression = new AttributeExpression(
categoryAttribute, AttributeOperation.EQUALS, category);
processAreaCategoriesTerm.add(categoryExpression);
}
return processAreaCategoriesTerm;
}
private static void processResolvedResults(
IQueryResult<IResolvedResult<IWorkItem>> resolvedResults,
IProgressMonitor monitor) throws TeamRepositoryException {
long processed = 0;
while (resolvedResults.hasNext(monitor)) {
IResolvedResult<IWorkItem> result = resolvedResults.next(monitor);
IWorkItem workItem = (IWorkItem) result.getItem();
System.out.println(workItem.getId());
processed++;
}
System.out.println("Processed work items: " + processed);
}