Search in sources :

Example 26 with LookupDto

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

the class InstanceServiceImpl method countRecordsByLookup.

@Override
public long countRecordsByLookup(Long entityId, String lookupName, Map<String, Object> lookupMap) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    LookupDto lookup = getLookupByName(entityId, lookupName);
    Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);
    MotechDataService service = getServiceForEntity(entity);
    try {
        LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);
        return lookupExecutor.executeCount(lookupMap);
    } catch (RuntimeException e) {
        throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
    }
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) MotechDataService(org.motechproject.mds.service.MotechDataService) LookupExecutionException(org.motechproject.mds.exception.lookup.LookupExecutionException) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 27 with LookupDto

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

the class InstanceServiceImpl method getEntityRecordsFromLookup.

@Override
public List<BasicEntityRecord> getEntityRecordsFromLookup(Long entityId, String lookupName, Map<String, Object> lookupMap, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    LookupDto lookup = getLookupByName(entityId, lookupName);
    List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
    Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);
    MotechDataService service = getServiceForEntity(entity);
    try {
        LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);
        Object result = lookupExecutor.execute(lookupMap, queryParams);
        if (lookup.isSingleObjectReturn()) {
            BasicEntityRecord record = instanceToBasicRecord(result, entity, fields, service, EntityType.STANDARD, getRelatedEntitiesFields(fields));
            return (record == null) ? new ArrayList<BasicEntityRecord>() : Collections.singletonList(record);
        } else {
            List instances = (List) result;
            return instancesToBasicRecords(instances, entity, fields, service, EntityType.STANDARD);
        }
    } catch (LookupExecutorException e) {
        if (e.getMessageKey() != null) {
            throw new LookupExecutionException(e, e.getMessageKey());
        } else {
            throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
        }
    } catch (RuntimeException e) {
        throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) MotechDataService(org.motechproject.mds.service.MotechDataService) EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) List(java.util.List) LookupExecutionException(org.motechproject.mds.exception.lookup.LookupExecutionException) FieldDto(org.motechproject.mds.dto.FieldDto) LookupExecutorException(org.motechproject.mds.exception.lookup.LookupExecutorException)

Example 28 with LookupDto

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

the class MdsDdeBundleIT method shouldLoadLookupsFromFile.

@Test
public void shouldLoadLookupsFromFile() {
    Entry entryOne = new Entry();
    entryOne.setValue("someValue");
    entryDataService.create(entryOne);
    Entry entryTwo = new Entry();
    entryTwo.setValue("someValueTwo");
    entryDataService.create(entryTwo);
    EntityDto entity = entityService.getEntityByClassName("org.motechproject.mds.test.domain.editablelookups.Entry");
    List<LookupDto> lookups = entityService.getEntityLookups(entity.getId());
    assertEquals(1, lookups.size());
    assertEquals("Find by Value", lookups.get(0).getLookupName());
    Map<String, String> params = new HashMap<>();
    params.put("value", entryOne.getValue());
    List<Entry> lookupResult = lookupService.findMany(entity.getClassName(), lookups.get(0).getLookupName(), params);
    assertEquals(1, lookupResult.size());
    assertEquals(entryOne.getValue(), lookupResult.get(0).getValue());
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Entry(org.motechproject.mds.test.domain.editablelookups.Entry) HashMap(java.util.HashMap) LookupDto(org.motechproject.mds.dto.LookupDto) Test(org.junit.Test)

Example 29 with LookupDto

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

the class SchemaComparator method lookupsDiffer.

/**
 * Returns true if lookups are different, false otherwise.
 * It compares only read-only lookups, that created
 * in the code by developer.
 *
 * @param entityId the id of entity
 * @param newLookups the newLookups defined in the code
 * @return true if lookups are different, false otherwise.
 */
public boolean lookupsDiffer(Long entityId, List<LookupDto> newLookups) {
    List<LookupDto> existingLookups = entityService.getEntityLookups(entityId);
    Map<String, LookupDto> entityLookupsMappings = new HashMap<>();
    for (LookupDto entityLookup : existingLookups) {
        if (entityLookup.isReadOnly()) {
            entityLookupsMappings.put(entityLookup.getLookupName(), entityLookup);
        }
    }
    if (entityLookupsMappings.size() != newLookups.size()) {
        return true;
    }
    for (LookupDto lookup : newLookups) {
        if (!lookupEquals(lookup, entityLookupsMappings.get(lookup.getLookupName()))) {
            return true;
        }
    }
    return false;
}
Also used : HashMap(java.util.HashMap) LookupDto(org.motechproject.mds.dto.LookupDto)

Example 30 with LookupDto

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

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