Search in sources :

Example 66 with CategoryOption

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

the class DefaultFieldFilterServiceTest method defaultClass.

@Test
void defaultClass() {
    final CategoryOption co1 = new CategoryOption();
    co1.setUid("abc1");
    final CategoryOption co2 = new CategoryOption();
    co2.setUid("abc2");
    co2.setName("default");
    final CategoryOption co3 = new CategoryOption();
    co3.setUid("abc3");
    final Category category = new Category();
    category.setUid("def1");
    category.getCategoryOptions().add(co1);
    category.getCategoryOptions().add(co2);
    category.getCategoryOptions().add(co3);
    final FieldFilterParams params = new FieldFilterParams(Collections.singletonList(category), Arrays.asList("id", "categoryOptions"));
    params.setDefaults(Defaults.EXCLUDE);
    final ComplexNode node = service.toComplexNode(params);
    Assertions.assertEquals("category", node.getName());
    Assertions.assertTrue(getNamedNode(node.getUnorderedChildren(), "id") instanceof SimpleNode);
    Assertions.assertEquals("def1", ((SimpleNode) getNamedNode(node.getUnorderedChildren(), "id")).getValue());
    Assertions.assertTrue(getNamedNode(node.getUnorderedChildren(), "categoryOptions") instanceof CollectionNode);
    final CollectionNode collectionNode = (CollectionNode) getNamedNode(node.getUnorderedChildren(), "categoryOptions");
    Assertions.assertEquals(2, collectionNode.getUnorderedChildren().size());
    final List<String> coIds = new ArrayList<>();
    Assertions.assertTrue(collectionNode.getUnorderedChildren().get(0) instanceof ComplexNode);
    ComplexNode complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get(0);
    Assertions.assertEquals("categoryOption", complexNode.getName());
    Assertions.assertEquals(1, complexNode.getUnorderedChildren().size());
    Assertions.assertTrue(complexNode.getUnorderedChildren().get(0) instanceof SimpleNode);
    SimpleNode simpleNode = (SimpleNode) complexNode.getUnorderedChildren().get(0);
    Assertions.assertEquals("id", simpleNode.getName());
    coIds.add(String.valueOf(simpleNode.getValue()));
    Assertions.assertTrue(collectionNode.getUnorderedChildren().get(1) instanceof ComplexNode);
    complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get(1);
    Assertions.assertEquals("categoryOption", complexNode.getName());
    Assertions.assertEquals(1, complexNode.getUnorderedChildren().size());
    Assertions.assertTrue(complexNode.getUnorderedChildren().get(0) instanceof SimpleNode);
    simpleNode = (SimpleNode) complexNode.getUnorderedChildren().get(0);
    Assertions.assertEquals("id", simpleNode.getName());
    coIds.add(String.valueOf(simpleNode.getValue()));
    assertThat(coIds, Matchers.containsInAnyOrder("abc1", "abc3"));
}
Also used : Category(org.hisp.dhis.category.Category) ComplexNode(org.hisp.dhis.node.types.ComplexNode) ArrayList(java.util.ArrayList) CategoryOption(org.hisp.dhis.category.CategoryOption) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) Test(org.junit.jupiter.api.Test)

Example 67 with CategoryOption

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

the class AnalyticsDimensionServiceTest method testGetRecommendedDimensions.

@Test
void testGetRecommendedDimensions() {
    CategoryOption coA = createCategoryOption('A');
    CategoryOption coB = createCategoryOption('B');
    CategoryOption coC = createCategoryOption('C');
    CategoryOption coD = createCategoryOption('D');
    Category caA = createCategory('A', coA);
    Category caB = createCategory('B', coB);
    Category caC = createCategory('C', coC);
    Category caD = createCategory('D', coD);
    caD.setDataDimension(false);
    CategoryCombo ccA = createCategoryCombo('A', caA, caB);
    CategoryCombo ccB = createCategoryCombo('A', caC, caD);
    DataSet dsA = createDataSet('A');
    dsA.setCategoryCombo(ccB);
    DataElement deA = createDataElement('A', ccA);
    DataElement deB = createDataElement('B', ccA);
    dsA.addDataSetElement(deB);
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(Lists.newArrayList(deA, deB)).build();
    List<DimensionalObject> dimensons = dimensionService.getRecommendedDimensions(params);
    assertEquals(3, dimensons.size());
    assertTrue(dimensons.contains(caA));
    assertTrue(dimensons.contains(caB));
    assertTrue(dimensons.contains(caC));
    params = DataQueryParams.newBuilder().withDataElements(Lists.newArrayList(deA)).build();
    dimensons = dimensionService.getRecommendedDimensions(params);
    assertEquals(2, dimensons.size());
    assertTrue(dimensons.contains(caA));
    assertTrue(dimensons.contains(caB));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Category(org.hisp.dhis.category.Category) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DataSet(org.hisp.dhis.dataset.DataSet) CategoryOption(org.hisp.dhis.category.CategoryOption) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 68 with CategoryOption

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

the class HibernateCategoryOptionComboStore method getCategoryOptionCombo.

@Override
public CategoryOptionCombo getCategoryOptionCombo(CategoryCombo categoryCombo, Set<CategoryOption> categoryOptions) {
    String hql = "from CategoryOptionCombo co where co.categoryCombo = :categoryCombo";
    for (CategoryOption option : categoryOptions) {
        hql += " and :option" + option.getId() + " in elements (co.categoryOptions)";
    }
    Query<CategoryOptionCombo> query = getQuery(hql);
    query.setParameter("categoryCombo", categoryCombo);
    for (CategoryOption option : categoryOptions) {
        query.setParameter("option" + option.getId(), option);
    }
    return query.uniqueResult();
}
Also used : CategoryOption(org.hisp.dhis.category.CategoryOption) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 69 with CategoryOption

use of org.hisp.dhis.category.CategoryOption 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 70 with CategoryOption

use of org.hisp.dhis.category.CategoryOption 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)

Aggregations

CategoryOption (org.hisp.dhis.category.CategoryOption)87 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)47 CategoryCombo (org.hisp.dhis.category.CategoryCombo)38 Category (org.hisp.dhis.category.Category)34 Test (org.junit.jupiter.api.Test)33 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)26 Program (org.hisp.dhis.program.Program)19 ArrayList (java.util.ArrayList)18 Event (org.hisp.dhis.tracker.domain.Event)15 BeforeEach (org.junit.jupiter.api.BeforeEach)15 HashSet (java.util.HashSet)14 Collectors (java.util.stream.Collectors)13 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)13 DataElement (org.hisp.dhis.dataelement.DataElement)13 Collections (java.util.Collections)12 DataSet (org.hisp.dhis.dataset.DataSet)12 CodeGenerator (org.hisp.dhis.common.CodeGenerator)11 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)11 CategoryService (org.hisp.dhis.category.CategoryService)10 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)10