Search in sources :

Example 26 with LookupFieldDto

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

the class LookupExecutor method getLookupArgs.

private List<Object> getLookupArgs(Map<String, ?> paramMap) {
    List<Object> args = new ArrayList<>();
    for (LookupFieldDto lookupField : lookup.getLookupFields()) {
        FieldDto field = fieldsByName.get(lookupField.getLookupFieldName());
        if (field == null) {
            throw new FieldNotFoundException(entityClass.getName(), lookupField.getName());
        }
        Object val = paramMap.get(lookupField.getLookupFieldName());
        String typeClass = getTypeClass(field);
        String genericType = getGenericTypeClass(field);
        Object arg;
        if (lookupField.getType() == LookupFieldType.RANGE) {
            arg = TypeHelper.toRange(val, typeClass);
        } else if (lookupField.getType() == LookupFieldType.SET) {
            arg = TypeHelper.toSet(val, typeClass, classLoader);
        } else {
            arg = TypeHelper.parse(val, lookupField.isUseGenericParam() ? genericType : typeClass, classLoader);
        }
        args.add(arg);
    }
    return args;
}
Also used : ArrayList(java.util.ArrayList) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 27 with LookupFieldDto

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

the class Entity method addLookup.

public void addLookup(Lookup lookup) {
    Lookup existing = getLookupByName(lookup.getLookupName());
    if (existing == null) {
        lookup.setEntity(this);
        getLookups().add(lookup);
    } else {
        LookupDto lookupDto = lookup.toDto();
        List<Field> lookupFields = new ArrayList<>();
        for (LookupFieldDto lookupField : lookupDto.getLookupFields()) {
            lookupFields.add(getField(lookupField.getId()));
        }
        existing.update(lookupDto, lookupFields);
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 28 with LookupFieldDto

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

the class Entity method updateIndexes.

public void updateIndexes(List<LookupDto> indexes) {
    // deletion
    Iterator<Lookup> it = getLookups().iterator();
    while (it.hasNext()) {
        Lookup lookup = it.next();
        boolean inNewList = false;
        for (LookupDto lookupDto : indexes) {
            if (Objects.equals(lookup.getId(), lookupDto.getId())) {
                inNewList = true;
                break;
            }
        }
        if (!inNewList) {
            it.remove();
        }
    }
    for (LookupDto lookupDto : indexes) {
        Lookup lookup = getLookupById(lookupDto.getId());
        List<Field> lookupFields = new ArrayList<>();
        List<String> lookupFieldsOrder = new ArrayList<>();
        for (LookupFieldDto lookupField : lookupDto.getLookupFields()) {
            Field field = getField(lookupField.getId());
            if (!lookupFields.contains(field)) {
                lookupFields.add(field);
            }
            String lookupFieldName = LookupName.buildLookupFieldName(field.getName(), lookupField.getRelatedName());
            lookupFieldsOrder.add(lookupFieldName);
            lookupField.setName(lookupFieldName);
        }
        lookupDto.setFieldsOrder(lookupFieldsOrder);
        if (lookup == null) {
            Lookup newLookup = new Lookup(lookupDto, lookupFields);
            addLookup(newLookup);
        } else {
            lookup.update(lookupDto, lookupFields);
        }
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 29 with LookupFieldDto

use of org.motechproject.mds.dto.LookupFieldDto 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 30 with LookupFieldDto

use of org.motechproject.mds.dto.LookupFieldDto 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)

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