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"));
}
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));
}
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();
}
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"));
}
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);
}
Aggregations