Search in sources :

Example 46 with Category

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

the class JdbcEventAnalyticsTableManagerTest method verifyGetTableWithCategoryCombo.

@Test
void verifyGetTableWithCategoryCombo() {
    Program program = createProgram('A');
    Category categoryA = createCategory('A');
    categoryA.setCreated(getDate(2019, 12, 3));
    Category categoryB = createCategory('B');
    categoryA.setCreated(getDate(2018, 8, 5));
    CategoryCombo categoryCombo = createCategoryCombo('B', categoryA, categoryB);
    addCategoryCombo(program, categoryCombo);
    when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(Lists.newArrayList(program));
    when(jdbcTemplate.queryForList(getYearQueryForCurrentYear(program, true), Integer.class)).thenReturn(Lists.newArrayList(2018, 2019));
    AnalyticsTableUpdateParams params = AnalyticsTableUpdateParams.newBuilder().withLastYears(2).withStartTime(START_TIME).withToday(today).build();
    List<AnalyticsTable> tables = subject.getAnalyticsTables(params);
    assertThat(tables, hasSize(1));
    new AnalyticsTableAsserter.Builder(tables.get(0)).withTableType(AnalyticsTableType.EVENT).withTableName(TABLE_PREFIX + program.getUid().toLowerCase()).withColumnSize(52).withDefaultColumns(subject.getFixedColumns()).addColumns(periodColumns).addColumn(categoryA.getUid(), CHARACTER_11, "acs.", categoryA.getCreated()).addColumn(categoryB.getUid(), CHARACTER_11, "acs.", categoryB.getCreated()).build().verify();
}
Also used : DhisConvenienceTest.createProgram(org.hisp.dhis.DhisConvenienceTest.createProgram) Program(org.hisp.dhis.program.Program) Category(org.hisp.dhis.category.Category) DhisConvenienceTest.createCategory(org.hisp.dhis.DhisConvenienceTest.createCategory) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DhisConvenienceTest.createCategoryCombo(org.hisp.dhis.DhisConvenienceTest.createCategoryCombo) AnalyticsTableAsserter(org.hisp.dhis.analytics.util.AnalyticsTableAsserter) AnalyticsTable(org.hisp.dhis.analytics.AnalyticsTable) AnalyticsTableUpdateParams(org.hisp.dhis.analytics.AnalyticsTableUpdateParams) Test(org.junit.jupiter.api.Test)

Example 47 with Category

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

the class EventAnalyticsServiceTest method testDimensionRestrictionWhenUserCannotReadCategoryOptions.

@Test
void testDimensionRestrictionWhenUserCannotReadCategoryOptions() {
    // Given
    // A program
    Program aProgram = createProgram('B', null, null, Sets.newHashSet(ouA, ouB), null);
    aProgram.setUid("aProgram123");
    idObjectManager.save(aProgram);
    // The category options
    CategoryOption coA = createCategoryOption('A');
    CategoryOption coB = createCategoryOption('B');
    coA.getSharing().setOwner("cannotRead");
    coB.getSharing().setOwner("cannotRead");
    categoryService.addCategoryOption(coA);
    categoryService.addCategoryOption(coB);
    // The categories
    Category caA = createCategory('A', coA);
    Category caB = createCategory('B', coB);
    categoryService.addCategory(caA);
    categoryService.addCategory(caB);
    removeUserAccess(coA);
    removeUserAccess(coB);
    categoryService.updateCategoryOption(coA);
    categoryService.updateCategoryOption(coB);
    // The constraints
    Set<Category> catDimensionConstraints = Sets.newHashSet(caA, caB);
    // The user
    User user = createUser("A", "F_VIEW_EVENT_ANALYTICS");
    user.setCatDimensionConstraints(catDimensionConstraints);
    userService.addUser(user);
    enableDataSharing(user, aProgram, AccessStringHelper.DATA_READ_WRITE);
    idObjectManager.update(user);
    injectSecurityContext(user);
    // All events in program B - 2017
    EventQueryParams events_2017_params = new EventQueryParams.Builder().withOrganisationUnits(Lists.newArrayList(ouA)).withStartDate(getDate(2017, 1, 1)).withEndDate(getDate(2017, 12, 31)).withProgram(aProgram).build();
    // Then
    Throwable exception = assertThrows(IllegalQueryException.class, () -> eventAnalyticsService.getAggregatedEventData(events_2017_params));
    assertThat(exception.getMessage(), containsString("Current user is constrained by a dimension but has access to no dimension items"));
}
Also used : EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) Program(org.hisp.dhis.program.Program) Category(org.hisp.dhis.category.Category) User(org.hisp.dhis.user.User) CategoryOption(org.hisp.dhis.category.CategoryOption) Test(org.junit.jupiter.api.Test)

