Search in sources :

Example 1 with DataAssociation

use of org.jbpm.workflow.core.node.DataAssociation in project jbpm by kiegroup.

the class TaskHandler method readDataInputAssociation.

protected void readDataInputAssociation(org.w3c.dom.Node xmlNode, WorkItemNode workItemNode, Map<String, String> dataInputs) {
    // sourceRef
    org.w3c.dom.Node subNode = xmlNode.getFirstChild();
    if ("sourceRef".equals(subNode.getNodeName())) {
        String source = subNode.getTextContent();
        // targetRef
        subNode = subNode.getNextSibling();
        String target = subNode.getTextContent();
        // transformation
        Transformation transformation = null;
        subNode = subNode.getNextSibling();
        if (subNode != null && "transformation".equals(subNode.getNodeName())) {
            String lang = subNode.getAttributes().getNamedItem("language").getNodeValue();
            String expression = subNode.getTextContent();
            DataTransformer transformer = transformerRegistry.find(lang);
            if (transformer == null) {
                throw new IllegalArgumentException("No transformer registered for language " + lang);
            }
            transformation = new Transformation(lang, expression);
            // transformation.setCompiledExpression(transformer.compile(expression));
            subNode = subNode.getNextSibling();
        }
        // assignments
        List<Assignment> assignments = new LinkedList<Assignment>();
        while (subNode != null) {
            org.w3c.dom.Node ssubNode = subNode.getFirstChild();
            String from = ssubNode.getTextContent();
            String to = ssubNode.getNextSibling().getTextContent();
            assignments.add(new Assignment("XPath", from, to));
            subNode = subNode.getNextSibling();
        }
        workItemNode.addInAssociation(new DataAssociation(source, dataInputs.get(target), assignments, transformation));
    } else {
        // targetRef
        String to = subNode.getTextContent();
        // assignment
        subNode = subNode.getNextSibling();
        if (subNode != null) {
            org.w3c.dom.Node subSubNode = subNode.getFirstChild();
            NodeList nl = subSubNode.getChildNodes();
            if (nl.getLength() > 1) {
                // not supported ?
                workItemNode.getWork().setParameter(dataInputs.get(to), subSubNode.getTextContent());
                return;
            } else if (nl.getLength() == 0) {
                return;
            }
            Object result = null;
            Object from = nl.item(0);
            if (from instanceof Text) {
                String text = ((Text) from).getTextContent();
                if (text.startsWith("\"") && text.endsWith("\"")) {
                    result = text.substring(1, text.length() - 1);
                } else {
                    result = text;
                }
            } else {
                result = nl.item(0);
            }
            workItemNode.getWork().setParameter(dataInputs.get(to), result);
        }
    }
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text) LinkedList(java.util.LinkedList) Assignment(org.jbpm.workflow.core.node.Assignment) DataTransformer(org.kie.api.runtime.process.DataTransformer)

Example 2 with DataAssociation

use of org.jbpm.workflow.core.node.DataAssociation in project jbpm by kiegroup.

the class BusinessRuleTaskHandler method readDataInputAssociation.

