Search in sources :

Example 6 with WkfTaskConfig

use of com.axelor.apps.bpm.db.WkfTaskConfig in project axelor-open-suite by axelor.

the class WkfDisplayServiceImpl method addActiveNodes.

private void addActiveNodes(List<Map<String, Object>> statusList, WkfInstance wkfInstance, String klassName) {
    HistoryService historyService = engineService.getEngine().getHistoryService();
    List<HistoricActivityInstance> activeNodes = historyService.createHistoricActivityInstanceQuery().processInstanceId(wkfInstance.getInstanceId()).unfinished().list();
    if (activeNodes.isEmpty()) {
        List<String> terminatedNodeIds = getTerminatedActivityIds(wkfInstance.getInstanceId());
        if (!terminatedNodeIds.isEmpty()) {
            activeNodes = historyService.createHistoricActivityInstanceQuery().activityId(terminatedNodeIds.get(0)).processInstanceId(wkfInstance.getInstanceId()).list();
        }
    }
    User activeUser = AuthUtils.getUser();
    for (HistoricActivityInstance node : activeNodes) {
        boolean valid = isValidNode(node.getActivityId(), wkfInstance.getWkfProcess(), klassName);
        if (!valid) {
            continue;
        }
        String title = node.getActivityName();
        if (title == null) {
            title = node.getActivityId();
        }
        String color = wkfInstance.getWkfProcess().getWkfModel().getWkfStatusColor();
        if (color == null) {
            color = "green";
        }
        Map<String, Object> statusMap = new HashMap<>();
        statusMap.put("name", node.getActivityId());
        statusMap.put("title", title);
        statusMap.put("color", color);
        if (!activeUser.getNoHelp()) {
            WkfTaskConfig config = wkfTaskConfigRepository.all().filter("self.name = ? and self.wkfModel.id = ?", node.getActivityId(), wkfInstance.getWkfProcess().getWkfModel().getId()).fetchOne();
            if (config != null) {
                statusMap.put("help", config.getHelpText());
            }
        }
        statusList.add(statusMap);
    }
}
Also used : User(com.axelor.auth.db.User) HashMap(java.util.HashMap) HistoryService(org.camunda.bpm.engine.HistoryService) WkfTaskConfig(com.axelor.apps.bpm.db.WkfTaskConfig) HistoricActivityInstance(org.camunda.bpm.engine.history.HistoricActivityInstance)

Example 7 with WkfTaskConfig

use of com.axelor.apps.bpm.db.WkfTaskConfig in project axelor-open-suite by axelor.

the class WkfTaskServiceImpl method runTasks.

