Search in sources :

Example 51 with LookupDto

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

the class LookupProcessorTest method shouldProcessMethodWithRestExposedAnnotation.

@Test
public void shouldProcessMethodWithRestExposedAnnotation() throws Exception {
    when(paranamer.lookupParameterNames(getTestMethodExposedViaRest())).thenReturn(argNames);
    EntityProcessorOutput eop = mockEntityProcessorOutput(new EntityDto(TestClass.class.getName()), Arrays.asList(new FieldDto("aaa", "bbb", TypeDto.STRING)));
    lookupProcessor.setEntityProcessingResult(Arrays.asList(eop));
    Method method = getTestMethodExposedViaRest();
    LookupDto dto = new LookupDto("Test Method Exposed Via Rest", true, true, lookupFieldDtos(argNames), true, "testMethodExposedViaRest", asList(argNames), true);
    lookupProcessor.process(method);
    Map<String, List<LookupDto>> elements = lookupProcessor.getProcessingResult();
    assertTrue(elements.containsKey(TEST_CLASS_NAME));
    List<LookupDto> list = elements.get(TEST_CLASS_NAME);
    assertEquals(1, list.size());
    assertEquals(dto, list.get(0));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Method(java.lang.reflect.Method) FieldTestHelper.lookupFieldDto(org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 52 with LookupDto

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

the class MdsRestFacadeImpl method readLookups.

private void readLookups(EntityInfo entity) {
    for (LookupDto lookup : entity.getLookups()) {
        Map<String, FieldDto> fieldMap = getLookupFieldsMapping(entity, lookup);
        String lookuMethodpName = lookup.getMethodName();
        if (lookup.isExposedViaRest()) {
            // we create executors for exposed lookups
            LookupExecutor executor = new LookupExecutor(dataService, lookup, fieldMap);
            lookupExecutors.put(lookuMethodpName, executor);
        } else {
            // we keep a list of forbidden lookups in order to print the appropriate error
            forbiddenLookupMethodNames.add(lookuMethodpName);
        }
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 53 with LookupDto

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

the class EntityInfrastructureBuilderImpl method getInterfaceCode.

private byte[] getInterfaceCode(String interfaceClassName, String className, EntityDto entity, SchemaHolder schemaHolder) {
    try {
        // the interface can come from the developer for DDE, but it doesn't have to
        // in which case it will be generated from scratch
        CtClass superInterface = null;
        if (null != entity && MotechClassPool.isServiceInterfaceRegistered(className)) {
            String ddeInterfaceName = MotechClassPool.getInterfaceName(className);
            Bundle declaringBundle = MdsBundleHelper.searchForBundle(bundleContext, entity);
            if (declaringBundle == null) {
                LOGGER.error("Unable to find bundle declaring the DDE interface for {}", className);
            } else {
                superInterface = JavassistUtil.loadClass(declaringBundle, ddeInterfaceName, classPool);
            }
        }
        // standard super interface - MotechDataService, for EUDE or DDE without an interface
        if (superInterface == null) {
            superInterface = classPool.getCtClass(MotechDataService.class.getName());
            superInterface.setGenericSignature(getGenericSignature(className));
        }
        CtClass interfaceClass = createOrRetrieveInterface(interfaceClassName, superInterface);
        List<CtMethod> methods = new ArrayList<>();
        // a count method for the lookup
        if (null != entity) {
            List<LookupDto> lookups = schemaHolder.getLookups(entity);
            for (LookupDto lookup : lookups) {
                for (LookupType lookupType : LookupType.values()) {
                    LookupBuilder lookupBuilder = new LookupBuilder(entity, lookup, interfaceClass, lookupType, schemaHolder);
                    methods.add(lookupBuilder.buildSignature());
                }
            }
        }
        // clear lookup methods before adding the new ones
        removeExistingMethods(interfaceClass);
        for (CtMethod method : methods) {
            interfaceClass.addMethod(method);
        }
        return interfaceClass.toBytecode();
    } catch (NotFoundException | IOException | CannotCompileException e) {
        throw new EntityInfrastructureException(interfaceClassName, e);
    }
}
Also used : EntityInfrastructureException(org.motechproject.mds.exception.entity.EntityInfrastructureException) Bundle(org.osgi.framework.Bundle) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) NotFoundException(javassist.NotFoundException) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod)

Example 54 with LookupDto

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

the class MdsRestBundleIT method prepareEntity.

private void prepareEntity() throws IOException {
    EntityDto entityDto = new EntityDto(ENTITY_NAME);
    entityDto = entityService.createEntity(entityDto);
    FieldDto strField = new FieldDto(null, entityDto.getId(), TypeDto.STRING, new FieldBasicDto("strFieldDisp", "strField", true, false), false, null);
    FieldDto intField = new FieldDto(null, entityDto.getId(), TypeDto.INTEGER, new FieldBasicDto("intFieldDisp", "intField"), false, null);
    entityService.addFields(entityDto, asList(strField, intField));
    RestOptionsDto restOptions = new RestOptionsDto(true, true, true, true, false);
    restOptions.setFieldNames(prepareAllRestFieldNames(entityService.getEntityFields(entityDto.getId())));
    entityService.updateRestOptions(entityDto.getId(), restOptions);
    // a set based lookup for our convenience
    LookupFieldDto intSetLookupField = new LookupFieldDto(null, "intField", LookupFieldType.SET);
    LookupDto setLookup = new LookupDto("byIntSet", false, true, asList(intSetLookupField), false);
    // list return REST lookup
    LookupFieldDto intLookupField = new LookupFieldDto(null, "intField", LookupFieldType.VALUE);
    LookupDto listLookup = new LookupDto("byInt", false, true, asList(intLookupField), false);
    // single return REST lookup
    LookupFieldDto strLookupField = new LookupFieldDto(null, "strField", LookupFieldType.VALUE);
    LookupDto singleLookup = new LookupDto("byStr", true, true, asList(strLookupField), false);
    entityService.addLookups(entityDto.getId(), asList(setLookup, listLookup, singleLookup));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) LookupDto(org.motechproject.mds.dto.LookupDto) RestOptionsDto(org.motechproject.mds.dto.RestOptionsDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

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