use of org.motechproject.mds.dto.CsvImportResults in project motech by motech.
the class CsvImportExportServiceImpl method importCsv.
@Override
public CsvImportResults importCsv(String entityClassName, Reader reader, String fileName, boolean continueOnError) {
LOGGER.debug("Importing instances of entity: {}", entityClassName);
CsvImportResults importResults;
try {
importResults = csvImporterExporter.importCsv(entityClassName, reader, continueOnError);
} catch (RuntimeException e) {
EntityDto entity = entityService.getEntityByClassName(entityClassName);
sendImportFailureEvent(entity, fileName, e);
throw e;
}
sendImportSuccessEvent(importResults, fileName);
return importResults;
}
use of org.motechproject.mds.dto.CsvImportResults in project motech by motech.
the class CsvImportExportServiceTest method shouldImportInstancesByClassName.
@Test
public void shouldImportInstancesByClassName() {
CsvImportResults importResults = new CsvImportResults(entityDto, NEW_IDS, UPDATED_IDS, null);
when(csvImporterExporter.importCsv(ENTITY_CLASS_NAME, reader, false)).thenReturn(importResults);
csvImportExportService.importCsv(ENTITY_CLASS_NAME, reader, FILE_NAME, false);
verify(csvImporterExporter).importCsv(ENTITY_CLASS_NAME, reader, false);
verifyImportSuccessEvent();
}
use of org.motechproject.mds.dto.CsvImportResults in project motech by motech.
the class CsvImporterExporterTest method shouldUseImportCustomizer.
@Test
public void shouldUseImportCustomizer() {
StringReader reader = new StringReader(getTestEntityRecordsAsCsv(IdMode.EMPTY_ID_COLUMN));
when(csvImportCustomizer.doCreate(any(Record2.class), eq(motechDataService))).thenAnswer(new CreateAnswer());
CsvImportResults results = csvImporterExporter.importCsv(ENTITY_ID, reader, csvImportCustomizer, CONTINUE_ON_ERROR, false);
ArgumentCaptor<Record2> captor = ArgumentCaptor.forClass(Record2.class);
verify(csvImportCustomizer, times(INSTANCE_COUNT)).findExistingInstance(anyMap(), eq(motechDataService));
verify(csvImportCustomizer, times(INSTANCE_COUNT)).doCreate(captor.capture(), eq(motechDataService));
verify(csvImportCustomizer, times(FIELD_COUNT)).findField(anyString(), anyList());
verify(csvImportCustomizer, never()).doUpdate(captor.capture(), eq(motechDataService));
assertNotNull(results);
assertEquals(INSTANCE_COUNT, results.totalNumberOfImportedInstances());
}
use of org.motechproject.mds.dto.CsvImportResults 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);
}
}
use of org.motechproject.mds.dto.CsvImportResults in project motech by motech.
the class CsvImportExportServiceTest method shouldClearTableAndImportInstancesById.
@Test
public void shouldClearTableAndImportInstancesById() {
CsvImportResults importResults = new CsvImportResults(entityDto, NEW_IDS, UPDATED_IDS, null);
when(csvImporterExporter.importCsv(eq(ENTITY_ID), eq(reader), any(DefaultCsvImportCustomizer.class), eq(false), eq(true))).thenReturn(importResults);
csvImportExportService.importCsv(ENTITY_ID, reader, FILE_NAME, false, true);
verify(csvImporterExporter).importCsv(eq(ENTITY_ID), eq(reader), any(DefaultCsvImportCustomizer.class), eq(false), eq(true));
verifyImportSuccessEvent();
}
Aggregations