Search in sources :

Example 11 with OptionSet

use of org.hisp.dhis.option.OptionSet in project dhis2-core by dhis2.

the class DefaultCsvImportService method setOptionSetsFromCsv.

/**
     * Option set format:
     * <p>
     * <ul>
     * <li>option set name</li>
     * <li>option set uid</li>
     * <li>option set code</li>
     * <li>option name</li>
     * <li>option uid</li>
     * <li>option code</li>
     * </ul>
     */
private void setOptionSetsFromCsv(CsvReader reader, Metadata metadata) throws IOException {
    ListMap<String, Option> nameOptionMap = new ListMap<>();
    Map<String, OptionSet> nameOptionSetMap = new HashMap<>();
    while (reader.readRecord()) {
        String[] values = reader.getValues();
        if (values != null && values.length > 0) {
            OptionSet optionSet = new OptionSet();
            setIdentifiableObject(optionSet, values);
            optionSet.setAutoFields();
            optionSet.setValueType(ValueType.TEXT);
            Option option = new Option();
            option.setName(getSafe(values, 3, null, 230));
            option.setUid(getSafe(values, 4, CodeGenerator.generateUid(), 11));
            option.setCode(getSafe(values, 5, null, 50));
            option.setAutoFields();
            if (optionSet.getName() == null || option.getCode() == null) {
                continue;
            }
            nameOptionSetMap.put(optionSet.getName(), optionSet);
            nameOptionMap.putValue(optionSet.getName(), option);
            metadata.getOptions().add(option);
        }
    }
    for (String optionSetName : nameOptionSetMap.keySet()) {
        OptionSet optionSet = nameOptionSetMap.get(optionSetName);
        List<Option> options = new ArrayList<>(nameOptionMap.get(optionSetName));
        optionSet.setOptions(options);
        metadata.getOptionSets().add(optionSet);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet) ListMap(org.hisp.dhis.common.ListMap)

Example 12 with OptionSet

use of org.hisp.dhis.option.OptionSet in project dhis2-core by dhis2.

the class DataValueSetServiceTest method setUpTest.

@Override
public void setUpTest() {
    mockDataValueBatchHandler = new MockBatchHandler<>();
    mockDataValueAuditBatchHandler = new MockBatchHandler<>();
    mockBatchHandlerFactory = new MockBatchHandlerFactory();
    mockBatchHandlerFactory.registerBatchHandler(DataValueBatchHandler.class, mockDataValueBatchHandler);
    mockBatchHandlerFactory.registerBatchHandler(DataValueAuditBatchHandler.class, mockDataValueAuditBatchHandler);
    setDependency(dataValueSetService, "batchHandlerFactory", mockBatchHandlerFactory);
    attribute = new Attribute("CUSTOM_ID", ValueType.TEXT);
    attribute.setUid(ATTRIBUTE_UID);
    attribute.setUnique(true);
    attribute.setOrganisationUnitAttribute(true);
    attribute.setDataElementAttribute(true);
    idObjectManager.save(attribute);
    categoryOptionA = createCategoryOption('A');
    categoryOptionB = createCategoryOption('B');
    categoryA = createDataElementCategory('A', categoryOptionA, categoryOptionB);
    categoryComboA = createCategoryCombo('A', categoryA);
    categoryComboDef = categoryService.getDefaultDataElementCategoryCombo();
    ocDef = categoryService.getDefaultDataElementCategoryOptionCombo();
    ocDef.setCode("OC_DEF_CODE");
    categoryService.updateDataElementCategoryOptionCombo(ocDef);
    osA = new OptionSet("OptionSetA", ValueType.INTEGER);
    osA.getOptions().add(new Option("Blue", "1"));
    osA.getOptions().add(new Option("Green", "2"));
    osA.getOptions().add(new Option("Yellow", "3"));
    ocA = createCategoryOptionCombo(categoryComboA, categoryOptionA);
    ocB = createCategoryOptionCombo(categoryComboA, categoryOptionB);
    deA = createDataElement('A', categoryComboDef);
    deB = createDataElement('B', categoryComboDef);
    deC = createDataElement('C', categoryComboDef);
    deD = createDataElement('D', categoryComboDef);
    deE = createDataElement('E');
    deE.setOptionSet(osA);
    deF = createDataElement('F', categoryComboDef);
    deF.setValueType(ValueType.BOOLEAN);
    deG = createDataElement('G', categoryComboDef);
    deG.setValueType(ValueType.TRUE_ONLY);
    dsA = createDataSet('A', new MonthlyPeriodType());
    dsA.setCategoryCombo(categoryComboDef);
    ouA = createOrganisationUnit('A');
    ouB = createOrganisationUnit('B');
    ouC = createOrganisationUnit('C');
    peA = createPeriod(PeriodType.getByNameIgnoreCase(MonthlyPeriodType.NAME), getDate(2012, 1, 1), getDate(2012, 1, 31));
    peB = createPeriod(PeriodType.getByNameIgnoreCase(MonthlyPeriodType.NAME), getDate(2012, 2, 1), getDate(2012, 2, 29));
    peC = createPeriod(PeriodType.getByNameIgnoreCase(MonthlyPeriodType.NAME), getDate(2012, 3, 1), getDate(2012, 3, 31));
    ocA.setUid("kjuiHgy67hg");
    ocB.setUid("Gad33qy67g5");
    deA.setUid("f7n9E0hX8qk");
    deB.setUid("Ix2HsbDMLea");
    deC.setUid("eY5ehpbEsB7");
    deE.setUid("jH26dja2f28");
    deF.setUid("jH26dja2f30");
    deG.setUid("jH26dja2f31");
    dsA.setUid("pBOMPrpg1QX");
    ouA.setUid("DiszpKrYNg8");
    ouB.setUid("BdfsJfj87js");
    ouC.setUid("j7Hg26FpoIa");
    ocA.setCode("OC_A");
    ocB.setCode("OC_B");
    deA.setCode("DE_A");
    deB.setCode("DE_B");
    deC.setCode("DE_C");
    deD.setCode("DE_D");
    dsA.setCode("DS_A");
    ouA.setCode("OU_A");
    ouB.setCode("OU_B");
    ouC.setCode("OU_C");
    categoryService.addDataElementCategoryOption(categoryOptionA);
    categoryService.addDataElementCategoryOption(categoryOptionB);
    categoryService.addDataElementCategory(categoryA);
    categoryService.addDataElementCategoryCombo(categoryComboA);
    categoryService.addDataElementCategoryOptionCombo(ocA);
    categoryService.addDataElementCategoryOptionCombo(ocB);
    attributeService.addAttributeValue(deA, createAttributeValue(attribute, "DE1"));
    dataElementService.addDataElement(deA);
    attributeService.addAttributeValue(deB, createAttributeValue(attribute, "DE2"));
    dataElementService.addDataElement(deB);
    attributeService.addAttributeValue(deC, createAttributeValue(attribute, "DE3"));
    dataElementService.addDataElement(deC);
    attributeService.addAttributeValue(deD, createAttributeValue(attribute, "DE4"));
    dataElementService.addDataElement(deD);
    dataElementService.addDataElement(deF);
    dataElementService.addDataElement(deG);
    idObjectManager.save(osA);
    dsA.addDataSetElement(deA);
    dsA.addDataSetElement(deB);
    dsA.addDataSetElement(deC);
    dsA.addDataSetElement(deD);
    attributeService.addAttributeValue(ouA, createAttributeValue(attribute, "OU1"));
    organisationUnitService.addOrganisationUnit(ouA);
    attributeService.addAttributeValue(ouB, createAttributeValue(attribute, "OU2"));
    organisationUnitService.addOrganisationUnit(ouB);
    attributeService.addAttributeValue(ouC, createAttributeValue(attribute, "OU3"));
    organisationUnitService.addOrganisationUnit(ouC);
    dsA.addOrganisationUnit(ouA);
    dsA.addOrganisationUnit(ouC);
    dataSetService.addDataSet(dsA);
    periodService.addPeriod(peA);
    periodService.addPeriod(peB);
    periodService.addPeriod(peC);
    user = createUser('A');
    user.setOrganisationUnits(Sets.newHashSet(ouA, ouB));
    CurrentUserService currentUserService = new MockCurrentUserService(user);
    setDependency(dataValueSetService, "currentUserService", currentUserService);
}
Also used : MockBatchHandlerFactory(org.hisp.dhis.mock.batchhandler.MockBatchHandlerFactory) Attribute(org.hisp.dhis.attribute.Attribute) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet)

Aggregations

OptionSet (org.hisp.dhis.option.OptionSet)12 Option (org.hisp.dhis.option.Option)6 DataElement (org.hisp.dhis.dataelement.DataElement)5 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DhisSpringTest (org.hisp.dhis.DhisSpringTest)3 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)3 Attribute (org.hisp.dhis.attribute.Attribute)2 DataElementCategoryOption (org.hisp.dhis.dataelement.DataElementCategoryOption)2 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)2 DataSet (org.hisp.dhis.dataset.DataSet)2 ProgramDataElementDimensionItem (org.hisp.dhis.program.ProgramDataElementDimensionItem)2 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Rectangle (com.lowagie.text.Rectangle)1 PdfPCell (com.lowagie.text.pdf.PdfPCell)1 PdfPTable (com.lowagie.text.pdf.PdfPTable)1 Date (java.util.Date)1