Search in sources :

Example 1 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.requireAttrOptionCombos) {
            throw new ImportConflictException(new ImportConflict("Attribute option combo", "Attribute option combo is required but is not specified"));
        } else {
            mdProps.attrOptCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
        }
    }
    final DataElementCategoryOptionCombo aoc = mdProps.attrOptCombo;
    DateRange range = aoc.getDateRange();
    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.attrOptComboOrgUnitMap.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) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 2 with DateRange

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

the class DataValueSetImportValidator method checkDataValuePeriodWithinAttrOptionComboRange.

private static void checkDataValuePeriodWithinAttrOptionComboRange(DataValueEntry dataValue, ImportContext context, DataSetContext dataSetContext, DataValueContext valueContext) {
    final CategoryOptionCombo aoc = valueContext.getAttrOptionCombo();
    DateRange aocDateRange = dataSetContext.getDataSet() != null ? context.getAttrOptionComboDateRangeMap().get(valueContext.getAttrOptionCombo().getUid() + dataSetContext.getDataSet().getUid(), () -> aoc.getDateRange(dataSetContext.getDataSet())) : context.getAttrOptionComboDateRangeMap().get(valueContext.getAttrOptionCombo().getUid() + valueContext.getDataElement().getUid(), () -> aoc.getDateRange(valueContext.getDataElement()));
    if ((aocDateRange.getStartDate() != null && aocDateRange.getStartDate().after(valueContext.getPeriod().getEndDate())) || (aocDateRange.getEndDate() != null && aocDateRange.getEndDate().before(valueContext.getPeriod().getStartDate()))) {
        context.addConflict(valueContext.getIndex(), DataValueImportConflict.PERIOD_NOT_VALID_FOR_ATTR_OPTION_COMBO, dataValue.getPeriod(), dataValue.getAttributeOptionCombo());
    }
}
Also used : DateRange(org.hisp.dhis.common.DateRange) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 3 with DateRange

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

the class CategoryOptionComboTest method testGetDateRangeDataSet.

@Test
void testGetDateRangeDataSet() {
    DateRange dateRange;
    // [Option combo date
    dateRange = optionComboA.getDateRange(dataSetA);
    // range: null]
    // setOpenPeriodsAfterCoEndDate:
    // +0
    assertNull(dateRange.getStartDate());
    assertNull(dateRange.getEndDate());
    // [Jan 1-4] +0
    dateRange = optionComboB.getDateRange(dataSetA);
    assertEquals(jan1, dateRange.getStartDate());
    assertEquals(jan4, dateRange.getEndDate());
    // [Jan 1-4] +1
    dateRange = optionComboB.getDateRange(dataSetB);
    assertEquals(jan1, dateRange.getStartDate());
    assertEquals(jan5, dateRange.getEndDate());
    // [Jan 1-4] +2
    dateRange = optionComboB.getDateRange(dataSetC);
    assertEquals(jan1, dateRange.getStartDate());
    assertEquals(jan6, dateRange.getEndDate());
    // [null, Jan 1-4,
    dateRange = optionComboC.getDateRange(dataSetA);
    // Jan 2-5] +0
    assertEquals(jan2, dateRange.getStartDate());
    assertEquals(jan4, dateRange.getEndDate());
    // [null, Jan 1-4,
    dateRange = optionComboC.getDateRange(dataSetB);
    // Jan 2-5] +1
    assertEquals(jan2, dateRange.getStartDate());
    assertEquals(jan5, dateRange.getEndDate());
    // [null, Jan 1-4,
    dateRange = optionComboC.getDateRange(dataSetC);
    // Jan 2-5] +2
    assertEquals(jan2, dateRange.getStartDate());
    assertEquals(jan6, dateRange.getEndDate());
}
Also used : DateRange(org.hisp.dhis.common.DateRange) Test(org.junit.jupiter.api.Test)

Example 4 with DateRange

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

the class CategoryOptionComboTest method testGetDateRangeDataElement.

@Test
void testGetDateRangeDataElement() {
    DateRange dateRange;
    // [null] +0, +1,
    dateRange = optionComboA.getDateRange(dataElement);
    // +2
    assertNull(dateRange.getStartDate());
    assertNull(dateRange.getEndDate());
    // [Jan 1-4] +0,
    dateRange = optionComboB.getDateRange(dataElement);
    // +1, +2
    assertEquals(jan1, dateRange.getStartDate());
    assertEquals(jan6, dateRange.getEndDate());
    // [null, Jan 1-4,
    dateRange = optionComboC.getDateRange(dataElement);
    // Jan 2-5] +0,
    // +1, +2
    assertEquals(jan2, dateRange.getStartDate());
    assertEquals(jan6, dateRange.getEndDate());
}
Also used : DateRange(org.hisp.dhis.common.DateRange) Test(org.junit.jupiter.api.Test)

Example 5 with DateRange

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

the class DataElementCategoryOptionCombo method getDateRange.

/**
     * Gets a range of valid dates for this (attribute) cateogry option combo.
     * <p>
     * The earliest valid date is the latest start date (if any) from all the
     * category options associated with this option combo.
     * <p>
     * The latest valid date is the earliest end date (if any) from all the
     * category options associated with this option combo.
     *
     * @return valid date range for this (attribute) category option combo.
     */
public DateRange getDateRange() {
    Date latestStartDate = null;
    Date earliestEndDate = null;
    for (DataElementCategoryOption option : getCategoryOptions()) {
        if (option.getStartDate() != null && (latestStartDate == null || option.getStartDate().compareTo(latestStartDate) > 0)) {
            latestStartDate = option.getStartDate();
        }
        if (option.getEndDate() != null && (earliestEndDate == null || option.getStartDate().compareTo(earliestEndDate) < 0)) {
            earliestEndDate = option.getEndDate();
        }
    }
    return new DateRange(latestStartDate, earliestEndDate);
}
Also used : DateRange(org.hisp.dhis.common.DateRange) Date(java.util.Date)

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