Search in sources :

Example 6 with ClassData

use of org.motechproject.mds.domain.ClassData in project motech by motech.

the class MDSConstructorImpl method buildClasses.

private Map<String, ClassData> buildClasses(List<EntityDto> entities, SchemaHolder schemaHolder) {
    Map<String, ClassData> classDataMap = new LinkedHashMap<>();
    // We build classes for all entities
    for (EntityDto entity : entities) {
        List<FieldDto> fields = schemaHolder.getFields(entity);
        ClassData classData = buildClass(entity, fields);
        ClassData historyClassData = null;
        if (entity.isRecordHistory()) {
            historyClassData = entityBuilder.buildHistory(entity, fields);
        }
        ClassData trashClassData = entityBuilder.buildTrash(entity, fields);
        String className = entity.getClassName();
        classDataMap.put(className, classData);
        if (historyClassData != null) {
            classDataMap.put(ClassName.getHistoryClassName(className), historyClassData);
        }
        classDataMap.put(ClassName.getTrashClassName(className), trashClassData);
    }
    return classDataMap;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ClassData(org.motechproject.mds.domain.ClassData) LinkedHashMap(java.util.LinkedHashMap) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 7 with ClassData

use of org.motechproject.mds.domain.ClassData in project motech by motech.

the class MDSConstructorImpl method buildInfrastructure.

private void buildInfrastructure(EntityDto entity, SchemaHolder schemaHolder) {
    String className = entity.getClassName();
    List<ClassData> infrastructure = infrastructureBuilder.buildInfrastructure(entity, schemaHolder);
    for (ClassData classData : infrastructure) {
        // coming from the UI
        if (classData.isInterfaceClass() && MotechClassPool.isServiceInterfaceRegistered(className)) {
            MotechClassPool.registerEnhancedClassData(classData);
        }
    }
}
Also used : ClassData(org.motechproject.mds.domain.ClassData)

Example 8 with ClassData

use of org.motechproject.mds.domain.ClassData in project motech by motech.

the class MDSConstructorImpl method buildEnum.

private void buildEnum(JavassistLoader loader, MdsJDOEnhancer enhancer, ComboboxHolder holder) {
    ClassData data = enumBuilder.build(holder);
    ByteArrayClassPath classPath = new ByteArrayClassPath(data.getClassName(), data.getBytecode());
    MotechClassPool.getDefault().appendClassPath(classPath);
    MotechClassPool.registerEnhancedClassData(data);
    // register with the classloader so that we avoid issues with the persistence manager
    MDSClassLoader.getInstance().safeDefineClass(data.getClassName(), data.getBytecode());
    addClassData(loader, enhancer, data);
}
Also used : ByteArrayClassPath(javassist.ByteArrayClassPath) ClassData(org.motechproject.mds.domain.ClassData)

Example 9 with ClassData

use of org.motechproject.mds.domain.ClassData in project motech by motech.

the class MDSConstructorImpl method buildClass.

private ClassData buildClass(EntityDto entity, List<FieldDto> fields) {
    ClassData classData;
    if (entity.isDDE()) {
        // for DDE we load the class coming from the bundle
        Bundle declaringBundle = MdsBundleHelper.searchForBundle(bundleContext, entity);
        if (declaringBundle == null) {
            throw new EntityCreationException("Declaring bundle unavailable for entity " + entity.getClassName());
        }
        classData = entityBuilder.buildDDE(entity, fields, declaringBundle);
    } else {
        classData = entityBuilder.build(entity, fields);
    }
    return classData;
}
Also used : ClassData(org.motechproject.mds.domain.ClassData) Bundle(org.osgi.framework.Bundle) EntityCreationException(org.motechproject.mds.exception.entity.EntityCreationException)

Example 10 with ClassData

use of org.motechproject.mds.domain.ClassData in project motech by motech.

the class EntityInfrastructureBuilderTest method shouldCreateCodeIfClassNotExistsInClassPath.

@Test
public void shouldCreateCodeIfClassNotExistsInClassPath() throws Exception {
    doReturn(null).when(classLoader).loadClass(SAMPLE_SERVICE);
    EntityDto entity = new EntityDto(Sample.class.getName());
    List<ClassData> data = entityInfrastructureBuilder.buildInfrastructure(entity, schemaHolder);
    assertNotNull(data);
    assertFalse(data.isEmpty());
    assertThat(data, hasItem(Matchers.<ClassData>hasProperty("className", equalTo(SAMPLE_REPOSITORY))));
    assertThat(data, hasItem(Matchers.<ClassData>hasProperty("className", equalTo(SAMPLE_INTERFACE))));
    assertThat(data, hasItem(Matchers.<ClassData>hasProperty("className", equalTo(SAMPLE_SERVICE))));
    assertThat(data, hasItem(Matchers.<ClassData>hasProperty("interfaceClass", equalTo(true))));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ClassData(org.motechproject.mds.domain.ClassData) Sample(org.motechproject.mds.builder.Sample) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ClassData (org.motechproject.mds.domain.ClassData)28 Test (org.junit.Test)9 FieldDto (org.motechproject.mds.dto.FieldDto)7 CtClass (javassist.CtClass)6 ArrayList (java.util.ArrayList)5 EntityDto (org.motechproject.mds.dto.EntityDto)5 IOException (java.io.IOException)4 CannotCompileException (javassist.CannotCompileException)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 HashSet (java.util.HashSet)2 ByteArrayClassPath (javassist.ByteArrayClassPath)2 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)2 MetadataDto (org.motechproject.mds.dto.MetadataDto)2 TypeDto (org.motechproject.mds.dto.TypeDto)2 EntityCreationException (org.motechproject.mds.exception.entity.EntityCreationException)2 EntBuilderTestClass (org.motechproject.mds.testutil.EntBuilderTestClass)2 MDSClassLoader (org.motechproject.mds.util.MDSClassLoader)2 Bundle (org.osgi.framework.Bundle)2 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1