use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class XmlBPMNProcessDumper method visitVariables.
public static void visitVariables(List<Variable> variables, StringBuilder xmlDump) {
if (!variables.isEmpty()) {
xmlDump.append(" <!-- process variables -->" + EOL);
for (Variable variable : variables) {
if (variable.getMetaData("DataObject") == null) {
xmlDump.append(" <property id=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(variable.getName()) + "\" ");
if (variable.getType() != null) {
xmlDump.append("itemSubjectRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute((String) variable.getMetaData("ItemSubjectRef")) + "\"");
}
// TODO: value?
Map<String, Object> metaData = getMetaData(variable.getMetaData());
if (metaData.isEmpty()) {
xmlDump.append("/>" + EOL);
} else {
xmlDump.append(">" + EOL + " <extensionElements>" + EOL);
writeMetaData(metaData, xmlDump);
xmlDump.append(" </extensionElements>" + EOL + " </property>" + EOL);
}
}
}
for (Variable variable : variables) {
if (variable.getMetaData("DataObject") != null) {
xmlDump.append(" <dataObject id=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(variable.getName()) + "\" ");
if (variable.getType() != null) {
xmlDump.append("itemSubjectRef=\"_" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(variable.getName()) + "\"");
}
// TODO: value?
Map<String, Object> metaData = getMetaData(variable.getMetaData());
if (metaData.isEmpty()) {
xmlDump.append("/>" + EOL);
} else {
xmlDump.append(">" + EOL + " <extensionElements>" + EOL);
writeMetaData(metaData, xmlDump);
xmlDump.append(" </extensionElements>" + EOL + " </property>" + EOL);
}
}
}
xmlDump.append(EOL);
}
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class VariableScopeInstance method setVariable.
@SuppressWarnings("unchecked")
public void setVariable(NodeInstance nodeInstance, String name, Object value) {
if (name == null) {
throw new IllegalArgumentException("The name of a variable may not be null!");
}
Variable var = getVariableScope().findVariable(name);
if (var != null) {
name = var.getName();
}
Object oldValue = getVariable(name);
if (oldValue == null) {
if (value == null) {
return;
}
}
// check if variable that is being set is readonly and has already been set
if (oldValue != null && getVariableScope().isReadOnly(name)) {
throw new VariableViolationException(getProcessInstance().getId(), name, "Variable '" + name + "' is already set and is marked as read only");
}
// in case variable is marked as notnull (via tag) then null values should be ignored
if (value == null && getVariableScope().isNullable(name)) {
return;
}
// in case variable is versioned store the old value into versioned variable by variable name
if (var != null && var.hasTag(Variable.VERSIONED_TAG)) {
Map<String, List<Object>> versions = (Map<String, List<Object>>) variables.computeIfAbsent(VariableScope.VERSIONED_VARIABLES, key -> new ConcurrentHashMap<>());
List<Object> varVersions = versions.computeIfAbsent(name, k -> new ArrayList<>());
int versionLimit = Integer.parseInt(var.getMetaData().getOrDefault(Variable.VAR_VERSIONS_LIMIT, "10").toString());
if (oldValue != null) {
varVersions.add(oldValue);
// and remove the oldest if exceeding
if (varVersions.size() > versionLimit) {
varVersions.remove(0);
}
}
}
if (getProcessInstance().getProcessRuntime().getVariableInitializer() != null) {
for (VariableAugmentor augmentor : getProcessInstance().getProcessRuntime().getVariableInitializer().augmentors()) {
if (augmentor.accept(var, value)) {
// run any of the available augmentors on the value
if (oldValue != null) {
value = augmentor.augmentOnUpdate(getProcessInstance().getProcess().getId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getId(), var, value);
} else if (value == null) {
augmentor.augmentOnDelete(getProcessInstance().getProcess().getId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getId(), var, oldValue);
} else {
value = augmentor.augmentOnCreate(getProcessInstance().getProcess().getId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getId(), var, value);
}
}
}
}
ProcessInstance processInstance = getProcessInstance();
if (nodeInstance != null) {
processInstance = nodeInstance.getProcessInstance();
}
ProcessEventSupport processEventSupport = ((InternalProcessRuntime) getProcessInstance().getProcessRuntime()).getProcessEventSupport();
processEventSupport.fireBeforeVariableChanged((variableIdPrefix == null ? "" : variableIdPrefix + ":") + name, (variableInstanceIdPrefix == null ? "" : variableInstanceIdPrefix + ":") + name, oldValue, value, getVariableScope().tags(name), processInstance, nodeInstance, getProcessInstance().getProcessRuntime());
internalSetVariable(name, value);
processEventSupport.fireAfterVariableChanged((variableIdPrefix == null ? "" : variableIdPrefix + ":") + name, (variableInstanceIdPrefix == null ? "" : variableInstanceIdPrefix + ":") + name, oldValue, value, getVariableScope().tags(name), processInstance, nodeInstance, getProcessInstance().getProcessRuntime());
processInstance.signalEvent("variableChanged", value);
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class VariableScopeInstance method setContextInstanceContainer.
public void setContextInstanceContainer(ContextInstanceContainer contextInstanceContainer) {
super.setContextInstanceContainer(contextInstanceContainer);
if (getVariableScope() != null) {
for (Variable variable : getVariableScope().getVariables()) {
if (variable.getValue() != null) {
setVariable(variable.getName(), variable.getValue());
}
}
}
if (contextInstanceContainer instanceof CompositeContextNodeInstance) {
this.variableIdPrefix = ((Node) ((CompositeContextNodeInstance) contextInstanceContainer).getNode()).getUniqueId();
this.variableInstanceIdPrefix = ((CompositeContextNodeInstance) contextInstanceContainer).getUniqueId();
}
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class VariableScopeInstance method getVariable.
@SuppressWarnings("unchecked")
public Object getVariable(String name) {
String defaultValue = null;
if (name.contains(":")) {
String[] items = name.split(":");
name = items[0];
defaultValue = items[1];
}
if (name.contains(VariableScope.VERSION_SEPARATOR) && !name.startsWith(VariableScope.VERSION_SEPARATOR)) {
String[] items = name.split("\\" + VariableScope.VERSION_SEPARATOR);
Variable var = getVariableScope().findVariable(items[0]);
if (var.hasTag(Variable.VERSIONED_TAG)) {
Map<String, List<Object>> versions = (Map<String, List<Object>>) variables.computeIfAbsent(VariableScope.VERSIONED_VARIABLES, key -> new ConcurrentHashMap<>());
List<Object> varVersions = (List<Object>) versions.getOrDefault(items[0], Collections.emptyList());
if (items.length == 1) {
return varVersions;
}
try {
int version = Integer.parseInt(items[1]);
if (version < 0) {
// negative position means last item
return varVersions.get(varVersions.size() - 1);
}
// otherwise return version with given number
return varVersions.get(version);
} catch (IndexOutOfBoundsException e) {
// in case position/version points at not existing version return null
return null;
}
}
}
Object value = internalGetVariable(name);
if (value != null) {
return value;
}
// support for processInstanceId and parentProcessInstanceId
if ("processInstanceId".equals(name) && getProcessInstance() != null) {
return getProcessInstance().getId();
} else if ("parentProcessInstanceId".equals(name) && getProcessInstance() != null) {
return getProcessInstance().getParentProcessInstanceId();
} else if (("correlationKey".equals(name) || "businessKey".equals(name)) && getProcessInstance() != null) {
return getProcessInstance().getCorrelationKey();
} else if ("rootProcessInstanceId".equals(name) && getProcessInstance() != null) {
return getProcessInstance().getRootProcessInstanceId();
} else if ("rootProcessId".equals(name) && getProcessInstance() != null) {
return getProcessInstance().getRootProcessId();
} else if ("processId".equals(name) && getProcessInstance() != null) {
return getProcessInstance().getProcessId();
} else if ("processName".equals(name) && getProcessInstance() != null) {
return getProcessInstance().getProcessName();
} else if ("processInstanceName".equals(name) && getProcessInstance() != null) {
return ((ProcessInstanceImpl) getProcessInstance()).getDescription();
}
return systemOrEnvironmentVariable(name, defaultValue);
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class CompositeContextNodeFactory method variable.
public CompositeContextNodeFactory variable(String name, DataType type, Object value, String metaDataName, Object metaDataValue) {
Variable variable = new Variable();
variable.setName(name);
variable.setType(type);
variable.setValue(value);
if (metaDataName != null && metaDataValue != null) {
variable.setMetaData(metaDataName, metaDataValue);
}
VariableScope variableScope = (VariableScope) getCompositeNode().getDefaultContext(VariableScope.VARIABLE_SCOPE);
if (variableScope == null) {
variableScope = new VariableScope();
getCompositeNode().addContext(variableScope);
getCompositeNode().setDefaultContext(variableScope);
}
variableScope.getVariables().add(variable);
return this;
}
Aggregations