protected void readDataInputAssociation(org.w3c.dom.Node xmlNode, RuleSetNode ruleSetNode, Map<String, String> dataInputs) {
    // sourceRef
    org.w3c.dom.Node subNode = xmlNode.getFirstChild();
    if ("sourceRef".equals(subNode.getNodeName())) {
        String source = subNode.getTextContent();
        // targetRef
        subNode = subNode.getNextSibling();
        String target = subNode.getTextContent();
        // transformation
        Transformation transformation = null;
        subNode = subNode.getNextSibling();
        if (subNode != null && "transformation".equals(subNode.getNodeName())) {
            String lang = subNode.getAttributes().getNamedItem("language").getNodeValue();
            String expression = subNode.getTextContent();
            DataTransformer transformer = transformerRegistry.find(lang);
            if (transformer == null) {
                throw new IllegalArgumentException("No transformer registered for language " + lang);
            }
            transformation = new Transformation(lang, expression);
            subNode = subNode.getNextSibling();
        }
        // assignments
        List<Assignment> assignments = new LinkedList<Assignment>();
        while (subNode != null) {
            org.w3c.dom.Node ssubNode = subNode.getFirstChild();
            String from = ssubNode.getTextContent();
            String to = ssubNode.getNextSibling().getTextContent();
            assignments.add(new Assignment("XPath", from, to));
            subNode = subNode.getNextSibling();
        }
        ruleSetNode.addInAssociation(new DataAssociation(source, dataInputs.get(target), assignments, transformation));
    } else {
        // targetRef
        String to = subNode.getTextContent();
        // assignment
        subNode = subNode.getNextSibling();
        if (subNode != null) {
            org.w3c.dom.Node subSubNode = subNode.getFirstChild();
            NodeList nl = subSubNode.getChildNodes();
            if (nl.getLength() > 1) {
                // not supported ?
                ruleSetNode.setParameter(dataInputs.get(to), subSubNode.getTextContent());
                return;
            } else if (nl.getLength() == 0) {
                return;
            }
            Object result = null;
            Object from = nl.item(0);
            if (from instanceof Text) {
                String text = ((Text) from).getTextContent();
                if (text.startsWith("\"") && text.endsWith("\"")) {
                    result = text.substring(1, text.length() - 1);
                } else {
                    result = text;
                }
            } else {
                result = nl.item(0);
            }
            ruleSetNode.setParameter(dataInputs.get(to), result);
        }
    }
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text) LinkedList(java.util.LinkedList) Assignment(org.jbpm.workflow.core.node.Assignment) DataTransformer(org.kie.api.runtime.process.DataTransformer)

Example 3 with DataAssociation

use of org.jbpm.workflow.core.node.DataAssociation in project jbpm by kiegroup.

the class BusinessRuleTaskHandler method readDataOutputAssociation.

protected void readDataOutputAssociation(org.w3c.dom.Node xmlNode, RuleSetNode ruleSetNode, Map<String, String> dataOutputs) {
    // sourceRef
    org.w3c.dom.Node subNode = xmlNode.getFirstChild();
    String source = subNode.getTextContent();
    // targetRef
    subNode = subNode.getNextSibling();
    String target = subNode.getTextContent();
    // transformation
    Transformation transformation = null;
    subNode = subNode.getNextSibling();
    if (subNode != null && "transformation".equals(subNode.getNodeName())) {
        String lang = subNode.getAttributes().getNamedItem("language").getNodeValue();
        String expression = subNode.getTextContent();
        DataTransformer transformer = transformerRegistry.find(lang);
        if (transformer == null) {
            throw new IllegalArgumentException("No transformer registered for language " + lang);
        }
        transformation = new Transformation(lang, expression, source);
        subNode = subNode.getNextSibling();
    }
    // assignments
    List<Assignment> assignments = new LinkedList<Assignment>();
    while (subNode != null) {
        org.w3c.dom.Node ssubNode = subNode.getFirstChild();
        String from = ssubNode.getTextContent();
        String to = ssubNode.getNextSibling().getTextContent();
        assignments.add(new Assignment("XPath", from, to));
        subNode = subNode.getNextSibling();
    }
    ruleSetNode.addOutAssociation(new DataAssociation(dataOutputs.get(source), target, assignments, transformation));
}
Also used : Assignment(org.jbpm.workflow.core.node.Assignment) Transformation(org.jbpm.workflow.core.node.Transformation) DataTransformer(org.kie.api.runtime.process.DataTransformer) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) LinkedList(java.util.LinkedList)

Example 4 with DataAssociation

use of org.jbpm.workflow.core.node.DataAssociation in project jbpm by kiegroup.

the class WorkItemNodeInstance method createWorkItem.

