Search in sources :

Example 6 with WkfProcess

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

the class BpmDeploymentServiceImpl method deployProcess.

protected Map<String, String> deployProcess(ProcessEngine engine, DeploymentBuilder deploymentBuilder, BpmnModelInstance bpmInstance) {
    Deployment deployment = deploymentBuilder.deploy();
    Map<String, String> processMap = new HashMap<String, String>();
    List<ProcessDefinition> definitions = engine.getRepositoryService().createProcessDefinitionQuery().deploymentId(deployment.getId()).list();
    if (wkfModel.getDeploymentId() != null && migrationMap != null) {
        migrateRunningInstances(wkfModel.getDeploymentId(), engine, definitions);
    }
    if (definitions.size() == 1) {
        wkfModel.setVersionTag(definitions.get(0).getVersionTag());
    }
    wkfModel.setDeploymentId(deployment.getId());
    log.debug("Definitions deployed: {}", definitions.size());
    for (ProcessDefinition definition : definitions) {
        WkfProcess process = wkfProcessRepository.all().filter("self.name = ? and self.wkfModel.id = ?", definition.getKey(), wkfModel.getId()).fetchOne();
        if (process == null) {
            process = new WkfProcess();
            wkfModel.addWkfProcessListItem(process);
        }
        process.setName(definition.getKey());
        process.setProcessId(definition.getId());
        process.setDescription(definition.getName());
        addProcessConfig(bpmInstance, process);
        addDisplayProperties(bpmInstance, process);
        processMap.put(definition.getKey(), definition.getId());
    }
    engine.getManagementService().registerProcessApplication(deployment.getId(), Beans.get(WkfProcessApplication.class).getReference());
    return processMap;
}
Also used : WkfProcess(com.axelor.apps.bpm.db.WkfProcess) HashMap(java.util.HashMap) Deployment(org.camunda.bpm.engine.repository.Deployment) ProcessDefinition(org.camunda.bpm.engine.repository.ProcessDefinition)

Example 7 with WkfProcess

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

the class WkfDisplayServiceImpl method getInstanceUrl.

