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