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;
}
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;
}
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;
}
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());
}
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;
}
Aggregations