...
 
Commits (4)
......@@ -29,7 +29,6 @@ import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import org.bonitasoft.engine.bdm.BusinessObjectModelConverter;
......@@ -61,9 +60,6 @@ import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.swt.graphics.Image;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.wiring.BundleWiring;
import org.xml.sax.SAXException;
import com.google.common.base.Predicate;
......@@ -90,13 +86,11 @@ public class BusinessObjectModelRepositoryStore<F extends AbstractBDMFileStore>
static {
extensions.add(BDM_TYPE_EXTENSION);
}
public BusinessObjectModelConverter getConverter() {
return converter;
}
@Override
public AbstractBDMFileStore createRepositoryFileStore(final String fileName) {
return new BusinessObjectModelFileStore(fileName, this);
......@@ -107,13 +101,11 @@ public class BusinessObjectModelRepositoryStore<F extends AbstractBDMFileStore>
return STORE_NAME;
}
@Override
public String getDisplayName() {
return Messages.businessObjectRepositoryStoreName;
}
@Override
public Image getIcon() {
return Pics.getImage("bdm.png", BusinessObjectPlugin.getDefault());
......@@ -131,7 +123,7 @@ public class BusinessObjectModelRepositoryStore<F extends AbstractBDMFileStore>
.map(fileStore -> fileStore.getBusinessObject(qualifiedName) != null ? fileStore : null);
}
public Optional<BusinessObject> getBusinessObjectByQualifiedName(final String qualifiedName) {
public Optional<BusinessObject> getBusinessObjectByQualifiedName(String qualifiedName) {
final Optional<BusinessObjectModelFileStore> businessObjectFileStore = Optional
.ofNullable((BusinessObjectModelFileStore) getChild(BusinessObjectModelFileStore.BOM_FILENAME));
return businessObjectFileStore.map(fileStore -> fileStore.getBusinessObject(qualifiedName));
......
......@@ -198,9 +198,8 @@ public class FieldToContractInputMappingOperationBuilderTest {
mapping, new NullProgressMonitor());
OperatorAssert.assertThat(operation.getOperator())
.hasType(ExpressionConstants.JAVA_METHOD_OPERATOR)
.hasInputTypes(Collection.class.getName())
.hasExpression("addAll");
.hasType(ExpressionConstants.ASSIGNMENT_OPERATOR)
.hasInputTypes(Collection.class.getName());
}
private FieldToContractInputMappingOperationBuilder createFixture() {
......
......@@ -27,17 +27,26 @@ public class AggregationReferencePropertyInitializerTest {
@Test
public void should_call_query_in_a_closure() throws Exception {
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(anAggregationField("country",
aBO("country").build()));
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(
anAggregationField("country",
aBO("country").build()));
context.setMapping(mapping);
context.setContractInput(aContractInput().withName("persistenceId").in(aContractInput().withName("country")).build());
context.setContractInput(
aContractInput().withName("persistenceId_string").in(aContractInput().withName("country")).build());
context.setLocalVariableName("countryVar");
final String initialValue = new AggregationReferencePropertyInitializer(null, context).getInitialValue();
assertThat(initialValue).isEqualTo("{" + System.lineSeparator()
+ "//Retrieve aggregated country using its DAO and persistenceId" + System.lineSeparator()
+ "def countryVar = countryDAO.findByPersistenceId(country.persistenceId.toLong())" + System.lineSeparator()
+ "def countryVar = countryDAO.findByPersistenceId(country.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!countryVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `country` with the persistence id \" + country.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "return countryVar}()");
}
}
......@@ -35,35 +35,47 @@ public class BusinessObjectQueryInitializerTest {
@Test
public void should_generate_a_script_using_query_to_retrieve_a_business_object_in_initial_value() throws Exception {
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(anAggregationField("country",
aBO("org.test.Country").build()));
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(
anAggregationField("country",
aBO("org.test.Country").build()));
context.setMapping(mapping);
context.setData(aBusinessData().withName("myCountry").build());
context.setContractInput(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)
.havingInput(aContractInput().withName("persistenceId")).build());
.havingInput(aContractInput().withName("persistenceId_string")).build());
context.setLocalVariableName("countryVar");
final BusinessObjectQueryInitializer initializer = new BusinessObjectQueryInitializer(null, context);
initializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(null, aStringField("persistenceId").build(),
aContractInput().withName("persistenceId").in(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)).build()));
aContractInput().withName("persistenceId_string")
.in(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)).build()));
initializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(null, aStringField("name").build(),
aContractInput().withName("name").in(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)).build()));
aContractInput().withName("name")
.in(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)).build()));
final String initialValue = initializer.getInitialValue();
assertThat(initialValue).isEqualTo(
"//Retrieve aggregated Country using its DAO and persistenceId" + System.lineSeparator()
+ "def countryVar = countryDAO.findByPersistenceId(countryInput.persistenceId.toLong())" + System.lineSeparator()
+ "countryVar.name = countryInput.name" + System.lineSeparator()
+ "def countryVar = countryDAO.findByPersistenceId(countryInput.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!countryVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `Country` with the persistence id \" + countryInput.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "countryVar.name = countryInput.name"
+ System.lineSeparator()
+ "return countryVar");
}
@Test
public void should_throw_an_IllegalStateException_if_persistenceId_is_not_found() throws Exception {
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(anAggregationField("country",
aBO("org.test.Country").build()));
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(
anAggregationField("country",
aBO("org.test.Country").build()));
context.setMapping(mapping);
context.setData(aBusinessData().withName("myCountry").build());
context.setContractInput(aContractInput().withName("countryInput").withType(ContractInputType.COMPLEX)
......
......@@ -32,7 +32,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class MultipleBusinessObjectQueryInitializerTest {
public class MultipleAggregationBusinessObjectQueryInitializerTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
......@@ -55,22 +55,34 @@ public class MultipleBusinessObjectQueryInitializerTest {
context.setData(aBusinessData().withName("myData").build());
context.setContractInput(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()
.havingInput(aContractInput()
.withName("persistenceId")).build());
.withName("persistenceId_string"))
.build());
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
final MultipleBusinessObjectQueryInitializer initializer = new MultipleBusinessObjectQueryInitializer(employeeBo, context);
initializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(employeeBo, nameField, aContractInput().withName("name")
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple()).build()));
final MultipleAggregationBusinessObjectQueryInitializer initializer = new MultipleAggregationBusinessObjectQueryInitializer(
employeeBo,
context);
initializer.addPropertyInitializer(new SimpleFieldPropertyInitializer(employeeBo, nameField,
aContractInput().withName("name")
.in(aContractInput().withName("employeeInput").withType(ContractInputType.COMPLEX).multiple())
.build()));
final String initialValue = initializer.getInitialValue();
assertThat(initialValue).isEqualTo("def employeeList = []" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employeeInput.each{" + System.lineSeparator()
+ "//Add aggregated Employee instance" + System.lineSeparator()
+ "//Add Employee instance" + System.lineSeparator()
+ "employeeList.add({ currentEmployeeInput ->" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId.toLong())" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!employeeVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `Employee` with the persistence id \" + currentEmployeeInput.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "employeeVar.name = currentEmployeeInput.name" + System.lineSeparator()
+ "return employeeVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
......@@ -90,6 +102,6 @@ public class MultipleBusinessObjectQueryInitializerTest {
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
new MultipleBusinessObjectQueryInitializer(null, context);
new MultipleAggregationBusinessObjectQueryInitializer(null, context);
}
}
......@@ -48,24 +48,30 @@ public class MultipleAggregationReferencePropertyInitializerTest {
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(employeesField);
context.setMapping(mapping);
context.setData(aBusinessData().withName("emp").build());
context.setContractInput(aContractInput().withName("persistenceId")
context.setContractInput(aContractInput().withName("persistenceId_string")
.in(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()).build());
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(null, employeeBo, context);
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(
null, employeeBo, context);
final String initialValue = initializer.getInitialValue();
assertThat(initialValue).isEqualTo("{" + System.lineSeparator()
+ "def employeeList = []" + System.lineSeparator()
+ "//Uncomment line below to append existing employees" + System.lineSeparator()
+ "//employeeList.addAll(emp.employees)" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employees.each{" + System.lineSeparator()
+ "//Add aggregated Employee instance" + System.lineSeparator()
+ "//Add Employee instance" + System.lineSeparator()
+ "employeeList.add({ currentEmployeeInput ->" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId.toLong())" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!employeeVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `Employee` with the persistence id \" + currentEmployeeInput.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "return employeeVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
+ "}" + System.lineSeparator()
......@@ -88,22 +94,30 @@ public class MultipleAggregationReferencePropertyInitializerTest {
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(employeesField);
context.setMapping(mapping);
context.setData(aBusinessData().withName("emp").build());
context.setContractInput(aContractInput().withName("persistenceId")
context.setContractInput(aContractInput().withName("persistenceId_string")
.in(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()).build());
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
context.setOnPool(true);
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(null, employeeBo, context);
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(
null, employeeBo, context);
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()
+ "//Add Employee instance" + System.lineSeparator()
+ "employeeList.add({ currentEmployeeInput ->" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId.toLong())" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!employeeVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `Employee` with the persistence id \" + currentEmployeeInput.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "return employeeVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
+ "}" + System.lineSeparator()
......@@ -129,12 +143,13 @@ public class MultipleAggregationReferencePropertyInitializerTest {
context.setMapping(mapping);
context.setData(aBusinessData().withName("emp").build());
context.setContractInput(aContractInput().withName("employees").withType(ContractInputType.COMPLEX).multiple()
.havingInput(aContractInput().withName("persistenceId"))
.havingInput(aContractInput().withName("persistenceId_string"))
.in(aContractInput().withName("direcotries").withType(ContractInputType.COMPLEX).multiple()).build());
context.setLocalVariableName("employeeVar");
context.setLocalListVariableName("employeeList");
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(directoryBo, employeeBo,
final MultipleAggregationReferencePropertyInitializer initializer = new MultipleAggregationReferencePropertyInitializer(
directoryBo, employeeBo,
context);
final String initialValue = initializer.getInitialValue();
......@@ -142,9 +157,16 @@ public class MultipleAggregationReferencePropertyInitializerTest {
+ "def employeeList = []" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "currentDirectoryInput.employees.each{" + System.lineSeparator()
+ "//Add aggregated Employee instance" + System.lineSeparator()
+ "//Add Employee instance" + System.lineSeparator()
+ "employeeList.add({ currentEmployeeInput ->" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId.toLong())" + System.lineSeparator()
+ "def employeeVar = employeeDAO.findByPersistenceId(currentEmployeeInput.persistenceId_string.toLong())"
+ System.lineSeparator()
+ "if(!employeeVar) {"
+ System.lineSeparator()
+ "throw new IllegalArgumentException(\"The aggregated reference of type `Employee` with the persistence id \" + currentEmployeeInput.persistenceId_string.toLong() + \" has not been found.\")"
+ System.lineSeparator()
+ "}"
+ System.lineSeparator()
+ "return employeeVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
+ "}" + System.lineSeparator()
......
......@@ -43,12 +43,11 @@ public class MultipleCompositionReferencePropertyInitializerTest {
context.setLocalVariableName("addressVar");
context.setLocalListVariableName("addressList");
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(null, context);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(
null, context);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + System.lineSeparator()
+ "//Uncomment line below to append existing addresses" + System.lineSeparator()
+ "//addressList.addAll(employee.addresses)" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employeeInput.addresses.each{" + System.lineSeparator()
+ "//Add a new composed Address instance" + System.lineSeparator()
......@@ -61,7 +60,8 @@ public class MultipleCompositionReferencePropertyInitializerTest {
}
@Test
public void should_build_a_closure_for_multiple_field_in_a_single_businessObject_without_existingValueOnPool() throws Exception {
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);
......@@ -76,7 +76,8 @@ public class MultipleCompositionReferencePropertyInitializerTest {
context.setLocalListVariableName("addressList");
context.setOnPool(true);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(null, context);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(
null, context);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + System.lineSeparator()
......@@ -92,7 +93,8 @@ public class MultipleCompositionReferencePropertyInitializerTest {
}
@Test
public void should_build_a_closure_using_parent_iterator_for_multiple_field_in_a_multiple_businessObject() throws Exception {
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());
field.setCollection(true);
......@@ -108,7 +110,8 @@ public class MultipleCompositionReferencePropertyInitializerTest {
context.setOnPool(true);
final BusinessObject employee = aBO("Employee").withField(field).build();
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(employee, context);
final MultipleCompositionReferencePropertyInitializer initializer = new MultipleCompositionReferencePropertyInitializer(
employee, context);
assertThat(initializer.getInitialValue()).isEqualTo("{" + System.lineSeparator()
+ "def addressList = []" + System.lineSeparator()
......
......@@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.bonitasoft.studio.model.businessObject.BusinessObjectBuilder.aBO;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aCompositionField;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aSimpleField;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aStringField;
import static org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder.aBusinessData;
import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput;
......@@ -26,41 +27,48 @@ import org.bonitasoft.engine.bdm.model.field.FieldType;
import org.bonitasoft.engine.bdm.model.field.RelationField;
import org.bonitasoft.engine.bdm.model.field.SimpleField;
import org.bonitasoft.studio.contract.core.mapping.RelationFieldToContractInputMapping;
import org.bonitasoft.studio.contract.core.mapping.SimpleFieldToContractInputMapping;
import org.bonitasoft.studio.model.process.ContractInputType;
import org.junit.Test;
public class NewBusinessObjectListInitializerTest {
@SuppressWarnings("unchecked")
@Test
public void should_initialize_new_object_property_for_multiple_composed_reference() 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);
SimpleField streetField = aSimpleField().withName("street").ofType(FieldType.STRING).notNullable().build();
BusinessObject businessObject = aBO("org.test.Address").withField(streetField).build();
RelationField addressField = aCompositionField("address", businessObject);
addressField.setCollection(true);
final InitializerContext context = new InitializerContext();
final RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(addressField);
InitializerContext context = new InitializerContext();
RelationFieldToContractInputMapping mapping = new RelationFieldToContractInputMapping(addressField);
mapping.addChild(new SimpleFieldToContractInputMapping(aStringField("persistenceId_string").build()));
context.setMapping(mapping);
context.setData(aBusinessData().withName("employee").build());
context.setContractInput(aContractInput().withName("address")
.multiple().in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX)).build());
context.setContractInput(aContractInput()
.withName("address")
.multiple()
.in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX))
.havingInput(aContractInput().withName("persistenceId_string").withType(ContractInputType.TEXT))
.build());
context.setLocalVariableName("addressVar");
context.setLocalListVariableName("addressList");
final AbstractBusinessObjectInitializer propertyInitializer = new NewBusinessObjectListInitializer(context);
final AbstractBusinessObjectInitializer propertyInitializer = new MultipleCompositionBusinessObjectQueryInitializer(
businessObject, context);
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()));
.in(aContractInput().withName("employee").withType(ContractInputType.COMPLEX)))
.build()));
assertThat(propertyInitializer.getInitialValue()).isEqualTo("def addressList = []" + System.lineSeparator()
+ "//Uncomment line below to append existing address" + System.lineSeparator()
+ "//addressList.addAll(employee.address)" + System.lineSeparator()
+ "//For each item collected in multiple input" + System.lineSeparator()
+ "employee.address.each{" + System.lineSeparator()
+ "//Add a new composed Address instance" + System.lineSeparator()
+ "//Add Address instance" + System.lineSeparator()
+ "addressList.add({ currentAddressInput ->" + System.lineSeparator()
+ "def addressVar = new org.test.Address()" + System.lineSeparator()
+ "def addressVar = employee.address.find { it.persistenceId.toString() == employee.address.persistenceId_string} ?: new org.test.Address()"
+ System.lineSeparator()
+ "addressVar.street = currentAddressInput.street" + System.lineSeparator()
+ "return addressVar" + System.lineSeparator()
+ "}(it))" + System.lineSeparator()
......@@ -69,7 +77,8 @@ public class NewBusinessObjectListInitializerTest {
}
@Test
public void should_initialize_new_object_property_for_multiple_composed_reference_withoutExistingOnPool() throws Exception {
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",
......@@ -90,7 +99,8 @@ public class NewBusinessObjectListInitializerTest {
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()));
.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()
......@@ -125,7 +135,8 @@ public class NewBusinessObjectListInitializerTest {
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()));
.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()
+ "addresses.each{" + System.lineSeparator()
......@@ -138,4 +149,5 @@ public class NewBusinessObjectListInitializerTest {
+ "}" + System.lineSeparator()
+ "return addressList");
}
}
......@@ -22,20 +22,28 @@ import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aCompositi
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aStringField;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.anAggregationField;
import static org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder.aBusinessData;
import static org.bonitasoft.studio.model.process.builders.PoolBuilder.aPool;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Optional;
import org.bonitasoft.engine.bdm.model.BusinessObject;
import org.bonitasoft.engine.bdm.model.field.RelationField;
import org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelFileStore;
import org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelRepositoryStore;
import org.bonitasoft.studio.contract.core.mapping.FieldToContractInputMapping;
import org.bonitasoft.studio.contract.core.mapping.FieldToContractInputMappingFactory;
import org.bonitasoft.studio.contract.core.mapping.operation.FieldToContractInputMappingBuilder;
import org.bonitasoft.studio.contract.core.mapping.operation.VariableNameResolver;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.BusinessObjectQueryInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.IPropertyInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.MultipleBusinessObjectQueryInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.MultipleAggregationBusinessObjectQueryInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.NewBusinessObjectInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.NewBusinessObjectListInitializer;
import org.bonitasoft.studio.model.process.BusinessObjectData;
import org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder;
import org.junit.Test;
public class BusinessObjectInitializerFactoryTest {
......@@ -47,7 +55,8 @@ 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(), false);
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping,
aBusinessData().withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(NewBusinessObjectInitializer.class);
}
......@@ -60,34 +69,53 @@ public class BusinessObjectInitializerFactoryTest {
aCompositionField.setCollection(true);
final FieldToContractInputMapping mapping = FieldToContractInputMappingBuilder
.aRelationMapping(aCompositionField)
.addChild(aSimpleMapping(aStringField("name").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping, aBusinessData().withName("employee").build(), false);
.addChild(aSimpleMapping(aStringField("name").build()))
.addChild(aSimpleMapping(aStringField("persistenceId_string").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping,
aBusinessData().withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(NewBusinessObjectListInitializer.class);
}
@Test
public void should_create_a_BusinessObjectQueryInitializer() throws Exception {
final BusinessObjectInitializerFactory factory = newFactory();
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(), false);
BusinessObjectInitializerFactory factory = newFactory();
BusinessObject businessObject = aBO("Employee").withField(anAggregationField("country", aBO("Country").build()))
.build();
BusinessObjectData businessObjectData = new BusinessObjectDataBuilder()
.withClassname(businessObject.getQualifiedName()).build();
BusinessObjectModelRepositoryStore<BusinessObjectModelFileStore> repositoryStore = mock(
BusinessObjectModelRepositoryStore.class);
when(repositoryStore.getBusinessObjectByQualifiedName(businessObjectData.getClassName()))
.thenReturn(Optional.of(businessObject));
List<FieldToContractInputMapping> mappings = new FieldToContractInputMappingFactory(repositoryStore)
.createMappingForBusinessObjectType(aPool().build(), businessObjectData);
IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mappings.get(0),
aBusinessData().withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(BusinessObjectQueryInitializer.class);
}
@Test
public void should_create_a_MultipleBusinessObjectQueryInitializer() throws Exception {
final BusinessObjectInitializerFactory factory = newFactory();
final RelationField anAggregationField = anAggregationField("countries", aBO("Country").build());
BusinessObjectInitializerFactory factory = newFactory();
RelationField anAggregationField = anAggregationField("countries", aBO("Country").build());
anAggregationField.setCollection(true);
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()
BusinessObject businessObject = aBO("Employee").withField(anAggregationField).build();
BusinessObjectData businessObjectData = new BusinessObjectDataBuilder()
.withClassname(businessObject.getQualifiedName()).build();
BusinessObjectModelRepositoryStore<BusinessObjectModelFileStore> repositoryStore = mock(
BusinessObjectModelRepositoryStore.class);
when(repositoryStore.getBusinessObjectByQualifiedName(businessObjectData.getClassName()))
.thenReturn(Optional.of(businessObject));
List<FieldToContractInputMapping> mappings = new FieldToContractInputMappingFactory(repositoryStore)
.createMappingForBusinessObjectType(aPool().build(), businessObjectData);
IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mappings.get(0), aBusinessData()
.withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(MultipleBusinessObjectQueryInitializer.class);
assertThat(propertyInitializer).isInstanceOf(MultipleAggregationBusinessObjectQueryInitializer.class);
}
private BusinessObjectInitializerFactory newFactory() {
......
......@@ -22,11 +22,17 @@ import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aCompositi
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.aStringField;
import static org.bonitasoft.studio.model.businessObject.FieldBuilder.anAggregationField;
import static org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder.aBusinessData;
import static org.bonitasoft.studio.model.process.builders.PoolBuilder.aPool;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Optional;
import org.bonitasoft.engine.bdm.model.BusinessObject;
import org.bonitasoft.engine.bdm.model.field.RelationField;
import org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelFileStore;
import org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelRepositoryStore;
import org.bonitasoft.studio.contract.core.mapping.FieldToContractInputMapping;
import org.bonitasoft.studio.contract.core.mapping.FieldToContractInputMappingFactory;
import org.bonitasoft.studio.contract.core.mapping.operation.VariableNameResolver;
......@@ -35,6 +41,8 @@ import org.bonitasoft.studio.contract.core.mapping.operation.initializer.Composi
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.IPropertyInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.MultipleAggregationReferencePropertyInitializer;
import org.bonitasoft.studio.contract.core.mapping.operation.initializer.MultipleCompositionReferencePropertyInitializer;
import org.bonitasoft.studio.model.process.BusinessObjectData;
import org.bonitasoft.studio.model.process.builders.BusinessObjectDataBuilder;
import org.junit.Test;
public class RelationPropertyInitializerFactoryTest {
......@@ -45,12 +53,13 @@ public class RelationPropertyInitializerFactoryTest {
final RelationField employeeField = aCompositionField("employee", aBO("Employee").build());
final RelationField addressField = aCompositionField("address", aBO("Address").build());
final FieldToContractInputMapping mapping =
aRelationMapping(employeeField)
.addChild(aRelationMapping(addressField))
.addChild(aSimpleMapping(aStringField("street").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping.getChildren().get(0), aBusinessData()
.withName("employee").build(), false);
final FieldToContractInputMapping mapping = aRelationMapping(employeeField)
.addChild(aRelationMapping(addressField))
.addChild(aSimpleMapping(aStringField("street").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping.getChildren().get(0),
aBusinessData()
.withName("employee").build(),
false);
assertThat(propertyInitializer).isInstanceOf(CompositionReferencePropertyInitializer.class);
}
......@@ -62,23 +71,32 @@ public class RelationPropertyInitializerFactoryTest {
final RelationField employeeField = aCompositionField("employee", aBO("Employee").build());
final RelationField addressField = aCompositionField("addresses", aBO("Address").build());
addressField.setCollection(true);
final FieldToContractInputMapping mapping =
aRelationMapping(employeeField)
.addChild(aRelationMapping(addressField))
.addChild(aSimpleMapping(aStringField("street").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping.getChildren().get(0), aBusinessData()
.withName("employee").build(), false);
final FieldToContractInputMapping mapping = aRelationMapping(employeeField)
.addChild(aRelationMapping(addressField))
.addChild(aSimpleMapping(aStringField("street").build())).build();
final IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping.getChildren().get(0),
aBusinessData()
.withName("employee").build(),
false);
assertThat(propertyInitializer).isInstanceOf(MultipleCompositionReferencePropertyInitializer.class);
}
@Test
public void should_create_a_AggregationReferencePropertyInitializer() throws Exception {
final RelationPropertyInitializerFactory factory = newFactory();
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()
RelationPropertyInitializerFactory factory = newFactory();
BusinessObject businessObject = aBO("Employee").withField(anAggregationField("country", aBO("Country").build()))
.build();
BusinessObjectData businessObjectData = new BusinessObjectDataBuilder()
.withClassname(businessObject.getQualifiedName()).build();
BusinessObjectModelRepositoryStore<BusinessObjectModelFileStore> repositoryStore = mock(
BusinessObjectModelRepositoryStore.class);
when(repositoryStore.getBusinessObjectByQualifiedName(businessObjectData.getClassName()))
.thenReturn(Optional.of(businessObject));
List<FieldToContractInputMapping> mappings = new FieldToContractInputMappingFactory(repositoryStore)
.createMappingForBusinessObjectType(aPool().build(), businessObjectData);
IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mappings.get(0), aBusinessData()
.withName("employee").build(), false);
assertThat(propertyInitializer).isInstanceOf(AggregationReferencePropertyInitializer.class);
......@@ -86,14 +104,22 @@ public class RelationPropertyInitializerFactoryTest {
@Test
public void should_create_a_MultipleAggregationReferencePropertyInitializer() throws Exception {
final RelationPropertyInitializerFactory factory = newFactory();
RelationPropertyInitializerFactory factory = newFactory();
final RelationField anAggregationField = anAggregationField("countries", aBO("Country").build());
RelationField anAggregationField = anAggregationField("countries", aBO("Country").build());
anAggregationField.setCollection(true);
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(), false);
BusinessObject businessObject = aBO("Employee").withField(anAggregationField).build();
BusinessObjectData businessObjectData = new BusinessObjectDataBuilder()
.withClassname(businessObject.getQualifiedName()).build();
BusinessObjectModelRepositoryStore<BusinessObjectModelFileStore> repositoryStore = mock(
BusinessObjectModelRepositoryStore.class);
when(repositoryStore.getBusinessObjectByQualifiedName(businessObjectData.getClassName()))
.thenReturn(Optional.of(businessObject));
List<FieldToContractInputMapping> mappings = new FieldToContractInputMappingFactory(repositoryStore)
.createMappingForBusinessObjectType(aPool().build(), businessObjectData);
FieldToContractInputMapping mapping = mappings.get(0);
IPropertyInitializer propertyInitializer = factory.newPropertyInitializer(mapping, businessObjectData,
false);
assertThat(propertyInitializer).isInstanceOf(MultipleAggregationReferencePropertyInitializer.class);
}
......
......@@ -259,8 +259,7 @@ public class ContractInputGenerationWizardTest {
when(preferenceStore.getString(ContractInputGenerationInfoDialogFactory.SHOW_GENERATION_SUCCESS_DIALOG))
.thenReturn("always");
when(operationBuilder.toOperation(any(BusinessObjectData.class), any(FieldToContractInputMapping.class),
any(IProgressMonitor.class))).thenReturn(
anOperation().build());
any(IProgressMonitor.class))).thenReturn(anOperation().build());
final ContractInputGenerationWizard wizard = new ContractInputGenerationWizard(task, editingDomain(),
repositoryAccessor, operationBuilder,
......
......@@ -19,7 +19,6 @@ import java.util.Collections;
import java.util.List;
import org.bonitasoft.engine.bdm.model.field.Field;
import org.bonitasoft.studio.contract.core.mapping.operation.BusinessObjectInstantiationException;
import org.bonitasoft.studio.contract.core.mapping.operation.MappingOperationScriptBuilder;
import org.bonitasoft.studio.contract.core.mapping.operation.PropertySetter;
import org.bonitasoft.studio.model.process.BusinessObjectData;
......@@ -91,7 +90,7 @@ public abstract class FieldToContractInputMapping {
return contractInput;
}
public MappingOperationScriptBuilder getScriptBuilder(final BusinessObjectData data) throws BusinessObjectInstantiationException {
public MappingOperationScriptBuilder getScriptBuilder(BusinessObjectData data) {
return new MappingOperationScriptBuilder(data, this);
}
......
......@@ -16,56 +16,88 @@ package org.bonitasoft.studio.contract.core.mapping;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.bonitasoft.engine.bdm.model.BusinessObject;
import org.bonitasoft.engine.bdm.model.field.Field;
import org.bonitasoft.engine.bdm.model.field.FieldType;
import org.bonitasoft.engine.bdm.model.field.RelationField;
import org.bonitasoft.engine.bdm.model.field.SimpleField;
import org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelFileStore;
import org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelRepositoryStore;
import org.bonitasoft.studio.model.process.BusinessObjectData;
import org.bonitasoft.studio.model.process.ContractContainer;
import org.bonitasoft.studio.model.process.Task;
/**
* @author aurelie
*/
public class FieldToContractInputMappingFactory {
private static final int MAX_DEPTH = 5;
public List<FieldToContractInputMapping> createMappingForBusinessObjectType(final BusinessObject businessObject) {
final List<FieldToContractInputMapping> mappings = new ArrayList<FieldToContractInputMapping>();
for (final Field field : businessObject.getFields()) {
mappings.add(createFieldToContractInputMapping(field, MAX_DEPTH));
public static final String PERSISTENCE_ID_STRING_FIELD_NAME = "persistenceId_string";
private BusinessObjectModelRepositoryStore<BusinessObjectModelFileStore> businessObjectRepositoryStore;
public FieldToContractInputMappingFactory(BusinessObjectModelRepositoryStore<BusinessObjectModelFileStore> store) {
this.businessObjectRepositoryStore = store;
}
public List<FieldToContractInputMapping> createMappingForBusinessObjectType(ContractContainer contractContainer,
BusinessObjectData data) {
List<FieldToContractInputMapping> mappings = new ArrayList<>();
toBusinessObject(data).ifPresent(businessObject -> businessObject.getFields().stream()
.map(field -> createFieldToContractInputMapping(contractContainer, field, MAX_DEPTH))
.forEach(mappings::add));
if (data.isMultiple() && contractContainer instanceof Task) {
mappings.add(createPersistenceIdContractInputMapping());
}
return mappings;
}
private FieldToContractInputMapping createFieldToContractInputMapping(final Field field, final int depth) {
private FieldToContractInputMapping createFieldToContractInputMapping(ContractContainer contractContainer,
final Field field, final int depth) {
if (field instanceof SimpleField) {
return new SimpleFieldToContractInputMapping((SimpleField) field);
} else if (field instanceof RelationField) {
return createRelationFieldToContractInputMapping((RelationField) field, depth);
return createRelationFieldToContractInputMapping(contractContainer, (RelationField) field, depth);
}
throw new IllegalStateException("Unkwown Field type");
}
private FieldToContractInputMapping createRelationFieldToContractInputMapping(final RelationField field, int depth) {
final RelationFieldToContractInputMapping relationFieldMapping = new RelationFieldToContractInputMapping(field);
if (RelationField.Type.AGGREGATION.equals(((RelationField) relationFieldMapping.getField()).getType())) {
addPersistenceIdMapping(relationFieldMapping);
private FieldToContractInputMapping createRelationFieldToContractInputMapping(ContractContainer contractContainer,
RelationField field, int depth) {
RelationFieldToContractInputMapping relationFieldMapping = new RelationFieldToContractInputMapping(field);
if (shouldAddPersistenceIdContractInputMapping(contractContainer, relationFieldMapping, field.isCollection())) {
relationFieldMapping.addChild(createPersistenceIdContractInputMapping());
}
if (depth > 0) {
depth--;
for (final Field child : field.getReference().getFields()) {
relationFieldMapping.addChild(createFieldToContractInputMapping(child, depth));
for (Field child : field.getReference().getFields()) {
relationFieldMapping.addChild(createFieldToContractInputMapping(contractContainer, child, depth));
}
}
return relationFieldMapping;
}
private void addPersistenceIdMapping(final RelationFieldToContractInputMapping relationFieldMapping) {
final SimpleField persistenceId = new SimpleField();
persistenceId.setName(Field.PERSISTENCE_ID);
persistenceId.setType(FieldType.LONG);
relationFieldMapping.addChild(new SimpleFieldToContractInputMapping(persistenceId));
public boolean shouldAddPersistenceIdContractInputMapping(ContractContainer contractContainer,
RelationFieldToContractInputMapping relationFieldMapping, boolean isCollection) {
return isAggregation(relationFieldMapping) || (contractContainer instanceof Task && isCollection);
}
private boolean isAggregation(RelationFieldToContractInputMapping relationFieldMapping) {
return Objects.equals(((RelationField) relationFieldMapping.getField()).getType(),
RelationField.Type.AGGREGATION);
}
private FieldToContractInputMapping createPersistenceIdContractInputMapping() {
SimpleField persistenceId = new SimpleField();
persistenceId.setName(PERSISTENCE_ID_STRING_FIELD_NAME);
persistenceId.setType(FieldType.STRING);
return new SimpleFieldToContractInputMapping(persistenceId);
}
private Optional<BusinessObject> toBusinessObject(BusinessObjectData data) {
return businessObjectRepositoryStore.getBusinessObjectByQualifiedName(data.getClassName());
}
}
......@@ -26,7 +26,6 @@ import org.bonitasoft.studio.contract.core.mapping.expression.FieldToContractInp
import org.bonitasoft.studio.contract.core.mapping.operation.BusinessObjectInstantiationException;
import org.bonitasoft.studio.contract.core.mapping.operation.FieldToContractInputMappingOperationBuilder;
import org.bonitasoft.studio.contract.core.mapping.operation.OperationCreationException;
import org.bonitasoft.studio.contract.i18n.Messages;
import org.bonitasoft.studio.model.expression.Expression;
import org.bonitasoft.studio.model.expression.Operation;
import org.bonitasoft.studio.model.process.BusinessObjectData;
......@@ -38,20 +37,21 @@ import org.eclipse.jdt.core.JavaModelException;
public class RootContractInputGenerator {
private final String rootContractInputName;
private String rootContractInputName;
private List<? extends FieldToContractInputMapping> children = new ArrayList<>();
private final List<Operation> mappingOperations = new ArrayList<>();
private List<Operation> mappingOperations = new ArrayList<>();
private ContractInput contractInput;
private final RepositoryAccessor repositoryAccessor;
private final FieldToContractInputMappingOperationBuilder operationBuilder;
private final FieldToContractInputMappingExpressionBuilder expressionBuilder;
private RepositoryAccessor repositoryAccessor;
private FieldToContractInputMappingOperationBuilder operationBuilder;
private FieldToContractInputMappingExpressionBuilder expressionBuilder;
private Expression initialValueExpression;
private boolean allAttributesGenerated = true;
public RootContractInputGenerator(final String rootContractInputName,
final List<? extends FieldToContractInputMapping> children,
final RepositoryAccessor repositoryAccessor, final FieldToContractInputMappingOperationBuilder operationBuilder,
final FieldToContractInputMappingExpressionBuilder expressionBuilder) {
public RootContractInputGenerator(String rootContractInputName,
List<? extends FieldToContractInputMapping> children,
RepositoryAccessor repositoryAccessor,
FieldToContractInputMappingOperationBuilder operationBuilder,
FieldToContractInputMappingExpressionBuilder expressionBuilder) {
this.rootContractInputName = rootContractInputName;
this.children = children;
this.repositoryAccessor = repositoryAccessor;
......@@ -59,26 +59,39 @@ public class RootContractInputGenerator {
this.expressionBuilder = expressionBuilder;
}
public void buildForInstanciation(final BusinessObjectData data, IProgressMonitor monitor)
/**
* To be used if the contract is on the pool -> initial value script will be generated
*/
public void buildForInstanciation(BusinessObjectData data, IProgressMonitor monitor)
throws OperationCreationException {
build(data, true, monitor);
initContractInput(data);
buildInitialValueExpression(data, monitor);
}
public void build(final BusinessObjectData data, IProgressMonitor monitor) throws OperationCreationException {
build(data, false, monitor);
/**
* To be used if the contract is on a task -> operations will be generated
*/
public void build(BusinessObjectData data, IProgressMonitor monitor) throws OperationCreationException {
initContractInput(data);
buildOperations(data, monitor);
}
private void build(final BusinessObjectData data, final boolean isOnPool, IProgressMonitor monitor)
private void buildInitialValueExpression(BusinessObjectData data, IProgressMonitor monitor)
throws OperationCreationException {
contractInput = ProcessFactory.eINSTANCE.createContractInput();
contractInput.setName(rootContractInputName);
contractInput.setType(ContractInputType.COMPLEX);
contractInput.setMultiple(data.isMultiple());
contractInput.setDataReference(data.getName());
monitor.beginTask("", children.size());
for (final FieldToContractInputMapping mapping : children) {
monitor.beginTask("Building initial value expression ...", IProgressMonitor.UNKNOWN);
try {
initialValueExpression = expressionBuilder.toExpression(data, createParentMapping(data, rootContractInputName),
true);
} catch (JavaModelException | BusinessObjectInstantiationException e) {
throw new OperationCreationException("Failed to create initial value expression", e);
}
monitor.done();
}
private void buildOperations(final BusinessObjectData data, IProgressMonitor monitor) throws OperationCreationException {
monitor.beginTask("Building operations ...", children.size());
for (FieldToContractInputMapping mapping : children) {
if (mapping.isGenerated()) {
mapping.toContractInput(contractInput);
if (!contractInput.isMultiple()) {
mappingOperations.add(operationBuilder.toOperation(data, mapping, monitor));
}
......@@ -90,13 +103,18 @@ public class RootContractInputGenerator {
mappingOperations
.add(operationBuilder.toOperation(data, createParentMapping(data, rootContractInputName), monitor));
}
monitor.beginTask(Messages.saving, IProgressMonitor.UNKNOWN);
try {
initialValueExpression = expressionBuilder.toExpression(data, createParentMapping(data, rootContractInputName),
isOnPool);
} catch (JavaModelException | BusinessObjectInstantiationException e) {
throw new OperationCreationException("Failed to create initial value expression", e);
}
monitor.done();
}
private void initContractInput(BusinessObjectData data) {
contractInput = ProcessFactory.eINSTANCE.createContractInput();
contractInput.setName(rootContractInputName);
contractInput.setType(ContractInputType.COMPLEX);
contractInput.setMultiple(data.isMultiple());
contractInput.setDataReference(data.getName());
children.stream()
.filter(FieldToContractInputMapping::isGenerated)
.forEach(mapping -> mapping.toContractInput(contractInput));
}
private RelationFieldToContractInputMapping createParentMapping(final BusinessObjectData data, final String inputName) {
......@@ -126,9 +144,6 @@ public class RootContractInputGenerator {
return initialValueExpression;
}
/**
* @return the allAttributesGenerated
*/
public boolean isAllAttributesGenerated() {
return allAttributesGenerated;
}
......
......@@ -47,46 +47,49 @@ import com.google.common.base.Joiner;
@Creatable
public class FieldToContractInputMappingExpressionBuilder {
private final ExpressionProviderService expressionEditorService;
private final RepositoryAccessor repositoryAccessor;
private ExpressionProviderService expressionEditorService;
private RepositoryAccessor repositoryAccessor;
@Inject
public FieldToContractInputMappingExpressionBuilder(final RepositoryAccessor repositoryAccessor, final ExpressionProviderService expressionEditorService) {
public FieldToContractInputMappingExpressionBuilder(RepositoryAccessor repositoryAccessor,
ExpressionProviderService expressionEditorService) {
this.repositoryAccessor = repositoryAccessor;
this.expressionEditorService = expressionEditorService;
}
public Expression toExpression(final BusinessObjectData data, final FieldToContractInputMapping mapping, final boolean isOnPool)
public Expression toExpression(BusinessObjectData data, FieldToContractInputMapping mapping, boolean isOnPool)
throws BusinessObjectInstantiationException, JavaModelException {
final ContractInput contractInput = mapping.getContractInput();
final MappingOperationScriptBuilder mappingOperationScriptBuilder = mapping.getScriptBuilder(data);
final String script = getScriptText(isOnPool, mappingOperationScriptBuilder);
final Expression scriptExpression = ExpressionHelper.createGroovyScriptExpression(script, mapping.getFieldType());
ContractInput contractInput = mapping.getContractInput();
MappingOperationScriptBuilder mappingOperationScriptBuilder = mapping.getScriptBuilder(data);
String script = getScriptText(isOnPool, mappingOperationScriptBuilder);
Expression scriptExpression = ExpressionHelper.createGroovyScriptExpression(script, mapping.getFieldType());
addScriptDependencies(mappingOperationScriptBuilder, mapping.getContractInput(), data, scriptExpression, isOnPool);
setGroovyScriptName(scriptExpression, data, contractInput, isOnPool);
return scriptExpression;
}
protected String getScriptText(final boolean isOnPool, final MappingOperationScriptBuilder mappingOperationScriptBuilder)
protected String getScriptText(boolean isOnPool, MappingOperationScriptBuilder mappingOperationScriptBuilder)
throws BusinessObjectInstantiationException {
if (isOnPool) {
return mappingOperationScriptBuilder.toInstanciationScript();
} else {
return mappingOperationScriptBuilder.toScript();
}
return mappingOperationScriptBuilder.toScript();
}
private void setGroovyScriptName(final Expression scriptExpression, final BusinessObjectData data, final ContractInput contractInput, final boolean isOnPool) {
private void setGroovyScriptName(final Expression scriptExpression, final BusinessObjectData data,
final ContractInput contractInput, final boolean isOnPool) {
if (isOnPool) {
final String dataName = data.getName();
final String nameToUpperCase = dataName.length() > 1 ? dataName.substring(0, 1).toUpperCase() + dataName.substring(1) : dataName.toUpperCase();
final String nameToUpperCase = dataName.length() > 1
? dataName.substring(0, 1).toUpperCase() + dataName.substring(1) : dataName.toUpperCase();
scriptExpression.setName("init" + nameToUpperCase + "()");
} else {
scriptExpression.setName(Joiner.on(".").join(toAncestorNameList().apply(contractInput)));
}
}
private void addScriptDependencies(final MappingOperationScriptBuilder scriptBuilder, final ContractInput contractInput, final BusinessObjectData data,
private void addScriptDependencies(final MappingOperationScriptBuilder scriptBuilder, final ContractInput contractInput,
final BusinessObjectData data,
final Expression groovyScriptExpression, final boolean isOnPool) throws JavaModelException {
groovyScriptExpression.getReferencedElements().add(
ExpressionHelper.createDependencyFromEObject(rootContractInput(contractInput)));
......@@ -97,14 +100,16 @@ public class FieldToContractInputMappingExpressionBuilder {
final GroovyCompilationUnit groovyCompilationUnit = groovyCompilationUnit(groovyScriptExpression);
final ComputeScriptDependenciesJob job = new ComputeScriptDependenciesJob(groovyCompilationUnit);
job.setNodes(availableDao());
job.getNodes().add(new ScriptVariable(data.getName(), data.getDataType().toString()));
job.setContext(ModelHelper.getParentPool(data));
groovyScriptExpression.getReferencedElements().addAll(job.findDependencies());
groovyCompilationUnit.delete(true, Repository.NULL_PROGRESS_MONITOR);
}
private List<ScriptVariable> availableDao() {
final List<ScriptVariable> scriptVariables = new ArrayList<ScriptVariable>();
final IExpressionProvider daoExpressionProvider = expressionEditorService.getExpressionProvider(ExpressionConstants.DAO_TYPE);
final List<ScriptVariable> scriptVariables = new ArrayList<>();
final IExpressionProvider daoExpressionProvider = expressionEditorService
.getExpressionProvider(ExpressionConstants.DAO_TYPE);
if (daoExpressionProvider != null) {
final List<Expression> expressions = newArrayList(daoExpressionProvider.getExpressions(null));
for (final Expression e : expressions) {
......@@ -114,8 +119,10 @@ public class FieldToContractInputMappingExpressionBuilder {
return scriptVariables;
}
protected GroovyCompilationUnit groovyCompilationUnit(final Expression groovyScriptExpression) throws JavaModelException {
return (GroovyCompilationUnit) new GroovyCompilationUnitFactory(repositoryAccessor).newCompilationUnit(groovyScriptExpression.getContent(),
protected GroovyCompilationUnit groovyCompilationUnit(final Expression groovyScriptExpression)
throws JavaModelException {
return (GroovyCompilationUnit) new GroovyCompilationUnitFactory(repositoryAccessor).newCompilationUnit(
groovyScriptExpression.getContent(),
Repository.NULL_PROGRESS_MONITOR);
}
......
......@@ -39,22 +39,21 @@ public class FieldToContractInputMappingOperationBuilder {
private final FieldToContractInputMappingExpressionBuilder expressionBuilder;
@Inject
public FieldToContractInputMappingOperationBuilder(final FieldToContractInputMappingExpressionBuilder expressionBuilder,
final ExpressionReturnTypeFilter expressionReturnTypeFilter) {
public FieldToContractInputMappingOperationBuilder(FieldToContractInputMappingExpressionBuilder expressionBuilder,
ExpressionReturnTypeFilter expressionReturnTypeFilter) {
this.expressionReturnTypeFilter = expressionReturnTypeFilter;
this.expressionBuilder = expressionBuilder;
}
public Operation toOperation(final BusinessObjectData data, final FieldToContractInputMapping mapping,
IProgressMonitor monitor)
public Operation toOperation(BusinessObjectData data, FieldToContractInputMapping mapping, IProgressMonitor monitor)
throws OperationCreationException {
monitor.setTaskName(String.format(Messages.creatingMappingOperation, mapping.getField().getName()));
final Operation operation = ExpressionFactory.eINSTANCE.createOperation();
Operation operation = ExpressionFactory.eINSTANCE.createOperation();
operation.setLeftOperand(ExpressionHelper.createVariableExpression(data));
operation.setOperator(operator(mapping, data));
try {
operation.setRightOperand(expressionBuilder.toExpression(data, mapping, false));
} catch (final BusinessObjectInstantiationException | JavaModelException e) {
} catch (BusinessObjectInstantiationException | JavaModelException e) {
throw new OperationCreationException("Failed to create right operand expression", e);
}
if (!typesMatches(operation)) {
......@@ -66,17 +65,23 @@ public class FieldToContractInputMappingOperationBuilder {
return operation;
}
private boolean typesMatches(final Operation operation) {
private boolean typesMatches(Operation operation) {
final String expectedType = operation.getOperator().getInputTypes().get(0);
final String returnType = operation.getRightOperand().getReturnType();
return expressionReturnTypeFilter.compatibleReturnTypes(expectedType, returnType);
}
private Operator operator(final FieldToContractInputMapping mapping, final BusinessObjectData data) {
final Operator operator = ExpressionFactory.eINSTANCE.createOperator();
operator.setType(ExpressionConstants.JAVA_METHOD_OPERATOR);
operator.getInputTypes().add(data.isMultiple() ? Collection.class.getName() : mapping.getFieldType());
operator.setExpression(data.isMultiple() ? "addAll" : mapping.getSetterName());
private Operator operator(FieldToContractInputMapping mapping, BusinessObjectData data) {
Operator operator = ExpressionFactory.eINSTANCE.createOperator();
if (data.isMultiple()) {
operator.setType(ExpressionConstants.ASSIGNMENT_OPERATOR);
operator.getInputTypes().add(Collection.class.getName());
operator.setExpression("addAll"); // required by the engine..
} else {
operator.setType(ExpressionConstants.JAVA_METHOD_OPERATOR);
operator.getInputTypes().add(mapping.getFieldType());
operator.setExpression(mapping.getSetterName());
}
return operator;
}
......
......@@ -49,7 +49,8 @@ public class MappingOperationScriptBuilder {
this.mapping = mapping;
final VariableNameResolver variableNameResolver = new VariableNameResolver();
businessObjectInitializerFactory = new BusinessObjectInitializerFactory(variableNameResolver);
propertyInitializerFactory = new PropertyInitializerFactory(new RelationPropertyInitializerFactory(variableNameResolver));
propertyInitializerFactory = new PropertyInitializerFactory(
new RelationPropertyInitializerFactory(variableNameResolver));
}
public String toInstanciationScript() throws BusinessObjectInstantiationException {
......@@ -62,7 +63,8 @@ public class MappingOperationScriptBuilder {
private String toScript(final boolean isOnPool) throws BusinessObjectInstantiationException {
mapping.getContractInput();
return format(buildPropertyInitializerTree(mapping, businessObjectInitializerFactory, data, isOnPool).getInitialValue());
return format(
buildPropertyInitializerTree(mapping, businessObjectInitializerFactory, data, isOnPool).getInitialValue());
}
private String format(final String initialValue) {
......@@ -75,23 +77,24 @@ public class MappingOperationScriptBuilder {
return document.get();
}
private IPropertyInitializer buildPropertyInitializerTree(final FieldToContractInputMapping mapping,
final InitializerFactory initializerFactory,
final BusinessObjectData data,
final boolean isOnPool) {
final Field field = mapping.getField();
private IPropertyInitializer buildPropertyInitializerTree(FieldToContractInputMapping mapping,
InitializerFactory initializerFactory,
BusinessObjectData data,
boolean isOnPool) {
Field field = mapping.getField();
if (field instanceof SimpleField) {
return propertyInitializerFactory.newPropertyInitializer(mapping, data, isOnPool);
}
if (field instanceof RelationField) {
final AbstractBusinessObjectInitializer scriptInitializer = (AbstractBusinessObjectInitializer) initializerFactory
AbstractBusinessObjectInitializer scriptInitializer = (AbstractBusinessObjectInitializer) initializerFactory
.newPropertyInitializer(
mapping,
data,
isOnPool);
for (final FieldToContractInputMapping child : mapping.getChildren()) {
for (FieldToContractInputMapping child : mapping.getChildren()) {
if (child.isGenerated()) {
scriptInitializer.addPropertyInitializer(buildPropertyInitializerTree(child, propertyInitializerFactory, data, isOnPool));
scriptInitializer.addPropertyInitializer(
buildPropertyInitializerTree(child, propertyInitializerFactory, data, isOnPool));
}
}
needsDataDependency = scriptInitializer instanceof NewBusinessObjectInitializer;
......
......@@ -21,10 +21,12 @@ import static com.google.common.collect.Sets.newHashSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.bonitasoft.engine.bdm.model.BusinessObject;
import org.bonitasoft.engine.bdm.model.field.Field;
import org.bonitasoft.studio.contract.core.mapping.FieldToContractInputMappingFactory;
import org.bonitasoft.studio.contract.core.mapping.operation.BusinessObjectInstantiationException;
import com.google.common.base.Function;
......@@ -32,7 +34,7 @@ import com.google.common.base.Predicate;
public abstract class AbstractBusinessObjectInitializer implements IPropertyInitializer {
protected final List<IPropertyInitializer> propertyInitializers = new ArrayList<IPropertyInitializer>();
protected final List<IPropertyInitializer> propertyInitializers = new ArrayList<>();
protected InitializerContext context;
private AbstractBusinessObjectInitializer parent;
......@@ -73,17 +75,22 @@ public abstract class AbstractBusinessObjectInitializer implements IPropertyInit
delcareVariable(scriptBuilder, localVariableName);
scriptBuilder.append(" = ");
constructor(scriptBuilder, businessObject);
scriptBuilder.append(System.lineSeparator());
for (final IPropertyInitializer propertyInitializer : propertyInitializers) {
initializeProperty(scriptBuilder, propertyInitializer, localVariableName);
}
initializeProperties(scriptBuilder, localVariableName);
returnVar(scriptBuilder, localVariableName);
return scriptBuilder.toString();
}
protected void initializeProperties(final StringBuilder scriptBuilder, final String localVariableName)
throws BusinessObjectInstantiationException {
for (IPropertyInitializer propertyInitializer : propertyInitializers) {
if (!Objects.equals(propertyInitializer.getPropertyName(),
FieldToContractInputMappingFactory.PERSISTENCE_ID_STRING_FIELD_NAME)) {
initializeProperty(scriptBuilder, propertyInitializer, localVariableName);
}
}
}
protected void addCommentBeforeConstructor(final StringBuilder scriptBuilder, final BusinessObject businessObject) {
// Can be subclasssed
}
......@@ -91,7 +98,8 @@ public abstract class AbstractBusinessObjectInitializer implements IPropertyInit
protected abstract boolean checkExistence();
protected void checkNotNullableFields(final BusinessObject businessObject) throws BusinessObjectInstantiationException {
final Set<String> uninitializedNonNullableFields = notNullableFieldNotInitialized(propertyInitializers, businessObject);
final Set<String> uninitializedNonNullableFields = notNullableFieldNotInitialized(propertyInitializers,
businessObject);
if (!uninitializedNonNullableFields.isEmpty()) {
throw new BusinessObjectInstantiationException(toArray(uninitializedNonNullableFields, String.class));
}
......@@ -108,7 +116,8 @@ public abstract class AbstractBusinessObjectInitializer implements IPropertyInit
scriptBuilder.append(System.lineSeparator());
}
private Set<String> notNullableFieldNotInitialized(final List<IPropertyInitializer> propertyInitializers, final BusinessObject bo) {
private Set<String> notNullableFieldNotInitialized(final List<IPropertyInitializer> propertyInitializers,
final BusinessObject bo) {
final Set<String> notNullableFields = newHashSet(transform(filter(bo.getFields(), notNullable()), toFieldName()));
final Set<String> initializedFields = newHashSet(transform(propertyInitializers, initializerToFieldName()));
notNullableFields.removeAll(initializedFields);
......
......@@ -24,6 +24,7 @@ import org.bonitasoft.engine.bdm.model.BusinessObject;
import org.bonitasoft.engine.bdm.model.field.Field;
import org.bonitasoft.engine.bdm.model.field.FieldType;
import org.bonitasoft.engine.bdm.model.field.SimpleField;
import org.bonitasoft.studio.contract.core.mapping.FieldToContractInputMappingFactory;
import org.bonitasoft.studio.contract.core.mapping.operation.BusinessObjectInstantiationException;
import org.bonitasoft.studio.model.process.ContractInput;
......@@ -34,7 +35,8 @@ public class BusinessObjectQueryInitializer extends AbstractBusinessObjectInitia
private final ContractInput persistenceIdInput;
private final BusinessObject multipleParentBusinessObject;
public BusinessObjectQueryInitializer(final BusinessObject multipleParentBusinessObject, final InitializerContext context) {
public BusinessObjectQueryInitializer(final BusinessObject multipleParentBusinessObject,
final InitializerContext context) {
super(context);
persistenceIdInput = persistenceIdInput(context.getContractInput());
this.multipleParentBusinessObject = multipleParentBusinessObject;
......@@ -50,7 +52,7 @@ public class BusinessObjectQueryInitializer extends AbstractBusinessObjectInitia
addCommentLine(
scriptBuilder,
String.format("Retrieve aggregated %s using its DAO and persistenceId",
BDMSimpleNameProvider.getSimpleBusinessObjectName(businessObject.getQualifiedName())));
BDMSimpleNameProvider.getSimpleBusinessObjectName(businessObject.getQualifiedName())));
}
@Override
......@@ -59,7 +61,8 @@ public class BusinessObjectQueryInitializer extends AbstractBusinessObjectInitia
}
@Override
protected void initializeProperty(final StringBuilder scriptBuilder, final IPropertyInitializer propertyInitializer, final String varName)
protected void initializeProperty(final StringBuilder scriptBuilder, final IPropertyInitializer propertyInitializer,
final String varName)
throws BusinessObjectInstantiationException {
if (!isPersistenceIdInitializer(propertyInitializer)) {
super.initializeProperty(scriptBuilder, propertyInitializer, varName);
......@@ -67,19 +70,32 @@ public class BusinessObjectQueryInitializer extends AbstractBusinessObjectInitia
}
private boolean isPersistenceIdInitializer(final IPropertyInitializer propertyInitializer) {
return propertyInitializer instanceof SimpleFieldPropertyInitializer && Objects.equals(propertyInitializer.getPropertyName(), Field.PERSISTENCE_ID);
return propertyInitializer instanceof SimpleFieldPropertyInitializer
&& Objects.equals(propertyInitializer.getPropertyName(), Field.PERSISTENCE_ID);
}
protected void daoQuery(final StringBuilder scriptBuilder, final BusinessObject bo) {
final SimpleField peristenceIdField = new SimpleField();
protected void daoQuery(StringBuilder scriptBuilder, BusinessObject bo) {
SimpleField peristenceIdField = new SimpleField();
peristenceIdField.setType(FieldType.LONG);
peristenceIdField.setName(Field.PERSISTENCE_ID);
final SimpleFieldPropertyInitializer persistenceIdInitializer = new SimpleFieldPropertyInitializer(multipleParentBusinessObject,
SimpleFieldPropertyInitializer persistenceIdInitializer = new SimpleFieldPropertyInitializer(
multipleParentBusinessObject,
peristenceIdField, persistenceIdInput);
scriptBuilder.append(daoName(bo));
scriptBuilder.append(".findByPersistenceId(");
scriptBuilder.append(persistenceIdInitializer.getInitialValue());
scriptBuilder.append(")");
validateQueryResult(scriptBuilder, persistenceIdInitializer);
}
private void validateQueryResult(StringBuilder scriptBuilder, SimpleFieldPropertyInitializer persistenceIdInitializer) {
String localVariableName = context.getLocalVariableName();
scriptBuilder.append(System.lineSeparator());
scriptBuilder.append(String.format("if(!%s) {\n", localVariableName));
scriptBuilder.append(String.format(
"throw new IllegalArgumentException(\"The aggregated reference of type `%s` with the persistence id \" + %s + \" has not been found.\")\n",
context.getField().getReference().getSimpleName(), persistenceIdInitializer.getInitialValue()));
scriptBuilder.append("}");
}