use of org.motechproject.mds.dto.TypeDto in project motech by motech.
the class MdsRestFacadeTest method setUp.
@Before
public void setUp() {
when(dataService.getClassType()).thenReturn(Record.class);
when(entityInfoReader.getEntityInfo(Record.class.getName())).thenReturn(entity);
when(entity.getName()).thenReturn(ENTITY_NAME);
when(entity.getModule()).thenReturn(TEST_MODULE);
when(entity.getClassName()).thenReturn(Record.class.getName());
when(entity.getNamespace()).thenReturn(NAMESPACE);
when(entity.getAdvancedSettings()).thenReturn(advancedSettingsDto);
when(advancedSettingsDto.getRestOptions()).thenReturn(restOptions);
// set up rest fields
FieldDto valueField = FieldTestHelper.fieldDto(3L, VALUE_FIELD, String.class.getName(), VALUE_FIELD, null);
FieldDto dateField = FieldTestHelper.fieldDto(4L, DATE_FIELD, Date.class.getName(), DATE_FIELD, null);
FieldDto blobField = FieldTestHelper.fieldDto(5L, BLOB_FIELD, Byte[].class.getName(), BLOB_FIELD, null);
blobField.setType(new TypeDto("mds.field.blob", StringUtils.EMPTY, BLOB_FIELD, Byte[].class.getName()));
when(restOptions.getFieldNames()).thenReturn(Arrays.asList(VALUE_FIELD, DATE_FIELD, BLOB_FIELD));
// set up lookups
FieldDto strField = FieldTestHelper.fieldDto(1L, STR_FIELD, String.class.getName(), STR_FIELD, null);
FieldDto intField = FieldTestHelper.fieldDto(2L, INT_FIELD, Integer.class.getName(), INT_FIELD, null);
when(entity.getFieldDtos()).thenReturn(asList(intField, strField, valueField, dateField, blobField));
when(entity.getField(STR_FIELD)).thenReturn(FieldTestHelper.fieldInfo(STR_FIELD, String.class, false, true));
when(entity.getField(INT_FIELD)).thenReturn(FieldTestHelper.fieldInfo(INT_FIELD, Integer.class, false, true));
LookupDto forbiddenLookup = new LookupDto(FORBIDDEN_LOOKUP_NAME, true, false, asList(FieldTestHelper.lookupFieldDto(1L, STR_FIELD), FieldTestHelper.lookupFieldDto(2L, INT_FIELD)), true);
LookupDto supportedLookup = new LookupDto(SUPPORTED_LOOKUP_NAME, false, true, asList(FieldTestHelper.lookupFieldDto(1L, STR_FIELD), FieldTestHelper.lookupFieldDto(2L, INT_FIELD)), true);
when(entity.getLookups()).thenReturn(asList(forbiddenLookup, supportedLookup));
// set up record
recordOne = testRecord();
// set up data service
when(dataService.retrieveAll(any(QueryParams.class))).thenReturn(asList(recordOne));
when(dataService.retrieveAll()).thenReturn(asList(recordOne));
when(dataService.findById(1l)).thenReturn(recordOne);
when(dataService.create(recordOne)).thenReturn(recordOne);
when(dataService.getDetachedField(recordOne, BLOB_FIELD)).thenReturn(blobFieldValue);
// do the initialization, normally called by Spring as @PostConstruct
mdsRestFacade.init();
}
use of org.motechproject.mds.dto.TypeDto in project motech by motech.
the class FieldTestHelper method fieldInfo.
public static FieldInfo fieldInfo(String name, Class typeClass, boolean required, boolean restExposed, boolean autoGenerated) {
FieldInfo field = new FieldInfo();
FieldDto fieldDto = new FieldDto();
fieldDto.setBasic(new FieldBasicDto());
fieldDto.getBasic().setName(name);
fieldDto.getBasic().setDisplayName(name + " disp");
fieldDto.getBasic().setRequired(required);
TypeDto type = new TypeDto();
type.setTypeClass(typeClass.getName());
fieldDto.setType(type);
field.setRestExposed(restExposed);
if (autoGenerated) {
fieldDto.addMetadata(new MetadataDto(Constants.Util.AUTO_GENERATED, Constants.Util.TRUE));
}
field.setField(fieldDto);
field.getTypeInfo().setType(typeClass.getName());
return field;
}
use of org.motechproject.mds.dto.TypeDto in project motech by motech.
the class FieldTestHelper method fieldDto.
public static FieldDto fieldDto(Long id, String name, String className, String displayName, Object defValue) {
FieldDto fieldDto = new FieldDto();
fieldDto.setType(new TypeDto(className, "", "", className));
fieldDto.setBasic(new FieldBasicDto(displayName, name));
fieldDto.getBasic().setDefaultValue(defValue);
fieldDto.setId(id);
return fieldDto;
}
use of org.motechproject.mds.dto.TypeDto 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.TypeDto in project motech by motech.
the class EntityBuilderImpl method createField.
private CtField createField(CtClass declaring, EntityDto entity, FieldDto field, EntityType entityType) throws IllegalAccessException, InstantiationException, CannotCompileException, ClassNotFoundException {
TypeDto fieldType = field.getType();
String genericSignature = null;
CtClass type = null;
if (fieldType.isCombobox()) {
ComboboxHolder holder = new ComboboxHolder(entity, field);
if (holder.isEnum() || holder.isEnumCollection()) {
type = classPool.getOrNull(holder.getEnumName());
if (holder.isEnumCollection()) {
genericSignature = JavassistUtil.genericSignature(holder.getTypeClassName(), holder.getEnumName());
type = classPool.getOrNull(holder.getTypeClassName());
}
} else if (holder.isStringCollection()) {
genericSignature = JavassistUtil.genericSignature(holder.getTypeClassName(), holder.getUnderlyingType());
type = classPool.getOrNull(holder.getTypeClassName());
} else if (holder.isString()) {
type = classPool.getOrNull(holder.getUnderlyingType());
}
} else if (fieldType.isRelationship()) {
Class fieldClass = getClass().getClassLoader().loadClass(fieldType.getTypeClass());
Relationship relationshipType = (Relationship) fieldClass.newInstance();
genericSignature = relationshipType.getGenericSignature(field, entityType);
type = classPool.getOrNull(relationshipType.getFieldType(field, entityType));
} else {
type = classPool.getOrNull(fieldType.getTypeClass());
}
return JavassistBuilder.createField(declaring, type, field.getBasic().getName(), genericSignature);
}
Aggregations