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