Search in sources :

Example 21 with LookupFieldDto

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);
    }
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) TypeDto(org.motechproject.mds.dto.TypeDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 22 with LookupFieldDto

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);
}
Also used : MDSClassLoader(org.motechproject.mds.util.MDSClassLoader) EntityDto(org.motechproject.mds.dto.EntityDto) ClassData(org.motechproject.mds.domain.ClassData) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) SampleWithLookups(org.motechproject.mds.builder.SampleWithLookups) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with LookupFieldDto

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()));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) MetadataDto(org.motechproject.mds.dto.MetadataDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 24 with LookupFieldDto

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);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) HashMap(java.util.HashMap) LookupDto(org.motechproject.mds.dto.LookupDto) Record(org.motechproject.mds.testutil.records.Record) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Before(org.junit.Before)

Example 25 with LookupFieldDto

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());
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Before(org.junit.Before)

Aggregations

LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)36 LookupDto (org.motechproject.mds.dto.LookupDto)25 FieldDto (org.motechproject.mds.dto.FieldDto)24 EntityDto (org.motechproject.mds.dto.EntityDto)18 ArrayList (java.util.ArrayList)16 Test (org.junit.Test)13 Method (java.lang.reflect.Method)5 TypeDto (org.motechproject.mds.dto.TypeDto)5 Arrays.asList (java.util.Arrays.asList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 RelationshipHolder (org.motechproject.mds.domain.RelationshipHolder)4 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)4 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)4 Before (org.junit.Before)3 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)3 LinkedList (java.util.LinkedList)2 Field (org.motechproject.mds.domain.Field)2 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)2 LookupFieldType (org.motechproject.mds.dto.LookupFieldType)2