Search in sources :

Example 1 with CsvImportOptions

use of org.hisp.dhis.dxf2.csv.CsvImportOptions in project dhis2-core by dhis2.

the class CsvMetadataImportTest method runImport.

private ImportReport runImport(String csvFile, CsvImportClass importClass, Consumer<Metadata> preCondition, Consumer<MetadataImportParams> modifier) throws IOException {
    InputStream input = new ClassPathResource(csvFile).getInputStream();
    Metadata metadata = csvImportService.fromCsv(input, new CsvImportOptions().setImportClass(importClass).setFirstRowIsHeader(true));
    if (preCondition != null) {
        preCondition.accept(metadata);
    }
    MetadataImportParams params = new MetadataImportParams();
    params.addMetadata(schemaService.getMetadataSchemas(), metadata);
    if (modifier != null) {
        modifier.accept(params);
    }
    return importService.importMetadata(params);
}
Also used : CsvImportOptions(org.hisp.dhis.dxf2.csv.CsvImportOptions) InputStream(java.io.InputStream) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 2 with CsvImportOptions

use of org.hisp.dhis.dxf2.csv.CsvImportOptions in project dhis2-core by dhis2.

the class CsvMetadataImportIntegrationTest method runImport.

private ImportReport runImport(String csvFile, CsvImportClass importClass, Consumer<Metadata> preCondition, Consumer<MetadataImportParams> modifier) throws IOException {
    InputStream input = new ClassPathResource(csvFile).getInputStream();
    Metadata metadata = csvImportService.fromCsv(input, new CsvImportOptions().setImportClass(importClass).setFirstRowIsHeader(true));
    if (preCondition != null) {
        preCondition.accept(metadata);
    }
    MetadataImportParams params = new MetadataImportParams();
    params.addMetadata(schemaService.getMetadataSchemas(), metadata);
    if (modifier != null) {
        modifier.accept(params);
    }
    return importService.importMetadata(params);
}
Also used : CsvImportOptions(org.hisp.dhis.dxf2.csv.CsvImportOptions) InputStream(java.io.InputStream) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 3 with CsvImportOptions

use of org.hisp.dhis.dxf2.csv.CsvImportOptions in project dhis2-core by dhis2.

the class MetadataImportExportController method postCsvMetadata.

@PostMapping(value = "", consumes = "application/csv")
@ResponseBody
public WebMessage postCsvMetadata(HttpServletRequest request) throws IOException {
    MetadataImportParams params = metadataImportService.getParamsFromMap(contextService.getParameterValuesMap());
    String classKey = request.getParameter("classKey");
    if (StringUtils.isEmpty(classKey) || !CsvImportClass.classExists(classKey)) {
        return conflict("Cannot find Csv import class:  " + classKey);
    }
    params.setCsvImportClass(CsvImportClass.valueOf(classKey));
    Metadata metadata = csvImportService.fromCsv(request.getInputStream(), new CsvImportOptions().setImportClass(params.getCsvImportClass()).setFirstRowIsHeader(params.isFirstRowIsHeader()));
    params.addMetadata(schemaService.getMetadataSchemas(), metadata);
    if (params.hasJobId()) {
        return startAsyncMetadata(params);
    }
    ImportReport importReport = metadataImportService.importMetadata(params);
    return importReport(importReport).withPlainResponseBefore(DhisApiVersion.V38);
}
Also used : CsvImportOptions(org.hisp.dhis.dxf2.csv.CsvImportOptions) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with CsvImportOptions

use of org.hisp.dhis.dxf2.csv.CsvImportOptions in project dhis2-core by dhis2.

the class CsvImportServiceTest method testImportCategories.

@Test
void testImportCategories() throws IOException {
    InputStream in = new ClassPathResource("csv/categories.csv").getInputStream();
    Metadata metadata = csvImportService.fromCsv(in, new CsvImportOptions().setImportClass(CsvImportClass.CATEGORY).setFirstRowIsHeader(true));
    assertEquals(3, metadata.getCategories().size());
    Category gender = metadata.getCategories().get(0);
    Category ageGroup = metadata.getCategories().get(1);
    Category partner = metadata.getCategories().get(2);
    assertEquals("Gender", gender.getName());
    assertEquals(DataDimensionType.DISAGGREGATION, gender.getDataDimensionType());
    assertEquals("AGEGROUP", ageGroup.getCode());
    assertEquals(DataDimensionType.ATTRIBUTE, partner.getDataDimensionType());
}
Also used : Category(org.hisp.dhis.category.Category) InputStream(java.io.InputStream) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 5 with CsvImportOptions

use of org.hisp.dhis.dxf2.csv.CsvImportOptions in project dhis2-core by dhis2.

the class CsvImportServiceTest method testImportCategoryCombos.

@Test
void testImportCategoryCombos() throws IOException {
    InputStream in = new ClassPathResource("csv/category_combos.csv").getInputStream();
    Metadata metadata = csvImportService.fromCsv(in, new CsvImportOptions().setImportClass(CsvImportClass.CATEGORY_COMBO).setFirstRowIsHeader(true));
    assertEquals(2, metadata.getCategoryCombos().size());
    CategoryCombo genderAge = metadata.getCategoryCombos().get(0);
    CategoryCombo partner = metadata.getCategoryCombos().get(1);
    assertEquals("Gender and Age", genderAge.getName());
    assertEquals(DataDimensionType.DISAGGREGATION, genderAge.getDataDimensionType());
    assertEquals(DataDimensionType.ATTRIBUTE, partner.getDataDimensionType());
}
Also used : CategoryCombo(org.hisp.dhis.category.CategoryCombo) InputStream(java.io.InputStream) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

Metadata (org.hisp.dhis.dxf2.metadata.Metadata)7 ClassPathResource (org.springframework.core.io.ClassPathResource)6 InputStream (java.io.InputStream)5 DhisSpringTest (org.hisp.dhis.DhisSpringTest)5 Test (org.junit.jupiter.api.Test)5 CsvImportOptions (org.hisp.dhis.dxf2.csv.CsvImportOptions)3 CategoryOption (org.hisp.dhis.category.CategoryOption)2 CsvReader (com.csvreader.CsvReader)1 Category (org.hisp.dhis.category.Category)1 CategoryCombo (org.hisp.dhis.category.CategoryCombo)1 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)1 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1