Search in sources :

Example 41 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class EntityServiceImpl method addOrUpdateLookups.

private void addOrUpdateLookups(Entity entity, Collection<LookupDto> lookups) {
    for (LookupDto lookupDto : lookups) {
        Lookup lookup = entity.getLookupByName(lookupDto.getLookupName());
        List<String> fieldsOrder = new ArrayList<>();
        List<Field> lookupFields = new ArrayList<>();
        for (LookupFieldDto lookupField : lookupDto.getLookupFields()) {
            String fieldName = lookupField.getName();
            Field field;
            field = entity.getField(fieldName);
            fieldsOrder.add(LookupName.buildLookupFieldName(lookupField.getName(), lookupField.getRelatedName()));
            if (field == null) {
                LOGGER.error("No field {} in entity {}", fieldName, entity.getClassName());
            } else {
                if (!lookupFields.contains(field)) {
                    lookupFields.add(field);
                }
            }
        }
        lookupDto.setFieldsOrder(fieldsOrder);
        if (lookup == null) {
            Lookup newLookup = new Lookup(lookupDto, lookupFields);
            entity.addLookup(newLookup);
        } else {
            lookup.update(lookupDto, lookupFields);
        }
    }
}
Also used : Field(org.motechproject.mds.domain.Field) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) Lookup(org.motechproject.mds.domain.Lookup) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 42 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class MdsDataProviderTest method setUp.

@Before
public void setUp() {
    when(entityService.getEntityByClassName(Record.class.getName())).thenReturn(entity);
    when(entity.getId()).thenReturn(ENTITY_ID);
    FieldDto fieldDto = FieldTestHelper.fieldDto(1L, "field", String.class.getName(), "disp", null);
    when(entityService.getEntityFields(ENTITY_ID)).thenReturn(asList(fieldDto));
    LookupFieldDto lookupField = FieldTestHelper.lookupFieldDto(1L, "field");
    LookupDto singleLookup = new LookupDto("singleLookup", true, false, asList(lookupField), false);
    LookupDto multiLookup = new LookupDto("multiLookup", false, false, asList(lookupField), false);
    lookupField = FieldTestHelper.lookupFieldDto(2L, "related");
    lookupField.setRelatedName("stringVar");
    LookupDto relatedLookup = new LookupDto("relatedLookup", false, false, asList(lookupField), false);
    when(entityService.getLookupByName(ENTITY_ID, "singleLookup")).thenReturn(singleLookup);
    when(entityService.getLookupByName(ENTITY_ID, "multiLookup")).thenReturn(multiLookup);
    when(entityService.getLookupByName(ENTITY_ID, "relatedLookup")).thenReturn(relatedLookup);
    Map<String, FieldDto> mapping = new HashMap<>();
    mapping.put("field", fieldDto);
    when(entityService.getLookupFieldsMapping(ENTITY_ID, "singleLookup")).thenReturn(mapping);
    when(entityService.getLookupFieldsMapping(ENTITY_ID, "multiLookup")).thenReturn(mapping);
    mapping = new HashMap<>();
    mapping.put("related.stringVar", fieldDto);
    when(entityService.getLookupFieldsMapping(ENTITY_ID, "relatedLookup")).thenReturn(mapping);
    when(bundleContext.getServiceReference(LookupService.class.getName())).thenReturn(serviceReference);
    when(bundleContext.getService(serviceReference)).thenReturn(new LookupService());
    dataProvider = new MDSDataProvider(resourceLoader);
    dataProvider.setEntityService(entityService);
    dataProvider.setBundleContext(bundleContext);
    MotechClassPool.registerServiceInterface(Record.class.getName(), LookupService.class.getName());
}
Also used : HashMap(java.util.HashMap) LookupDto(org.motechproject.mds.dto.LookupDto) MDSDataProvider(org.motechproject.mds.tasks.MDSDataProvider) 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 43 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class SchemaComparatorTest method shouldCompareLookupsWithDifferentSize.

@Test
public void shouldCompareLookupsWithDifferentSize() {
    LookupDto lookupDto = new LookupDto();
    lookupDto.setReadOnly(true);
    entityLookups = asList(lookupDto);
    newLookups = asList(lookupDto, lookupDto);
    when(entityService.getEntityLookups(ENTITY_ID)).thenReturn(entityLookups);
    boolean lookupsDiffer = schemaComparator.lookupsDiffer(ENTITY_ID, newLookups);
    assertTrue(lookupsDiffer);
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) Test(org.junit.Test)

Example 44 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class SchemaComparatorTest method shouldCompareLookupsWithDifferentFields.

@Test
public void shouldCompareLookupsWithDifferentFields() {
    LookupDto lookupDto = new LookupDto("lookupName", true, true, new ArrayList<LookupFieldDto>(), true, "methodName", new ArrayList<String>());
    LookupFieldDto lookupFieldDto = new LookupFieldDto(1L, "name", LookupFieldType.RANGE, "customOperator", true, "relatedName");
    lookupDto.setLookupFields(asList(lookupFieldDto));
    LookupDto lookupDto2 = new LookupDto("lookupName", true, true, new ArrayList<LookupFieldDto>(), true, "methodName", new ArrayList<String>());
    LookupFieldDto lookupFieldDto2 = new LookupFieldDto(1L, "name", LookupFieldType.RANGE, "customOperator", true, "differentRelatedName");
    lookupDto2.setLookupFields(asList(lookupFieldDto2));
    entityLookups = asList(lookupDto);
    newLookups = asList(lookupDto2);
    when(entityService.getEntityLookups(ENTITY_ID)).thenReturn(entityLookups);
    boolean lookupsDiffer = schemaComparator.lookupsDiffer(ENTITY_ID, newLookups);
    assertTrue(lookupsDiffer);
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 45 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class SchemaComparatorTest method shouldCompareTheSameLookups.

@Test
public void shouldCompareTheSameLookups() {
    LookupDto lookupDto = new LookupDto("lookupName", true, true, new ArrayList<LookupFieldDto>(), true, "methodName", new ArrayList<String>());
    entityLookups = asList(lookupDto);
    newLookups = asList(lookupDto);
    when(entityService.getEntityLookups(ENTITY_ID)).thenReturn(entityLookups);
    boolean lookupsDiffer = schemaComparator.lookupsDiffer(ENTITY_ID, newLookups);
    assertFalse(lookupsDiffer);
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Aggregations

LookupDto (org.motechproject.mds.dto.LookupDto)54 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)32 FieldDto (org.motechproject.mds.dto.FieldDto)29 ArrayList (java.util.ArrayList)26 EntityDto (org.motechproject.mds.dto.EntityDto)24 Test (org.junit.Test)23 List (java.util.List)10 Method (java.lang.reflect.Method)9 Arrays.asList (java.util.Arrays.asList)8 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)7 HashMap (java.util.HashMap)6 Field (org.motechproject.mds.domain.Field)5 Lookup (org.motechproject.mds.domain.Lookup)5 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)5 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)5 Before (org.junit.Before)4 Matchers.anyString (org.mockito.Matchers.anyString)4 RestOptionsDto (org.motechproject.mds.dto.RestOptionsDto)4 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)4 MetadataDto (org.motechproject.mds.dto.MetadataDto)3