Search in sources :

Example 6 with DateRange

use of org.hisp.dhis.common.DateRange in project dhis2-core by dhis2.

the class DefaultCompleteDataSetRegistrationExchangeService method validateAttrOptCombo.

private void validateAttrOptCombo(MetadataProperties mdProps, MetadataCaches mdCaches, ImportConfig config) throws ImportConflictException {
    final Period pe = mdProps.period;
    if (mdProps.attrOptCombo == null) {
        if (config.isRequireAttrOptionCombos()) {
            throw new ImportConflictException(new ImportConflict("Attribute option combo", "Attribute option combo is required but is not specified"));
        } else {
            mdProps.attrOptCombo = categoryService.getDefaultCategoryOptionCombo();
        }
    }
    final CategoryOptionCombo aoc = mdProps.attrOptCombo;
    DateRange range = aoc.getDateRange(mdProps.dataSet);
    if ((range.getStartDate() != null && range.getStartDate().compareTo(pe.getStartDate()) > 0) || (range.getEndDate() != null && range.getEndDate().compareTo(pe.getEndDate()) < 0)) {
        throw new ImportConflictException(new ImportConflict(mdProps.orgUnit.getUid(), String.format("Period: %s is not within range of attribute option combo: %s", pe.getIsoDate(), aoc.getUid())));
    }
    final String aocOrgUnitKey = aoc.getUid() + mdProps.orgUnit.getUid();
    boolean isOrgUnitValidForAoc = mdCaches.getAttrOptComboOrgUnitMap().get(aocOrgUnitKey, () -> {
        Set<OrganisationUnit> aocOrgUnits = aoc.getOrganisationUnits();
        return aocOrgUnits == null || mdProps.orgUnit.isDescendant(aocOrgUnits);
    });
    if (!isOrgUnitValidForAoc) {
        throw new ImportConflictException(new ImportConflict(mdProps.orgUnit.getUid(), String.format("Organisation unit: %s is not valid for attribute option combo %s", mdProps.orgUnit.getUid(), aoc.getUid())));
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DateRange(org.hisp.dhis.common.DateRange) Period(org.hisp.dhis.period.Period) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 7 with DateRange

use of org.hisp.dhis.common.DateRange in project dhis2-core by dhis2.

the class DataValueSetImportValidatorTest method testCheckDataValuePeriodWithinAttrOptionComboRange.

@Test
void testCheckDataValuePeriodWithinAttrOptionComboRange() {
    DataValue dataValue = createRandomDataValue();
    DataValueContext valueContext = createDataValueContext(dataValue).build();
    DataSetContext dataSetContext = createMinimalDataSetContext().build();
    ImportContext context = createMinimalImportContext(valueContext).build();
    String key = valueContext.getAttrOptionCombo().getUid() + valueContext.getDataElement().getUid();
    context.getAttrOptionComboDateRangeMap().put(key, new DateRange(new Date(), null));
    assertTrue(validator.skipDataValue(dataValue, context, dataSetContext, valueContext));
    assertConflict(ErrorCode.E7638, "Period: `<object1>` is not within date range of attribute option combo: `<object2>`", context, dataValue.getPeriod(), dataValue.getAttributeOptionCombo());
}
Also used : DateRange(org.hisp.dhis.common.DateRange) DataValue(org.hisp.dhis.dxf2.datavalue.DataValue) DataSetContext(org.hisp.dhis.dxf2.datavalueset.ImportContext.DataSetContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DataValueContext(org.hisp.dhis.dxf2.datavalueset.ImportContext.DataValueContext) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 8 with DateRange

use of org.hisp.dhis.common.DateRange in project dhis2-core by dhis2.

the class CategoryOptionComboTest method testGetDateRangeProgram.

@Test
void testGetDateRangeProgram() {
    DateRange dateRange;
    dateRange = optionComboA.getDateRange(program);
    assertNull(dateRange.getStartDate());
    assertNull(dateRange.getEndDate());
    dateRange = optionComboB.getDateRange(program);
    assertEquals(jan1, dateRange.getStartDate());
    assertEquals(jan4, dateRange.getEndDate());
    dateRange = optionComboC.getDateRange(program);
    assertEquals(jan2, dateRange.getStartDate());
    assertEquals(jan4, dateRange.getEndDate());
    program.setOpenDaysAfterCoEndDate(2);
    dateRange = optionComboA.getDateRange(program);
    assertNull(dateRange.getStartDate());
    assertNull(dateRange.getEndDate());
    dateRange = optionComboB.getDateRange(program);
    assertEquals(jan1, dateRange.getStartDate());
    assertEquals(jan6, dateRange.getEndDate());
    dateRange = optionComboC.getDateRange(program);
    assertEquals(jan2, dateRange.getStartDate());
    assertEquals(jan6, dateRange.getEndDate());
}
Also used : DateRange(org.hisp.dhis.common.DateRange) Test(org.junit.jupiter.api.Test)

Aggregations

DateRange (org.hisp.dhis.common.DateRange)8 Test (org.junit.jupiter.api.Test)4 Date (java.util.Date)2 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)2 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 Period (org.hisp.dhis.period.Period)2 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)1 DataValue (org.hisp.dhis.dxf2.datavalue.DataValue)1 DataSetContext (org.hisp.dhis.dxf2.datavalueset.ImportContext.DataSetContext)1 DataValueContext (org.hisp.dhis.dxf2.datavalueset.ImportContext.DataValueContext)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1