Search in sources :

Example 16 with ComboboxHolder

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

the class MDSConstructorImpl method buildEnum.

private void buildEnum(JavassistLoader loader, MdsJDOEnhancer enhancer, EntityDto entity, SchemaHolder schemaHolder) {
    for (FieldDto field : schemaHolder.getFields(entity.getClassName())) {
        TypeDto type = field.getType();
        if (!type.isCombobox()) {
            continue;
        }
        ComboboxHolder holder = new ComboboxHolder(entity, field);
        if (holder.isEnum() || holder.isEnumCollection()) {
            if (field.isReadOnly()) {
                String enumName = holder.getEnumName();
                Class<?> definition = loadClass(entity, enumName);
                if (null != definition) {
                    MotechClassPool.registerEnum(enumName);
                    CtClass ctClass = MotechClassPool.getDefault().getOrNull(enumName);
                    if (null != ctClass) {
                        try {
                            ctClass.defrost();
                            byte[] bytecode = ctClass.toBytecode();
                            ClassData data = new ClassData(enumName, bytecode);
                            // register with the classloader so that we avoid issues with the persistence manager
                            MDSClassLoader.getInstance().safeDefineClass(data.getClassName(), data.getBytecode());
                            addClassData(loader, enhancer, data);
                        } catch (IOException | CannotCompileException e) {
                            LOGGER.error("Could not load enum: {}", enumName);
                        }
                    }
                }
            } else {
                buildEnum(loader, enhancer, holder);
            }
        }
    }
}
Also used : CtClass(javassist.CtClass) ClassData(org.motechproject.mds.domain.ClassData) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) TypeDto(org.motechproject.mds.dto.TypeDto) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)16 TypeDto (org.motechproject.mds.dto.TypeDto)6 FieldDto (org.motechproject.mds.dto.FieldDto)5 ArrayList (java.util.ArrayList)3 EntityDto (org.motechproject.mds.dto.EntityDto)3 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)3 CtClass (javassist.CtClass)2 ClassData (org.motechproject.mds.domain.ClassData)2 RelationshipHolder (org.motechproject.mds.domain.RelationshipHolder)2 Type (org.motechproject.mds.domain.Type)2 IOException (java.io.IOException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 CannotCompileException (javassist.CannotCompileException)1 CtField (javassist.CtField)1 FieldMetadata (javax.jdo.metadata.FieldMetadata)1 ForeignKeyMetadata (javax.jdo.metadata.ForeignKeyMetadata)1