Commit f278a5e4 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 (follow-up of the
previous commit...)
parent b368f0eb
......@@ -59,7 +59,7 @@ public class FieldToContractInputMappingExpressionBuilderTest {
mapping.toContractInput(aContractInput().withName("employee").withType(ContractInputType.COMPLEX).build());
final BusinessObjectData businessObjectData = aBusinessData().withName("myEmployee").build();
final Expression expression = expressionBuilder.toExpression(businessObjectData,
mapping);
mapping, false);
ExpressionAssert.assertThat(expression)
.hasName("employee.address")
......@@ -77,7 +77,7 @@ public class FieldToContractInputMappingExpressionBuilderTest {
final SimpleField lastNameField = aSimpleField().withName("lastName").ofType(FieldType.STRING).build();
final FieldToContractInputMapping mapping = aSimpleMapping(lastNameField).build();
final Expression expression = expressionBuilder.toExpression(aBusinessData().withName("myEmployee").build(),
mapping);
mapping, false);
ExpressionAssert.assertThat(expression)
.hasName("lastName")
......@@ -98,7 +98,7 @@ public class FieldToContractInputMappingExpressionBuilderTest {
final BusinessObjectData data = aBusinessData().withName("myEmployee").build();
final Expression expression = expressionBuilder.toExpression(data,
mapping);
mapping, false);
ExpressionAssert.assertThat(expression)
.hasName("employee.lastName")
......
......@@ -24,6 +24,7 @@ import static org.bonitasoft.studio.model.expression.builders.ExpressionBuilder.
import static org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder.aBusinessData;
import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
......@@ -81,7 +82,7 @@ public class FieldToContractInputMappingOperationBuilderTest {
final SimpleField lastNameField = aSimpleField().withName("lastName").ofType(FieldType.STRING).build();
final FieldToContractInputMapping mapping = aSimpleMapping(lastNameField).build();
final BusinessObjectData data = aBusinessData().withName("myEmployee").build();
when(expressionBuilder.toExpression(data, mapping)).thenReturn(anExpression().build());
when(expressionBuilder.toExpression(data, mapping, false)).thenReturn(anExpression().build());
final Operation operation = inputToOperation.toOperation(data,
mapping);
......@@ -95,7 +96,7 @@ public class FieldToContractInputMappingOperationBuilderTest {
.hasType(ExpressionConstants.VARIABLE_TYPE);
assertThat(operation.getLeftOperand().getReferencedElements()).hasSize(1);
verify(expressionBuilder).toExpression(data, mapping);
verify(expressionBuilder).toExpression(data, mapping, false);
}
@Test
......@@ -108,7 +109,7 @@ public class FieldToContractInputMappingOperationBuilderTest {
.withName("employee").build());
final BusinessObjectData data = aBusinessData().withName("myEmployee").build();
when(expressionBuilder.toExpression(data, mapping)).thenReturn(anExpression().build());
when(expressionBuilder.toExpression(data, mapping, false)).thenReturn(anExpression().build());
final Operation operation = inputToOperation.toOperation(data,
mapping);
......@@ -121,7 +122,7 @@ public class FieldToContractInputMappingOperationBuilderTest {
.hasName("myEmployee").hasContent("myEmployee")
.hasType(ExpressionConstants.VARIABLE_TYPE);
assertThat(operation.getLeftOperand().getReferencedElements()).hasSize(1);
verify(expressionBuilder).toExpression(data, mapping);
verify(expressionBuilder).toExpression(data, mapping, false);
}
@Test
......@@ -132,7 +133,7 @@ public class FieldToContractInputMappingOperationBuilderTest {
final FieldToContractInputMapping mapping = aRelationMapping(address).build();
mapping.toContractInput(aContractInput().withName("employee").withType(ContractInputType.COMPLEX).build());
final BusinessObjectData businessObjectData = aBusinessData().withName("myEmployee").build();
when(expressionBuilder.toExpression(businessObjectData, mapping)).thenReturn(anExpression().build());
when(expressionBuilder.toExpression(businessObjectData, mapping, false)).thenReturn(anExpression().build());
final Operation operation = inputToOperation.toOperation(businessObjectData,
mapping);
......@@ -145,7 +146,7 @@ public class FieldToContractInputMappingOperationBuilderTest {
.hasType(ExpressionConstants.VARIABLE_TYPE);
assertThat(operation.getLeftOperand().getReferencedElements()).hasSize(1);
verify(expressionBuilder).toExpression(businessObjectData, mapping);
verify(expressionBuilder).toExpression(businessObjectData, mapping, false);
}
@Test
......@@ -153,7 +154,8 @@ public class FieldToContractInputMappingOperationBuilderTest {
final FieldToContractInputMappingOperationBuilder inputToOperation = createFixture();
when(expressionReturnTypeFilter.compatibleReturnTypes(anyString(), anyString())).thenReturn(false);
final SimpleField lastNameField = aSimpleField().withName("lastName").ofType(FieldType.STRING).build();
when(expressionBuilder.toExpression(any(BusinessObjectData.class), any(FieldToContractInputMapping.class))).thenReturn(anExpression().build());
when(expressionBuilder.toExpression(any(BusinessObjectData.class), any(FieldToContractInputMapping.class), anyBoolean())).thenReturn(
anExpression().build());
thrown.expect(OperationCreationException.class);
inputToOperation.toOperation(aBusinessData().withName("myEmployee").build(),
aSimpleMapping(lastNameField).build());
......@@ -168,9 +170,10 @@ public class FieldToContractInputMappingOperationBuilderTest {
final FieldToContractInputMapping mapping = spy(aRelationMapping(address).build());
mapping.toContractInput(aContractInput().withName("employee").withType(ContractInputType.COMPLEX).build());
final MappingOperationScriptBuilder fakeScriptBuilder = mock(MappingOperationScriptBuilder.class);
when(fakeScriptBuilder.toScript()).thenThrow(BusinessObjectInstantiationException.class);
when(fakeScriptBuilder.toScript(false)).thenThrow(BusinessObjectInstantiationException.class);
doReturn(fakeScriptBuilder).when(mapping).getScriptBuilder(any(BusinessObjectData.class));
when(expressionBuilder.toExpression(any(BusinessObjectData.class), any(FieldToContractInputMapping.class))).thenReturn(anExpression().build());
when(expressionBuilder.toExpression(any(BusinessObjectData.class), any(FieldToContractInputMapping.class), anyBoolean())).thenReturn(
anExpression().build());
thrown.expect(OperationCreationException.class);
inputToOperation.toOperation(aBusinessData().withName("myEmployee").build(),
mapping);
......@@ -182,7 +185,8 @@ public class FieldToContractInputMappingOperationBuilderTest {
final SimpleField lastNameField = aSimpleField().withName("lastName").ofType(FieldType.STRING).build();
final FieldToContractInputMapping mapping = aSimpleMapping(lastNameField).build();
when(expressionBuilder.toExpression(any(BusinessObjectData.class), any(FieldToContractInputMapping.class))).thenReturn(anExpression().build());
when(expressionBuilder.toExpression(any(BusinessObjectData.class), any(FieldToContractInputMapping.class), anyBoolean())).thenReturn(
anExpression().build());
final Operation operation = inputToOperation.toOperation(aBusinessData().multiple().withName("employees").build(),
mapping);
......
......@@ -44,7 +44,7 @@ public class MappingOperationScriptBuilderTest {
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
new SimpleFieldToContractInputMapping(firstNameField), firstNameField);
final String script = scriptBuilder.toScript();
final String script = scriptBuilder.toScript(false);
assertThat(scriptBuilder.needsDataDependency()).isFalse();
assertThat(script).isEqualTo("firstName");
......@@ -59,7 +59,7 @@ public class MappingOperationScriptBuilderTest {
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
relationFieldToContractInputMapping, addressField);
final String script = scriptBuilder.toScript();
final String script = scriptBuilder.toScript(false);
assertThat(scriptBuilder.needsDataDependency()).isTrue();
assertThat(script).isEqualTo("def addressVar = new Address()" + System.lineSeparator()
......@@ -78,7 +78,7 @@ public class MappingOperationScriptBuilderTest {
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
relationFieldToContractInputMapping, addressField);
final String script = scriptBuilder.toScript();
final String script = scriptBuilder.toScript(false);
assertThat(script).isEqualTo("def addressVar = new Address()" + System.lineSeparator()
+ "return addressVar");
......@@ -95,7 +95,7 @@ public class MappingOperationScriptBuilderTest {
final MappingOperationScriptBuilder scriptBuilder = new MappingOperationScriptBuilder(aBusinessData().withName("employee").build(),
relationFieldToContractInputMapping, addressField);
final String script = scriptBuilder.toScript();
final String script = scriptBuilder.toScript(false);
assertThat(script).isEqualTo("def addressVar = new Address()" + System.lineSeparator()
+ "addressVar.street = address.street" + System.lineSeparator()
......@@ -126,6 +126,6 @@ public class MappingOperationScriptBuilderTest {
mapping, field);
thrown.expect(UnsupportedOperationException.class);
scriptBuilder.toScript();
scriptBuilder.toScript(false);
}
}
......@@ -44,7 +44,7 @@ public class MultipleAggregationReferencePropertyInitializerTest {
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(null, employeeBo,
employeesField, aContractInput().withName("persistenceId")
.in(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()).build(), "emp");
.in(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()).build(), "emp", false);
final String initialValue = initializer.getInitialValue();
......@@ -63,6 +63,37 @@ public class MultipleAggregationReferencePropertyInitializerTest {
+ "return employeeList}()");
}
@Test
public void should_embbed_aggregation_list_in_a_closure_WithoutExistingOnAPool() throws Exception {
final BusinessObject employeeBo = aBO("Employee").build();
final SimpleField pIdField = new SimpleField();
pIdField.setName(Field.PERSISTENCE_ID);
pIdField.setType(FieldType.LONG);
employeeBo.addField(pIdField);
final SimpleField nameField = aStringField("name").build();
employeeBo.addField(nameField);
final RelationField employeesField = anAggregationField("employees", employeeBo);
employeesField.setCollection(true);
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(null, employeeBo,
employeesField, aContractInput().withName("persistenceId")
.in(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()).build(), "emp", true);
final String initialValue = initializer.getInitialValue();
assertThat(initialValue).isEqualTo("{" + System.lineSeparator()
+ "def employeeList = []" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employees.each{" + System.lineSeparator()
+ "//Add aggregated Employee instance" + System.lineSeparator()
+ "employeeList.add({ currentEmployeeInput ->" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId.toLong())" + System.lineSeparator()
+ "return employeeVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
+ "}" + System.lineSeparator()
+ "return employeeList}()");
}
@Test
public void should_use_parent_as_iterator() throws Exception {
final BusinessObject directoryBo = aBO("Directory").build();
......@@ -81,7 +112,7 @@ public class MultipleAggregationReferencePropertyInitializerTest {
employeesField,
aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()
.havingInput(aContractInput().withName("persistenceId"))
.in(aContractInput().withName("direcotries").withType(ContractInputType.COMPLEX).multiple()).build(), "emp");
.in(aContractInput().withName("direcotries").withType(ContractInputType.COMPLEX).multiple()).build(), "emp", false);
final String initialValue = initializer.getInitialValue();
......
......@@ -46,7 +46,7 @@ public class BusinessObjectInitializerFactoryTest {
final RelationField aCompositionField = aCompositionField("employee", aBO("Employee").build());
final FieldToContractInputMapping mapping = aRelationMapping(aCompositionField)
.addChild(aSimpleMapping(aStringField("name").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping, aBusinessData().withName("employee").build());
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping, aBusinessData().withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(NewBusinessObjectInitializer.class);
}
......@@ -60,7 +60,7 @@ public class BusinessObjectInitializerFactoryTest {
final FieldToContractInputMapping mapping = FieldToContractInputMappingBuilder
.aRelationMapping(aCompositionField)
.addChild(aSimpleMapping(aStringField("name").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping, aBusinessData().withName("employee").build());
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping, aBusinessData().withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(NewBusinessObjectListInitializer.class);
}
......@@ -71,7 +71,7 @@ public class BusinessObjectInitializerFactoryTest {
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());
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mappings.get(0), aBusinessData().withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(BusinessObjectQueryInitializer.class);
}
......@@ -84,7 +84,7 @@ public class BusinessObjectInitializerFactoryTest {
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(MultipleBusinessObjectQueryInitializer.class);
}
......
......@@ -22,6 +22,7 @@ import static org.bonitasoft.studio.model.process.builders.ContractBuilder.aCont
import static org.bonitasoft.studio.model.process.builders.PoolBuilder.aPool;
import static org.bonitasoft.studio.model.process.builders.TaskBuilder.aTask;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
......@@ -158,7 +159,7 @@ public class ContractInputGenerationWizardTest {
BusinessObjectBuilder.aBO("org.company.Employee").withField(SimpleFieldBuilder.aStringField("firstName").build()).build());
when(repositoryAccessor.getRepositoryStore(BusinessObjectModelRepositoryStore.class)).thenReturn(store);
when(preferenceStore.getString(ContractInputGenerationWizard.HIDE_GENERATION_SUCCESS_DIALOG)).thenReturn("always");
when(expressionBuilder.toExpression(any(BusinessObjectData.class), any(FieldToContractInputMapping.class))).thenReturn(
when(expressionBuilder.toExpression(any(BusinessObjectData.class), any(FieldToContractInputMapping.class), anyBoolean())).thenReturn(
aGroovyScriptExpression().build());
final ContractInputGenerationWizard wizard = new ContractInputGenerationWizard(process, editingDomain(), repositoryAccessor, operationBuilder,
expressionBuilder,
......
......@@ -56,11 +56,11 @@ public class FieldToContractInputMappingExpressionBuilder {
this.expressionEditorService = expressionEditorService;
}
public Expression toExpression(final BusinessObjectData data, final FieldToContractInputMapping mapping)
public Expression toExpression(final BusinessObjectData data, final FieldToContractInputMapping mapping, final boolean isOnPool)
throws BusinessObjectInstantiationException, JavaModelException {
final ContractInput contractInput = mapping.getContractInput();
final MappingOperationScriptBuilder mappingOperationScriptBuilder = mapping.getScriptBuilder(data);
final Expression scriptExpression = ExpressionHelper.createGroovyScriptExpression(mappingOperationScriptBuilder.toScript(),
final Expression scriptExpression = ExpressionHelper.createGroovyScriptExpression(mappingOperationScriptBuilder.toScript(isOnPool),
mapping.getFieldType());
addScriptDependencies(mappingOperationScriptBuilder, mapping.getContractInput(), data, scriptExpression);
scriptExpression.setName(Joiner.on(".").join(toAncestorNameList().apply(contractInput)));
......
......@@ -43,12 +43,13 @@ public class FieldToContractInputMappingOperationBuilder {
this.expressionBuilder = expressionBuilder;
}
public Operation toOperation(final BusinessObjectData data, final FieldToContractInputMapping mapping) throws OperationCreationException {
public Operation toOperation(final BusinessObjectData data, final FieldToContractInputMapping mapping)
throws OperationCreationException {
final Operation operation = ExpressionFactory.eINSTANCE.createOperation();
operation.setLeftOperand(ExpressionHelper.createVariableExpression(data));
operation.setOperator(operator(mapping, data));
try {
operation.setRightOperand(expressionBuilder.toExpression(data, mapping));
operation.setRightOperand(expressionBuilder.toExpression(data, mapping, false));
} catch (final BusinessObjectInstantiationException | JavaModelException e) {
throw new OperationCreationException("Failed to create right operand expression", e);
}
......
......@@ -54,9 +54,9 @@ public class MappingOperationScriptBuilder {
this.field = field;
}
public String toScript() throws BusinessObjectInstantiationException {
public String toScript(final boolean isOnPool) throws BusinessObjectInstantiationException {
mapping.getContractInput();
return format(buildPropertyInitializerTree(mapping, field, data).getInitialValue());
return format(buildPropertyInitializerTree(mapping, field, data, isOnPool).getInitialValue());
}
private String format(final String initialValue) {
......@@ -69,19 +69,20 @@ public class MappingOperationScriptBuilder {
return document.get();
}
private IPropertyInitializer buildPropertyInitializerTree(final FieldToContractInputMapping mapping, final Field rootField, final BusinessObjectData data) {
private IPropertyInitializer buildPropertyInitializerTree(final FieldToContractInputMapping mapping, final Field rootField, final BusinessObjectData data,
final boolean isOnPool) {
final Field field = mapping.getField();
if (field instanceof SimpleField) {
return propertyInitializerFactory.newPropertyInitializer(mapping, data);
return propertyInitializerFactory.newPropertyInitializer(mapping, data, isOnPool);
}
if (field instanceof RelationField) {
final AbstractBusinessObjectInitializer scriptInitializer = (AbstractBusinessObjectInitializer) findInitializerFactory(field, rootField)
.newPropertyInitializer(
mapping,
data);
data, isOnPool);
for (final FieldToContractInputMapping child : mapping.getChildren()) {
if (child.isGenerated()) {
scriptInitializer.addPropertyInitializer(buildPropertyInitializerTree(child, rootField, data));
scriptInitializer.addPropertyInitializer(buildPropertyInitializerTree(child, rootField, data, isOnPool));
}
}
needsDataDependency = scriptInitializer instanceof NewBusinessObjectInitializer;
......
......@@ -30,8 +30,8 @@ public class MultipleAggregationReferencePropertyInitializer extends MultipleBus
public MultipleAggregationReferencePropertyInitializer(final BusinessObject multipleParentBusinessObject, final BusinessObject businessObject,
final RelationField field,
final ContractInput contractInput, final String refName) {
super(businessObject, field, contractInput, refName);
final ContractInput contractInput, final String refName, final boolean isOnPool) {
super(businessObject, field, contractInput, refName, isOnPool);
this.multipleParentBusinessObject = multipleParentBusinessObject;
}
......@@ -48,7 +48,7 @@ public class MultipleAggregationReferencePropertyInitializer extends MultipleBus
@Override
protected boolean shouldAppendExistingObjects() {
return multipleParentBusinessObject == null;
return multipleParentBusinessObject == null && !isOnPool;
}
@Override
......
......@@ -28,18 +28,19 @@ import org.bonitasoft.studio.model.process.ContractInput;
public class BusinessObjectInitializerFactory 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 ? newAggregatedObjectInitializer(mapping, data, relationField) : newComposedObjectInitializer(
mapping, data, relationField);
return relationField.getType() == Type.AGGREGATION ? newAggregatedObjectInitializer(mapping, data, relationField, isOnPool)
: newComposedObjectInitializer(
mapping, data, relationField, isOnPool);
}
private IPropertyInitializer newAggregatedObjectInitializer(final FieldToContractInputMapping mapping,
final BusinessObjectData data,
final RelationField relationField) {
final RelationField relationField, final boolean isOnPool) {
return relationField.isCollection() ?
new MultipleBusinessObjectQueryInitializer(businessObject(mapping), relationField,
mapping.getContractInput(), toRefName(mapping, data))
mapping.getContractInput(), toRefName(mapping, data), isOnPool)
: new BusinessObjectQueryInitializer(firstMultipleParentBusinessObject(mapping), relationField,
mapping.getContractInput(),
toRefName(mapping, data));
......@@ -47,9 +48,9 @@ public class BusinessObjectInitializerFactory extends AbsractInitializerFactory
private IPropertyInitializer newComposedObjectInitializer(final FieldToContractInputMapping mapping,
final BusinessObjectData data,
final RelationField relationField) {
final RelationField relationField, final boolean isOnPool) {
return relationField.isCollection() ?
new NewBusinessObjectListInitializer(relationField, mapping.getContractInput(), toRefName(mapping, data)) :
new NewBusinessObjectListInitializer(relationField, mapping.getContractInput(), toRefName(mapping, data), isOnPool) :
new NewBusinessObjectInitializer(relationField, toRefName(mapping, data), mapping.getContractInput().eContainer() instanceof ContractInput);
}
......
......@@ -20,5 +20,5 @@ import org.bonitasoft.studio.model.process.BusinessObjectData;
public interface InitializerFactory {
IPropertyInitializer newPropertyInitializer(final FieldToContractInputMapping mapping, final BusinessObjectData data);
IPropertyInitializer newPropertyInitializer(final FieldToContractInputMapping mapping, final BusinessObjectData data, final boolean isOnPool);
}
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