@Override
@CallMethod
public String getInstanceUrl(WkfInstance wkfInstance) {
    try {
        ProcessEngine engine = engineService.getEngine();
        String processInstanceId = wkfInstance.getInstanceId();
        List<String> activeIds = getActivityIds(processInstanceId);
        Map<String, Integer> activityCountMap = new HashMap<>();
        getActivityPassCount(wkfInstance.getInstanceId(), activityCountMap);
        WkfProcess wkfProcess = wkfInstance.getWkfProcess();
        BpmnModelInstance modelInstance = engine.getRepositoryService().getBpmnModelInstance(wkfProcess.getProcessId());
        Iterator<Process> processIter = modelInstance.getModelElementsByType(Process.class).iterator();
        HistoricVariableInstanceQuery variableQuery = engine.getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(processInstanceId);
        while (processIter.hasNext()) {
            Process process = processIter.next();
            if (process.getId().equals(wkfProcess.getName())) {
                continue;
            }
            HistoricVariableInstance variableInstance = variableQuery.variableName(process.getId()).singleResult();
            if (variableInstance != null) {
                activeIds.addAll(getActivityIds((String) variableInstance.getValue()));
                getActivityPassCount((String) variableInstance.getValue(), activityCountMap);
            }
        }
        String activityCount = activityCountMap.keySet().stream().map(it -> it + ":" + activityCountMap.get(it)).collect(Collectors.joining(","));
        log.trace("Active node ids: {}", activeIds);
        String url = String.format(engineService.getWkfViewerUrl(), "instanceId=" + wkfInstance.getInstanceId(), Joiner.on(",").join(activeIds), activityCount);
        log.debug("Url created: {}", url);
        return url;
    } catch (Exception e) {
    }
    return null;
}
Also used : HistoryService(org.camunda.bpm.engine.HistoryService) CallMethod(com.axelor.meta.CallMethod) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HistoricProcessInstance(org.camunda.bpm.engine.history.HistoricProcessInstance) HistoricProcessInstanceQuery(org.camunda.bpm.engine.history.HistoricProcessInstanceQuery) ProcessEngineService(com.axelor.apps.bpm.service.init.ProcessEngineService) WkfInstanceRepository(com.axelor.apps.bpm.db.repo.WkfInstanceRepository) WkfInstanceService(com.axelor.apps.bpm.service.execution.WkfInstanceService) WkfInstance(com.axelor.apps.bpm.db.WkfInstance) Map(java.util.Map) BpmnModelInstance(org.camunda.bpm.model.bpmn.BpmnModelInstance) HistoricVariableInstanceQuery(org.camunda.bpm.engine.history.HistoricVariableInstanceQuery) WkfTaskConfigRepository(com.axelor.apps.bpm.db.repo.WkfTaskConfigRepository) WkfModel(com.axelor.apps.bpm.db.WkfModel) JPA(com.axelor.db.JPA) Logger(org.slf4j.Logger) Model(com.axelor.db.Model) Iterator(java.util.Iterator) ActivityTypes(org.camunda.bpm.engine.ActivityTypes) Set(java.util.Set) ProcessEngine(org.camunda.bpm.engine.ProcessEngine) WkfProcess(com.axelor.apps.bpm.db.WkfProcess) Collectors(java.util.stream.Collectors) HistoricActivityInstance(org.camunda.bpm.engine.history.HistoricActivityInstance) List(java.util.List) Beans(com.axelor.inject.Beans) WkfTaskConfig(com.axelor.apps.bpm.db.WkfTaskConfig) Process(org.camunda.bpm.model.bpmn.instance.Process) HistoricVariableInstance(org.camunda.bpm.engine.history.HistoricVariableInstance) RuntimeService(org.camunda.bpm.engine.RuntimeService) AuthUtils(com.axelor.auth.AuthUtils) User(com.axelor.auth.db.User) MetaJsonRecord(com.axelor.meta.db.MetaJsonRecord) Joiner(com.google.common.base.Joiner) HashMap(java.util.HashMap) WkfProcess(com.axelor.apps.bpm.db.WkfProcess) Process(org.camunda.bpm.model.bpmn.instance.Process) BpmnModelInstance(org.camunda.bpm.model.bpmn.BpmnModelInstance) WkfProcess(com.axelor.apps.bpm.db.WkfProcess) HistoricVariableInstanceQuery(org.camunda.bpm.engine.history.HistoricVariableInstanceQuery) HistoricVariableInstance(org.camunda.bpm.engine.history.HistoricVariableInstance) ProcessEngine(org.camunda.bpm.engine.ProcessEngine) CallMethod(com.axelor.meta.CallMethod)

Example 8 with WkfProcess

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

the class BpmDashboardServiceImpl method getWkfModelsByUser.

private List<WkfModel> getWkfModelsByUser(User user) {
    List<WkfModel> wkfModelList = wkfModelRepo.all().order("code").fetch();
    List<WkfModel> filterWkfModels = new ArrayList<>();
    for (WkfModel wkfModel : wkfModelList) {
        List<WkfProcess> processes = wkfModelService.getProcesses(wkfModel);
        if (CollectionUtils.isEmpty(processes)) {
            continue;
        }
        boolean isSuperAdmin = user.getCode().equals("admin");
        boolean isAdmin = wkfModelController.isAdmin(wkfModel, user);
        boolean isManager = wkfModelController.isManager(wkfModel, user);
        boolean isUser = wkfModelController.isUser(wkfModel, user);
        if (!isSuperAdmin && !isAdmin && !isManager && !isUser) {
            continue;
        }
        filterWkfModels.add(wkfModel);
    }
    return filterWkfModels;
}
Also used : WkfProcess(com.axelor.apps.bpm.db.WkfProcess) WkfModel(com.axelor.apps.bpm.db.WkfModel) ArrayList(java.util.ArrayList)

Example 9 with WkfProcess

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

the class WkfInstanceServiceImpl method startInstance.

protected void startInstance(WkfProcessConfig wkfProcessConfig, Model model) throws AxelorException {
    WkfProcessConfig previousConfig = wkfService.findOldProcessConfig(model);
    if (previousConfig != null && previousConfig.getProcessPath() != null && addRelatedInstance(model, previousConfig)) {
        return;
    }
    log.debug("Start instance for the model: {}, id: {}", model.getClass().getName(), model.getId());
    WkfProcess wkfProcess = wkfProcessConfig.getWkfProcess();
    ProcessEngine engine = engineService.getEngine();
    RuntimeService runTimeService = engine.getRuntimeService();
    ProcessInstantiationBuilder builder = runTimeService.createProcessInstanceById(wkfProcess.getProcessId());
    Map<String, Object> modelMap = new HashMap<String, Object>();
    modelMap.put(wkfService.getVarName(model), new FullContext(model));
    builder.setVariables(wkfService.createVariables(modelMap));
    ProcessInstance processInstance = builder.executeWithVariablesInReturn();
    WkfInstance instance = wkfInstanceRepository.findByInstnaceId(model.getProcessInstanceId());
    if (instance != null) {
        instance.setModelId(model.getId());
        instance.setModelName(model.getClass().getName());
        instance.setProcessInstanceId(processInstance.getId());
    }
    model.setProcessInstanceId(processInstance.getId());
}
Also used : WkfProcess(com.axelor.apps.bpm.db.WkfProcess) WkfProcessConfig(com.axelor.apps.bpm.db.WkfProcessConfig) RuntimeService(org.camunda.bpm.engine.RuntimeService) HashMap(java.util.HashMap) FullContext(com.axelor.apps.tool.context.FullContext) WkfInstance(com.axelor.apps.bpm.db.WkfInstance) HistoricProcessInstance(org.camunda.bpm.engine.history.HistoricProcessInstance) ProcessInstance(org.camunda.bpm.engine.runtime.ProcessInstance) ProcessInstantiationBuilder(org.camunda.bpm.engine.runtime.ProcessInstantiationBuilder) ProcessEngine(org.camunda.bpm.engine.ProcessEngine)

Example 10 with WkfProcess

use of com.axelor.apps.bpm.db.WkfProcess 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

WkfProcess (com.axelor.apps.bpm.db.WkfProcess)11 HashMap (java.util.HashMap)8 WkfProcessConfig (com.axelor.apps.bpm.db.WkfProcessConfig)6 ArrayList (java.util.ArrayList)6 WkfModel (com.axelor.apps.bpm.db.WkfModel)4 User (com.axelor.auth.db.User)4 List (java.util.List)4 Map (java.util.Map)4 WkfInstance (com.axelor.apps.bpm.db.WkfInstance)3 WkfTaskConfig (com.axelor.apps.bpm.db.WkfTaskConfig)2 FullContext (com.axelor.apps.tool.context.FullContext)2 Model (com.axelor.db.Model)2 AxelorException (com.axelor.exception.AxelorException)2 MetaJsonRecord (com.axelor.meta.db.MetaJsonRecord)2 ProcessEngine (org.camunda.bpm.engine.ProcessEngine)2 RuntimeService (org.camunda.bpm.engine.RuntimeService)2 HistoricProcessInstance (org.camunda.bpm.engine.history.HistoricProcessInstance)2 NodeList (org.w3c.dom.NodeList)2 WkfInstanceRepository (com.axelor.apps.bpm.db.repo.WkfInstanceRepository)1 WkfProcessRepository (com.axelor.apps.bpm.db.repo.WkfProcessRepository)1