Search in sources :

Example 31 with Category

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

the class EventImportTest method setUpTest.

@Override
protected void setUpTest() throws Exception {
    userService = _userService;
    organisationUnitA = createOrganisationUnit('A');
    organisationUnitB = createOrganisationUnit('B');
    manager.save(organisationUnitA);
    manager.save(organisationUnitB);
    TrackedEntityType trackedEntityType = createTrackedEntityType('A');
    trackedEntityTypeService.addTrackedEntityType(trackedEntityType);
    org.hisp.dhis.trackedentity.TrackedEntityInstance maleA = createTrackedEntityInstance(organisationUnitA);
    maleA.setTrackedEntityType(trackedEntityType);
    manager.save(maleA);
    trackedEntityInstanceMaleA = trackedEntityInstanceService.getTrackedEntityInstance(maleA);
    CategoryOption categoryOption1 = new CategoryOption("male");
    categoryOption1.setAutoFields();
    CategoryOption categoryOption2 = new CategoryOption("female");
    categoryOption2.setAutoFields();
    manager.save(Lists.newArrayList(categoryOption1, categoryOption2));
    Category cat1 = new Category("cat1", DataDimensionType.DISAGGREGATION);
    cat1.setShortName(cat1.getName());
    cat1.setCategoryOptions(Lists.newArrayList(categoryOption1, categoryOption2));
    manager.save(Lists.newArrayList(cat1));
    CategoryCombo categoryCombo = manager.getByName(CategoryCombo.class, "default");
    categoryCombo.setCategories(Lists.newArrayList(cat1));
    dataElementA = createDataElement('A');
    dataElementA.setValueType(ValueType.INTEGER);
    dataElementA.setCategoryCombo(categoryCombo);
    manager.save(dataElementA);
    dataElementA2 = createDataElement('a');
    dataElementA2.setValueType(ValueType.INTEGER);
    dataElementA2.setCategoryCombo(categoryCombo);
    manager.save(dataElementA2);
    dataElementB = createDataElement('B');
    dataElementB.setValueType(ValueType.INTEGER);
    dataElementB.setCategoryCombo(categoryCombo);
    manager.save(dataElementB);
    programStageA = createProgramStage('A', 0);
    programStageA.setFeatureType(FeatureType.POINT);
    manager.save(programStageA);
    programStageA2 = createProgramStage('a', 0);
    programStageA2.setFeatureType(FeatureType.POINT);
    programStageA2.setRepeatable(true);
    manager.save(programStageA2);
    programStageB = createProgramStage('B', 0);
    programStageB.setFeatureType(FeatureType.POINT);
    manager.save(programStageB);
    programA = createProgram('A', new HashSet<>(), organisationUnitA);
    programA.setProgramType(ProgramType.WITH_REGISTRATION);
    programA.setCategoryCombo(categoryCombo);
    manager.save(programA);
    programB = createProgram('B', new HashSet<>(), organisationUnitB);
    programB.setProgramType(ProgramType.WITHOUT_REGISTRATION);
    programB.setCategoryCombo(categoryCombo);
    manager.save(programB);
    ProgramStageDataElement programStageDataElement = new ProgramStageDataElement();
    programStageDataElement.setDataElement(dataElementA);
    programStageDataElement.setProgramStage(programStageA);
    programStageDataElementService.addProgramStageDataElement(programStageDataElement);
    ProgramStageDataElement programStageDataElementA2 = new ProgramStageDataElement();
    programStageDataElementA2.setDataElement(dataElementA2);
    programStageDataElementA2.setProgramStage(programStageA2);
    programStageDataElementService.addProgramStageDataElement(programStageDataElementA2);
    ProgramStageDataElement programStageDataElementB = new ProgramStageDataElement();
    programStageDataElementB.setDataElement(dataElementB);
    programStageDataElementB.setProgramStage(programStageB);
    programStageDataElementService.addProgramStageDataElement(programStageDataElementB);
    programStageA.getProgramStageDataElements().add(programStageDataElement);
    programStageA2.getProgramStageDataElements().add(programStageDataElementA2);
    programStageA.setProgram(programA);
    programStageA2.setProgram(programA);
    programA.getProgramStages().add(programStageA);
    programA.getProgramStages().add(programStageA2);
    programStageB.getProgramStageDataElements().add(programStageDataElementB);
    programStageB.setProgram(programB);
    programB.getProgramStages().add(programStageB);
    manager.update(programStageA);
    manager.update(programStageA2);
    manager.update(programA);
    manager.update(programStageB);
    manager.update(programB);
    pi = new ProgramInstance();
    pi.setEnrollmentDate(new Date());
    pi.setIncidentDate(new Date());
    pi.setProgram(programB);
    pi.setStatus(ProgramStatus.ACTIVE);
    pi.setStoredBy("test");
    pi.setName("EventImportTestPI");
    pi.setUid(CodeGenerator.generateUid());
    manager.save(pi);
    event = createEvent("eventUid001");
    createUserAndInjectSecurityContext(true);
    // Flush all data to disk
    manager.flush();
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Category(org.hisp.dhis.category.Category) CategoryCombo(org.hisp.dhis.category.CategoryCombo) ProgramInstance(org.hisp.dhis.program.ProgramInstance) CategoryOption(org.hisp.dhis.category.CategoryOption) Date(java.util.Date) HashSet(java.util.HashSet)

Example 32 with Category

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

the class ObjectBundleServiceTest method testCreateDataSetWithSectionsAndGreyedFields.

@Test
void testCreateDataSetWithSectionsAndGreyedFields() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_sections_gf.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertFalse(validate.hasErrorReports());
    objectBundleService.commit(bundle);
    List<DataSet> dataSets = manager.getAll(DataSet.class);
    List<Section> sections = manager.getAll(Section.class);
    List<OrganisationUnit> organisationUnits = manager.getAll(OrganisationUnit.class);
    List<DataElement> dataElements = manager.getAll(DataElement.class);
    List<UserAuthorityGroup> userRoles = manager.getAll(UserAuthorityGroup.class);
    List<User> users = manager.getAll(User.class);
    List<DataElementOperand> dataElementOperands = manager.getAll(DataElementOperand.class);
    List<TrackedEntityType> trackedEntityTypes = manager.getAll(TrackedEntityType.class);
    List<OrganisationUnitLevel> organisationUnitLevels = manager.getAll(OrganisationUnitLevel.class);
    assertFalse(organisationUnits.isEmpty());
    assertEquals(1, organisationUnitLevels.size());
    assertEquals(1, trackedEntityTypes.size());
    assertFalse(dataElements.isEmpty());
    assertFalse(users.isEmpty());
    assertFalse(userRoles.isEmpty());
    assertEquals(1, dataSets.size());
    assertEquals(2, sections.size());
    assertEquals(1, dataElementOperands.size());
    DataSet dataSet = dataSets.get(0);
    assertEquals(2, dataSet.getSections().size());
    Section section1 = sections.get(0);
    Section section2 = sections.get(1);
    assertEquals(1, section1.getDataElements().size());
    assertEquals(1, section2.getDataElements().size());
    assertNotNull(section1.getDataSet());
    assertNotNull(section2.getDataSet());
    Section section = manager.get(Section.class, "C50M0WxaI7y");
    assertNotNull(section.getDataSet());
    assertEquals(1, section.getCategoryCombos().size());
    assertEquals(1, section.getGreyedFields().size());
    CategoryCombo categoryCombo = manager.get(CategoryCombo.class, "faV8QvLgIwB");
    assertNotNull(categoryCombo);
    Category category = manager.get(Category.class, "XJGLlMAMCcn");
    assertNotNull(category);
    CategoryOption categoryOption1 = manager.get(CategoryOption.class, "JYiFOMKa25J");
    CategoryOption categoryOption2 = manager.get(CategoryOption.class, "tdaMRD34m8o");
    assertNotNull(categoryOption1);
    assertNotNull(categoryOption2);
}
Also used : User(org.hisp.dhis.user.User) Category(org.hisp.dhis.category.Category) DataSet(org.hisp.dhis.dataset.DataSet) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DataElement(org.hisp.dhis.dataelement.DataElement) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) CategoryCombo(org.hisp.dhis.category.CategoryCombo) CategoryOption(org.hisp.dhis.category.CategoryOption) List(java.util.List) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) Section(org.hisp.dhis.dataset.Section) ClassPathResource(org.springframework.core.io.ClassPathResource) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 33 with Category

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

