Search in sources :

Example 1 with ProcessInstanceResolverFactory

use of org.jbpm.workflow.instance.impl.ProcessInstanceResolverFactory in project jbpm by kiegroup.

the class ProcessInstanceImpl method getDescription.

public String getDescription() {
    if (description == null) {
        description = process.getName();
        if (process != null) {
            Object metaData = process.getMetaData().get("customDescription");
            if (metaData instanceof String) {
                String customDescription = (String) metaData;
                Map<String, String> replacements = new HashMap<String, String>();
                Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(customDescription);
                while (matcher.find()) {
                    String paramName = matcher.group(1);
                    if (replacements.get(paramName) == null) {
                        try {
                            String value = (String) MVELSafeHelper.getEvaluator().eval(paramName, new ProcessInstanceResolverFactory(((WorkflowProcessInstance) this)));
                            replacements.put(paramName, value);
                        } catch (Throwable t) {
                            logger.error("Could not resolve customDescription, parameter " + paramName, t);
                            logger.error("Continuing without setting description.");
                        }
                    }
                }
                for (Map.Entry<String, String> replacement : replacements.entrySet()) {
                    customDescription = customDescription.replace("#{" + replacement.getKey() + "}", replacement.getValue());
                }
                description = customDescription;
            }
        }
    }
    return description;
}
Also used : ProcessInstanceResolverFactory(org.jbpm.workflow.instance.impl.ProcessInstanceResolverFactory) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) Map(java.util.Map) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance)

Example 2 with ProcessInstanceResolverFactory

use of org.jbpm.workflow.instance.impl.ProcessInstanceResolverFactory in project jbpm by kiegroup.

the class DynamicUtils method internalAddDynamicWorkItem.

private static void internalAddDynamicWorkItem(final WorkflowProcessInstance processInstance, final DynamicNodeInstance dynamicContext, KieRuntime ksession, String workItemName, Map<String, Object> parameters) {
    final WorkItemImpl workItem = new WorkItemImpl();
    workItem.setState(WorkItem.ACTIVE);
    workItem.setProcessInstanceId(processInstance.getId());
    workItem.setDeploymentId((String) ksession.getEnvironment().get(EnvironmentName.DEPLOYMENT_ID));
    workItem.setName(workItemName);
    workItem.setParameters(parameters);
    for (Map.Entry<String, Object> entry : workItem.getParameters().entrySet()) {
        if (entry.getValue() instanceof String) {
            String s = (String) entry.getValue();
            Object variableValue = null;
            Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
            while (matcher.find()) {
                String paramName = matcher.group(1);
                variableValue = processInstance.getVariable(paramName);
                if (variableValue == null) {
                    try {
                        variableValue = MVELSafeHelper.getEvaluator().eval(paramName, new ProcessInstanceResolverFactory(processInstance));
                    } catch (Throwable t) {
                        logger.error("Could not find variable scope for variable {}", paramName);
                        logger.error("when trying to replace variable in string for Dynamic Work Item {}", workItemName);
                        logger.error("Continuing without setting parameter.");
                    }
                }
            }
            if (variableValue != null) {
                workItem.setParameter(entry.getKey(), variableValue);
            }
        }
    }
    final WorkItemNodeInstance workItemNodeInstance = new WorkItemNodeInstance();
    workItemNodeInstance.internalSetWorkItem(workItem);
    workItemNodeInstance.setMetaData("NodeType", workItemName);
    workItem.setNodeInstanceId(workItemNodeInstance.getId());
    if (ksession instanceof StatefulKnowledgeSessionImpl) {
        workItemNodeInstance.setProcessInstance(processInstance);
        workItemNodeInstance.setNodeInstanceContainer(dynamicContext == null ? processInstance : dynamicContext);
        workItemNodeInstance.addEventListeners();
        executeWorkItem((StatefulKnowledgeSessionImpl) ksession, workItem, workItemNodeInstance);
    } else if (ksession instanceof CommandBasedStatefulKnowledgeSession) {
        ExecutableRunner runner = ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
        runner.execute(new ExecutableCommand<Void>() {

            private static final long serialVersionUID = 5L;

            public Void execute(Context context) {
                StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
                WorkflowProcessInstance realProcessInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstance.getId());
                workItemNodeInstance.setProcessInstance(realProcessInstance);
                if (dynamicContext == null) {
                    workItemNodeInstance.setNodeInstanceContainer(realProcessInstance);
                } else {
                    DynamicNodeInstance realDynamicContext = findDynamicContext(realProcessInstance, dynamicContext.getUniqueId());
                    workItemNodeInstance.setNodeInstanceContainer(realDynamicContext);
                }
                workItemNodeInstance.addEventListeners();
                executeWorkItem((StatefulKnowledgeSessionImpl) ksession, workItem, workItemNodeInstance);
                return null;
            }
        });
    } else {
        throw new IllegalArgumentException("Unsupported ksession: " + ksession == null ? "null" : ksession.getClass().getName());
    }
}
Also used : RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) Matcher(java.util.regex.Matcher) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) ProcessInstanceResolverFactory(org.jbpm.workflow.instance.impl.ProcessInstanceResolverFactory) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) KieSession(org.kie.api.runtime.KieSession) Map(java.util.Map) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance)

Aggregations

Map (java.util.Map)2 Matcher (java.util.regex.Matcher)2 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)2 ProcessInstanceResolverFactory (org.jbpm.workflow.instance.impl.ProcessInstanceResolverFactory)2 HashMap (java.util.HashMap)1 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)1 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)1 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)1 Context (org.kie.api.runtime.Context)1 ExecutableRunner (org.kie.api.runtime.ExecutableRunner)1 KieSession (org.kie.api.runtime.KieSession)1 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)1