Search in sources :

Example 1 with QueryProperty

use of org.activiti.engine.query.QueryProperty in project carbon-business-process by wso2.

the class CorrelationProcess method getQueryResponse.

public Response getQueryResponse(CorrelationActionRequest correlationActionRequest, UriInfo uriInfo) {
    RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
    ExecutionQuery query = runtimeService.createExecutionQuery();
    String value = correlationActionRequest.getProcessDefinitionId();
    if (value != null) {
        query.processDefinitionId(value);
    }
    value = correlationActionRequest.getProcessDefinitionKey();
    if (value != null) {
        query.processDefinitionKey(value);
    }
    value = correlationActionRequest.getMessageName();
    if (value != null) {
        query.messageEventSubscriptionName(value);
    }
    value = correlationActionRequest.getSignalName();
    if (value != null) {
        query.signalEventSubscriptionName(value);
    }
    List<QueryVariable> queryVariableList = correlationActionRequest.getCorrelationVariables();
    if (queryVariableList != null) {
        List<QueryVariable> updatedQueryVariableList = new ArrayList<>();
        for (QueryVariable queryVariable : queryVariableList) {
            if (queryVariable.getVariableOperation() == null) {
                queryVariable.setOperation("equals");
            }
            updatedQueryVariableList.add(queryVariable);
        }
        addVariables(query, updatedQueryVariableList, true);
    }
    value = correlationActionRequest.getTenantId();
    if (value != null) {
        query.executionTenantId(value);
    }
    QueryProperty qp = allowedSortProperties.get("processInstanceId");
    ((AbstractQuery) query).orderBy(qp);
    query.asc();
    List<Execution> executionList = query.listPage(0, 10);
    int size = executionList.size();
    if (size == 0) {
        throw new ActivitiIllegalArgumentException("No Executions found to correlate with given information");
    }
    if (size > 1) {
        throw new ActivitiIllegalArgumentException("More than one Executions found to correlate with given information");
    }
    Execution execution = executionList.get(0);
    String action = correlationActionRequest.getAction();
    if (CorrelationActionRequest.ACTION_SIGNAL.equals(action)) {
        if (correlationActionRequest.getVariables() != null) {
            runtimeService.signal(execution.getId(), getVariablesToSet(correlationActionRequest));
        } else {
            runtimeService.signal(execution.getId());
        }
    } else if (CorrelationActionRequest.ACTION_SIGNAL_EVENT_RECEIVED.equals(action)) {
        if (correlationActionRequest.getSignalName() == null) {
            throw new ActivitiIllegalArgumentException("Signal name is required");
        }
        if (correlationActionRequest.getVariables() != null) {
            runtimeService.signalEventReceived(correlationActionRequest.getSignalName(), execution.getId(), getVariablesToSet(correlationActionRequest));
        } else {
            runtimeService.signalEventReceived(correlationActionRequest.getSignalName(), execution.getId());
        }
    } else if (CorrelationActionRequest.ACTION_MESSAGE_EVENT_RECEIVED.equals(action)) {
        if (correlationActionRequest.getMessageName() == null) {
            throw new ActivitiIllegalArgumentException("Message name is required");
        }
        if (correlationActionRequest.getVariables() != null) {
            runtimeService.messageEventReceived(correlationActionRequest.getMessageName(), execution.getId(), getVariablesToSet(correlationActionRequest));
        } else {
            runtimeService.messageEventReceived(correlationActionRequest.getMessageName(), execution.getId());
        }
    } else {
        throw new ActivitiIllegalArgumentException("Invalid action: '" + correlationActionRequest.getAction() + "'.");
    }
    Response.ResponseBuilder responseBuilder = Response.ok();
    // Re-fetch the execution, could have changed due to action or even completed
    execution = runtimeService.createExecutionQuery().executionId(execution.getId()).singleResult();
    if (execution == null) {
        // Execution is finished, return empty body to inform user
        responseBuilder.status(Response.Status.NO_CONTENT);
        return responseBuilder.build();
    } else {
        return responseBuilder.entity(new RestResponseFactory().createExecutionResponse(execution, uriInfo.getBaseUri().toString())).build();
    }
}
Also used : RuntimeService(org.activiti.engine.RuntimeService) QueryVariable(org.wso2.carbon.bpmn.rest.engine.variable.QueryVariable) ArrayList(java.util.ArrayList) CorrelationQueryProperty(org.wso2.carbon.bpmn.rest.model.common.CorrelationQueryProperty) QueryProperty(org.activiti.engine.query.QueryProperty) ExecutionQuery(org.activiti.engine.runtime.ExecutionQuery) Response(javax.ws.rs.core.Response) Execution(org.activiti.engine.runtime.Execution) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) AbstractQuery(org.activiti.engine.impl.AbstractQuery)

Example 2 with QueryProperty

use of org.activiti.engine.query.QueryProperty in project Activiti by Activiti.

the class AbstractPaginateList method paginateList.

/**
   * uses the pagination parameters form the request and makes sure to order the result and set all pagination
   * attributes for the response to render
   *
   * @param req The request containing the pagination parameters
   * @param query The query to get the paged list from
   * @param listName The name model attribute name to use for the result list
   * @param model The model to put the list and the pagination attributes in
   * @param defaultSort THe default sort column (the rest attribute) that later will be mapped to an internal engine name
   */
