use of org.motechproject.mds.exception.csv.CsvImportException in project motech by motech.
the class CsvImportExportServiceTest method shouldThrowImportFailureExceptionWhenImportingById.
@Test
public void shouldThrowImportFailureExceptionWhenImportingById() {
when(csvImporterExporter.importCsv(eq(ENTITY_ID), eq(reader), any(DefaultCsvImportCustomizer.class), eq(false), eq(false))).thenThrow(new CsvImportException(FAILURE_EX_MSG));
when(entityService.getEntity(ENTITY_ID)).thenReturn(entityDto);
boolean thrown = false;
try {
csvImportExportService.importCsv(ENTITY_ID, reader, FILE_NAME, false, false);
} catch (CsvImportException e) {
thrown = true;
}
assertTrue("CSV Import exception was not propagated", thrown);
verify(csvImporterExporter).importCsv(eq(ENTITY_ID), eq(reader), any(DefaultCsvImportCustomizer.class), eq(false), eq(false));
verifyImportFailureEvent();
}
use of org.motechproject.mds.exception.csv.CsvImportException in project motech by motech.
the class CsvImportExportServiceTest method shouldThrowImportFailureExceptionWhenImportingByClassName.
@Test
public void shouldThrowImportFailureExceptionWhenImportingByClassName() {
when(csvImporterExporter.importCsv(ENTITY_CLASS_NAME, reader, false)).thenThrow(new CsvImportException(FAILURE_EX_MSG));
when(entityService.getEntityByClassName(ENTITY_CLASS_NAME)).thenReturn(entityDto);
boolean thrown = false;
try {
csvImportExportService.importCsv(ENTITY_CLASS_NAME, reader, FILE_NAME, false);
} catch (CsvImportException e) {
thrown = true;
}
assertTrue("CSV Import exception was not propagated", thrown);
verify(csvImporterExporter).importCsv(ENTITY_CLASS_NAME, reader, false);
verifyImportFailureEvent();
}
use of org.motechproject.mds.exception.csv.CsvImportException 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.exception.csv.CsvImportException in project motech by motech.
the class InstanceController method importCsv.
@RequestMapping(value = "/instances/{entityId}/csvimport", method = RequestMethod.POST)
@ResponseBody
public CsvImportResults importCsv(@PathVariable long entityId, @RequestParam(required = true) MultipartFile file, @RequestParam(required = false) boolean continueOnError, @RequestParam(required = false) boolean clearData) {
instanceService.verifyEntityAccess(entityId);
instanceService.validateNonEditableProperty(entityId);
try {
try (InputStream in = file.getInputStream()) {
Reader reader = new InputStreamReader(in);
return csvImportExportService.importCsv(entityId, reader, file.getOriginalFilename(), continueOnError, clearData);
}
} catch (IOException e) {
throw new CsvImportException("Unable to open uploaded file", e);
}
}
use of org.motechproject.mds.exception.csv.CsvImportException in project motech by motech.
the class CsvImporterExporter method importCsv.
private CsvImportResults importCsv(final EntityInfo entityInfo, final Reader reader, CsvImportCustomizer importCustomizer, boolean continueOnError, boolean clearData) {
final MotechDataService dataService = DataServiceHelper.getDataService(getBundleContext(), entityInfo.getClassName());
Map<String, FieldDto> fieldCacheMap = new HashMap<>();
if (clearData) {
dataService.deleteAll();
}
try (CsvMapReader csvMapReader = new CsvMapReader(reader, CsvPreference.STANDARD_PREFERENCE)) {
List<Long> newInstanceIDs = new ArrayList<>();
List<Long> updatedInstanceIDs = new ArrayList<>();
Map<Integer, String> exceptions = new HashMap<>();
Map<String, String> row;
int rowNum = 0;
final String[] headers = csvMapReader.getHeader(true);
while ((row = csvMapReader.read(headers)) != null) {
rowNum++;
try {
// import a row
RowImportResult rowImportResult = importInstanceFromRow(entityInfo.getEntity(), row, headers, fieldCacheMap, entityInfo.getFieldDtos(), dataService, importCustomizer);
Long id = rowImportResult.getId();
// put its ID in the correct list
if (rowImportResult.isNewInstance()) {
newInstanceIDs.add(id);
} else {
updatedInstanceIDs.add(id);
}
} catch (RuntimeException e) {
if (continueOnError) {
exceptions.put(rowNum, e.getMessage());
} else {
throw e;
}
}
}
return new CsvImportResults(entityInfo.getEntity(), newInstanceIDs, updatedInstanceIDs, exceptions);
} catch (IOException e) {
throw new CsvImportException("IO Error when importing CSV", e);
}
}
Aggregations