Search in sources :

Example 71 with CategoryOptionCombo

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

the class DataApprovalController method getDataApprovalList.

private List<DataApproval> getDataApprovalList(ApprovalsDto approvals) throws WebMessageException {
    Set<DataApprovalWorkflow> workflows = getAndValidateWorkflows(approvals.getDs(), approvals.getWf());
    List<Period> periods = PeriodType.getPeriodsFromIsoStrings(approvals.getPe());
    periods = periodService.reloadPeriods(periods);
    if (periods.isEmpty()) {
        throw new WebMessageException(conflict("Approvals must have periods"));
    }
    User user = currentUserService.getCurrentUser();
    CategoryOptionCombo defaultOptionCombo = categoryService.getDefaultCategoryOptionCombo();
    Date date = new Date();
    List<DataApproval> dataApprovals = new ArrayList<>();
    List<CategoryOptionCombo> optionCombos = categoryService.getAllCategoryOptionCombos();
    Map<String, CategoryOptionCombo> comboMap = optionCombos.stream().collect(Collectors.toMap(CategoryOptionCombo::getUid, c -> c));
    Map<String, OrganisationUnit> ouCache = new HashMap<>();
    for (ApprovalDto approval : approvals.getApprovals()) {
        CategoryOptionCombo atributeOptionCombo = comboMap.get(approval.getAoc());
        atributeOptionCombo = ObjectUtils.firstNonNull(atributeOptionCombo, defaultOptionCombo);
        OrganisationUnit unit = ouCache.get(approval.getOu());
        if (unit == null) {
            unit = getAndValidateOrgUnit(approval.getOu());
            ouCache.put(approval.getOu(), unit);
        }
        for (DataApprovalWorkflow workflow : workflows) {
            for (Period period : periods) {
                dataApprovals.add(new DataApproval(null, workflow, period, unit, atributeOptionCombo, false, date, user));
            }
        }
    }
    return dataApprovals;
}
Also used : CategoryService(org.hisp.dhis.category.CategoryService) RequestParam(org.springframework.web.bind.annotation.RequestParam) DataApprovalWorkflow(org.hisp.dhis.dataapproval.DataApprovalWorkflow) DataApprovalService(org.hisp.dhis.dataapproval.DataApprovalService) Date(java.util.Date) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) PeriodService(org.hisp.dhis.period.PeriodService) Autowired(org.springframework.beans.factory.annotation.Autowired) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) NodeUtils(org.hisp.dhis.node.NodeUtils) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) Preset(org.hisp.dhis.node.Preset) ApprovalDto(org.hisp.dhis.webapi.webdomain.approval.ApprovalDto) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Period(org.hisp.dhis.period.Period) PostMapping(org.springframework.web.bind.annotation.PostMapping) ContextService(org.hisp.dhis.webapi.service.ContextService) Collection(java.util.Collection) FieldFilterService(org.hisp.dhis.fieldfilter.FieldFilterService) MediaType(org.springframework.http.MediaType) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) WebMessageUtils.conflict(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict) DataApprovalStateResponse(org.hisp.dhis.dataapproval.DataApprovalStateResponse) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DataApproval(org.hisp.dhis.dataapproval.DataApproval) DataApprovalLevel(org.hisp.dhis.dataapproval.DataApprovalLevel) RootNode(org.hisp.dhis.node.types.RootNode) ApprovalsDto(org.hisp.dhis.webapi.webdomain.approval.ApprovalsDto) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) DataApprovalStatus(org.hisp.dhis.dataapproval.DataApprovalStatus) DataSet(org.hisp.dhis.dataset.DataSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) ApprovalStatusDto(org.hisp.dhis.webapi.webdomain.approval.ApprovalStatusDto) Controller(org.springframework.stereotype.Controller) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RequestBody(org.springframework.web.bind.annotation.RequestBody) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) User(org.hisp.dhis.user.User) GetMapping(org.springframework.web.bind.annotation.GetMapping) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) DataApprovalLevelService(org.hisp.dhis.dataapproval.DataApprovalLevelService) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) DataApprovalPermissions(org.hisp.dhis.dataapproval.DataApprovalPermissions) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectUtils(org.hisp.dhis.util.ObjectUtils) HttpStatus(org.springframework.http.HttpStatus) Collectors.toList(java.util.stream.Collectors.toList) CurrentUserService(org.hisp.dhis.user.CurrentUserService) PeriodType(org.hisp.dhis.period.PeriodType) DataSetService(org.hisp.dhis.dataset.DataSetService) DataApproval(org.hisp.dhis.dataapproval.DataApproval) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) DataApprovalWorkflow(org.hisp.dhis.dataapproval.DataApprovalWorkflow) Date(java.util.Date) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) ApprovalDto(org.hisp.dhis.webapi.webdomain.approval.ApprovalDto)

Example 72 with CategoryOptionCombo

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