Example 48 with Category

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

the class DataElementDefaultDimensionPopulator method executeInTransaction.

// -------------------------------------------------------------------------
// Execute
// -------------------------------------------------------------------------
@Override
public void executeInTransaction() {
    Category defaultCategory = categoryService.getCategoryByName(Category.DEFAULT_NAME);
    if (defaultCategory == null) {
        categoryService.generateDefaultDimension();
        defaultCategory = categoryService.getCategoryByName(Category.DEFAULT_NAME);
        log.info("Added default category");
    }
    categoryService.updateCategory(defaultCategory);
    String defaultName = CategoryCombo.DEFAULT_CATEGORY_COMBO_NAME;
    CategoryCombo categoryCombo = categoryService.getCategoryComboByName(defaultName);
    if (categoryCombo == null) {
        categoryService.generateDefaultDimension();
        log.info("Added default dataelement dimension");
        categoryCombo = categoryService.getCategoryComboByName(defaultName);
    }
    // ---------------------------------------------------------------------
    // Any data elements without dimensions need to be associated at least
    // with the default dimension
    // ---------------------------------------------------------------------
    Collection<DataElement> dataElements = dataElementService.getAllDataElements();
    for (DataElement dataElement : dataElements) {
        if (dataElement.getCategoryCombo() == null) {
            dataElement.setCategoryCombo(categoryCombo);
            dataElementService.updateDataElement(dataElement);
        }
    }
}
Also used : Category(org.hisp.dhis.category.Category) CategoryCombo(org.hisp.dhis.category.CategoryCombo)

Example 49 with Category

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

the class DefaultCategoryService method generateDefaultDimension.

@Override
@Transactional
public void generateDefaultDimension() {
    // ---------------------------------------------------------------------
    // CategoryOption
    // ---------------------------------------------------------------------
    CategoryOption categoryOption = new CategoryOption(CategoryOption.DEFAULT_NAME);
    categoryOption.setUid("xYerKDKCefk");
    categoryOption.setCode("default");
    addCategoryOption(categoryOption);
    categoryOption.setPublicAccess(AccessStringHelper.CATEGORY_OPTION_DEFAULT);
    updateCategoryOption(categoryOption);
    // ---------------------------------------------------------------------
    // Category
    // ---------------------------------------------------------------------
    Category category = new Category(Category.DEFAULT_NAME, DataDimensionType.DISAGGREGATION);
    category.setUid("GLevLNI9wkl");
    category.setCode("default");
    category.setShortName("default");
    category.setDataDimension(false);
    category.addCategoryOption(categoryOption);
    addCategory(category);
    category.setPublicAccess(AccessStringHelper.CATEGORY_NO_DATA_SHARING_DEFAULT);
    updateCategory(category);
    // ---------------------------------------------------------------------
    // CategoryCombo
    // ---------------------------------------------------------------------
    CategoryCombo categoryCombo = new CategoryCombo(CategoryCombo.DEFAULT_CATEGORY_COMBO_NAME, DataDimensionType.DISAGGREGATION);
    categoryCombo.setUid("bjDvmb4bfuf");
    categoryCombo.setCode("default");
    categoryCombo.setDataDimensionType(DataDimensionType.DISAGGREGATION);
    categoryCombo.addCategory(category);
    addCategoryCombo(categoryCombo);
    categoryCombo.setPublicAccess(AccessStringHelper.CATEGORY_NO_DATA_SHARING_DEFAULT);
    updateCategoryCombo(categoryCombo);
    // ---------------------------------------------------------------------
    // CategoryOptionCombo
    // ---------------------------------------------------------------------
    CategoryOptionCombo categoryOptionCombo = new CategoryOptionCombo();
    categoryOptionCombo.setUid("HllvX50cXC0");
    categoryOptionCombo.setCode("default");
    categoryOptionCombo.setCategoryCombo(categoryCombo);
    categoryOptionCombo.addCategoryOption(categoryOption);
    addCategoryOptionCombo(categoryOptionCombo);
    categoryOptionCombo.setPublicAccess(AccessStringHelper.CATEGORY_NO_DATA_SHARING_DEFAULT);
    updateCategoryOptionCombo(categoryOptionCombo);
    Set<CategoryOptionCombo> categoryOptionCombos = new HashSet<>();
    categoryOptionCombos.add(categoryOptionCombo);
    categoryCombo.setOptionCombos(categoryOptionCombos);
    updateCategoryCombo(categoryCombo);
    categoryOption.setCategoryOptionCombos(categoryOptionCombos);
    updateCategoryOption(categoryOption);
}
Also used : Category(org.hisp.dhis.category.Category) CategoryCombo(org.hisp.dhis.category.CategoryCombo) CategoryOption(org.hisp.dhis.category.CategoryOption) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 50 with Category

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

