Search in sources :

Example 66 with CategoryCombo

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

the class CategoryOptionComboSupplier method getCategoryOptionCombo.

private CategoryOptionCombo getCategoryOptionCombo(Program program, Event event, ProgramStageInstance psi, IdScheme idScheme) {
    final CategoryCombo programCatCombo = program.getCategoryCombo();
    String aoc = event.getAttributeOptionCombo();
    String attributeCatOptions = event.getAttributeCategoryOptions();
    /*
         * Event create request contain aoc information in payload but for event
         * update aoc should be fetched from ProgramStageInstance
         */
    if (psi != null && aoc == null) {
        aoc = psi.getAttributeOptionCombo().getUid();
    }
    CategoryOptionCombo categoryOptionCombo;
    if (isNotEmpty(aoc)) {
        categoryOptionCombo = attributeOptionComboLoader.getCategoryOptionCombo(idScheme, aoc);
        if (categoryOptionCombo == null) {
            categoryOptionCombo = attributeOptionComboLoader.getDefault();
        }
    } else if (programCatCombo != null && isNotEmpty(attributeCatOptions)) {
        categoryOptionCombo = attributeOptionComboLoader.getAttributeOptionCombo(programCatCombo, attributeCatOptions, aoc, idScheme);
    } else {
        categoryOptionCombo = attributeOptionComboLoader.getDefault();
    }
    return categoryOptionCombo;
}
Also used : CategoryCombo(org.hisp.dhis.category.CategoryCombo) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 67 with CategoryCombo

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

the class ProgramSupplier method loadPrograms.