the class SectionUtilsTest method testDataSetSection.

@Test
void testDataSetSection() {
    CategoryOption categoryOptionA = new CategoryOption("OptionA");
    CategoryOption categoryOptionB = new CategoryOption("OptionB");
    CategoryOption categoryOptionC = new CategoryOption("OptionC");
    CategoryOption categoryOptionD = new CategoryOption("OptionD");
    CategoryOption categoryOptionE = new CategoryOption("OptionE");
    CategoryOption categoryOptionF = new CategoryOption("OptionF");
    CategoryOption categoryOptionG = new CategoryOption("OptionG");
    categoryService.addCategoryOption(categoryOptionA);
    categoryService.addCategoryOption(categoryOptionB);
    categoryService.addCategoryOption(categoryOptionC);
    categoryService.addCategoryOption(categoryOptionD);
    categoryService.addCategoryOption(categoryOptionE);
    categoryService.addCategoryOption(categoryOptionF);
    categoryService.addCategoryOption(categoryOptionG);
    Category categoryA = createCategory('A', categoryOptionA, categoryOptionB);
    Category categoryB = createCategory('B', categoryOptionC, categoryOptionD);
    Category categoryC = createCategory('C', categoryOptionE, categoryOptionF);
    categoryService.addCategory(categoryA);
    categoryService.addCategory(categoryB);
    categoryService.addCategory(categoryC);
    List<Category> categoriesAB = new ArrayList<>();
    List<Category> categoriesABC = new ArrayList<>();
    categoriesAB.add(categoryA);
    categoriesAB.add(categoryB);
    categoriesABC.add(categoryA);
    categoriesABC.add(categoryB);
    categoriesABC.add(categoryC);
    CategoryCombo categoryComboAB = new CategoryCombo("CategoryComboA", DataDimensionType.DISAGGREGATION, categoriesAB);
    CategoryCombo categoryComboABC = new CategoryCombo("CategoryComboB", DataDimensionType.DISAGGREGATION, categoriesABC);
    long catAB = categoryService.addCategoryCombo(categoryComboAB);
    long catABC = categoryService.addCategoryCombo(categoryComboABC);
    long catDefault = categoryService.getDefaultCategoryCombo().getId();
    DataElement dataElementA = createDataElement('A');
    DataElement dataElementB = createDataElement('B');
    DataElement dataElementC = createDataElement('C', categoryComboAB);
    DataElement dataElementD = createDataElement('D', categoryComboAB);
    DataElement dataElementE = createDataElement('E');
    DataElement dataElementF = createDataElement('F', categoryComboABC);
    DataElement dataElementG = createDataElement('G', categoryComboABC);
    DataElement dataElementH = createDataElement('H', categoryComboAB);
    DataElement dataElementI = createDataElement('I', categoryComboABC);
    dataElementService.addDataElement(dataElementA);
    dataElementService.addDataElement(dataElementB);
    dataElementService.addDataElement(dataElementC);
    dataElementService.addDataElement(dataElementD);
    dataElementService.addDataElement(dataElementE);
    dataElementService.addDataElement(dataElementF);
    dataElementService.addDataElement(dataElementG);
    dataElementService.addDataElement(dataElementH);
    dataElementService.addDataElement(dataElementI);
    PeriodType periodType = new MonthlyPeriodType();
    DataSet dataSetA = createDataSet('A', periodType);
    dataSetA.addDataSetElement(dataElementA);
    dataSetA.addDataSetElement(dataElementB);
    dataSetA.addDataSetElement(dataElementC);
    dataSetA.addDataSetElement(dataElementD);
    dataSetA.addDataSetElement(dataElementE);
    dataSetA.addDataSetElement(dataElementF);
    dataSetA.addDataSetElement(dataElementG);
    dataSetA.addDataSetElement(dataElementH);
    dataSetA.addDataSetElement(dataElementI);
    Section section = new Section();
    List<DataElement> dataElements = new ArrayList<>();
    dataElements.add(dataElementA);
    dataElements.add(dataElementB);
    dataElements.add(dataElementC);
    dataElements.add(dataElementD);
    dataElements.add(dataElementE);
    dataElements.add(dataElementF);
    dataElements.add(dataElementG);
    dataElements.add(dataElementH);
    dataElements.add(dataElementI);
    section.setDataElements(dataElements);
    dataSetA.getSections().add(section);
    long idA = dataSetService.addDataSet(dataSetA);
    DataSet ds = dataSetService.getDataSet(idA);
    assertEquals(idA, ds.getId());
    assertEquals(9, ds.getDataSetElements().size());
    assertEquals(1, ds.getSections().size());
    Section sec = ds.getSections().iterator().next();
    assertEquals(9, sec.getDataElements().size());
    Map<String, Collection<DataElement>> orderedDataElements = sectionUtils.getOrderedDataElementsMap(sec);
    List<String> keys = new ArrayList<>(orderedDataElements.keySet());
    assertEquals(6, orderedDataElements.keySet().size());
    String key1 = sec.getId() + "-" + catDefault + "-" + "0";
    String key2 = sec.getId() + "-" + catAB + "-" + "1";
    String key3 = sec.getId() + "-" + catDefault + "-" + "2";
    String key4 = sec.getId() + "-" + catABC + "-" + "3";
    String key5 = sec.getId() + "-" + catAB + "-" + "4";
    String key6 = sec.getId() + "-" + catABC + "-" + "5";
    assertEquals(key1, keys.get(0));
    assertEquals(key2, keys.get(1));
    assertEquals(key3, keys.get(2));
    assertEquals(key4, keys.get(3));
    assertEquals(key5, keys.get(4));
    assertEquals(key6, keys.get(5));
    assertEquals(2, orderedDataElements.get(key1).size());
    assertEquals(2, orderedDataElements.get(key2).size());
    assertEquals(1, orderedDataElements.get(key3).size());
    assertEquals(2, orderedDataElements.get(key4).size());
    assertEquals(1, orderedDataElements.get(key5).size());
    assertEquals(1, orderedDataElements.get(key6).size());
}
Also used : MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) Category(org.hisp.dhis.category.Category) DataSet(org.hisp.dhis.dataset.DataSet) ArrayList(java.util.ArrayList) Section(org.hisp.dhis.dataset.Section) DataElement(org.hisp.dhis.dataelement.DataElement) CategoryCombo(org.hisp.dhis.category.CategoryCombo) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) CategoryOption(org.hisp.dhis.category.CategoryOption) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 34 with Category

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

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

