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);
}
}
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);
}
}
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);
}
}
Aggregations