It's all about the answers!

Ask a question

RTC Java API Query for workitems created last 24h


Marcin Blacha (14618) | asked Dec 01 '22, 3:41 a.m.
edited Dec 01 '22, 4:37 a.m.
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



permanent link
Ralph Schoon (61.6k33643) | answered Dec 01 '22, 11:31 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

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;
}


Comments
Ralph Schoon commented Dec 01 '22, 11:38 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Note in the server you would have to use IAuditableCommon and not the client. 


permanent link
Marcin Blacha (14618) | answered Dec 02 '22, 4:08 a.m.
edited Dec 02 '22, 4:14 a.m.
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;        }

Your answer


Register or to post your answer.