private Map<String, Program> loadPrograms(IdSchemes idSchemes) {
    // Get Id scheme for programs, programs should support also the
    // attribute scheme based on JSONB
    IdScheme idScheme = idSchemes.getProgramIdScheme();
    String sqlSelect = "select p.programid as id, p.uid, p.code, p.name, p.sharing as program_sharing, " + "p.type, p.opendaysaftercoenddate, tet.trackedentitytypeid, tet.sharing  as tet_sharing, " + "tet.uid           as tet_uid, c.categorycomboid as catcombo_id, " + "c.uid             as catcombo_uid, c.name            as catcombo_name, " + "c.code            as catcombo_code, ps.programstageid as ps_id, ps.uid as ps_uid, " + "ps.code           as ps_code, ps.name           as ps_name, " + "ps.featuretype    as ps_feature_type, ps.sort_order, ps.sharing   as ps_sharing, " + "ps.repeatable     as ps_repeatable, ps.enableuserassignment, ps.validationstrategy";
    if (idScheme.isAttribute()) {
        sqlSelect += ",p.attributevalues->'" + idScheme.getAttribute() + "'->>'value' as " + ATTRIBUTESCHEME_COL;
    }
    final String sql = sqlSelect + " from program p " + "LEFT JOIN categorycombo c on p.categorycomboid = c.categorycomboid " + "LEFT JOIN trackedentitytype tet on p.trackedentitytypeid = tet.trackedentitytypeid " + "LEFT JOIN programstage ps on p.programid = ps.programid " + "LEFT JOIN program_organisationunits pou on p.programid = pou.programid " + "LEFT JOIN organisationunit ou on pou.organisationunitid = ou.organisationunitid " + "group by p.programid, tet.trackedentitytypeid, c.categorycomboid, ps.programstageid, ps.sort_order " + "order by p.programid, ps.sort_order";
    return jdbcTemplate.query(sql, (ResultSet rs) -> {
        Map<String, Program> results = new HashMap<>();
        long programId = 0;
        while (rs.next()) {
            if (programId != rs.getLong("id")) {
                Set<ProgramStage> programStages = new HashSet<>();
                Program program = new Program();
                program.setId(rs.getLong("id"));
                program.setUid(rs.getString("uid"));
                program.setName(rs.getString("name"));
                program.setCode(rs.getString("code"));
                program.setOpenDaysAfterCoEndDate(rs.getInt("opendaysaftercoenddate"));
                program.setProgramType(ProgramType.fromValue(rs.getString("type")));
                program.setSharing(toSharing(rs.getString("program_sharing")));
                // Program Stage without a Program Id
                if (rs.getLong("ps_id") != 0) {
                    programStages.add(toProgramStage(rs));
                }
                CategoryCombo categoryCombo = new CategoryCombo();
                categoryCombo.setId(rs.getLong("catcombo_id"));
                categoryCombo.setUid(rs.getString("catcombo_uid"));
                categoryCombo.setName(rs.getString("catcombo_name"));
                categoryCombo.setCode(rs.getString("catcombo_code"));
                program.setCategoryCombo(categoryCombo);
                long tetId = rs.getLong(TRACKED_ENTITY_TYPE_ID);
                if (tetId != 0) {
                    TrackedEntityType trackedEntityType = new TrackedEntityType();
                    trackedEntityType.setId(tetId);
                    trackedEntityType.setUid(rs.getString("tet_uid"));
                    trackedEntityType.setSharing(toSharing(rs.getString("tet_sharing")));
                    program.setTrackedEntityType(trackedEntityType);
                }
                program.setProgramStages(programStages);
                results.put(getProgramKey(idScheme, rs), program);
                programId = program.getId();
            } else {
                results.get(getProgramKey(idScheme, rs)).getProgramStages().add(toProgramStage(rs));
            }
        }
        return results;
    });
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Program(org.hisp.dhis.program.Program) HashMap(java.util.HashMap) CategoryCombo(org.hisp.dhis.category.CategoryCombo) ResultSet(java.sql.ResultSet) IdScheme(org.hisp.dhis.common.IdScheme) ProgramStage(org.hisp.dhis.program.ProgramStage) HashSet(java.util.HashSet)

Example 68 with CategoryCombo

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

the class DefaultDataSetMetadataExportService method getDataSetMetadata.

// TODO add lock exceptions
// TODO add validation caching (ETag and If-None-Match).
@Override
public ObjectNode getDataSetMetadata() {
    User user = currentUserService.getCurrentUser();
    List<DataSet> dataSets = idObjectManager.getDataWriteAll(DataSet.class);
    Set<DataElement> dataElements = flatMapToSet(dataSets, DataSet::getDataElements);
    Set<Indicator> indicators = flatMapToSet(dataSets, DataSet::getIndicators);
    Set<CategoryCombo> dataElementCategoryCombos = flatMapToSet(dataElements, DataElement::getCategoryCombos);
    Set<CategoryCombo> dataSetCategoryCombos = mapToSet(dataSets, DataSet::getCategoryCombo);
    Set<Category> dataElementCategories = flatMapToSet(dataElementCategoryCombos, CategoryCombo::getCategories);
    Set<Category> dataSetCategories = flatMapToSet(dataSetCategoryCombos, CategoryCombo::getCategories);
    Set<CategoryOption> categoryOptions = getCategoryOptions(dataElementCategories, dataSetCategories, user);
    expressionService.substituteIndicatorExpressions(indicators);
    ObjectNode rootNode = fieldFilterService.createObjectNode();
    rootNode.putArray(DataSetSchemaDescriptor.PLURAL).addAll(asObjectNodes(dataSets, FIELDS_DATA_SETS, DataSet.class));
    rootNode.putArray(DataElementSchemaDescriptor.PLURAL).addAll(asObjectNodes(dataElements, FIELDS_DATA_ELEMENTS, DataElement.class));
    rootNode.putArray(IndicatorSchemaDescriptor.PLURAL).addAll(asObjectNodes(indicators, FIELDS_INDICATORS, Indicator.class));
    rootNode.putArray(CategoryComboSchemaDescriptor.PLURAL).addAll(asObjectNodes(dataElementCategoryCombos, FIELDS_DATAELEMENT_CAT_COMBOS, CategoryCombo.class)).addAll(asObjectNodes(dataSetCategoryCombos, FIELDS_DATA_SET_CAT_COMBOS, CategoryCombo.class));
    rootNode.putArray(CategorySchemaDescriptor.PLURAL).addAll(asObjectNodes(dataElementCategories, FIELDS_CATEGORIES, Category.class)).addAll(asObjectNodes(dataSetCategories, FIELDS_CATEGORIES, Category.class));
    rootNode.putArray(CategoryOptionSchemaDescriptor.PLURAL).addAll(asObjectNodes(categoryOptions, FIELDS_CATEGORIES, CategoryOption.class));
    return rootNode;
}
Also used : User(org.hisp.dhis.user.User) Category(org.hisp.dhis.category.Category) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DataSet(org.hisp.dhis.dataset.DataSet) Indicator(org.hisp.dhis.indicator.Indicator) DataElement(org.hisp.dhis.dataelement.DataElement) CategoryCombo(org.hisp.dhis.category.CategoryCombo) CategoryOption(org.hisp.dhis.category.CategoryOption)

Example 69 with CategoryCombo

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

the class InputUtils method getAttributeOptionComboInternal.

private CategoryOptionCombo getAttributeOptionComboInternal(String cc, String cp, boolean skipFallback) {
    Set<String> opts = TextUtils.splitToArray(cp, TextUtils.SEMICOLON);
    if ((cc == null && opts != null || (cc != null && opts == null))) {
        throw new IllegalQueryException("Both or none of category combination and category options must be present");
    }
    CategoryCombo categoryCombo = null;
    if (cc != null && (categoryCombo = idObjectManager.get(CategoryCombo.class, cc)) == null) {
        throw new IllegalQueryException("Illegal category combo identifier: " + cc);
    }
    if (categoryCombo == null) {
        if (skipFallback) {
            return null;
        }
        categoryCombo = categoryService.getDefaultCategoryCombo();
    }
    return getAttributeOptionCombo(categoryCombo, opts, null, IdScheme.UID);
}
Also used : CategoryCombo(org.hisp.dhis.category.CategoryCombo) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException)

