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);
}
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);
}
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"));
}
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);
}
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
}
}
Aggregations