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