Example 70 with CategoryCombo

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

the class DefaultCompleteDataSetRegistrationExchangeServiceTest method verifyUserHasNoWritePermissionOnCategoryOption.

@Test
void verifyUserHasNoWritePermissionOnCategoryOption() {
    OrganisationUnit organisationUnit = createOrganisationUnit('A');
    DataSet dataSetA = createDataSet('A', new MonthlyPeriodType());
    CategoryCombo categoryCombo = createCategoryCombo('A');
    CategoryOption categoryOptionA = createCategoryOption('A');
    CategoryOption categoryOptionB = createCategoryOption('B');
    CategoryOptionCombo categoryOptionCombo = createCategoryOptionCombo(categoryCombo, categoryOptionA, categoryOptionB);
    Period period = createPeriod("201907");
    String payload = createPayload(period, organisationUnit, dataSetA, categoryCombo, categoryOptionA, categoryOptionB);
    try (MockedConstruction<MetadataCaches> mocked = mockConstruction(MetadataCaches.class, (mock, context) -> {
        when(mock.getDataSets()).thenReturn(datasetCache);
        when(mock.getPeriods()).thenReturn(periodCache);
        when(mock.getOrgUnits()).thenReturn(orgUnitCache);
        aocCache = new CachingMap<>();
        when(mock.getAttrOptionCombos()).thenReturn(aocCache);
        when(mock.getOrgUnitInHierarchyMap()).thenReturn(orgUnitInHierarchyCache);
        when(mock.getAttrOptComboOrgUnitMap()).thenReturn(attrOptComboOrgUnitCache);
    })) {
        when(currentUserService.getCurrentUser()).thenReturn(user);
        when(batchHandler.init()).thenReturn(batchHandler);
        when(idObjManager.get(CategoryCombo.class, categoryCombo.getUid())).thenReturn(categoryCombo);
        when(idObjManager.getObject(CategoryOption.class, IdScheme.UID, categoryOptionA.getUid())).thenReturn(categoryOptionA);
        when(idObjManager.getObject(CategoryOption.class, IdScheme.UID, categoryOptionB.getUid())).thenReturn(categoryOptionB);
        when(categoryService.getCategoryOptionCombo(categoryCombo, Sets.newHashSet(categoryOptionA, categoryOptionB))).thenReturn(categoryOptionCombo);
        when(datasetCache.get(eq(dataSetA.getUid()), any())).thenReturn(dataSetA);
        when(periodCache.get(eq(period.getIsoDate()), any())).thenReturn(period);
        when(orgUnitCache.get(eq(organisationUnit.getUid()), any())).thenReturn(createOrganisationUnit('A'));
        when(orgUnitInHierarchyCache.get(eq(organisationUnit.getUid()), any())).thenReturn(Boolean.TRUE);
        when(attrOptComboOrgUnitCache.get(eq(categoryOptionCombo.getUid() + organisationUnit.getUid()), any())).thenReturn(Boolean.TRUE);
        when(categoryService.getCategoryOptionCombo(categoryOptionCombo.getUid())).thenReturn(categoryOptionCombo);
        // force error on access check for Category Option Combo
        when(aclService.canDataWrite(user, dataSetA)).thenReturn(true);
        when(aclService.canDataWrite(user, categoryOptionA)).thenReturn(false);
        when(aclService.canDataWrite(user, categoryOptionB)).thenReturn(true);
        when(notifier.clear(null)).thenReturn(notifier);
        when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_PERIODS)).thenReturn(false);
        when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_ATTRIBUTE_OPTION_COMBOS)).thenReturn(false);
        when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_ORGANISATION_UNITS)).thenReturn(false);
        when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_REQUIRE_ATTRIBUTE_OPTION_COMBO)).thenReturn(false);
        when(currentUserService.getCurrentUserOrganisationUnits()).thenReturn(Collections.singleton(createOrganisationUnit('A')));
        when(i18nManager.getI18n()).thenReturn(i18n);
        when(categoryService.getDefaultCategoryOptionCombo()).thenReturn(DEFAULT_COC);
        when(batchHandlerFactory.createBatchHandler(CompleteDataSetRegistrationBatchHandler.class)).thenReturn(batchHandler);
        when(notifier.notify(any(), anyString())).thenReturn(notifier);
        when(notifier.notify(null, NotificationLevel.INFO, "Import done", true)).thenReturn(notifier);
        // call method under test
        ImportSummary summary = subject.saveCompleteDataSetRegistrationsJson(new ByteArrayInputStream(payload.getBytes()), new ImportOptions());
        assertThat(summary.getStatus(), is(ImportStatus.ERROR));
        assertThat(summary.getImportCount().getIgnored(), is(1));
        assertEquals(1, summary.getConflictCount());
        assertThat(summary.getConflicts().iterator().next().getValue(), is("User has no data write access for CategoryOption: " + categoryOptionA.getUid()));
    }
}
Also used : DhisConvenienceTest.createOrganisationUnit(org.hisp.dhis.DhisConvenienceTest.createOrganisationUnit) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DhisConvenienceTest.createDataSet(org.hisp.dhis.DhisConvenienceTest.createDataSet) DataSet(org.hisp.dhis.dataset.DataSet) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) DhisConvenienceTest.createPeriod(org.hisp.dhis.DhisConvenienceTest.createPeriod) Period(org.hisp.dhis.period.Period) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DhisConvenienceTest.createCategoryCombo(org.hisp.dhis.DhisConvenienceTest.createCategoryCombo) ByteArrayInputStream(java.io.ByteArrayInputStream) CategoryOption(org.hisp.dhis.category.CategoryOption) DhisConvenienceTest.createCategoryOption(org.hisp.dhis.DhisConvenienceTest.createCategoryOption) DhisConvenienceTest.createCategoryOptionCombo(org.hisp.dhis.DhisConvenienceTest.createCategoryOptionCombo) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) 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