Search in sources :

Example 71 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class CsvImporterExporter method importInstanceFromRow.

private RowImportResult importInstanceFromRow(EntityDto entityDto, Map<String, String> row, String[] headers, Map<String, FieldDto> fieldMap, List<FieldDto> fields, MotechDataService dataService, CsvImportCustomizer importCustomizer) {
    Class entityClass = dataService.getClassType();
    boolean isNewInstance = true;
    Object instance;
    try {
        instance = importCustomizer.findExistingInstance(row, dataService);
        if (instance == null) {
            LOGGER.debug("Creating new {}", entityClass.getName());
            instance = entityClass.newInstance();
        } else {
            isNewInstance = false;
            LOGGER.debug("Updating {} with id {}", entityClass.getName(), row.get(Constants.Util.ID_FIELD_NAME));
        }
    } catch (InstantiationException | IllegalAccessException e) {
        throw new CsvImportException("Unable to create instance of " + entityClass.getName(), e);
    }
    for (String fieldName : headers) {
        FieldDto field = findField(fieldName, fields, fieldMap, importCustomizer);
        if (field == null) {
            LOGGER.warn("No field with name {} in entity {}, however such row exists in CSV. Ignoring.", fieldName, entityClass.getName());
            continue;
        }
        if (row.containsKey(fieldName)) {
            String csvValue = row.get(fieldName);
            Object parsedValue = parseValue(entityDto, csvValue, field, entityClass.getClassLoader());
            try {
                PropertyUtil.setProperty(instance, StringUtils.uncapitalize(field.getBasic().getName()), parsedValue);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                String msg = String.format("Error when processing field: %s, value in CSV file is %s", fieldName, csvValue);
                throw new CsvImportException(msg, e);
            }
        }
    }
    Object importedInstance;
    if (isNewInstance) {
        importedInstance = importCustomizer.doCreate(instance, dataService);
    } else {
        importedInstance = importCustomizer.doUpdate(instance, dataService);
    }
    Long importedId = (Long) PropertyUtil.safeGetProperty(importedInstance, Constants.Util.ID_FIELD_NAME);
    return new RowImportResult(importedId, isNewInstance);
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) CsvImportException(org.motechproject.mds.exception.csv.CsvImportException) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 72 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class CsvImporterExporter method findField.

private FieldDto findField(String fieldName, List<FieldDto> fields, Map<String, FieldDto> fieldMap, CsvImportCustomizer importCustomizer) {
    if (!fieldMap.containsKey(fieldName)) {
        FieldDto field = importCustomizer.findField(fieldName, fields);
        fieldMap.put(fieldName, field);
    }
    return fieldMap.get(fieldName);
}
Also used : FieldDto(org.motechproject.mds.dto.FieldDto)

Example 73 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class RestIgnoreProcessorTest method shouldProperlyDiscoverRestIgnoredFields.

@Test
public void shouldProperlyDiscoverRestIgnoredFields() throws Exception {
    RestOptionsDto restOptionsDto = new RestOptionsDto();
    List<FieldDto> fields = mockEntityFields();
    processor.setClazz(AnotherSample.class);
    processor.setFields(fields);
    processor.setRestOptions(restOptionsDto);
    processor.execute(bundle, schemaHolder);
    // entity fields + auto generated fields
    Assert.assertEquals(7, processor.getProcessingResult().getFieldNames().size());
    // @RestIgnore annotated
    Assert.assertFalse(processor.getProcessingResult().containsField("modificationDate"));
    Assert.assertFalse(processor.getProcessingResult().containsField("restIgnoreBoolean"));
    // not annotated, regular fields
    Assert.assertTrue(processor.getProcessingResult().containsField("anotherInt"));
    Assert.assertTrue(processor.getProcessingResult().containsField("anotherString"));
}
Also used : RestOptionsDto(org.motechproject.mds.dto.RestOptionsDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 74 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityBuilderTest method setUp.

@Before
public void setUp() {
    mdsClassLoader = MDSClassLoader.getStandaloneInstance(getClass().getClassLoader());
    when(entity.getClassName()).thenReturn(ENTITY_NAME);
    fields = new ArrayList<>();
    FieldDto modificationDateField = fieldDto(MODIFICATION_DATE_FIELD_NAME, DateTime.class);
    FieldDto modifiedByField = fieldDto(MODIFIED_BY_FIELD_NAME, String.class);
    modificationDateField.setReadOnly(true);
    modifiedByField.setReadOnly(true);
    fields.add(modificationDateField);
    fields.add(modifiedByField);
}
Also used : FieldDto(org.motechproject.mds.dto.FieldDto) Before(org.junit.Before)

Example 75 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityBuilderTest method shouldEditClasses.

@Test
public void shouldEditClasses() throws Exception {
    FieldDto firstField = fieldDto("name", Integer.class);
    fields.add(firstField);
    Class<?> clazz = buildClass();
    assertField(clazz, "name", Integer.class);
    fields.remove(firstField);
    fields.add(fieldDto("name2", String.class));
    // reload the classloader for class edit
    mdsClassLoader = MDSClassLoader.getStandaloneInstance(getClass().getClassLoader());
    clazz = buildClass();
    assertField(clazz, "name2", String.class);
    // assert that the first field no longer exists
    try {
        clazz.getDeclaredField("name");
        fail("Field 'name' was preserved in the class, although it was removed from the entity");
    } catch (NoSuchFieldException e) {
    // expected
    }
}
Also used : FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Aggregations

FieldDto (org.motechproject.mds.dto.FieldDto)158 Test (org.junit.Test)61 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)60 EntityDto (org.motechproject.mds.dto.EntityDto)53 ArrayList (java.util.ArrayList)47 LookupDto (org.motechproject.mds.dto.LookupDto)29 List (java.util.List)23 MetadataDto (org.motechproject.mds.dto.MetadataDto)22 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)19 TypeDto (org.motechproject.mds.dto.TypeDto)17 Method (java.lang.reflect.Method)14 MotechDataService (org.motechproject.mds.service.MotechDataService)14 HashMap (java.util.HashMap)12 Field (org.motechproject.mds.domain.Field)12 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)12 Arrays.asList (java.util.Arrays.asList)11 SettingDto (org.motechproject.mds.dto.SettingDto)11 Before (org.junit.Before)7 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)7