if(properties.get("taskname") != null && properties.get("taskname").length() > 0) {
if(isCustomElement(properties.get("tasktype"), preProcessingData)) {
// add droolsjbpm-specific attribute "taskName"
ExtendedMetaData metadata = ExtendedMetaData.INSTANCE;
EAttributeImpl extensionAttribute = (EAttributeImpl) metadata.demandFeature(
"http://www.jboss.org/drools", "taskName", false, false);
SimpleFeatureMapEntry extensionEntry = new SimpleFeatureMapEntry(extensionAttribute,
properties.get("taskname").replaceAll("&","").replaceAll(" ", ""));
task.getAnyAttribute().add(extensionEntry);
}
// map the taskName to iospecification
taskNameDataInput = Bpmn2Factory.eINSTANCE.createDataInput();
taskNameDataInput.setId(task.getId() + "_TaskNameInputX");
taskNameDataInput.setName("TaskName");
if(task.getIoSpecification() == null) {
InputOutputSpecification iospec = Bpmn2Factory.eINSTANCE.createInputOutputSpecification();
task.setIoSpecification(iospec);
}
task.getIoSpecification().getDataInputs().add(taskNameDataInput);
// taskName also needs to be in dataInputAssociation
DataInputAssociation taskNameDataInputAssociation = Bpmn2Factory.eINSTANCE.createDataInputAssociation();
taskNameDataInputAssociation.setTargetRef(taskNameDataInput);
Assignment taskNameAssignment = Bpmn2Factory.eINSTANCE.createAssignment();
FormalExpression fromExp = Bpmn2Factory.eINSTANCE.createFormalExpression();
fromExp.setBody(properties.get("taskname").replaceAll("&","").replaceAll(" ", ""));
taskNameAssignment.setFrom(fromExp);
FormalExpression toExp = Bpmn2Factory.eINSTANCE.createFormalExpression();
toExp.setBody(task.getId() + "_TaskNameInputX");
taskNameAssignment.setTo(toExp);
taskNameDataInputAssociation.getAssignment().add(taskNameAssignment);
task.getDataInputAssociations().add(taskNameDataInputAssociation);
}
//process lanes
if(properties.get("lanes") != null && properties.get("lanes").length() > 0) {
ExtendedMetaData metadata = ExtendedMetaData.INSTANCE;
EAttributeImpl extensionAttribute = (EAttributeImpl) metadata.demandFeature(
"http://www.jboss.org/drools", "lanes", false, false);
SimpleFeatureMapEntry extensionEntry = new SimpleFeatureMapEntry(extensionAttribute,
properties.get("lanes"));
task.getAnyAttribute().add(extensionEntry);
}
//process data input set
if(properties.get("datainputset") != null && properties.get("datainputset").trim().length() > 0) {
String[] allDataInputs = properties.get("datainputset").split( ",\\s*" );
if(task.getIoSpecification() == null) {
InputOutputSpecification iospec = Bpmn2Factory.eINSTANCE.createInputOutputSpecification();
task.setIoSpecification(iospec);
}
InputSet inset = Bpmn2Factory.eINSTANCE.createInputSet();
for(String dataInput : allDataInputs) {
if(dataInput.trim().length() > 0) {
DataInput nextInput = Bpmn2Factory.eINSTANCE.createDataInput();
String[] dataInputParts = dataInput.split( ":\\s*" );
if(dataInputParts.length == 2) {
nextInput.setId(task.getId() + "_" + dataInputParts[0] + (dataInputParts[0].endsWith("InputX") ? "" : "InputX"));
nextInput.setName(dataInputParts[0]);
ExtendedMetaData metadata = ExtendedMetaData.INSTANCE;
EAttributeImpl extensionAttribute = (EAttributeImpl) metadata.demandFeature(
"http://www.jboss.org/drools", "dtype", false, false);
SimpleFeatureMapEntry extensionEntry = new SimpleFeatureMapEntry(extensionAttribute,
dataInputParts[1]);
nextInput.getAnyAttribute().add(extensionEntry);
} else {
nextInput.setId(task.getId() + "_" + dataInput + (dataInput.endsWith("InputX") ? "" : "InputX"));
nextInput.setName(dataInput);
ExtendedMetaData metadata = ExtendedMetaData.INSTANCE;
EAttributeImpl extensionAttribute = (EAttributeImpl) metadata.demandFeature(
"http://www.jboss.org/drools", "dtype", false, false);
SimpleFeatureMapEntry extensionEntry = new SimpleFeatureMapEntry(extensionAttribute,
"Object");
nextInput.getAnyAttribute().add(extensionEntry);
}
task.getIoSpecification().getDataInputs().add(nextInput);
inset.getDataInputRefs().add(nextInput);
}
}
// add the taskName as well if it was defined
if(taskNameDataInput != null) {
inset.getDataInputRefs().add(taskNameDataInput);
}
task.getIoSpecification().getInputSets().add(inset);
} else {
if(task.getIoSpecification() != null) {
task.getIoSpecification().getInputSets().add(Bpmn2Factory.eINSTANCE.createInputSet());
}
}
//process data output set
if(properties.get("dataoutputset") != null && properties.get("dataoutputset").trim().length() > 0) {
String[] allDataOutputs = properties.get("dataoutputset").split( ",\\s*" );
if(task.getIoSpecification() == null) {
InputOutputSpecification iospec = Bpmn2Factory.eINSTANCE.createInputOutputSpecification();
task.setIoSpecification(iospec);
}
OutputSet outset = Bpmn2Factory.eINSTANCE.createOutputSet();
for(String dataOutput : allDataOutputs) {
if(dataOutput.trim().length() > 0) {
DataOutput nextOut = Bpmn2Factory.eINSTANCE.createDataOutput();
String[] dataOutputParts = dataOutput.split( ":\\s*" );
if(dataOutputParts.length == 2) {
nextOut.setId(task.getId() + "_" + dataOutputParts[0] + (dataOutputParts[0].endsWith("OutputX") ? "" : "OutputX"));
nextOut.setName(dataOutputParts[0]);
ExtendedMetaData metadata = ExtendedMetaData.INSTANCE;
EAttributeImpl extensionAttribute = (EAttributeImpl) metadata.demandFeature(
"http://www.jboss.org/drools", "dtype", false, false);
SimpleFeatureMapEntry extensionEntry = new SimpleFeatureMapEntry(extensionAttribute,
dataOutputParts[1]);
nextOut.getAnyAttribute().add(extensionEntry);
} else {
nextOut.setId(task.getId() + "_" + dataOutput + (dataOutput.endsWith("OutputX") ? "" : "OutputX"));
nextOut.setName(dataOutput);
ExtendedMetaData metadata = ExtendedMetaData.INSTANCE;
EAttributeImpl extensionAttribute = (EAttributeImpl) metadata.demandFeature(
"http://www.jboss.org/drools", "dtype", false, false);
SimpleFeatureMapEntry extensionEntry = new SimpleFeatureMapEntry(extensionAttribute,
"Object");
nextOut.getAnyAttribute().add(extensionEntry);
}
task.getIoSpecification().getDataOutputs().add(nextOut);
outset.getDataOutputRefs().add(nextOut);
}
}
task.getIoSpecification().getOutputSets().add(outset);
} else {
if(task.getIoSpecification() != null) {
task.getIoSpecification().getOutputSets().add(Bpmn2Factory.eINSTANCE.createOutputSet());
}
}
//process assignments
if(properties.get("assignments") != null && properties.get("assignments").length() > 0) {
String[] allAssignments = properties.get("assignments").split( ",\\s*" );
for(String assignment : allAssignments) {
if(assignment.contains("=")) {
String[] assignmentParts = assignment.split( "=\\s*" );
DataInputAssociation dia = Bpmn2Factory.eINSTANCE.createDataInputAssociation();
boolean foundTaskName = false;
if(task.getIoSpecification() != null && task.getIoSpecification().getDataOutputs() != null) {
List<DataInput> dataInputs = task.getIoSpecification().getDataInputs();
for(DataInput di : dataInputs) {
if(di.getId().equals(task.getId() + "_" + assignmentParts[0] + (assignmentParts[0].endsWith("InputX") ? "" : "InputX"))) {
dia.setTargetRef(di);
if(di.getName().equals("TaskName")) {
foundTaskName = true;
break;
}
}
}
}
// if we are dealing with TaskName and none has been defined, add it
if(assignmentParts[0].equals("TaskName") && !foundTaskName) {
DataInput assignmentTaskNameDataInput = Bpmn2Factory.eINSTANCE.createDataInput();
assignmentTaskNameDataInput.setId(task.getId() + "_TaskNameInputX");
assignmentTaskNameDataInput.setName("TaskName");
if(task.getIoSpecification() == null) {
InputOutputSpecification iospec = Bpmn2Factory.eINSTANCE.createInputOutputSpecification();
task.setIoSpecification(iospec);
}
task.getIoSpecification().getDataInputs().add(assignmentTaskNameDataInput);
dia.setTargetRef(assignmentTaskNameDataInput);
InputSet inset = task.getIoSpecification().getInputSets().get(0);
inset.getDataInputRefs().add(assignmentTaskNameDataInput);
}
Assignment a = Bpmn2Factory.eINSTANCE.createAssignment();
FormalExpression fromExpression = Bpmn2Factory.eINSTANCE.createFormalExpression();
if(assignmentParts.length > 1) {
String replacer = assignmentParts[1].replaceAll("##", ",");
replacer = replacer.replaceAll("\\|\\|", "=");
fromExpression.setBody(wrapInCDATABlock(replacer));
} else {
fromExpression.setBody("");
}
FormalExpression toExpression = Bpmn2Factory.eINSTANCE.createFormalExpression();
toExpression.setBody(dia.getTargetRef().getId());
a.setFrom(fromExpression);
a.setTo(toExpression);
dia.getAssignment().add(a);
task.getDataInputAssociations().add(dia);
} else if(assignment.contains("<->")) {
String[] assignmentParts = assignment.split( "<->\\s*" );
DataInputAssociation dia = Bpmn2Factory.eINSTANCE.createDataInputAssociation();
DataOutputAssociation doa = Bpmn2Factory.eINSTANCE.createDataOutputAssociation();
ItemAwareElement ie = Bpmn2Factory.eINSTANCE.createItemAwareElement();
ie.setId(assignmentParts[0]);
dia.getSourceRef().add(ie);
doa.setTargetRef(ie);
List<DataInput> dataInputs = task.getIoSpecification().getDataInputs();
for(DataInput di : dataInputs) {
if(di.getId().equals(task.getId() + "_" + assignmentParts[1] + (assignmentParts[1].endsWith("InputX") ? "" : "InputX"))) {
dia.setTargetRef(di);
break;
}
}
List<DataOutput> dataOutputs = task.getIoSpecification().getDataOutputs();
for(DataOutput dout : dataOutputs) {
if(dout.getId().equals(task.getId() + "_" + assignmentParts[1] + (assignmentParts[1].endsWith("OutputX") ? "" : "OutputX"))) {
doa.getSourceRef().add(dout);
break;
}
}
task.getDataInputAssociations().add(dia);
task.getDataOutputAssociations().add(doa);
} else if(assignment.contains("->")) {
String[] assignmentParts = assignment.split( "->\\s*" );
// we need to check if this is an data input or data output assignment
boolean leftHandAssignMentIsDO = false;
List<DataOutput> dataOutputs = task.getIoSpecification().getDataOutputs();
for(DataOutput dout : dataOutputs) {
if(dout.getId().equals(task.getId() + "_" + assignmentParts[0] + (assignmentParts[0].endsWith("OutputX") ? "" : "OutputX"))) {
leftHandAssignMentIsDO = true;
break;
}
}
if(leftHandAssignMentIsDO) {
// doing data output
DataOutputAssociation doa = Bpmn2Factory.eINSTANCE.createDataOutputAssociation();
for(DataOutput dout : dataOutputs) {
if(dout.getId().equals(task.getId() + "_" + assignmentParts[0] + (assignmentParts[0].endsWith("OutputX") ? "" : "OutputX"))) {
doa.getSourceRef().add(dout);
break;
}
}
ItemAwareElement ie = Bpmn2Factory.eINSTANCE.createItemAwareElement();
ie.setId(assignmentParts[1]);
doa.setTargetRef(ie);
task.getDataOutputAssociations().add(doa);
} else {
// doing data input
DataInputAssociation dia = Bpmn2Factory.eINSTANCE.createDataInputAssociation();
// association from process var to dataInput var
ItemAwareElement ie = Bpmn2Factory.eINSTANCE.createItemAwareElement();
ie.setId(assignmentParts[0]);
dia.getSourceRef().add(ie);
List<DataInput> dataInputs = task.getIoSpecification().getDataInputs();
for(DataInput di : dataInputs) {
if(di.getId().equals(task.getId() + "_" + assignmentParts[1] + (assignmentParts[1].endsWith("InputX") ? "" : "InputX"))) {
dia.setTargetRef(di);
break;
}
}
task.getDataInputAssociations().add(dia);
}
} else {
// TODO throw exception here?
}
}
// check if multiple taskname datainput associations exist and remove them
List<DataInputAssociation> dataInputAssociations = task.getDataInputAssociations();
boolean haveTaskNameInput = false;
for(Iterator<DataInputAssociation> itr = dataInputAssociations.iterator(); itr.hasNext();)
{
DataInputAssociation da = itr.next();
if(da.getAssignment() != null && da.getAssignment().size() > 0) {
Assignment a = da.getAssignment().get(0);
if(((FormalExpression) a.getTo()).getBody().equals(task.getId() + "_TaskNameInputX")) {
if(!haveTaskNameInput) {
haveTaskNameInput = true;
} else {
itr.remove();
}
}
}
}
}
// process on-entry and on-exit actions as custom elements
if(properties.get("onentryactions") != null && properties.get("onentryactions").length() > 0) {
String[] allActions = properties.get("onentryactions").split( "\\|\\s*" );
for(String action : allActions) {
OnEntryScriptType onEntryScript = DroolsFactory.eINSTANCE.createOnEntryScriptType();
onEntryScript.setScript(wrapInCDATABlock(action));
String scriptLanguage = "";
if(properties.get("script_language").equals("java")) {
scriptLanguage = "http://www.java.com/java";
} else if(properties.get("script_language").equals("mvel")) {
scriptLanguage = "http://www.mvel.org/2.0";
} else {
// default to java
scriptLanguage = "http://www.java.com/java";
}
onEntryScript.setScriptFormat(scriptLanguage);
if(task.getExtensionValues() == null || task.getExtensionValues().size() < 1) {
ExtensionAttributeValue extensionElement = Bpmn2Factory.eINSTANCE.createExtensionAttributeValue();
task.getExtensionValues().add(extensionElement);
}
FeatureMap.Entry extensionElementEntry = new SimpleFeatureMapEntry(
(Internal) DroolsPackage.Literals.DOCUMENT_ROOT__ON_ENTRY_SCRIPT, onEntryScript);
task.getExtensionValues().get(0).getValue().add(extensionElementEntry);
}
}
if(properties.get("onexitactions") != null && properties.get("onexitactions").length() > 0) {
String[] allActions = properties.get("onexitactions").split( "\\|\\s*" );
for(String action : allActions) {
OnExitScriptType onExitScript = DroolsFactory.eINSTANCE.createOnExitScriptType();
onExitScript.setScript(wrapInCDATABlock(action));
String scriptLanguage;
if(properties.get("script_language").equals("java")) {
scriptLanguage = "http://www.java.com/java";
} else if(properties.get("script_language").equals("mvel")) {
scriptLanguage = "http://www.mvel.org/2.0";
} else {
// default to java
scriptLanguage = "http://www.java.com/java";
}
onExitScript.setScriptFormat(scriptLanguage);
if(task.getExtensionValues() == null || task.getExtensionValues().size() < 1) {
ExtensionAttributeValue extensionElement = Bpmn2Factory.eINSTANCE.createExtensionAttributeValue();
task.getExtensionValues().add(extensionElement);
}
FeatureMap.Entry extensionElementEntry = new SimpleFeatureMapEntry(
(Internal) DroolsPackage.Literals.DOCUMENT_ROOT__ON_EXIT_SCRIPT, onExitScript);
task.getExtensionValues().get(0).getValue().add(extensionElementEntry);
}
}
// multi instance
if(properties.get("multipleinstance") != null && properties.get("multipleinstance").length() > 0 && properties.get("multipleinstance").equals("true")) {
// will be revisited at end
ExtendedMetaData metadata = ExtendedMetaData.INSTANCE;
EAttributeImpl extensionAttribute = (EAttributeImpl) metadata.demandFeature(
"http://www.jboss.org/drools", "mitask", false, false);
StringBuffer buff = new StringBuffer();
buff.append( (properties.get("multipleinstancecollectioninput") != null && properties.get("multipleinstancecollectioninput").length() > 0) ? properties.get("multipleinstancecollectioninput") : " ");
buff.append("@");
buff.append((properties.get("multipleinstancecollectionoutput") != null && properties.get("multipleinstancecollectionoutput").length() > 0) ? properties.get("multipleinstancecollectionoutput") : " ");