the class DefaultDataApprovalAuditService method retainFromDimensionConstraints.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
 * Retain the DataApprovalAudits that the user can read despite any
 * dimension constraints that the user my have.
 *
 * @param audits the list of audit records.
 */
private void retainFromDimensionConstraints(List<DataApprovalAudit> audits) {
    User user = currentUserService.getCurrentUser();
    Set<CategoryOptionGroupSet> cogDimensionConstraints = user.getCogsDimensionConstraints();
    Set<Category> catDimensionConstraints = user.getCatDimensionConstraints();
    if (currentUserService.currentUserIsSuper() || (CollectionUtils.isEmpty(cogDimensionConstraints) && CollectionUtils.isEmpty(catDimensionConstraints))) {
        return;
    }
    // Local
    Map<CategoryOptionCombo, Boolean> readableOptionCombos = new HashMap<>();
    for (Iterator<DataApprovalAudit> i = audits.iterator(); i.hasNext(); ) {
        CategoryOptionCombo optionCombo = i.next().getAttributeOptionCombo();
        Boolean canRead = readableOptionCombos.get(optionCombo);
        if (canRead == null) {
            canRead = canReadOptionCombo(user, optionCombo, cogDimensionConstraints, catDimensionConstraints);
            readableOptionCombos.put(optionCombo, canRead);
        }
        if (!canRead) {
            i.remove();
        }
    }
}
Also used : User(org.hisp.dhis.user.User) Category(org.hisp.dhis.category.Category) HashMap(java.util.HashMap) CategoryOptionGroupSet(org.hisp.dhis.category.CategoryOptionGroupSet) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Aggregations

Category (org.hisp.dhis.category.Category)61 CategoryOption (org.hisp.dhis.category.CategoryOption)25 CategoryCombo (org.hisp.dhis.category.CategoryCombo)22 Test (org.junit.jupiter.api.Test)21 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)13 CategoryOptionGroupSet (org.hisp.dhis.category.CategoryOptionGroupSet)13 ArrayList (java.util.ArrayList)12 User (org.hisp.dhis.user.User)12 DataElement (org.hisp.dhis.dataelement.DataElement)11 HashSet (java.util.HashSet)8 DataSet (org.hisp.dhis.dataset.DataSet)8 OrganisationUnitGroupSet (org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)8 HashMap (java.util.HashMap)7 Date (java.util.Date)5 AnalyticsTableColumn (org.hisp.dhis.analytics.AnalyticsTableColumn)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 OrganisationUnitLevel (org.hisp.dhis.organisationunit.OrganisationUnitLevel)5 List (java.util.List)4 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)4 Attribute (org.hisp.dhis.attribute.Attribute)4