Search in sources :

Example 1 with CsvImportException

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

Example 2 with CsvImportException

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

Example 3 with CsvImportException

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);
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) CsvImportException(org.motechproject.mds.exception.csv.CsvImportException) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 4 with CsvImportException

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) CsvImportException(org.motechproject.mds.exception.csv.CsvImportException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with CsvImportException

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

Aggregations

CsvImportException (org.motechproject.mds.exception.csv.CsvImportException)6 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 FieldDto (org.motechproject.mds.dto.FieldDto)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 CsvImportResults (org.motechproject.mds.dto.CsvImportResults)1 DefaultCsvImportCustomizer (org.motechproject.mds.service.DefaultCsvImportCustomizer)1 MotechDataService (org.motechproject.mds.service.MotechDataService)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 CsvMapReader (org.supercsv.io.CsvMapReader)1