Search in sources :

Example 1 with AdvancedQueryContext

use of org.kie.api.runtime.query.AdvancedQueryContext in project jbpm by kiegroup.

the class QueryServiceImplTest method testGetProcessInstancesWithOrderByClause.

/*
     * JBPM-5468 adding support for full ORDER BY clause with QueryService.  This test
     * selects 3 processes and verifies sorting criteria specified by the ORDER BY clause.
     */
@Test
public void testGetProcessInstancesWithOrderByClause() {
    query = new SqlQueryDefinition("getAllProcessInstances", dataSourceJNDIname);
    query.setExpression("select * from processinstancelog");
    queryService.registerQuery(query);
    List<QueryDefinition> queries = queryService.getQueries(new QueryContext());
    assertNotNull(queries);
    assertEquals(1, queries.size());
    QueryDefinition registeredQuery = queries.get(0);
    assertNotNull(registeredQuery);
    assertEquals(query.getName(), registeredQuery.getName());
    assertEquals(query.getSource(), registeredQuery.getSource());
    assertEquals(query.getExpression(), registeredQuery.getExpression());
    assertEquals(query.getTarget(), registeredQuery.getTarget());
    registeredQuery = queryService.getQuery(query.getName());
    assertNotNull(registeredQuery);
    assertEquals(query.getName(), registeredQuery.getName());
    assertEquals(query.getSource(), registeredQuery.getSource());
    assertEquals(query.getExpression(), registeredQuery.getExpression());
    assertEquals(query.getTarget(), registeredQuery.getTarget());
    List<ProcessInstanceDesc> instances = queryService.query(query.getName(), ProcessInstanceQueryMapper.get(), new AdvancedQueryContext());
    assertNotNull(instances);
    assertEquals(0, instances.size());
    processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), "org.jbpm.writedocument");
    assertNotNull(processInstanceId);
    Long processInstanceId2 = processService.startProcess(deploymentUnit.getIdentifier(), "org.jbpm.writedocument");
    assertNotNull(processInstanceId2);
    Long processInstanceId3 = processService.startProcess(deploymentUnit.getIdentifier(), "org.jboss.qa.bpms.HumanTask");
    assertNotNull(processInstanceId3);
    String orderByClause = "processId asc, processInstanceId desc";
    instances = queryService.query(query.getName(), ProcessInstanceQueryMapper.get(), new AdvancedQueryContext(orderByClause));
    assertNotNull(instances);
    assertEquals(3, instances.size());
    // Check that sorting criteria for processInstanceId is descending
    assertTrue("Testing ORDER BY clause with descending sort order on processInstanceId failed", instances.get(2).getId() < instances.get(1).getId());
    processService.abortProcessInstance(processInstanceId2);
    processService.abortProcessInstance(processInstanceId);
    processInstanceId = null;
}
Also used : SqlQueryDefinition(org.jbpm.kie.services.impl.query.SqlQueryDefinition) QueryDefinition(org.jbpm.services.api.query.model.QueryDefinition) SqlQueryDefinition(org.jbpm.kie.services.impl.query.SqlQueryDefinition) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) AdvancedQueryContext(org.kie.api.runtime.query.AdvancedQueryContext) AdvancedQueryContext(org.kie.api.runtime.query.AdvancedQueryContext) QueryContext(org.kie.api.runtime.query.QueryContext) AbstractKieServicesBaseTest(org.jbpm.kie.test.util.AbstractKieServicesBaseTest) Test(org.junit.Test)

Example 2 with AdvancedQueryContext

use of org.kie.api.runtime.query.AdvancedQueryContext in project jbpm by kiegroup.

the class QueryServiceImpl method query.