@SuppressWarnings("rawtypes")
public DataResponse paginateList(Map<String, String> requestParams, PaginateRequest paginateRequest, Query query, String defaultSort, Map<String, QueryProperty> properties) {
    if (paginateRequest == null) {
        paginateRequest = new PaginateRequest();
    }
    // In case pagination request is incomplete, fill with values found in URL if possible
    if (paginateRequest.getStart() == null) {
        paginateRequest.setStart(RequestUtil.getInteger(requestParams, "start", 0));
    }
    if (paginateRequest.getSize() == null) {
        paginateRequest.setSize(RequestUtil.getInteger(requestParams, "size", 10));
    }
    if (paginateRequest.getOrder() == null) {
        paginateRequest.setOrder(requestParams.get("order"));
    }
    if (paginateRequest.getSort() == null) {
        paginateRequest.setSort(requestParams.get("sort"));
    }
    // Use defaults for paging, if not set in the PaginationRequest, nor in the URL
    Integer start = paginateRequest.getStart();
    if (start == null || start < 0) {
        start = 0;
    }
    Integer size = paginateRequest.getSize();
    if (size == null || size < 0) {
        size = 10;
    }
    String sort = paginateRequest.getSort();
    if (sort == null) {
        sort = defaultSort;
    }
    String order = paginateRequest.getOrder();
    if (order == null) {
        order = "asc";
    }
    // Sort order
    if (sort != null && !properties.isEmpty()) {
        QueryProperty qp = properties.get(sort);
        if (qp == null) {
            throw new ActivitiIllegalArgumentException("Value for param 'sort' is not valid, '" + sort + "' is not a valid property");
        }
        ((AbstractQuery) query).orderBy(qp);
        if (order.equals("asc")) {
            query.asc();
        } else if (order.equals("desc")) {
            query.desc();
        } else {
            throw new ActivitiIllegalArgumentException("Value for param 'order' is not valid : '" + order + "', must be 'asc' or 'desc'");
        }
    }
    // Get result and set pagination parameters
    List list = processList(query.listPage(start, size));
    DataResponse response = new DataResponse();
    response.setStart(start);
    response.setSize(list.size());
    response.setSort(sort);
    response.setOrder(order);
    response.setTotal(query.count());
    response.setData(list);
    return response;
}
Also used : ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) QueryProperty(org.activiti.engine.query.QueryProperty) AbstractQuery(org.activiti.engine.impl.AbstractQuery) List(java.util.List)

Example 3 with QueryProperty

use of org.activiti.engine.query.QueryProperty in project carbon-business-process by wso2.

the class AbstractPaginateList method paginateList.

public DataResponse paginateList(Map<String, String> requestParams, PaginateRequest paginateRequest, Query query, String defaultSort, Map<String, QueryProperty> properties) {
    if (paginateRequest == null) {
        paginateRequest = new PaginateRequest();
    }
    // In case pagination request is incomplete, fill with values found in URL if possible
    if (paginateRequest.getStart() == null) {
        paginateRequest.setStart(RequestUtil.getInteger(requestParams, "start", 0));
    }
    if (paginateRequest.getSize() == null) {
        paginateRequest.setSize(RequestUtil.getInteger(requestParams, "size", 10));
    }
    if (paginateRequest.getOrder() == null) {
        paginateRequest.setOrder(requestParams.get("order"));
    }
    if (paginateRequest.getSort() == null) {
        paginateRequest.setSort(requestParams.get("sort"));
    }
    // Use defaults for paging, if not set in the PaginationRequest, nor in the URL
    Integer start = paginateRequest.getStart();
    if (start == null || start < 0) {
        start = 0;
    }
    Integer size = paginateRequest.getSize();
    if (size == null || size < 0) {
        size = 10;
    }
    String sort = paginateRequest.getSort();
    if (sort == null) {
        // id
        sort = defaultSort;
    }
    String order = paginateRequest.getOrder();
    if (order == null) {
        order = "asc";
    }
    // Sort order
    if (sort != null && !properties.isEmpty()) {
        QueryProperty qp = properties.get(sort);
        if (qp == null) {
            throw new ActivitiIllegalArgumentException("Value for param 'sort' is not valid, '" + sort + "' is not a valid property");
        }
        ((AbstractQuery) query).orderBy(qp);
        if (order.equals("asc")) {
            query.asc();
        } else if (order.equals("desc")) {
            query.desc();
        } else {
            throw new ActivitiIllegalArgumentException("Value for param 'order' is not valid : '" + order + "', must be 'asc' or 'desc'");
        }
    }
    // Get result and set pagination parameters
    List list = processList(query.listPage(start, size));
    DataResponse response = new DataResponse();
    response.setStart(start);
    response.setSize(list.size());
    response.setSort(sort);
    response.setOrder(order);
    response.setTotal(query.count());
    response.setData(list);
    return response;
}
Also used : DataResponse(org.wso2.carbon.bpmn.rest.model.common.DataResponse) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) QueryProperty(org.activiti.engine.query.QueryProperty) AbstractQuery(org.activiti.engine.impl.AbstractQuery) List(java.util.List)

Aggregations

ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)3 AbstractQuery (org.activiti.engine.impl.AbstractQuery)3 QueryProperty (org.activiti.engine.query.QueryProperty)3 List (java.util.List)2 ArrayList (java.util.ArrayList)1 Response (javax.ws.rs.core.Response)1 RuntimeService (org.activiti.engine.RuntimeService)1 Execution (org.activiti.engine.runtime.Execution)1 ExecutionQuery (org.activiti.engine.runtime.ExecutionQuery)1 QueryVariable (org.wso2.carbon.bpmn.rest.engine.variable.QueryVariable)1 CorrelationQueryProperty (org.wso2.carbon.bpmn.rest.model.common.CorrelationQueryProperty)1 DataResponse (org.wso2.carbon.bpmn.rest.model.common.DataResponse)1