Search in sources :

Example 1 with DatePosition

use of org.alfresco.service.cmr.workflow.WorkflowInstanceQuery.DatePosition in project alfresco-remote-api by Alfresco.

the class WorkflowInstancesGet method buildModel.

@Override
protected Map<String, Object> buildModel(WorkflowModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) {
    WorkflowInstanceQuery workflowInstanceQuery = new WorkflowInstanceQuery();
    Map<String, String> params = req.getServiceMatch().getTemplateVars();
    // state is not included into filters list as it will be taken into account before filtering
    WorkflowState state = getState(req);
    // get filter param values
    Map<QName, Object> filters = new HashMap<QName, Object>(9);
    if (req.getParameter(PARAM_INITIATOR) != null)
        filters.put(QNAME_INITIATOR, personService.getPerson(req.getParameter(PARAM_INITIATOR)));
    if (req.getParameter(PARAM_PRIORITY) != null)
        filters.put(WorkflowModel.PROP_WORKFLOW_PRIORITY, req.getParameter(PARAM_PRIORITY));
    String excludeParam = req.getParameter(PARAM_EXCLUDE);
    if (excludeParam != null && excludeParam.length() > 0) {
        workflowInstanceQuery.setExcludedDefinitions(Arrays.asList(StringUtils.tokenizeToStringArray(excludeParam, ",")));
    }
    // process all the date related parameters
    Map<DatePosition, Date> dateParams = new HashMap<DatePosition, Date>();
    Date dueBefore = getDateFromRequest(req, PARAM_DUE_BEFORE);
    if (dueBefore != null) {
        dateParams.put(DatePosition.BEFORE, dueBefore);
    }
    Date dueAfter = getDateFromRequest(req, PARAM_DUE_AFTER);
    if (dueAfter != null) {
        dateParams.put(DatePosition.AFTER, dueAfter);
    }
    if (dateParams.isEmpty()) {
        if (req.getParameter(PARAM_DUE_BEFORE) != null || req.getParameter(PARAM_DUE_AFTER) != null) {
            filters.put(WorkflowModel.PROP_WORKFLOW_DUE_DATE, null);
        }
    } else {
        filters.put(WorkflowModel.PROP_WORKFLOW_DUE_DATE, dateParams);
    }
    workflowInstanceQuery.setStartBefore(getDateFromRequest(req, PARAM_STARTED_BEFORE));
    workflowInstanceQuery.setStartAfter(getDateFromRequest(req, PARAM_STARTED_AFTER));
    workflowInstanceQuery.setEndBefore(getDateFromRequest(req, PARAM_COMPLETED_BEFORE));
    workflowInstanceQuery.setEndAfter(getDateFromRequest(req, PARAM_COMPLETED_AFTER));
    // determine if there is a definition id to filter by
    String workflowDefinitionId = params.get(VAR_DEFINITION_ID);
    if (workflowDefinitionId == null) {
        workflowDefinitionId = req.getParameter(PARAM_DEFINITION_ID);
    }
    // default workflow state to ACTIVE if not supplied
    if (state == null) {
        state = WorkflowState.ACTIVE;
    }
    workflowInstanceQuery.setActive(state == WorkflowState.ACTIVE);
    workflowInstanceQuery.setCustomProps(filters);
    List<WorkflowInstance> workflows = new ArrayList<WorkflowInstance>();
    int total = 0;
    // MNT-9074 My Tasks fails to render if tasks quantity is excessive
    int maxItems = getIntParameter(req, PARAM_MAX_ITEMS, DEFAULT_MAX_ITEMS);
    int skipCount = getIntParameter(req, PARAM_SKIP_COUNT, DEFAULT_SKIP_COUNT);
    if (workflowDefinitionId == null && req.getParameter(PARAM_DEFINITION_NAME) != null) {
        /**
         * If we are searching by workflow definition name then there may be many workflow definition instances.
         */
        int workingSkipCount = skipCount;
        /**
         * Yes there could be multiple process definitions with that definition name
         */
        String definitionName = req.getParameter(PARAM_DEFINITION_NAME);
        List<WorkflowDefinition> defs = workflowService.getAllDefinitionsByName(definitionName);
        int itemsToQuery = maxItems;
        for (WorkflowDefinition def : defs) {
            workflowDefinitionId = def.getId();
            workflowInstanceQuery.setWorkflowDefinitionId(workflowDefinitionId);
            if (maxItems < 0 || itemsToQuery > 0) {
                workflows.addAll(workflowService.getWorkflows(workflowInstanceQuery, itemsToQuery, workingSkipCount));
            }
            if (maxItems > 0) {
                itemsToQuery = maxItems - workflows.size();
            }
            total += (int) workflowService.countWorkflows(workflowInstanceQuery);
            if (workingSkipCount > 0) {
                workingSkipCount = skipCount - total;
                if (workingSkipCount < 0) {
                    workingSkipCount = 0;
                }
            }
        }
    } else {
        /**
         * This is the old single task implementation
         */
        if (workflowDefinitionId != null) {
            workflowInstanceQuery.setWorkflowDefinitionId(workflowDefinitionId);
        }
        workflows.addAll(workflowService.getWorkflows(workflowInstanceQuery, maxItems, skipCount));
        total = (int) workflowService.countWorkflows(workflowInstanceQuery);
    }
    List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(total);
    // init empty list
    results.addAll(Arrays.asList((Map<String, Object>[]) new Map[total]));
    for (WorkflowInstance workflow : workflows) {
        // set to special index
        results.set(skipCount, modelBuilder.buildSimple(workflow));
        skipCount++;
    }
    // create and return results, paginated if necessary
    return createResultModel(req, "workflowInstances", results);
}
Also used : HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) WorkflowDefinition(org.alfresco.service.cmr.workflow.WorkflowDefinition) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance) Date(java.util.Date) WorkflowInstanceQuery(org.alfresco.service.cmr.workflow.WorkflowInstanceQuery) DatePosition(org.alfresco.service.cmr.workflow.WorkflowInstanceQuery.DatePosition) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 WorkflowDefinition (org.alfresco.service.cmr.workflow.WorkflowDefinition)1 WorkflowInstance (org.alfresco.service.cmr.workflow.WorkflowInstance)1 WorkflowInstanceQuery (org.alfresco.service.cmr.workflow.WorkflowInstanceQuery)1 DatePosition (org.alfresco.service.cmr.workflow.WorkflowInstanceQuery.DatePosition)1 QName (org.alfresco.service.namespace.QName)1