Search in sources :

Example 16 with MetadataDto

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

Example 17 with MetadataDto

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

Example 18 with MetadataDto

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

Example 19 with MetadataDto

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);
}
Also used : FieldMetadata(org.motechproject.mds.domain.FieldMetadata) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldSetting(org.motechproject.mds.domain.FieldSetting) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) TypeValidation(org.motechproject.mds.domain.TypeValidation) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) ValidationCriterionDto(org.motechproject.mds.dto.ValidationCriterionDto) TypeSetting(org.motechproject.mds.domain.TypeSetting) FieldValidationDto(org.motechproject.mds.dto.FieldValidationDto) SettingDto(org.motechproject.mds.dto.SettingDto) FieldValidation(org.motechproject.mds.domain.FieldValidation)

Example 20 with MetadataDto

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);
}
Also used : ClassData(org.motechproject.mds.domain.ClassData) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Aggregations

MetadataDto (org.motechproject.mds.dto.MetadataDto)28 FieldDto (org.motechproject.mds.dto.FieldDto)22 Test (org.junit.Test)9 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)8 TypeDto (org.motechproject.mds.dto.TypeDto)7 ArrayList (java.util.ArrayList)6 SettingDto (org.motechproject.mds.dto.SettingDto)6 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)5 LinkedList (java.util.LinkedList)3 FieldMetadata (javax.jdo.metadata.FieldMetadata)3 EntityDto (org.motechproject.mds.dto.EntityDto)3 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)3 LookupDto (org.motechproject.mds.dto.LookupDto)3 Method (java.lang.reflect.Method)2 List (java.util.List)2 ForeignKeyMetadata (javax.jdo.metadata.ForeignKeyMetadata)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 ClassData (org.motechproject.mds.domain.ClassData)2 ValidationCriterionDto (org.motechproject.mds.dto.ValidationCriterionDto)2 ResultActions (org.springframework.test.web.server.ResultActions)2