Search in sources :

Example 36 with LookupDto

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

the class EditableLookupsLoader method addEditableEntityLookups.

private void addEditableEntityLookups(MDSProcessorOutput output, EntityLookups entityLookups) {
    String entityClassName = entityLookups.getEntityClassName();
    List<LookupDto> lookupsToAdd = new ArrayList<>();
    for (LookupDto lookup : entityLookups.getLookups()) {
        if (!jsonLookupService.exists(entityClassName, lookup.getLookupName())) {
            lookupsToAdd.add(lookup);
            JsonLookupDto jsonLookup = new JsonLookupDto(entityClassName, lookup.getLookupName());
            jsonLookupService.createJsonLookup(jsonLookup);
            LOGGER.debug(LOOKUP_ADDED, lookup.getLookupName(), entityClassName);
        }
    }
    List<LookupDto> lookups = getLookups(output, entityClassName);
    if (lookupsToAdd.size() > 0) {
        if (lookups == null) {
            lookups = new ArrayList<>();
            output.getLookupProcessorOutputs().put(entityClassName, lookups);
        }
        lookups.addAll(lookupsToAdd);
    }
}
Also used : JsonLookupDto(org.motechproject.mds.dto.JsonLookupDto) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) JsonLookupDto(org.motechproject.mds.dto.JsonLookupDto)

Example 37 with LookupDto

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

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

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

the class EntityServiceImpl method removeLookup.

private void removeLookup(Entity entity, Collection<LookupDto> lookups) {
    Iterator<Lookup> iterator = entity.getLookups().iterator();
    while (iterator.hasNext()) {
        Lookup lookup = iterator.next();
        // don't remove user defined lookups
        if (!lookup.isReadOnly()) {
            continue;
        }
        boolean found = false;
        for (LookupDto lookupDto : lookups) {
            if (lookup.getLookupName().equalsIgnoreCase(lookupDto.getLookupName())) {
                found = true;
                break;
            }
        }
        if (!found) {
            iterator.remove();
        }
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) Lookup(org.motechproject.mds.domain.Lookup)

Example 40 with LookupDto

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

the class EntityServiceImpl method addNonPersistentFieldsData.

private List<FieldDto> addNonPersistentFieldsData(List<FieldDto> fieldDtos, Entity entity) {
    List<LookupDto> lookupDtos = new ArrayList<>();
    for (FieldDto fieldDto : fieldDtos) {
        List<LookupDto> fieldLookups = fieldDto.getLookups();
        if (fieldLookups != null) {
            lookupDtos.addAll(fieldLookups);
        }
    }
    addLookupsReferences(lookupDtos, entity.getClassName());
    return fieldDtos;
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto)

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