Search in sources :

Example 31 with CategoryCombo

use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.

the class DefaultDataSetReportService method getSectionDataSetReport.

private List<Grid> getSectionDataSetReport(DataSet dataSet, List<Period> periods, OrganisationUnit unit, Set<String> filters, boolean selectedUnitOnly) {
    I18nFormat format = i18nManager.getI18nFormat();
    I18n i18n = i18nManager.getI18n();
    List<Section> sections = new ArrayList<>(dataSet.getSections());
    sections.sort(new SectionOrderComparator());
    Map<String, Object> valueMap = dataSetReportStore.getAggregatedValues(dataSet, periods, unit, filters);
    Map<String, Object> subTotalMap = dataSetReportStore.getAggregatedSubTotals(dataSet, periods, unit, filters);
    Map<String, Object> totalMap = dataSetReportStore.getAggregatedTotals(dataSet, periods, unit, filters);
    List<Grid> grids = new ArrayList<>();
    for (Section section : sections) {
        for (CategoryCombo categoryCombo : section.getCategoryCombos()) {
            Grid grid = new ListGrid().setTitle(section.getName() + SPACE + categoryCombo.getName()).setSubtitle(unit.getName() + SPACE + formatPeriods(periods, format));
            // -----------------------------------------------------------------
            // Grid headers
            // -----------------------------------------------------------------
            grid.addHeader(new GridHeader(i18n.getString("dataelement"), false, true));
            List<CategoryOptionCombo> optionCombos = categoryCombo.getSortedOptionCombos();
            for (CategoryOptionCombo optionCombo : optionCombos) {
                grid.addHeader(new GridHeader(optionCombo.isDefault() ? DEFAULT_HEADER : optionCombo.getName(), false, false));
            }
            if (// Sub-total
            categoryCombo.doSubTotals() && !selectedUnitOnly) {
                for (CategoryOption categoryOption : categoryCombo.getCategoryOptions()) {
                    grid.addHeader(new GridHeader(categoryOption.getName(), false, false));
                }
            }
            if (// Total
            categoryCombo.doTotal() && !selectedUnitOnly) {
                grid.addHeader(new GridHeader(TOTAL_HEADER, false, false));
            }
            // -----------------------------------------------------------------
            // Grid values
            // -----------------------------------------------------------------
            List<DataElement> dataElements = new ArrayList<>(section.getDataElementsByCategoryCombo(categoryCombo));
            FilterUtils.filter(dataElements, AggregatableDataElementFilter.INSTANCE);
            for (DataElement dataElement : dataElements) {
                grid.addRow();
                // Data
                grid.addValue(new GridValue(dataElement.getFormNameFallback()));
                for (// Values
                CategoryOptionCombo optionCombo : // Values
                optionCombos) {
                    Map<Object, Object> attributes = new HashMap<>();
                    attributes.put(ATTR_DE, dataElement.getUid());
                    attributes.put(ATTR_CO, optionCombo.getUid());
                    Object value;
                    if (selectedUnitOnly) {
                        value = getSelectedUnitValue(dataElement, periods, unit, optionCombo);
                    } else {
                        value = valueMap.get(dataElement.getUid() + SEPARATOR + optionCombo.getUid());
                    }
                    grid.addValue(new GridValue(value, attributes));
                }
                if (// Sub-total
                categoryCombo.doSubTotals() && !selectedUnitOnly) {
                    for (CategoryOption categoryOption : categoryCombo.getCategoryOptions()) {
                        Object value = subTotalMap.get(dataElement.getUid() + SEPARATOR + categoryOption.getUid());
                        grid.addValue(new GridValue(value));
                    }
                }
                if (// Total
                categoryCombo.doTotal() && !selectedUnitOnly) {
                    Object value = totalMap.get(String.valueOf(dataElement.getUid()));
                    grid.addValue(new GridValue(value));
                }
            }
            grids.add(grid);
        }
    }
    return grids;
}
Also used : HashMap(java.util.HashMap) SectionOrderComparator(org.hisp.dhis.dataset.comparator.SectionOrderComparator) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) I18nFormat(org.hisp.dhis.i18n.I18nFormat) Section(org.hisp.dhis.dataset.Section) ListGrid(org.hisp.dhis.system.grid.ListGrid) GridHeader(org.hisp.dhis.common.GridHeader) DataElement(org.hisp.dhis.dataelement.DataElement) CategoryCombo(org.hisp.dhis.category.CategoryCombo) GridValue(org.hisp.dhis.common.GridValue) CategoryOption(org.hisp.dhis.category.CategoryOption) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) I18n(org.hisp.dhis.i18n.I18n)

Example 32 with CategoryCombo

use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.

the class DefaultDataSetReportService method getDefaultDataSetReport.