protected WorkItem createWorkItem(WorkItemNode workItemNode) {
    Work work = workItemNode.getWork();
    workItem = new WorkItemImpl();
    ((WorkItem) workItem).setName(work.getName());
    ((WorkItem) workItem).setProcessInstanceId(getProcessInstance().getId());
    ((WorkItem) workItem).setParameters(new HashMap<String, Object>(work.getParameters()));
    // if there are any dynamic parameters add them
    if (dynamicParameters != null) {
        ((WorkItem) workItem).getParameters().putAll(dynamicParameters);
    }
    for (Iterator<DataAssociation> iterator = workItemNode.getInAssociations().iterator(); iterator.hasNext(); ) {
        DataAssociation association = iterator.next();
        if (association.getTransformation() != null) {
            Transformation transformation = association.getTransformation();
            DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
            if (transformer != null) {
                Object parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(association));
                if (parameterValue != null) {
                    ((WorkItem) workItem).setParameter(association.getTarget(), parameterValue);
                }
            }
        } else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
            Object parameterValue = null;
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getSources().get(0));
            if (variableScopeInstance != null) {
                parameterValue = variableScopeInstance.getVariable(association.getSources().get(0));
            } else {
                try {
                    parameterValue = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new NodeInstanceResolverFactory(this));
                } catch (Throwable t) {
                    logger.error("Could not find variable scope for variable {}", association.getSources().get(0));
                    logger.error("when trying to execute Work Item {}", work.getName());
                    logger.error("Continuing without setting parameter.");
                }
            }
            if (parameterValue != null) {
                ((WorkItem) workItem).setParameter(association.getTarget(), parameterValue);
            }
        } else {
            for (Iterator<Assignment> it = association.getAssignments().iterator(); it.hasNext(); ) {
                handleAssignment(it.next());
            }
        }
    }
    for (Map.Entry<String, Object> entry : workItem.getParameters().entrySet()) {
        if (entry.getValue() instanceof String) {
            String s = (String) entry.getValue();
            Map<String, String> replacements = new HashMap<String, String>();
            Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
            while (matcher.find()) {
                String paramName = matcher.group(1);
                if (replacements.get(paramName) == null) {
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
                    if (variableScopeInstance != null) {
                        Object variableValue = variableScopeInstance.getVariable(paramName);
                        String variableValueString = variableValue == null ? "" : variableValue.toString();
                        replacements.put(paramName, variableValueString);
                    } else {
                        try {
                            Object variableValue = MVELSafeHelper.getEvaluator().eval(paramName, new NodeInstanceResolverFactory(this));
                            String variableValueString = variableValue == null ? "" : variableValue.toString();
                            replacements.put(paramName, variableValueString);
                        } catch (Throwable t) {
                            logger.error("Could not find variable scope for variable {}", paramName);
                            logger.error("when trying to replace variable in string for Work Item {}", work.getName());
                            logger.error("Continuing without setting parameter.");
                        }
                    }
                }
            }
            for (Map.Entry<String, String> replacement : replacements.entrySet()) {
                s = s.replace("#{" + replacement.getKey() + "}", replacement.getValue());
            }
            ((WorkItem) workItem).setParameter(entry.getKey(), s);
        }
    }
    return workItem;
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) WorkItem(org.drools.core.process.instance.WorkItem) DataTransformer(org.kie.api.runtime.process.DataTransformer) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) Work(org.jbpm.process.core.Work) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) NodeInstanceResolverFactory(org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with DataAssociation

use of org.jbpm.workflow.core.node.DataAssociation in project jbpm by kiegroup.

the class WorkItemNodeInstance method triggerCompleted.

public void triggerCompleted(WorkItem workItem) {
    this.workItem = workItem;
    WorkItemNode workItemNode = getWorkItemNode();
    if (workItemNode != null && workItem.getState() == WorkItem.COMPLETED) {
        validateWorkItemResultVariable(getProcessInstance().getProcessName(), workItemNode.getOutAssociations(), workItem);
        for (Iterator<DataAssociation> iterator = getWorkItemNode().getOutAssociations().iterator(); iterator.hasNext(); ) {
            DataAssociation association = iterator.next();
            if (association.getTransformation() != null) {
                Transformation transformation = association.getTransformation();
                DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
                if (transformer != null) {
                    Object parameterValue = transformer.transform(transformation.getCompiledExpression(), workItem.getResults());
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
                    if (variableScopeInstance != null && parameterValue != null) {
                        variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), parameterValue);
                        variableScopeInstance.setVariable(association.getTarget(), parameterValue);
                    } else {
                        logger.warn("Could not find variable scope for variable {}", association.getTarget());
                        logger.warn("when trying to complete Work Item {}", workItem.getName());
                        logger.warn("Continuing without setting variable.");
                    }
                    if (parameterValue != null) {
                        ((WorkItem) workItem).setParameter(association.getTarget(), parameterValue);
                    }
                }
            } else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
                if (variableScopeInstance != null) {
                    Object value = workItem.getResult(association.getSources().get(0));
                    if (value == null) {
                        try {
                            value = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new WorkItemResolverFactory(workItem));
                        } catch (Throwable t) {
                        // do nothing
                        }
                    }
                    Variable varDef = variableScopeInstance.getVariableScope().findVariable(association.getTarget());
                    DataType dataType = varDef.getType();
                    // exclude java.lang.Object as it is considered unknown type
                    if (!dataType.getStringType().endsWith("java.lang.Object") && !dataType.getStringType().endsWith("Object") && value instanceof String) {
                        value = dataType.readValue((String) value);
                    } else {
                        variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), value);
                    }
                    variableScopeInstance.setVariable(association.getTarget(), value);
                } else {
                    logger.warn("Could not find variable scope for variable {}", association.getTarget());
                    logger.warn("when trying to complete Work Item {}", workItem.getName());
                    logger.warn("Continuing without setting variable.");
                }
            } else {
                try {
                    for (Iterator<Assignment> it = association.getAssignments().iterator(); it.hasNext(); ) {
                        handleAssignment(it.next());
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    // handle dynamic nodes
    if (getNode() == null) {
        setMetaData("NodeType", workItem.getName());
        mapDynamicOutputData(workItem.getResults());
    }
    if (isInversionOfControl()) {
        KieRuntime kruntime = ((ProcessInstance) getProcessInstance()).getKnowledgeRuntime();
        kruntime.update(kruntime.getFactHandle(this), this);
    } else {
        triggerCompleted();
    }
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) Variable(org.jbpm.process.core.context.variable.Variable) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) KieRuntime(org.kie.api.runtime.KieRuntime) WorkItem(org.drools.core.process.instance.WorkItem) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) WorkItemHandlerNotFoundException(org.drools.core.WorkItemHandlerNotFoundException) WorkItemResolverFactory(org.jbpm.workflow.instance.impl.WorkItemResolverFactory) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) DataTransformer(org.kie.api.runtime.process.DataTransformer) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) Iterator(java.util.Iterator) DataType(org.jbpm.process.core.datatype.DataType) ProcessInstance(org.jbpm.process.instance.ProcessInstance)