@Override
public <T> T query(String queryName, QueryResultMapper<T> mapper, QueryContext queryContext, QueryParamBuilder<?> paramBuilder) throws QueryNotFoundException {
    if (dataSetDefRegistry.getDataSetDef(queryName) == null) {
        throw new QueryNotFoundException("Query " + queryName + " not found");
    }
    logger.debug("About to query using {} definition with number of rows {} and starting at {} offset", queryName, queryContext.getCount(), queryContext.getOffset());
    DataSetLookupBuilder<?> builder = DataSetLookupFactory.newDataSetLookupBuilder().dataset(queryName).rowNumber(queryContext.getCount()).rowOffset(queryContext.getOffset());
    Object filter = paramBuilder.build();
    while (filter != null) {
        if (filter instanceof ColumnFilter) {
            // add filter
            builder.filter((ColumnFilter) filter);
        } else if (filter instanceof AggregateColumnFilter) {
            // add aggregate function
            builder.column(((AggregateColumnFilter) filter).getColumnId(), ((AggregateColumnFilter) filter).getType(), ((AggregateColumnFilter) filter).getColumnId());
        } else if (filter instanceof GroupColumnFilter) {
            GroupColumnFilter groupFilter = ((GroupColumnFilter) filter);
            // add group function
            builder.group(((GroupColumnFilter) filter).getColumnId(), ((GroupColumnFilter) filter).getNewColumnId());
            if (groupFilter.getIntervalSize() != null) {
                ((AbstractDataSetLookupBuilder<?>) builder).dynamic(groupFilter.getMaxIntervals(), DateIntervalType.valueOf(groupFilter.getIntervalSize()), true);
            }
        } else if (filter instanceof ExtraColumnFilter) {
            // add extra column
            builder.column(((ExtraColumnFilter) filter).getColumnId(), ((ExtraColumnFilter) filter).getNewColumnId());
        } else {
            logger.warn("Unsupported filter '{}' generated by '{}'", filter, paramBuilder);
        }
        // call builder again in case more parameters are available
        filter = paramBuilder.build();
    }
    // if advanced ordering is used process the ORDER BY clause into order by and sort order pairs
    if (queryContext instanceof AdvancedQueryContext && ((AdvancedQueryContext) queryContext).getOrderByClause() != null && !((AdvancedQueryContext) queryContext).getOrderByClause().isEmpty()) {
        String[] orderBySortOrderItems = ((AdvancedQueryContext) queryContext).getOrderByClause().split(",");
        for (String orderBySortOrderItem : orderBySortOrderItems) {
            String[] orderBySortOrder = orderBySortOrderItem.trim().split(" ");
            // check that sort order is given.  default to 'asc'.
            String sortOrder = orderBySortOrder.length == 1 ? "asc" : orderBySortOrder[1].trim();
            logger.debug("Applying order by clause '{}' with {}ending sort order", orderBySortOrder[0].trim(), sortOrder);
            builder.sort(orderBySortOrder[0].trim(), sortOrder);
        }
    } else {
        // use default simple ordering
        if (queryContext.getOrderBy() != null) {
            String[] oderByItems = queryContext.getOrderBy().split(",");
            for (String orderBy : oderByItems) {
                logger.debug("Applying order by {} and ascending {}", orderBy, queryContext.isAscending());
                builder.sort(orderBy.trim(), queryContext.isAscending() ? "asc" : "desc");
            }
        }
    }
    DataSet result = dataSetManager.lookupDataSet(builder.buildLookup());
    logger.debug("Query result is {}", result);
    T mappedResult = mapper.map(result);
    logger.debug("Mapped result is {}", mappedResult);
    return mappedResult;
}
Also used : AbstractDataSetLookupBuilder(org.dashbuilder.dataset.impl.AbstractDataSetLookupBuilder) DataSet(org.dashbuilder.dataset.DataSet) AdvancedQueryContext(org.kie.api.runtime.query.AdvancedQueryContext) ColumnFilter(org.dashbuilder.dataset.filter.ColumnFilter) QueryNotFoundException(org.jbpm.services.api.query.QueryNotFoundException)

Aggregations

AdvancedQueryContext (org.kie.api.runtime.query.AdvancedQueryContext)2 DataSet (org.dashbuilder.dataset.DataSet)1 ColumnFilter (org.dashbuilder.dataset.filter.ColumnFilter)1 AbstractDataSetLookupBuilder (org.dashbuilder.dataset.impl.AbstractDataSetLookupBuilder)1 SqlQueryDefinition (org.jbpm.kie.services.impl.query.SqlQueryDefinition)1 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)1 ProcessInstanceDesc (org.jbpm.services.api.model.ProcessInstanceDesc)1 QueryNotFoundException (org.jbpm.services.api.query.QueryNotFoundException)1 QueryDefinition (org.jbpm.services.api.query.model.QueryDefinition)1 Test (org.junit.Test)1 QueryContext (org.kie.api.runtime.query.QueryContext)1