Search in sources :

Example 1 with EntityInfrastructureException

use of org.motechproject.mds.exception.entity.EntityInfrastructureException in project motech by motech.

the class EntityInfrastructureBuilderImpl method getServiceCode.

private byte[] getServiceCode(String serviceClassName, String interfaceClassName, String className, EntityDto entity, SchemaHolder schemaHolder) {
    try {
        CtClass superClass = classPool.getCtClass(TransactionalMotechDataService.class.getName());
        superClass.setGenericSignature(getGenericSignature(className));
        CtClass serviceInterface = classPool.getCtClass(interfaceClassName);
        CtClass serviceClass = createOrRetrieveClass(serviceClassName, superClass);
        // add the interface if its not already there
        if (!JavassistUtil.hasInterface(serviceClass, serviceInterface)) {
            serviceClass.addInterface(serviceInterface);
        }
        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, serviceClass, lookupType, schemaHolder);
                    methods.add(lookupBuilder.buildMethod());
                }
            }
        }
        // clear lookup methods before adding the new ones
        removeExistingMethods(serviceClass);
        for (CtMethod method : methods) {
            serviceClass.addMethod(method);
        }
        return serviceClass.toBytecode();
    } catch (NotFoundException | IOException | CannotCompileException e) {
        throw new EntityInfrastructureException(serviceClassName, e);
    }
}
Also used : EntityInfrastructureException(org.motechproject.mds.exception.entity.EntityInfrastructureException) 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) TransactionalMotechDataService(org.motechproject.mds.service.TransactionalMotechDataService)

Example 2 with EntityInfrastructureException

use of org.motechproject.mds.exception.entity.EntityInfrastructureException in project motech by motech.

the class EntityInfrastructureBuilderImpl method getRepositoryCode.

private byte[] getRepositoryCode(String repositoryClassName, String typeName, Integer fetchDepth) {
    try {
        CtClass superClass = classPool.getCtClass(MotechDataRepository.class.getName());
        superClass.setGenericSignature(getGenericSignature(typeName));
        CtClass subClass = createOrRetrieveClass(repositoryClassName, superClass);
        String repositoryName = ClassName.getSimpleName(repositoryClassName);
        removeDefaultConstructor(subClass);
        String constructorAsString;
        // the fetch depth parameter is optional
        if (fetchDepth == null) {
            constructorAsString = String.format("public %s(){super(%s.class);}", repositoryName, typeName);
        } else {
            constructorAsString = String.format("public %s(){super(%s.class, %d);}", repositoryName, typeName, fetchDepth);
        }
        CtConstructor constructor = CtNewConstructor.make(constructorAsString, subClass);
        subClass.addConstructor(constructor);
        return subClass.toBytecode();
    } catch (NotFoundException | CannotCompileException | IOException e) {
        throw new EntityInfrastructureException(repositoryClassName, e);
    }
}
Also used : CtClass(javassist.CtClass) EntityInfrastructureException(org.motechproject.mds.exception.entity.EntityInfrastructureException) NotFoundException(javassist.NotFoundException) MotechDataRepository(org.motechproject.mds.repository.MotechDataRepository) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) CtConstructor(javassist.CtConstructor)

Example 3 with EntityInfrastructureException

use of org.motechproject.mds.exception.entity.EntityInfrastructureException 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)

Aggregations

IOException (java.io.IOException)3 CannotCompileException (javassist.CannotCompileException)3 CtClass (javassist.CtClass)3 NotFoundException (javassist.NotFoundException)3 EntityInfrastructureException (org.motechproject.mds.exception.entity.EntityInfrastructureException)3 ArrayList (java.util.ArrayList)2 CtMethod (javassist.CtMethod)2 LookupDto (org.motechproject.mds.dto.LookupDto)2 CtConstructor (javassist.CtConstructor)1 MotechDataRepository (org.motechproject.mds.repository.MotechDataRepository)1 TransactionalMotechDataService (org.motechproject.mds.service.TransactionalMotechDataService)1 Bundle (org.osgi.framework.Bundle)1