Aggregations

DataAssociation (org.jbpm.workflow.core.node.DataAssociation)12 Transformation (org.jbpm.workflow.core.node.Transformation)12 DataTransformer (org.kie.api.runtime.process.DataTransformer)12 HashMap (java.util.HashMap)6 VariableScopeInstance (org.jbpm.process.instance.context.variable.VariableScopeInstance)5 Assignment (org.jbpm.workflow.core.node.Assignment)5 LinkedList (java.util.LinkedList)4 Map (java.util.Map)3 WorkflowProcess (org.jbpm.workflow.core.WorkflowProcess)3 NodeInstanceResolverFactory (org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory)3 Iterator (java.util.Iterator)2 Matcher (java.util.regex.Matcher)2 WorkItem (org.drools.core.process.instance.WorkItem)2 Variable (org.jbpm.process.core.context.variable.Variable)2 DataType (org.jbpm.process.core.datatype.DataType)2 ProcessInstance (org.jbpm.process.instance.ProcessInstance)2 RuleSetNode (org.jbpm.workflow.core.node.RuleSetNode)2 WorkItemNode (org.jbpm.workflow.core.node.WorkItemNode)2 KieRuntime (org.kie.api.runtime.KieRuntime)2 NodeList (org.w3c.dom.NodeList)2