Search in sources :

Example 21 with TypeDto

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();
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) Record(org.motechproject.mds.testutil.records.Record) TypeDto(org.motechproject.mds.dto.TypeDto) QueryParams(org.motechproject.mds.query.QueryParams) Matchers.anyString(org.mockito.Matchers.anyString) Date(java.util.Date) FieldDto(org.motechproject.mds.dto.FieldDto) Before(org.junit.Before)

Example 22 with TypeDto

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;
}
Also used : FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) TypeDto(org.motechproject.mds.dto.TypeDto) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldInfo(org.motechproject.mds.entityinfo.FieldInfo) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 23 with TypeDto

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;
}
Also used : FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) TypeDto(org.motechproject.mds.dto.TypeDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 24 with TypeDto

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;
}
Also used : TypeDto(org.motechproject.mds.dto.TypeDto) MetadataDto(org.motechproject.mds.dto.MetadataDto)

Example 25 with TypeDto

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);
}
Also used : CtClass(javassist.CtClass) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) Relationship(org.motechproject.mds.domain.Relationship) TypeDto(org.motechproject.mds.dto.TypeDto) CtClass(javassist.CtClass)

Aggregations

TypeDto (org.motechproject.mds.dto.TypeDto)30 FieldDto (org.motechproject.mds.dto.FieldDto)17 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)9 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)9 EntityDto (org.motechproject.mds.dto.EntityDto)7 MetadataDto (org.motechproject.mds.dto.MetadataDto)7 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)6 CtClass (javassist.CtClass)3 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)3 LookupDto (org.motechproject.mds.dto.LookupDto)3 IOException (java.io.IOException)2 AccessibleObject (java.lang.reflect.AccessibleObject)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 CannotCompileException (javassist.CannotCompileException)2 Before (org.junit.Before)2 Test (org.junit.Test)2 ClassData (org.motechproject.mds.domain.ClassData)2 Field (org.motechproject.mds.domain.Field)2