@Override
public String runTasks(ProcessEngine engine, WkfInstance instance, ProcessInstance processInstance, String signal) throws ClassNotFoundException, AxelorException {
    WkfProcess wkfProcess = instance.getWkfProcess();
    List<Task> tasks = getActiveTasks(engine, processInstance.getId());
    boolean taskExecuted = false;
    String helpText = null;
    Map<String, Object> context = getContext(instance);
    // TODO: Check if its required both variables from context and from processInstance, if
    Map<String, Object> processVariables = engine.getRuntimeService().getVariables(processInstance.getId());
    processVariables.entrySet().removeIf(it -> Strings.isNullOrEmpty(it.getKey()));
    Map<String, Object> expressionVariables = null;
    Map<String, Object> ctxVariables = wkfService.createVariables(context);
    for (Task task : tasks) {
        WkfTaskConfig config = wkfTaskConfigRepository.all().filter("self.name = ? and self.wkfModel.id = ?", task.getTaskDefinitionKey(), wkfProcess.getWkfModel().getId()).fetchOne();
        if (config == null) {
            continue;
        }
        List<String> validButtons = getValidButtons(signal, config.getButton());
        if (validButtons == null) {
            continue;
        }
        if (expressionVariables == null) {
            expressionVariables = new HashMap<String, Object>();
            expressionVariables.putAll(processVariables);
            expressionVariables.putAll(context);
        }
        if (!validButtons.isEmpty() || config.getExpression() != null) {
            Map<String, Object> btnVariables = new HashMap<String, Object>();
            for (String button : validButtons) {
                btnVariables.put(button, button.equals(signal));
            }
            Map<String, Object> variables = wkfService.createVariables(btnVariables);
            variables.putAll(ctxVariables);
            if (config.getExpression() != null) {
                expressionVariables.putAll(engine.getTaskService().getVariables(task.getId()));
                expressionVariables.entrySet().removeIf(it -> Strings.isNullOrEmpty(it.getKey()));
                Boolean validExpr = (Boolean) wkfService.evalExpression(expressionVariables, config.getExpression());
                if (validExpr == null || !validExpr) {
                    log.debug("Not a valid expr: {}", config.getExpression());
                    if (!validButtons.isEmpty()) {
                        helpText = config.getHelpText();
                    }
                    continue;
                }
                log.debug("Valid expr: {}", config.getExpression());
            }
            User user = AuthUtils.getUser();
            if (user != null) {
                engine.getTaskService().setAssignee(task.getId(), user.getId().toString());
            } else {
                engine.getTaskService().setAssignee(task.getId(), "0");
            }
            engine.getTaskService().complete(task.getId(), variables);
            taskExecuted = true;
        }
    }
    Execution execution = engine.getRuntimeService().createExecutionQuery().active().executionId(processInstance.getId()).singleResult();
    if (execution != null) {
        engine.getRuntimeService().setVariables(execution.getId(), ctxVariables);
    }
    recursiveTaskExecutionCount++;
    if (recursiveTaskExecutionCount >= RECURSIVE_TASK_EXECUTION_COUNT_LIMIT && ChronoUnit.SECONDS.between(recursiveTaskExecutionTime, LocalTime.now()) <= RECURSIVE_TASK_EXECUTION_SECONDS_LIMIT) {
        throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(ITranslation.INFINITE_EXECUTION));
    }
    if (taskExecuted && wkfInstanceService.isActiveProcessInstance(processInstance.getId(), engine.getRuntimeService())) {
        log.debug("Check tasks again");
        runTasks(engine, instance, processInstance, signal);
    }
    return helpText;
}
Also used : AxelorException(com.axelor.exception.AxelorException) Task(org.camunda.bpm.engine.task.Task) User(com.axelor.auth.db.User) HashMap(java.util.HashMap) WkfTaskConfig(com.axelor.apps.bpm.db.WkfTaskConfig) WkfProcess(com.axelor.apps.bpm.db.WkfProcess) Execution(org.camunda.bpm.engine.runtime.Execution)

Aggregations

WkfTaskConfig (com.axelor.apps.bpm.db.WkfTaskConfig)7 HashMap (java.util.HashMap)3 User (com.axelor.auth.db.User)2 WkfProcess (com.axelor.apps.bpm.db.WkfProcess)1 WkfTaskConfigRepository (com.axelor.apps.bpm.db.repo.WkfTaskConfigRepository)1 WkfCommonService (com.axelor.apps.bpm.service.WkfCommonService)1 WkfInstanceService (com.axelor.apps.bpm.service.execution.WkfInstanceService)1 FullContext (com.axelor.apps.tool.context.FullContext)1 Model (com.axelor.db.Model)1 AxelorException (com.axelor.exception.AxelorException)1 MetaAttrs (com.axelor.meta.db.MetaAttrs)1 MetaJsonRecord (com.axelor.meta.db.MetaJsonRecord)1 ArrayList (java.util.ArrayList)1 MultiMap (org.apache.commons.collections.MultiMap)1 MultiValueMap (org.apache.commons.collections.map.MultiValueMap)1 HistoryService (org.camunda.bpm.engine.HistoryService)1 HistoricActivityInstance (org.camunda.bpm.engine.history.HistoricActivityInstance)1 Execution (org.camunda.bpm.engine.runtime.Execution)1 Task (org.camunda.bpm.engine.task.Task)1 Activity (org.camunda.bpm.model.bpmn.instance.Activity)1