the class PreCheckDataRelationsValidationHookTest method eventValidationFailsWhenEventAOCAndEventCOsAreSetAndInProgramCCButDoNotMatch.

@Test
void eventValidationFailsWhenEventAOCAndEventCOsAreSetAndInProgramCCButDoNotMatch() {
    OrganisationUnit orgUnit = setupOrgUnit();
    Program program = setupProgram(orgUnit);
    CategoryCombo cc = categoryCombo();
    program.setCategoryCombo(cc);
    CategoryOptionCombo aoc1 = cc.getSortedOptionCombos().get(0);
    CategoryOption co1 = (CategoryOption) aoc1.getCategoryOptions().toArray()[0];
    when(preheat.getCategoryOption(co1.getUid())).thenReturn(co1);
    CategoryOptionCombo aoc2 = cc.getSortedOptionCombos().get(1);
    when(preheat.getCategoryOptionCombo(aoc2.getUid())).thenReturn(aoc2);
    Event event = eventBuilder().attributeOptionCombo(aoc2.getUid()).attributeCategoryOptions(co1.getUid()).build();
    hook.validateEvent(reporter, event);
    assertEquals(1, reporter.getReportList().size());
    assertTrue(reporter.hasErrorReport(r -> r.getErrorCode() == TrackerErrorCode.E1053 && r.getErrorMessage().contains(co1.getUid()) && r.getErrorMessage().contains(aoc2.getUid())));
    assertNull(reporter.getValidationContext().getCachedEventCategoryOptionCombo(event.getEvent()), "AOC id should not be cached");
    verify(preheat, times(0)).put(any(), (IdentifiableObject) any());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CategoryService(org.hisp.dhis.category.CategoryService) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CategoryOption(org.hisp.dhis.category.CategoryOption) TrackerIdentifier(org.hisp.dhis.tracker.TrackerIdentifier) RelationshipConstraint(org.hisp.dhis.relationship.RelationshipConstraint) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ReferenceTrackerEntity(org.hisp.dhis.tracker.preheat.ReferenceTrackerEntity) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Category(org.hisp.dhis.category.Category) RelationshipItem(org.hisp.dhis.tracker.domain.RelationshipItem) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProgramType(org.hisp.dhis.program.ProgramType) Optional(java.util.Optional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DataDimensionType(org.hisp.dhis.common.DataDimensionType) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Mock(org.mockito.Mock) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Relationship(org.hisp.dhis.tracker.domain.Relationship) Program(org.hisp.dhis.program.Program) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) TrackerIdentifierParams(org.hisp.dhis.tracker.TrackerIdentifierParams) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) RelationshipType(org.hisp.dhis.relationship.RelationshipType) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TRACKED_ENTITY_INSTANCE(org.hisp.dhis.relationship.RelationshipEntity.TRACKED_ENTITY_INSTANCE) RelationshipEntity(org.hisp.dhis.relationship.RelationshipEntity) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Event(org.hisp.dhis.tracker.domain.Event) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ProgramStage(org.hisp.dhis.program.ProgramStage) Mockito.verify(org.mockito.Mockito.verify) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Matchers.hasItem(org.hamcrest.Matchers.hasItem) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Collections(java.util.Collections) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) CategoryCombo(org.hisp.dhis.category.CategoryCombo) CategoryOption(org.hisp.dhis.category.CategoryOption) Event(org.hisp.dhis.tracker.domain.Event) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 73 with CategoryOptionCombo

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

the class PreCheckDataRelationsValidationHookTest method categoryComboWithTwoCategories.

private CategoryCombo categoryComboWithTwoCategories() {
    char uniqueIdentifier = 'A';
    CategoryOption co1 = createCategoryOption(uniqueIdentifier);
    Category ca1 = createCategory(uniqueIdentifier, co1);
    CategoryOption co2 = createCategoryOption(uniqueIdentifier);
    Category ca2 = createCategory(uniqueIdentifier, co2);
    CategoryCombo cc = createCategoryCombo(uniqueIdentifier, ca1, ca2);
    cc.setDataDimensionType(DataDimensionType.ATTRIBUTE);
    CategoryOptionCombo aoc1 = createCategoryOptionCombo(cc, co1, co2);
    cc.setOptionCombos(Sets.newHashSet(aoc1));
    return cc;
}
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)

Example 74 with CategoryOptionCombo

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

the class PreCheckDataRelationsValidationHookTest method eventValidationFailsWhenOnlyAOCIsSetToDefaultAOCNotInProgramCC.

