use of org.motechproject.mds.dto.MetadataDto in project motech by motech.
the class CsvImporterExporter method parseValue.
private Object parseValue(EntityDto entityDto, String csvValue, FieldDto field, ClassLoader entityCl) {
final TypeDto type = field.getType();
Object value;
if (type.isCombobox()) {
value = parseComboboxValue(entityDto, csvValue, field, entityCl);
} else if (type.isRelationship()) {
value = parseRelationshipValue(csvValue, field);
} else if (type.isMap()) {
MetadataDto keyMetadata = field.getMetadata(MAP_KEY_TYPE);
MetadataDto valueMetadata = field.getMetadata(MAP_VALUE_TYPE);
String mapKeyType = keyMetadata != null ? keyMetadata.getValue() : String.class.getName();
String mapValueType = valueMetadata != null ? valueMetadata.getValue() : String.class.getName();
value = TypeHelper.parseStringToMap(mapKeyType, mapValueType, csvValue);
} else {
value = TypeHelper.parse(csvValue, type.getTypeClass());
}
// for strings, return a blank if the columns is in the file
if (value == null && String.class.equals(type.getTypeClass())) {
value = "";
}
return value;
}
use of org.motechproject.mds.dto.MetadataDto in project motech by motech.
the class CsvTestHelper method relationshipField.
private static FieldDto relationshipField(Long id, String name, TypeDto relationshipType, Class collectionType) {
FieldDto fieldDto = new FieldDto(name, name + " Disp", relationshipType);
fieldDto.setId(id);
fieldDto.addMetadata(new MetadataDto(Constants.MetadataKeys.RELATED_CLASS, RelatedClass.class.getName()));
if (collectionType != null) {
fieldDto.addMetadata(new MetadataDto(Constants.MetadataKeys.RELATIONSHIP_COLLECTION_TYPE, collectionType.getName()));
}
return fieldDto;
}
use of org.motechproject.mds.dto.MetadataDto in project motech by motech.
the class CsvTestHelper method comboboxField.
private static FieldDto comboboxField(Long id, String name, boolean isList) {
FieldDto fieldDto = new FieldDto(name, name + " Disp", TypeDto.COLLECTION);
fieldDto.setId(id);
fieldDto.addMetadata(new MetadataDto(Constants.MetadataKeys.ENUM_CLASS_NAME, RecordEnum.class.getName()));
SettingDto settingDto = new SettingDto(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS, String.valueOf(isList), TypeDto.BOOLEAN);
fieldDto.addSetting(settingDto);
return fieldDto;
}
use of org.motechproject.mds.dto.MetadataDto in project motech by motech.
the class EntityServiceImpl method addField.
private void addField(Entity entity, FieldDto fieldDto) {
FieldBasicDto basic = fieldDto.getBasic();
String typeClass = fieldDto.getType().getTypeClass();
Type type = allTypes.retrieveByClassName(typeClass);
Field field = new Field(entity, basic.getName(), basic.getDisplayName(), basic.isRequired(), basic.isUnique(), fieldDto.isReadOnly(), fieldDto.isNonEditable(), fieldDto.isNonDisplayable(), (String) basic.getDefaultValue(), basic.getTooltip(), basic.getPlaceholder(), null);
field.setType(type);
if (type.hasSettings()) {
for (TypeSetting setting : type.getSettings()) {
SettingDto settingDto = fieldDto.getSetting(setting.getName());
FieldSetting fieldSetting = new FieldSetting(field, setting);
if (null != settingDto) {
fieldSetting.setValue(settingDto.getValueAsString());
}
field.addSetting(fieldSetting);
}
}
if (type.hasValidation()) {
for (TypeValidation validation : type.getValidations()) {
FieldValidation fieldValidation = new FieldValidation(field, validation);
FieldValidationDto validationDto = fieldDto.getValidation();
if (null != validationDto) {
ValidationCriterionDto criterion = validationDto.getCriterion(validation.getDisplayName());
if (null != criterion) {
fieldValidation.setValue(criterion.valueAsString());
fieldValidation.setEnabled(criterion.isEnabled());
}
}
field.addValidation(fieldValidation);
}
}
for (MetadataDto metadata : fieldDto.getMetadata()) {
field.addMetadata(new FieldMetadata(metadata));
}
entity.addField(field);
}
use of org.motechproject.mds.dto.MetadataDto in project motech by motech.
the class EntityBuilderTest method shouldNotAddVersionFieldToTheHistoryClass.
@Test(expected = NoSuchFieldException.class)
public void shouldNotAddVersionFieldToTheHistoryClass() throws Exception {
FieldDto versionField = fieldDto("version", Long.class);
versionField.addMetadata(new MetadataDto(Constants.MetadataKeys.VERSION_FIELD, "true"));
fields.addAll(asList(fieldDto("id", Long.class), versionField, fieldDto("count", Integer.class), fieldDto("str", String.class)));
ClassData classData = entityBuilder.buildHistory(entity, fields);
assertEquals("xx.yy.history.BuilderTest__History", classData.getClassName());
Class<?> clazz = mdsClassLoader.safeDefineClass(classData.getClassName(), classData.getBytecode());
assertNotNull(clazz);
try {
assertField(clazz, "id", Long.class);
assertField(clazz, "count", Integer.class);
assertField(clazz, "str", String.class);
} catch (NoSuchFieldException e) {
LOGGER.error("Cannot find field in the history class", e);
fail();
}
assertField(clazz, "version", Long.class);
}
Aggregations