Search in sources :

Example 1 with EntityCreationException

use of org.motechproject.mds.exception.entity.EntityCreationException 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 2 with EntityCreationException

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

the class EntityBuilderImpl method build.

private ClassData build(EntityDto entity, List<FieldDto> fields, EntityType type, Bundle bundle) {
    try {
        CtClass declaring = makeClass(entity, fields, type, bundle);
        switch(type) {
            case HISTORY:
                String className = type.getClassName(entity.getClassName());
                String simpleName = ClassName.getSimpleName(className);
                TypeDto idType = TypeDto.LONG;
                // add 4 extra fields to history class definition
                // this field is related with id field in entity
                addProperty(declaring, idType.getTypeClass(), simpleName + Constants.Util.CURRENT_VERSION, null);
                // this field contains information about the schema version of an entity
                addProperty(declaring, Long.class.getName(), simpleName + StringUtils.capitalize(Constants.Util.SCHEMA_VERSION_FIELD_NAME), null);
                break;
            case TRASH:
                // this field contains information about the schema version of an entity
                addProperty(declaring, Long.class.getName(), Constants.Util.SCHEMA_VERSION_FIELD_NAME, null);
                break;
            default:
        }
        return new ClassData(declaring.getName(), entity.getModule(), entity.getNamespace(), declaring.toBytecode(), type);
    } catch (ReflectiveOperationException | CannotCompileException | IOException | NotFoundException e) {
        throw new EntityCreationException("Unable to create entity " + entity.getName(), e);
    }
}
Also used : CtClass(javassist.CtClass) ClassData(org.motechproject.mds.domain.ClassData) NotFoundException(javassist.NotFoundException) TypeDto(org.motechproject.mds.dto.TypeDto) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) EntityCreationException(org.motechproject.mds.exception.entity.EntityCreationException)

Example 3 with EntityCreationException

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

the class EntityBuilderImpl method makeClass.

private CtClass makeClass(EntityDto entity, List<FieldDto> fields, EntityType type, Bundle bundle) throws NotFoundException, CannotCompileException, ReflectiveOperationException {
    // try to get declaring class
    CtClass declaring = getDeclaringClass(entity, type, bundle);
    // check and add default constructor if necessary
    injectDefaultConstructor(declaring);
    // create properties (add fields, getters and setters)
    for (FieldDto field : fields) {
        String fieldName = field.getBasic().getName();
        try {
            // We skip version fields for trash and history
            if (field.isVersionField() && type != EntityType.STANDARD) {
                continue;
            }
            CtField ctField;
            if (!shouldLeaveExistingField(field, declaring)) {
                JavassistUtil.removeFieldIfExists(declaring, fieldName);
                ctField = createField(declaring, entity, field, type);
                if (isObjectNullOrBlankString(field.getBasic().getDefaultValue())) {
                    declaring.addField(ctField);
                } else {
                    declaring.addField(ctField, createInitializer(entity, field));
                }
            } else {
                ctField = JavassistUtil.findField(declaring, fieldName);
            }
            String getter = MemberUtil.getGetterName(fieldName, declaring);
            String setter = MemberUtil.getSetterName(fieldName);
            if (!shouldLeaveExistingMethod(field, getter, declaring)) {
                createGetter(declaring, fieldName, ctField);
            }
            if (!shouldLeaveExistingMethod(field, setter, declaring)) {
                createSetter(declaring, fieldName, ctField);
            }
        } catch (RuntimeException e) {
            throw new EntityCreationException("Error while processing field " + fieldName, e);
        }
    }
    return declaring;
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) EntityCreationException(org.motechproject.mds.exception.entity.EntityCreationException) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

EntityCreationException (org.motechproject.mds.exception.entity.EntityCreationException)3 CtClass (javassist.CtClass)2 ClassData (org.motechproject.mds.domain.ClassData)2 IOException (java.io.IOException)1 CannotCompileException (javassist.CannotCompileException)1 CtField (javassist.CtField)1 NotFoundException (javassist.NotFoundException)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 TypeDto (org.motechproject.mds.dto.TypeDto)1 Bundle (org.osgi.framework.Bundle)1