@Test
void eventValidationFailsWhenOnlyAOCIsSetToDefaultAOCNotInProgramCC() {
    OrganisationUnit orgUnit = setupOrgUnit();
    Program program = setupProgram(orgUnit);
    program.setCategoryCombo(categoryCombo('A'));
    CategoryOptionCombo defaultAOC = firstCategoryOptionCombo(defaultCategoryCombo());
    when(preheat.getCategoryOptionCombo(defaultAOC.getUid())).thenReturn(defaultAOC);
    Event event = eventBuilder().attributeOptionCombo(defaultAOC.getUid()).build();
    hook.validateEvent(reporter, event);
    assertEquals(1, reporter.getReportList().size());
    assertTrue(reporter.hasErrorReport(r -> r.getErrorCode() == TrackerErrorCode.E1055));
    assertNull(reporter.getValidationContext().getCachedEventCategoryOptionCombo(event.getEvent()), "AOC id should not be cached");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CategoryService(org.hisp.dhis.category.CategoryService) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CategoryOption(org.hisp.dhis.category.CategoryOption) TrackerIdentifier(org.hisp.dhis.tracker.TrackerIdentifier) RelationshipConstraint(org.hisp.dhis.relationship.RelationshipConstraint) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ReferenceTrackerEntity(org.hisp.dhis.tracker.preheat.ReferenceTrackerEntity) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Category(org.hisp.dhis.category.Category) RelationshipItem(org.hisp.dhis.tracker.domain.RelationshipItem) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProgramType(org.hisp.dhis.program.ProgramType) Optional(java.util.Optional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DataDimensionType(org.hisp.dhis.common.DataDimensionType) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Mock(org.mockito.Mock) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Relationship(org.hisp.dhis.tracker.domain.Relationship) Program(org.hisp.dhis.program.Program) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) TrackerIdentifierParams(org.hisp.dhis.tracker.TrackerIdentifierParams) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) RelationshipType(org.hisp.dhis.relationship.RelationshipType) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TRACKED_ENTITY_INSTANCE(org.hisp.dhis.relationship.RelationshipEntity.TRACKED_ENTITY_INSTANCE) RelationshipEntity(org.hisp.dhis.relationship.RelationshipEntity) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Event(org.hisp.dhis.tracker.domain.Event) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ProgramStage(org.hisp.dhis.program.ProgramStage) Mockito.verify(org.mockito.Mockito.verify) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Matchers.hasItem(org.hamcrest.Matchers.hasItem) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Collections(java.util.Collections) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) Event(org.hisp.dhis.tracker.domain.Event) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 75 with CategoryOptionCombo

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

the class PreCheckDataRelationsValidationHookTest method eventValidationSucceedsWhenOnlyCOsAreSetAndExist.

@Test
void eventValidationSucceedsWhenOnlyCOsAreSetAndExist() {
    OrganisationUnit orgUnit = setupOrgUnit();
    Program program = setupProgram(orgUnit);
    CategoryCombo cc = categoryCombo();
    program.setCategoryCombo(cc);
    CategoryOption co = cc.getCategoryOptions().get(0);
    when(preheat.getCategoryOption(co.getUid())).thenReturn(co);
    CategoryOptionCombo aoc = firstCategoryOptionCombo(cc);
    when(categoryService.getCategoryOptionCombo(cc, Sets.newHashSet(co))).thenReturn(aoc);
    when(preheat.getIdentifiers()).thenReturn(TrackerIdentifierParams.builder().categoryOptionComboIdScheme(TrackerIdentifier.NAME).build());
    Event event = eventBuilder().attributeOptionCombo(null).attributeCategoryOptions(co.getUid()).build();
    hook.validateEvent(reporter, event);
    assertFalse(reporter.hasErrors());
    assertEquals(aoc, reporter.getValidationContext().getCachedEventCategoryOptionCombo(event.getEvent()), "AOC id should be cached");
    assertEquals(aoc.getName(), event.getAttributeOptionCombo(), "AOC id should be set by the validation");
    verify(preheat, times(1)).put(TrackerIdentifier.NAME, aoc);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) CategoryCombo(org.hisp.dhis.category.CategoryCombo) CategoryOption(org.hisp.dhis.category.CategoryOption) Event(org.hisp.dhis.tracker.domain.Event) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Aggregations

CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)218 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)94 DataElement (org.hisp.dhis.dataelement.DataElement)68 Period (org.hisp.dhis.period.Period)67 Test (org.junit.jupiter.api.Test)58 CategoryCombo (org.hisp.dhis.category.CategoryCombo)52 CategoryOption (org.hisp.dhis.category.CategoryOption)51 ArrayList (java.util.ArrayList)39 Program (org.hisp.dhis.program.Program)31 DataValue (org.hisp.dhis.datavalue.DataValue)30 Collectors (java.util.stream.Collectors)26 Category (org.hisp.dhis.category.Category)26 Date (java.util.Date)25 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)25 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)23 CategoryService (org.hisp.dhis.category.CategoryService)20 DataSet (org.hisp.dhis.dataset.DataSet)20 ProgramStage (org.hisp.dhis.program.ProgramStage)20 Event (org.hisp.dhis.tracker.domain.Event)20 ProgramInstance (org.hisp.dhis.program.ProgramInstance)16