Search in sources :

Example 6 with CsvImportOptions

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

the class DefaultCsvImportService method fromCsv.

// -------------------------------------------------------------------------
// CsvImportService implementation
// -------------------------------------------------------------------------
// TODO Add unit tests
@Override
public Metadata fromCsv(InputStream input, CsvImportOptions options) throws IOException {
    CsvReader reader = CsvUtils.getReader(input);
    // Disabled due to large geometry
    reader.setSafetySwitch(false);
    if (options.isFirstRowIsHeader()) {
        // Ignore first row
        reader.readRecord();
    }
    Metadata metadata = new Metadata();
    switch(options.getImportClass()) {
        case DATA_ELEMENT:
            metadata.setDataElements(dataElementsFromCsv(reader));
            break;
        case DATA_ELEMENT_GROUP:
            metadata.setDataElementGroups(dataElementGroupsFromCsv(reader));
            break;
        case DATA_ELEMENT_GROUP_MEMBERSHIP:
            metadata.setDataElementGroups(dataElementGroupMembersFromCsv(reader));
            break;
        case INDICATOR_GROUP_MEMBERSHIP:
            metadata.setIndicatorGroups(indicatorGroupMembersFromCsv(reader));
            break;
        case CATEGORY_OPTION:
            metadata.setCategoryOptions(categoryOptionsFromCsv(reader));
            break;
        case CATEGORY:
            metadata.setCategories(categoriesFromCsv(reader));
            break;
        case CATEGORY_COMBO:
            metadata.setCategoryCombos(categoryCombosFromCsv(reader));
            break;
        case CATEGORY_OPTION_GROUP:
            metadata.setCategoryOptionGroups(categoryOptionGroupsFromCsv(reader));
            break;
        case ORGANISATION_UNIT:
            metadata.setOrganisationUnits(orgUnitsFromCsv(reader));
            break;
        case ORGANISATION_UNIT_GROUP:
            metadata.setOrganisationUnitGroups(orgUnitGroupsFromCsv(reader));
            break;
        case ORGANISATION_UNIT_GROUP_MEMBERSHIP:
            metadata.setOrganisationUnitGroups(orgUnitGroupMembersFromCsv(reader));
            break;
        case VALIDATION_RULE:
            metadata.setValidationRules(validationRulesFromCsv(reader));
            break;
        case OPTION_SET:
            setOptionSetsFromCsv(reader, metadata);
            break;
        case OPTION_GROUP:
            setOptionGroupsFromCsv(reader, metadata);
            break;
        case OPTION_GROUP_SET:
            metadata.setOptionGroupSets(setOptionGroupSetFromCsv(reader));
            break;
        case OPTION_GROUP_SET_MEMBERSHIP:
            metadata.setOptionGroupSets(optionGroupSetMembersFromCsv(reader));
            break;
        default:
            break;
    }
    return metadata;
}
Also used : CsvReader(com.csvreader.CsvReader) Metadata(org.hisp.dhis.dxf2.metadata.Metadata)

Example 7 with CsvImportOptions

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

the class CsvImportServiceTest method testCategoryOptionImportNoHeader.

@Test
void testCategoryOptionImportNoHeader() throws IOException {
    inputBasicObjects = new ClassPathResource("csv/basic_objects_no_header.csv").getInputStream();
    Metadata metadata = csvImportService.fromCsv(inputBasicObjects, new CsvImportOptions().setImportClass(CsvImportClass.CATEGORY_OPTION).setFirstRowIsHeader(false));
    assertEquals(3, metadata.getCategoryOptions().size());
    for (CategoryOption categoryOption : metadata.getCategoryOptions()) {
        assertNotNull(categoryOption.getUid());
        assertNotNull(categoryOption.getName());
        assertNotNull(categoryOption.getShortName());
    }
}
Also used : Metadata(org.hisp.dhis.dxf2.metadata.Metadata) CategoryOption(org.hisp.dhis.category.CategoryOption) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 8 with CsvImportOptions

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

the class CsvImportServiceTest method testCategoryOptionImport.

@Test
void testCategoryOptionImport() throws IOException {
    Metadata metadata = csvImportService.fromCsv(inputBasicObjects, new CsvImportOptions().setImportClass(CsvImportClass.CATEGORY_OPTION).setFirstRowIsHeader(true));
    assertEquals(3, metadata.getCategoryOptions().size());
    for (CategoryOption categoryOption : metadata.getCategoryOptions()) {
        assertNotNull(categoryOption.getUid());
        assertNotNull(categoryOption.getName());
        assertNotNull(categoryOption.getShortName());
    }
}
Also used : Metadata(org.hisp.dhis.dxf2.metadata.Metadata) CategoryOption(org.hisp.dhis.category.CategoryOption) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 9 with CsvImportOptions

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

the class CsvImportServiceTest method testOrganisationUnitGroupMembershipImport.

@Test
void testOrganisationUnitGroupMembershipImport() throws IOException {
    InputStream in = new ClassPathResource("csv/org_unit_group_memberships.csv").getInputStream();
    Metadata metadata = csvImportService.fromCsv(in, new CsvImportOptions().setImportClass(CsvImportClass.ORGANISATION_UNIT_GROUP_MEMBERSHIP).setFirstRowIsHeader(true));
    assertEquals(2, metadata.getOrganisationUnitGroups().size());
}
Also used : 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