use of org.motechproject.mds.dto.LookupFieldDto in project motech by motech.
the class LookupProcessor method setUseGenericParam.
private void setUseGenericParam(EntityDto entity, Class<?> methodParameterType, LookupFieldDto lookupField) {
FieldDto field = findEntityFieldByName(entity.getClassName(), lookupField.getName());
TypeDto fieldType = field.getType();
EntityDto relatedEntity = null;
if (fieldType.isRelationship()) {
relatedEntity = findEntityByClassName(field.getMetadata(Constants.MetadataKeys.RELATED_CLASS).getValue());
field = findEntityFieldByName(field.getMetadata(Constants.MetadataKeys.RELATED_CLASS).getValue(), lookupField.getRelatedName());
}
if (fieldType.isCombobox()) {
ComboboxHolder holder = new ComboboxHolder(relatedEntity == null ? entity : relatedEntity, field);
boolean isCollection = holder.isCollection();
boolean isCollectionParam = Collection.class.isAssignableFrom(methodParameterType);
lookupField.setUseGenericParam(isCollection && !isCollectionParam);
}
}
use of org.motechproject.mds.dto.LookupFieldDto in project motech by motech.
the class EntityInfrastructureBuilderTest method shouldCreateCodeForClassWithLookups.
@Test
public void shouldCreateCodeForClassWithLookups() throws Exception {
MDSClassLoader mdsClassLoaderImpl = MDSClassLoader.getStandaloneInstance(getClass().getClassLoader());
EntityDto entity = new EntityDto(SampleWithLookups.class.getName());
entity.setMaxFetchDepth(-1);
LookupDto lookup = new LookupDto();
lookup.setLookupName("testLookup");
lookup.setMethodName("testLookupMethod");
FieldDto testField = fieldDto("TestField", "testDispName", String.class);
FieldDto testField2 = fieldDto("TestField2", "DisplayName with space", String.class);
FieldDto dateField = fieldDto("dateField", "Display names should not affect methods", DateTime.class);
FieldDto timeField = fieldDto("timeField", Time.class);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "TestField")).thenReturn(testField);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "TestField2")).thenReturn(testField2);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "dateField")).thenReturn(dateField);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "timeField")).thenReturn(timeField);
lookup.setFieldsOrder(asList("TestField", "TestField2", "dateField", "timeField"));
lookup.setSingleObjectReturn(true);
when(schemaHolder.getLookups(entity)).thenReturn(singletonList(lookup));
List<LookupFieldDto> lookupFields = new ArrayList<>();
lookupFields.add(new LookupFieldDto("TestField", LookupFieldType.VALUE));
lookupFields.add(new LookupFieldDto("TestField2", LookupFieldType.VALUE));
lookupFields.add(new LookupFieldDto("dateField", LookupFieldType.RANGE));
lookupFields.add(new LookupFieldDto("timeField", LookupFieldType.SET));
lookup.setLookupFields(lookupFields);
List<ClassData> data = entityInfrastructureBuilder.buildInfrastructure(entity, schemaHolder);
for (ClassData classData : data) {
mdsClassLoaderImpl.safeDefineClass(classData.getClassName(), classData.getBytecode());
}
verifySingleLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifySingleLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
// lookup with multiple return
lookup.setSingleObjectReturn(false);
mdsClassLoaderImpl = MDSClassLoader.getStandaloneInstance(getClass().getClassLoader());
data = entityInfrastructureBuilder.buildInfrastructure(entity, schemaHolder);
for (ClassData classData : data) {
mdsClassLoaderImpl.safeDefineClass(classData.getClassName(), classData.getBytecode());
}
verifyMultiReturnLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifyMultiReturnLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
verifyFetchDepthInRepository(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_REPOSITORY), -1);
}
use of org.motechproject.mds.dto.LookupFieldDto in project motech by motech.
the class EntityServiceContextIT method shouldAddLookupWithRelatedField.
@Test
public void shouldAddLookupWithRelatedField() {
EntityDto entityWithLookup = new EntityDto();
entityWithLookup.setName("entityWithLookup");
entityWithLookup = entityService.createEntity(entityWithLookup);
EntityDto relatedEntity = new EntityDto();
relatedEntity.setName("relatedEntity");
relatedEntity = entityService.createEntity(relatedEntity);
FieldDto stringField = FieldTestHelper.fieldDto("stringField", String.class);
FieldDto nameField = FieldTestHelper.fieldDto("nameField", String.class);
FieldDto lengthField = FieldTestHelper.fieldDto("lengthField", Long.class);
FieldDto relationField = FieldTestHelper.fieldDto("relatedField", TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass());
relationField.addMetadata(new MetadataDto(RELATED_CLASS, relatedEntity.getClassName()));
entityService.addFields(entityWithLookup, asList(stringField, relationField));
entityService.addFields(relatedEntity, asList(nameField, lengthField));
List<LookupFieldDto> lookupFieldDtos = lookupFieldsFromNames("relatedField.nameField", "relatedField.lengthField", "stringField");
LookupDto lookup = new LookupDto("lookup", false, false, lookupFieldDtos, true);
entityService.addLookups(entityWithLookup.getId(), lookup);
LookupDto lookupFromDb = entityService.getLookupByName(entityWithLookup.getId(), "lookup");
assertNotNull(lookupFromDb);
assertEquals(asList("relatedField.nameField", "relatedField.lengthField", "stringField"), lookupFromDb.getFieldsOrder());
assertEquals(asList("relatedField", "relatedField", "stringField"), extract(lookupFromDb.getLookupFields(), on(LookupFieldDto.class).getName()));
}
use of org.motechproject.mds.dto.LookupFieldDto in project motech by motech.
the class MdsLookupServiceTest method setUp.
@Before
public void setUp() {
MotechClassPool.registerServiceInterface(Record.class.getName(), TestDataService.class.getName());
EntityDto entity = new EntityDto(ENTITY_ID, ENTITY_CLASS_NAME);
FieldDto strField = FieldTestHelper.fieldDto(STR_FIELD_ID, STR_PARAM, String.class.getName(), "strDisp", null);
FieldDto intField = FieldTestHelper.fieldDto(INT_FIELD_ID, INT_PARAM, Integer.class.getName(), "strDisp", null);
LookupFieldDto strLookupField = new LookupFieldDto(STR_FIELD_ID, STR_PARAM, LookupFieldType.VALUE);
LookupFieldDto intLookupField = new LookupFieldDto(INT_FIELD_ID, INT_PARAM, LookupFieldType.VALUE);
LookupDto firstLookup = new LookupDto(FIRST_LOOKUP_NAME, true, false, asList(strLookupField, intLookupField), false);
LookupDto secondLookup = new LookupDto(SECOND_LOOKUP_NAME, false, false, asList(strLookupField, intLookupField), false);
when(entityService.getEntityByClassName(ENTITY_CLASS_NAME)).thenReturn(entity);
when(entityService.getEntityFields(ENTITY_ID)).thenReturn(asList(intField, strField));
when(entityService.getLookupByName(ENTITY_ID, FIRST_LOOKUP_NAME)).thenReturn(firstLookup);
when(entityService.getLookupByName(ENTITY_ID, SECOND_LOOKUP_NAME)).thenReturn(secondLookup);
Map<String, FieldDto> mapping = new HashMap<>();
mapping.put(STR_PARAM, strField);
mapping.put(INT_PARAM, intField);
when(entityService.getLookupFieldsMapping(ENTITY_ID, FIRST_LOOKUP_NAME)).thenReturn(mapping);
when(entityService.getLookupFieldsMapping(ENTITY_ID, SECOND_LOOKUP_NAME)).thenReturn(mapping);
when(bundleContext.getServiceReference(TestDataService.class.getName())).thenReturn(serviceReference);
when(bundleContext.getService(serviceReference)).thenReturn(dataService);
}
use of org.motechproject.mds.dto.LookupFieldDto in project motech by motech.
the class LookupExecutorTest method setUp.
@Before
public void setUp() {
LookupDto lookupDto = new LookupDto(LOOKUP_NAME, false, false, asList(new LookupFieldDto(1L, STR_FIELD_NAME, LookupFieldType.VALUE), new LookupFieldDto(2L, INT_FIELD_NAME, LookupFieldType.VALUE), new LookupFieldDto(3L, TEXTAREA_FIELD_NAME, LookupFieldType.VALUE)), false, LOOKUP_METHOD_NAME_1, asList(STR_FIELD_NAME, INT_FIELD_NAME, TEXTAREA_FIELD_NAME));
List<FieldDto> fields = Arrays.asList(FieldTestHelper.fieldDto(1L, STR_FIELD_NAME, String.class.getName(), "strFieldDisp", null), FieldTestHelper.fieldDto(2L, INT_FIELD_NAME, Integer.class.getName(), "intFieldDisp", null), FieldTestHelper.fieldDto(3L, TEXTAREA_FIELD_NAME, "mds.field.textArea", "textAreaFieldDisp", null));
lookupExecutor1 = new LookupExecutor(dataService, lookupDto, DtoHelper.asFieldMapByName(fields));
lookupDto = new LookupDto(LOOKUP_NAME, false, false, asList(new LookupFieldDto(1l, STR_FIELD_NAME, LookupFieldType.VALUE, null, false, null), new LookupFieldDto(3l, RELATED_FIELD_NAME, LookupFieldType.VALUE, null, false, "id"), new LookupFieldDto(2l, INT_FIELD_NAME, LookupFieldType.VALUE, null, false, null), new LookupFieldDto(4l, RELATED_FIELD_NAME, LookupFieldType.VALUE, null, false, "name")), false, LOOKUP_METHOD_NAME_2, asList(STR_FIELD_NAME, LookupName.buildLookupFieldName(RELATED_FIELD_NAME, ID), INT_FIELD_NAME, LookupName.buildLookupFieldName(RELATED_FIELD_NAME, NAME)));
lookupExecutor2 = new LookupExecutor(dataService, lookupDto, getFieldMapping());
}
Aggregations