Search in sources :

Example 6 with CsvImportResults

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

Example 7 with CsvImportResults

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

Example 8 with CsvImportResults

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());
}
Also used : StringReader(java.io.StringReader) Record2(org.motechproject.mds.testutil.records.Record2) CsvImportResults(org.motechproject.mds.dto.CsvImportResults) Test(org.junit.Test)

Example 9 with CsvImportResults

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);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MotechDataService(org.motechproject.mds.service.MotechDataService) CsvImportResults(org.motechproject.mds.dto.CsvImportResults) CsvImportException(org.motechproject.mds.exception.csv.CsvImportException) CsvMapReader(org.supercsv.io.CsvMapReader) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 10 with CsvImportResults

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();
}
Also used : DefaultCsvImportCustomizer(org.motechproject.mds.service.DefaultCsvImportCustomizer) CsvImportResults(org.motechproject.mds.dto.CsvImportResults) Test(org.junit.Test)

Aggregations

CsvImportResults (org.motechproject.mds.dto.CsvImportResults)10 Test (org.junit.Test)5 StringReader (java.io.StringReader)4 Record2 (org.motechproject.mds.testutil.records.Record2)3 ArrayList (java.util.ArrayList)2 EntityDto (org.motechproject.mds.dto.EntityDto)2 DefaultCsvImportCustomizer (org.motechproject.mds.service.DefaultCsvImportCustomizer)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 LocalDateTime (java.time.LocalDateTime)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1 Period (org.joda.time.Period)1