private List<Grid> getDefaultDataSetReport(DataSet dataSet, List<Period> periods, OrganisationUnit unit, Set<String> filters, boolean selectedUnitOnly) {
    ListMap<CategoryCombo, DataElement> map = new ListMap<>();
    for (DataSetElement element : dataSet.getDataSetElements()) {
        map.putValue(element.getResolvedCategoryCombo(), element.getDataElement());
    }
    DataSet tmpDataSet = new DataSet(dataSet.getName(), dataSet.getShortName(), dataSet.getPeriodType());
    tmpDataSet.setDataSetElements(dataSet.getDataSetElements());
    for (CategoryCombo categoryCombo : map.keySet()) {
        List<DataElement> dataElements = map.get(categoryCombo);
        String name = categoryCombo.isDefault() ? dataSet.getName() : categoryCombo.getName();
        Section section = new Section(name, dataSet, dataElements, null);
        tmpDataSet.getSections().add(section);
    }
    return getSectionDataSetReport(tmpDataSet, periods, unit, filters, selectedUnitOnly);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DataSet(org.hisp.dhis.dataset.DataSet) DataSetElement(org.hisp.dhis.dataset.DataSetElement) Section(org.hisp.dhis.dataset.Section) ListMap(org.hisp.dhis.common.ListMap)

Example 33 with CategoryCombo

use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.

the class DhisConvenienceTest method createCategoryCombo.

/**
 * @param categoryComboUniqueIdentifier A unique character to identify the
 *        category option combo.
 * @param categories the categories category options.
 * @return CategoryOptionCombo
 */
public static CategoryCombo createCategoryCombo(char categoryComboUniqueIdentifier, Category... categories) {
    CategoryCombo categoryCombo = new CategoryCombo("CategoryCombo" + categoryComboUniqueIdentifier, DataDimensionType.DISAGGREGATION);
    categoryCombo.setAutoFields();
    for (Category category : categories) {
        categoryCombo.getCategories().add(category);
    }
    return categoryCombo;
}
Also used : Category(org.hisp.dhis.category.Category) CategoryCombo(org.hisp.dhis.category.CategoryCombo)

Example 34 with CategoryCombo

use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.

the class ObjectBundleServiceTest method testPreheatValidationsWithCatCombo.

@Test
void testPreheatValidationsWithCatCombo() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/de_validate1.json").getInputStream(), RenderFormat.JSON);
    CategoryCombo categoryCombo = manager.getByName(CategoryCombo.class, "default");
    categoryCombo.setUid("p0KPaWEg3cf");
    manager.update(categoryCombo);
    OptionSet optionSet = new OptionSet("OptionSet: pQYCiuosBnZ", ValueType.TEXT);
    optionSet.setAutoFields();
    optionSet.setUid("pQYCiuosBnZ");
    manager.save(optionSet);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.VALIDATE);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertTrue(validate.hasErrorReports());
    validate.forEachErrorReport(errorReport -> {
        assertTrue(errorReport instanceof PreheatErrorReport);
        PreheatErrorReport preheatErrorReport = (PreheatErrorReport) errorReport;
        assertEquals(PreheatIdentifier.UID, preheatErrorReport.getPreheatIdentifier());
        if (preheatErrorReport.getValue() instanceof CategoryCombo) {
            fail();
        } else if (preheatErrorReport.getValue() instanceof User) {
            assertEquals("GOLswS44mh8", preheatErrorReport.getObjectReference().getUid());
        } else if (preheatErrorReport.getValue() instanceof OptionSet) {
            fail();
        }
    });
}
Also used : User(org.hisp.dhis.user.User) CategoryCombo(org.hisp.dhis.category.CategoryCombo) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) List(java.util.List) OptionSet(org.hisp.dhis.option.OptionSet) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 35 with CategoryCombo

use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.

the class ObjectBundleServiceTest method testPreheatValidations.

@Test
void testPreheatValidations() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/de_validate1.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.VALIDATE);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertFalse(validate.isEmpty());
    assertTrue(validate.hasErrorReports());
    validate.forEachErrorReport(errorReport -> {
        assertTrue(errorReport instanceof PreheatErrorReport);
        PreheatErrorReport preheatErrorReport = (PreheatErrorReport) errorReport;
        assertEquals(PreheatIdentifier.UID, preheatErrorReport.getPreheatIdentifier());
        if (preheatErrorReport.getValue() instanceof CategoryCombo) {
            assertEquals("p0KPaWEg3cf", preheatErrorReport.getObjectReference().getUid());
        } else if (preheatErrorReport.getValue() instanceof User) {
            assertEquals("GOLswS44mh8", preheatErrorReport.getObjectReference().getUid());
        } else if (preheatErrorReport.getValue() instanceof OptionSet) {
            assertEquals("pQYCiuosBnZ", preheatErrorReport.getObjectReference().getUid());
        }
    });
}
Also used : User(org.hisp.dhis.user.User) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) CategoryCombo(org.hisp.dhis.category.CategoryCombo) List(java.util.List) OptionSet(org.hisp.dhis.option.OptionSet) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

CategoryCombo (org.hisp.dhis.category.CategoryCombo)114 Test (org.junit.jupiter.api.Test)66 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)53 CategoryOption (org.hisp.dhis.category.CategoryOption)47 Category (org.hisp.dhis.category.Category)41 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)39 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)36 Program (org.hisp.dhis.program.Program)31 Event (org.hisp.dhis.tracker.domain.Event)27 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)23 BeforeEach (org.junit.jupiter.api.BeforeEach)23 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)22 Collectors (java.util.stream.Collectors)21 ProgramInstance (org.hisp.dhis.program.ProgramInstance)21 Collections (java.util.Collections)20 CategoryService (org.hisp.dhis.category.CategoryService)20 CodeGenerator (org.hisp.dhis.common.CodeGenerator)20 ProgramStage (org.hisp.dhis.program.ProgramStage)20 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)20 Sets (com.google.common.collect.Sets)19