the class AnalyticsDataSetReportStore method getAggregatedSubTotals.

@Override
public Map<String, Object> getAggregatedSubTotals(DataSet dataSet, List<Period> periods, OrganisationUnit unit, Set<String> filters) {
    Map<String, Object> dataMap = new HashMap<>();
    for (Section section : dataSet.getSections()) {
        List<DataElement> dataElements = new ArrayList<>(section.getDataElements());
        Set<Category> categories = new HashSet<>();
        for (CategoryCombo categoryCombo : section.getCategoryCombos()) {
            categories.addAll(categoryCombo.getCategories());
        }
        FilterUtils.filter(dataElements, AggregatableDataElementFilter.INSTANCE);
        if (dataElements.isEmpty() || categories == null || categories.isEmpty()) {
            continue;
        }
        for (Category category : categories) {
            if (category.isDefault()) {
                // No need for sub-total for default
                continue;
            }
            if (!category.isDataDimension()) {
                log.warn("Could not get sub-total for category: " + category.getUid() + " for data set report: " + dataSet + ", not a data dimension");
                continue;
            }
            DataQueryParams.Builder params = DataQueryParams.newBuilder().withDataElements(dataElements).withPeriods(periods).withOrganisationUnit(unit).withCategory(category);
            if (filters != null) {
                params.addFilters(dataQueryService.getDimensionalObjects(filters, null, null, null, false, IdScheme.UID));
            }
            Map<String, Object> map = analyticsService.getAggregatedDataValueMapping(params.build());
            for (Entry<String, Object> entry : map.entrySet()) {
                String[] split = entry.getKey().split(SEPARATOR);
                addToMap(dataMap, split[0] + SEPARATOR + split[3], entry.getValue());
            }
        }
    }
    return dataMap;
}
Also used : Category(org.hisp.dhis.category.Category) Section(org.hisp.dhis.dataset.Section) DataElement(org.hisp.dhis.dataelement.DataElement) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) CategoryCombo(org.hisp.dhis.category.CategoryCombo)

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