RTC Java API Query for workitems created last 24h
Hello,
I' trying to write server side async task, that will be searching for workitems created in last 24h, that state was not changed. I'm using IQueryCommon and AttributeExpresion:
Date date = new Date(); Timestamp creationDate = new Timestamp(date.getTime()-(24*60*60*1000)); AttributeExpression creationDateExpression = new AttributeExpression(attribute, AttributeOperation.BEFORE, creationDate);
but this expression is not respecting time, only date. In WebUI when you create query, you can select option created in last xx hours:
Is it possible to create this kind of expression in Java API?
2 answers
The code you show is incomplete.
In the SDK I find:
assertResult(custom, AttributeOperation.BEFORE, new Timestamp(now.getTime() + ONE_DAY), copy.getWorkItem());
So, I assume you are following the correct approach, but there is something else going on.
the SDK code:
private void assertResult(IAttribute custom, AttributeOperation operator, Object value, IWorkItem expected) throws TeamRepositoryException {
IQueryResult<IResolvedResult<IWorkItem>> result= getResult(custom, operator, value);
assertEquals(1, result.getTotalSize(null));
IWorkItem resultItem= result.next(null).getItem();
assertEquals(expected.getId(), resultItem.getId());
}
private IQueryResult<IResolvedResult<IWorkItem>> getResult(IAttribute custom, AttributeOperation operator, Object value) throws TeamRepositoryException {
IQueryableAttributeFactory factory= QueryableAttributes.getFactory(IWorkItem.ITEM_TYPE);
IQueryableAttribute attribute= factory.findAttribute(fgProjectArea, custom.getIdentifier(), fgAuditableClient, null);
AttributeExpression expression= new AttributeExpression(attribute, operator, value);
IQueryResult<IResolvedResult<IWorkItem>> result= fgQueryClient.getResolvedExpressionResults(fgProjectArea, expression, IWorkItem.SMALL_PROFILE);
result.setLimit(10);
return result;
}
Hi Ralph,
here is full function used to find workitems in project area:
private List<IWorkItem> findWorkItems(IProjectAreaHandle projectArea, IProgressMonitor monitor) throws TeamRepositoryException { List<IWorkItem> matchingWorkItems = new ArrayList<IWorkItem>(); try { IQueryResult<IResolvedResult<IWorkItem>> result; IQueryableAttribute attribute = findQueryableAttribute(projectArea, IWorkItem.CREATION_DATE_PROPERTY, monitor); if (attribute == null) return Collections.emptyList(); IWorkItemType escalationWItype = findWIType(projectArea, "Escalation", monitor); if (escalationWItype == null) return Collections.emptyList(); Date now = new Date(); AttributeExpression creationDateExpression = new AttributeExpression(attribute, AttributeOperation.BEFORE, new Timestamp(now.getTime() - (24*60*60*1000+1000))); IQueryableAttribute projectAreaAttribute = findQueryableAttribute(projectArea, IWorkItem.PROJECT_AREA_PROPERTY, monitor); AttributeExpression projectAreaExpression = new AttributeExpression(projectAreaAttribute, AttributeOperation.EQUALS, projectArea); IQueryableAttribute stateAttribute = findQueryableAttribute(projectArea, IWorkItem.STATE_PROPERTY, monitor); VariableAttributeExpression stateGroupExpression = new VariableAttributeExpression( stateAttribute, AttributeOperation.EQUALS,
new StatusVariable(IWorkflowInfo.OPEN_STATES_GROUP)); IQueryableAttribute typeAttribute = findQueryableAttribute(projectArea, IWorkItem.TYPE_PROPERTY, monitor); AttributeExpression wiTypeExpression = new AttributeExpression(typeAttribute, AttributeOperation.EQUALS, escalationWItype.getIdentifier());
Term term = new Term(Operator.AND); term.add(projectAreaExpression);
term.add(creationDateExpression);
term.add(stateGroupExpression);
term.add(wiTypeExpression);
IQueryCommon queryService = getQueryCommon(); result = queryService
.getResolvedExpressionResults(projectArea, term, IWorkItem.FULL_PROFILE); while (result.hasNext(monitor)) { matchingWorkItems.add(result.next(monitor).getItem());
} } catch (Exception ex) { getLog().error(NLS.bind("Error during finding workitems for project area {0}, error message: {1}, stack trace {2}", projectArea.toString(), ex.getMessage(), ex.getStackTrace().toString()));\ } return matchingWorkItems; }
Generally, I'm basing on example from https://jazz.net/wiki/bin/view/Main/QueryDevGuide and from yours article from https://rsjazz.wordpress.com/2015/10/16/due-date-notifier-an-asynchronous-task-example/