Commit b368f0eb authored by Aurelien Pupier's avatar Aurelien Pupier
Browse files

BS-14249: support generation of script for initial value of Business

Variable of types containing composition multiple
parent eebb6f75
......@@ -92,7 +92,7 @@ public class RelationFieldToContractInputMappingTest {
final MappingOperationScriptBuilder scriptBuilder = fieldToContractInputMapping.getScriptBuilder(aBusinessData().withName("myEmployee").build());
assertThat(scriptBuilder.toScript()).isEqualTo(
assertThat(scriptBuilder.toScript(false)).isEqualTo(
"def addressVar = myEmployee.address == null ? new Address() : myEmployee.address" + System.lineSeparator()
+ "addressVar.street = employee.address.street" + System.lineSeparator()
+ "return addressVar");
......
......@@ -169,7 +169,7 @@ public class RootContractInputGeneratorTest {
rootContractInputGenerator.build(businessObjectData);
final ArgumentCaptor<FieldToContractInputMapping> argumentCaptor = ArgumentCaptor.forClass(FieldToContractInputMapping.class);
verify(expressionBuilder).toExpression(eq(businessObjectData), argumentCaptor.capture());
verify(expressionBuilder).toExpression(eq(businessObjectData), argumentCaptor.capture(), eq(false));
final FieldToContractInputMapping fieldToContractInputMapping = argumentCaptor.getValue();
assertThat(fieldToContractInputMapping.getField().getName()).isEqualTo("rootInputName");
assertThat(fieldToContractInputMapping.getField().isCollection()).isFalse();
......@@ -192,7 +192,7 @@ public class RootContractInputGeneratorTest {
rootContractInputGenerator.build(businessObjectData);
final ArgumentCaptor<FieldToContractInputMapping> argumentCaptor = ArgumentCaptor.forClass(FieldToContractInputMapping.class);
verify(expressionBuilder).toExpression(eq(businessObjectData), argumentCaptor.capture());
verify(expressionBuilder).toExpression(eq(businessObjectData), argumentCaptor.capture(), eq(false));
final FieldToContractInputMapping fieldToContractInputMapping = argumentCaptor.getValue();
assertThat(fieldToContractInputMapping.getField().getName()).isEqualTo("rootInputName");
assertThat(fieldToContractInputMapping.getField().isCollection()).isTrue();
......
......@@ -50,7 +50,7 @@ public class MultipleBusinessObjectQueryInitializerTest {
aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()
.havingInput(aContractInput()
.withName("persistenceId")).build(),
"myData.employees");
"myData.employees", false);
initializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(employeeBo, nameField, aContractInput().withName("name")
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()).build()));
......@@ -77,6 +77,6 @@ public class MultipleBusinessObjectQueryInitializerTest {
aContractInput()
.withName("notPersistenceId")
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()).build(),
"myData.employees");
"myData.employees", false);
}
}
......@@ -34,7 +34,7 @@ public class MultipleCompositionReferencePropertyInitializerTest {
aContractInput().withName("addresses").multiple()
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX))
.build(),
"employee");
"employee", false);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + System.lineSeparator()
......@@ -51,6 +51,29 @@ public class MultipleCompositionReferencePropertyInitializerTest {
+ "return addressList}()");
}
@Test
public void should_build_a_closure_for_multiple_field_in_a_single_businessObject_without_existingValueOnPool() throws Exception {
final RelationField field = aCompositionField("addresses", aBO("Address").build());
field.setCollection(true);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(null, field,
aContractInput().withName("addresses").multiple()
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX))
.build(),
"employee", true);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employeeInput.addresses.each{" + System.lineSeparator()
+ "//Add a new composed Address instance" + System.lineSeparator()
+ "addressList.add({ currentAddressInput ->" + System.lineSeparator()
+ "def addressVar = new Address()" + System.lineSeparator()
+ "return addressVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
+ "}" + System.lineSeparator()
+ "return addressList}()");
}
@Test
public void should_build_a_closure_using_parent_iterator_for_multiple_field_in_a_multiple_businessObject() throws Exception {
final RelationField field = aCompositionField("addresses", aBO("Address").build());
......@@ -60,7 +83,7 @@ public class MultipleCompositionReferencePropertyInitializerTest {
aContractInput().withName("addresses").multiple()
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple())
.build(),
"employee");
"employee", false);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + System.lineSeparator()
......
......@@ -37,7 +37,7 @@ public class NewBusinessObjectListInitializerTest {
businessObject);
addressField.setCollection(true);
final AbstractBusinessObjectInitializer propertyInitializer = new NewBusinessObjectListInitializer(addressField, aContractInput().withName("address")
.multiple().in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX)).build(), "myAddresses");
.multiple().in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX)).build(), "myAddresses", false);
propertyInitializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(businessObject,
streetField, aContractInput().withName("street")
.in(aContractInput().withName("address").withType(ContractInputType.COMPLEX).multiple()
......@@ -57,6 +57,32 @@ public class NewBusinessObjectListInitializerTest {
+ "return addressList");
}
@Test
public void should_initialize_new_object_property_for_multiple_composed_reference_withoutExistingOnPool() throws Exception {
final SimpleField streetField = aSimpleField().withName("street").ofType(FieldType.STRING).notNullable().build();
final BusinessObject businessObject = aBO("org.test.Address").withField(streetField).build();
final RelationField addressField = aCompositionField("address",
businessObject);
addressField.setCollection(true);
final AbstractBusinessObjectInitializer propertyInitializer = new NewBusinessObjectListInitializer(addressField, aContractInput().withName("address")
.multiple().in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX)).build(), "myAddresses", true);
propertyInitializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(businessObject,
streetField, aContractInput().withName("street")
.in(aContractInput().withName("address").withType(ContractInputType.COMPLEX).multiple()
.in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX))).build()));
assertThat(propertyInitializer.getInitialValue()).isEqualTo("def addressList = []" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employee.address.each{" + System.lineSeparator()
+ "//Add a new composed Address instance" + System.lineSeparator()
+ "addressList.add({ currentAddressInput ->" + System.lineSeparator()
+ "def addressVar = new org.test.Address()" + System.lineSeparator()
+ "addressVar.street = currentAddressInput.street" + System.lineSeparator()
+ "return addressVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
+ "}" + System.lineSeparator()
+ "return addressList");
}
@Test
public void should_not_add_existing_object_to_list_input_is_a_root_input() throws Exception {
final SimpleField streetField = aSimpleField().withName("street").ofType(FieldType.STRING).notNullable().build();
......@@ -65,7 +91,7 @@ public class NewBusinessObjectListInitializerTest {
businessObject);
addressField.setCollection(true);
final AbstractBusinessObjectInitializer propertyInitializer = new NewBusinessObjectListInitializer(addressField, aContractInput().withName("addresses")
.multiple().build(), "myAddress");
.multiple().build(), "myAddress", false);
propertyInitializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(businessObject,
streetField, aContractInput().withName("street")
.in(aContractInput().withName("address").withType(ContractInputType.COMPLEX).multiple()
......
......@@ -51,7 +51,7 @@ public class PropertyInitializerFactoryTest {
final PropertyInitializerFactory factory = newFactory();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(aSimpleMapping(aStringField("name").build()).build(), aBusinessData()
.withName("employee").build());
.withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(SimpleFieldPropertyInitializer.class);
}
......@@ -66,7 +66,7 @@ public class PropertyInitializerFactoryTest {
.aRelationMapping(employee)
.addChild(aSimpleMapping(aStringField("name").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping.getChildren().get(0), aBusinessData()
.withName("employee").build());
.withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(SimpleFieldPropertyInitializer.class);
assertThat(((SimpleFieldPropertyInitializer) propertyInitializer).getParentBusinessObject()).isNotNull();
......@@ -82,9 +82,9 @@ public class PropertyInitializerFactoryTest {
.addChild(aRelationMapping(employee)).build();
final BusinessObjectData businessObjectData = aBusinessData()
.withName("employee").build();
factory.newPropertyInitializer(mapping.getChildren().get(0), businessObjectData);
factory.newPropertyInitializer(mapping.getChildren().get(0), businessObjectData, false);
verify(relationFactory).newPropertyInitializer(mapping.getChildren().get(0), businessObjectData);
verify(relationFactory).newPropertyInitializer(mapping.getChildren().get(0), businessObjectData, false);
}
@Test
......@@ -93,7 +93,7 @@ public class PropertyInitializerFactoryTest {
thrown.expect(UnsupportedOperationException.class);
factory.newPropertyInitializer(new FakeMapping(new Field() {
}), aBusinessData().build());
}), aBusinessData().build(), false);
}
private PropertyInitializerFactory newFactory() {
......
......@@ -49,7 +49,7 @@ public class RelationPropertyInitializerFactoryTest {
.addChild(aRelationMapping(addressField))
.addChild(aSimpleMapping(aStringField("street").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping.getChildren().get(0), aBusinessData()
.withName("employee").build());
.withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(CompositionReferencePropertyInitializer.class);
}
......@@ -66,7 +66,7 @@ public class RelationPropertyInitializerFactoryTest {
.addChild(aRelationMapping(addressField))
.addChild(aSimpleMapping(aStringField("street").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping.getChildren().get(0), aBusinessData()
.withName("employee").build());
.withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(MultipleCompositionReferencePropertyInitializer.class);
}
......@@ -78,7 +78,7 @@ public class RelationPropertyInitializerFactoryTest {
final BusinessObject businessObject = aBO("Employee").withField(anAggregationField("country", aBO("Country").build())).build();
final List<FieldToContractInputMapping> mappings = new FieldToContractInputMappingFactory().createMappingForBusinessObjectType(businessObject);
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mappings.get(0), aBusinessData()
.withName("employee").build());
.withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(AggregationReferencePropertyInitializer.class);
}
......@@ -92,7 +92,7 @@ public class RelationPropertyInitializerFactoryTest {
final BusinessObject businessObject = aBO("Employee").withField(anAggregationField).build();
final List<FieldToContractInputMapping> mappings = new FieldToContractInputMappingFactory().createMappingForBusinessObjectType(businessObject);
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mappings.get(0), aBusinessData()
.withName("employee").build());
.withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(MultipleAggregationReferencePropertyInitializer.class);
}
......
......@@ -56,6 +56,10 @@ public class RootContractInputGenerator {
}
public void build(final BusinessObjectData data) throws OperationCreationException {
build(data, false);
}
public void build(final BusinessObjectData data, final boolean isOnPool) throws OperationCreationException {
contractInput = ProcessFactory.eINSTANCE.createContractInput();
contractInput.setName(rootContractInputName);
contractInput.setType(ContractInputType.COMPLEX);
......@@ -74,7 +78,7 @@ public class RootContractInputGenerator {
mappingOperations.add(operationBuilder.toOperation(data, createParentMapping(data, rootContractInputName)));
}
try {
initialValueExpression = expressionBuilder.toExpression(data, createParentMapping(data, rootContractInputName));
initialValueExpression = expressionBuilder.toExpression(data, createParentMapping(data, rootContractInputName), isOnPool);
} catch (JavaModelException | BusinessObjectInstantiationException e) {
throw new OperationCreationException("Failed to create initial value expression", e);
}
......
......@@ -39,8 +39,8 @@ public class MultipleBusinessObjectQueryInitializer extends NewBusinessObjectLis
public MultipleBusinessObjectQueryInitializer(final BusinessObject businessObject,
final RelationField field,
final ContractInput contractInput, final String refName) {
super(field, contractInput, refName);
final ContractInput contractInput, final String refName, final boolean isOnPool) {
super(field, contractInput, refName, isOnPool);
persistenceIdInput = persistenceIdInput(contractInput);
this.businessObject = businessObject;
}
......
......@@ -30,8 +30,8 @@ public class MultipleCompositionReferencePropertyInitializer extends NewBusiness
public MultipleCompositionReferencePropertyInitializer(final BusinessObject parentBusinessObject, final RelationField field,
final ContractInput contractInput,
final String refName) {
super(field, contractInput, refName);
final String refName, final boolean isOnPool) {
super(field, contractInput, refName, isOnPool);
this.parentBusinessObject = parentBusinessObject;
}
......@@ -48,7 +48,7 @@ public class MultipleCompositionReferencePropertyInitializer extends NewBusiness
@Override
protected boolean shouldAppendExistingObjects() {
return parentBusinessObject == null;
return parentBusinessObject == null && !isOnPool;
}
@Override
......
......@@ -29,10 +29,12 @@ import com.google.common.base.Splitter;
public class NewBusinessObjectListInitializer extends AbstractBusinessObjectInitializer implements IPropertyInitializer {
protected ContractInput contractInput;
protected final boolean isOnPool;
public NewBusinessObjectListInitializer(final RelationField field, final ContractInput contractInput, final String refName) {
public NewBusinessObjectListInitializer(final RelationField field, final ContractInput contractInput, final String refName, final boolean isOnPool) {
super(field, refName);
this.contractInput = contractInput;
this.isOnPool = isOnPool;
}
@Override
......@@ -60,7 +62,7 @@ public class NewBusinessObjectListInitializer extends AbstractBusinessObjectInit
}
protected boolean shouldAppendExistingObjects() {
return contractInput.eContainer() != null;
return contractInput.eContainer() != null && !isOnPool;
}
private void returnListVar(final StringBuilder scriptBuilder, final BusinessObject businessObject) {
......
......@@ -31,12 +31,12 @@ public class PropertyInitializerFactory extends AbsractInitializerFactory implem
}
@Override
public IPropertyInitializer newPropertyInitializer(final FieldToContractInputMapping mapping, final BusinessObjectData data) {
public IPropertyInitializer newPropertyInitializer(final FieldToContractInputMapping mapping, final BusinessObjectData data, final boolean isOnPool) {
final Field field = mapping.getField();
if (field instanceof SimpleField) {
return new SimpleFieldPropertyInitializer(firstMultipleParentBusinessObject(mapping), (SimpleField) field, mapping.getContractInput());
} else if (field instanceof RelationField) {
return relationPropertyInitializerFactory.newPropertyInitializer(mapping, data);
return relationPropertyInitializerFactory.newPropertyInitializer(mapping, data, isOnPool);
}
throw new UnsupportedOperationException(field.getClass().getName() + " is not supported");
}
......
......@@ -27,33 +27,34 @@ import org.bonitasoft.studio.model.process.BusinessObjectData;
public class RelationPropertyInitializerFactory extends AbsractInitializerFactory implements InitializerFactory {
@Override
public IPropertyInitializer newPropertyInitializer(final FieldToContractInputMapping mapping, final BusinessObjectData data) {
public IPropertyInitializer newPropertyInitializer(final FieldToContractInputMapping mapping, final BusinessObjectData data, final boolean isOnPool) {
final RelationField relationField = (RelationField) mapping.getField();
return relationField.getType() == Type.AGGREGATION ? newAggregatedReferenceInitializer(mapping, data, relationField) : newComposedReferenceInitializer(
return relationField.getType() == Type.AGGREGATION ? newAggregatedReferenceInitializer(mapping, data, relationField, isOnPool)
: newComposedReferenceInitializer(
mapping,
data, relationField);
data, relationField, isOnPool);
}
private IPropertyInitializer newAggregatedReferenceInitializer(final FieldToContractInputMapping mapping,
final BusinessObjectData data,
final RelationField relationField) {
final RelationField relationField, final boolean isOnPool) {
return relationField.isCollection() ?
new MultipleAggregationReferencePropertyInitializer(firstMultipleParentBusinessObject(mapping),
businessObject(mapping),
relationField,
mapping.getContractInput(),
toRefName(mapping, data))
toRefName(mapping, data), isOnPool)
: new AggregationReferencePropertyInitializer(firstMultipleParentBusinessObject(mapping), relationField,
mapping.getContractInput(), toRefName(mapping, data));
}
private IPropertyInitializer newComposedReferenceInitializer(final FieldToContractInputMapping mapping,
final BusinessObjectData data,
final RelationField relationField) {
final RelationField relationField, final boolean isOnPool) {
return relationField.isCollection() ?
new MultipleCompositionReferencePropertyInitializer(firstMultipleParentBusinessObject(mapping), relationField,
mapping.getContractInput(),
toRefName(mapping.getParent(), data))
toRefName(mapping.getParent(), data), isOnPool)
: new CompositionReferencePropertyInitializer(relationField, mapping.getContractInput(),
toRefName(mapping, data));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment