use of org.motechproject.mds.dto.TypeDto 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.dto.TypeDto in project motech by motech.
the class LookupBuilder method body.
private String body() {
StringBuilder body = new StringBuilder();
body.append("java.util.List properties = new java.util.ArrayList();");
Map<String, String> jdoQueryVariables = new HashMap<>();
for (String lookupFieldName : lookup.getFieldsOrder()) {
boolean appendJdoVariableName = false;
FieldDto field = getLookupField(lookupFieldName);
// don't use lookupFieldName for fetching fields, as it can contain dots, etc.
LookupFieldDto lookupField = lookup.getLookupField(field.getBasic().getName());
FieldDto relationField = null;
if (lookupFieldName.contains(".")) {
EntityDto relatedEntity = schemaHolder.getEntityByClassName(new RelationshipHolder(field).getRelatedClass());
relationField = schemaHolder.getFieldByName(relatedEntity, LookupName.getRelatedFieldName(lookupFieldName));
}
TypeDto type = field.getType();
String typeClassName = resolveClassName(type, relationField);
body.append("properties.add(");
if (type.isCombobox()) {
ComboboxHolder holder = new ComboboxHolder(entity, field);
typeClassName = holder.getUnderlyingType();
body.append(resolvePropertyForCombobox(holder, type));
} else if (relationField != null && relationField.getType().isCombobox()) {
RelationshipHolder relationshipHolder = new RelationshipHolder(field);
EntityDto relatedEntity = schemaHolder.getEntityByClassName(relationshipHolder.getRelatedClass());
ComboboxHolder holder = new ComboboxHolder(relatedEntity, relationField);
typeClassName = holder.getUnderlyingType();
body.append(resolvePropertyForCombobox(holder, type));
addJdoVariableName(jdoQueryVariables, lookupFieldName);
appendJdoVariableName = needsJdoVariable(type);
} else if (relationField != null && (type.getTypeClass().equals(TypeDto.MANY_TO_MANY_RELATIONSHIP.getTypeClass()) || type.getTypeClass().equals(TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass()))) {
// related class collection
body.append(PropertyBuilder.class.getName());
body.append(".createRelationProperty");
addJdoVariableName(jdoQueryVariables, lookupFieldName);
appendJdoVariableName = true;
} else {
body.append(PropertyBuilder.class.getName());
body.append(".create");
}
body.append(buildPropertyParameters(appendJdoVariableName, lookupFieldName, lookupField, typeClassName, jdoQueryVariables));
}
body.append(buildReturn());
return body.toString();
}
use of org.motechproject.mds.dto.TypeDto 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);
}
}
}
}
use of org.motechproject.mds.dto.TypeDto in project motech by motech.
the class FieldProcessor method createValidation.
private FieldValidationDto createValidation(AccessibleObject ac, TypeDto type) {
FieldValidationDto validationDto = null;
for (Annotation annotation : ac.getAnnotations()) {
List<TypeValidationDto> validations = getSchemaHolder().findValidations(type.getTypeClass(), annotation.annotationType());
for (TypeValidationDto validation : validations) {
String displayName = validation.getDisplayName();
TypeDto valueType = getSchemaHolder().getType(validation.getValueType());
if (null == valueType) {
throw new IllegalStateException("The valueType is not set in: " + validation);
}
String valueAsString = getValidationValue(displayName, annotation);
if (InSet.class.isAssignableFrom(annotation.annotationType()) || NotInSet.class.isAssignableFrom(annotation.annotationType())) {
valueAsString = valueAsString.replaceAll("(\\{|\\})", "");
}
Object value = TypeHelper.parse(valueAsString, valueType.getClassObjectForType());
ValidationCriterionDto dto = new ValidationCriterionDto();
dto.setDisplayName(displayName);
dto.setType(valueType);
dto.setEnabled(true);
dto.setValue(value);
if (null == validationDto) {
validationDto = new FieldValidationDto();
}
validationDto.addCriterion(dto);
}
}
return validationDto;
}
use of org.motechproject.mds.dto.TypeDto in project motech by motech.
the class FieldProcessor method process.
@Override
protected void process(AnnotatedElement element) {
AccessibleObject ac = (AccessibleObject) element;
Class<?> classType = MemberUtil.getCorrectType(ac);
Class<?> genericType = MemberUtil.getGenericType(element);
Class<?> declaringClass = MemberUtil.getDeclaringClass(ac);
Class<?> valueType = null;
if (null != classType) {
if (Map.class.isAssignableFrom(classType)) {
valueType = MemberUtil.getGenericType(element, 1);
}
String fieldName = MemberUtil.getFieldName(ac);
FieldDto currentField = getFieldByName(declaringClass.getName(), fieldName);
boolean isRelationship = ReflectionsUtil.hasAnnotationClassLoaderSafe(genericType, genericType, Entity.class);
boolean isOwningSide = isRelationship && getMappedBy(ac) == null;
boolean isCollection = Collection.class.isAssignableFrom(classType);
Field annotation = getAnnotationClassLoaderSafe(ac, classType, Field.class);
String relatedFieldName = getRelatedFieldName(ac, classType, genericType, declaringClass, isRelationship);
java.lang.reflect.Field relatedField = (relatedFieldName != null) ? ReflectionUtils.findField(genericType, relatedFieldName) : null;
boolean relatedFieldIsCollection = isRelatedFieldCollection(relatedField);
boolean isTextArea = getAnnotationValue(annotation, TYPE, EMPTY).equalsIgnoreCase("text");
TypeDto type = getCorrectType(classType, isCollection, isRelationship, relatedFieldIsCollection);
FieldBasicDto basic = new FieldBasicDto();
basic.setDisplayName(isUiChanged(currentField) ? currentField.getBasic().getDisplayName() : getAnnotationValue(annotation, DISPLAY_NAME, convertFromCamelCase(fieldName)));
basic.setName(fieldName);
basic.setDefaultValue(getDefaultValueForField(annotation, classType));
basic.setRequired(isFieldRequired(annotation, classType));
basic.setUnique(isFieldUnique(ac));
FieldDto field = new FieldDto();
if (null != annotation) {
basic.setTooltip(isUiChanged(currentField) ? currentField.getBasic().getTooltip() : annotation.tooltip());
basic.setPlaceholder(annotation.placeholder());
String fn = getAnnotationValue(annotation, NAME, EMPTY);
if (!fn.equals(EMPTY)) {
field.addMetadata(new MetadataDto(DATABASE_COLUMN_NAME, fn));
}
}
field.setEntityId(entity.getId());
field.setType(type);
field.setBasic(basic);
field.setValidation(createValidation(ac, type));
field.setReadOnly(true);
field.setNonEditable(false);
field.setNonDisplayable(false);
field.setUiChanged(isUiChanged(currentField));
field.setUiFilterable(isUiFilterable(currentField));
setFieldSettings(ac, classType, isRelationship, isTextArea, field);
setFieldMetadata(classType, genericType, valueType, isCollection, isRelationship, relatedFieldIsCollection, isOwningSide, field, relatedFieldName);
add(field);
} else {
LOGGER.warn("Field type is unknown in: {}", ac);
}
}
Aggregations