use of org.hisp.dhis.category.CategoryOption in project dhis2-core by dhis2.
the class EventTrackerConverterService method to.
@Override
public List<Event> to(List<ProgramStageInstance> programStageInstances) {
List<Event> events = new ArrayList<>();
programStageInstances.forEach(psi -> {
Event event = new Event();
event.setEvent(psi.getUid());
event.setFollowup(BooleanUtils.toBoolean(psi.getProgramInstance().getFollowup()));
event.setStatus(psi.getStatus());
event.setOccurredAt(DateUtils.instantFromDate(psi.getExecutionDate()));
event.setScheduledAt(DateUtils.instantFromDate(psi.getDueDate()));
event.setStoredBy(psi.getStoredBy());
event.setCompletedBy(psi.getCompletedBy());
event.setCompletedAt(DateUtils.instantFromDate(psi.getCompletedDate()));
event.setCreatedAt(DateUtils.instantFromDate(psi.getCreated()));
event.setCreatedAtClient(DateUtils.instantFromDate(psi.getCreatedAtClient()));
event.setUpdatedAt(DateUtils.instantFromDate(psi.getLastUpdated()));
event.setUpdatedAtClient(DateUtils.instantFromDate(psi.getLastUpdatedAtClient()));
event.setGeometry(psi.getGeometry());
event.setDeleted(psi.isDeleted());
OrganisationUnit ou = psi.getOrganisationUnit();
if (ou != null) {
event.setOrgUnit(ou.getUid());
event.setOrgUnitName(ou.getName());
}
event.setEnrollment(psi.getProgramInstance().getUid());
event.setProgramStage(psi.getProgramStage().getUid());
event.setAttributeOptionCombo(psi.getAttributeOptionCombo().getUid());
event.setAttributeCategoryOptions(psi.getAttributeOptionCombo().getCategoryOptions().stream().map(CategoryOption::getUid).collect(Collectors.joining(";")));
Set<EventDataValue> dataValues = psi.getEventDataValues();
for (EventDataValue dataValue : dataValues) {
DataValue value = new DataValue();
value.setCreatedAt(DateUtils.instantFromDate(dataValue.getCreated()));
value.setUpdatedAt(DateUtils.instantFromDate(dataValue.getLastUpdated()));
value.setDataElement(dataValue.getDataElement());
value.setValue(dataValue.getValue());
value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
value.setStoredBy(dataValue.getStoredBy());
value.setLastUpdatedBy(Optional.ofNullable(dataValue.getLastUpdatedByUserInfo()).map(UserInfoSnapshot::getUsername).orElse(""));
value.setCreatedBy(Optional.ofNullable(dataValue.getCreatedByUserInfo()).map(UserInfoSnapshot::getUsername).orElse(""));
event.getDataValues().add(value);
}
events.add(event);
});
return events;
}
use of org.hisp.dhis.category.CategoryOption in project dhis2-core by dhis2.
the class PreCheckDataRelationsValidationHookTest method eventValidationSucceedsWhenEventAOCAndEventCOsAreSetAndProgramHasDefaultCC.
@Test
void eventValidationSucceedsWhenEventAOCAndEventCOsAreSetAndProgramHasDefaultCC() {
OrganisationUnit orgUnit = setupOrgUnit();
Program program = setupProgram(orgUnit);
CategoryCombo defaultCC = defaultCategoryCombo();
program.setCategoryCombo(defaultCC);
CategoryOptionCombo defaultAOC = firstCategoryOptionCombo(defaultCC);
when(preheat.getCategoryOptionCombo(defaultAOC.getUid())).thenReturn(defaultAOC);
CategoryOption defaultCO = defaultCC.getCategoryOptions().get(0);
program.setCategoryCombo(defaultCC);
when(preheat.getCategoryOption(defaultCO.getUid())).thenReturn(defaultCO);
Event event = eventBuilder().attributeOptionCombo(defaultAOC.getUid()).attributeCategoryOptions(defaultCO.getUid()).build();
hook.validateEvent(reporter, event);
assertFalse(reporter.hasErrors());
assertEquals(defaultAOC, reporter.getValidationContext().getCachedEventCategoryOptionCombo(event.getEvent()), "AOC id should be cached");
}
use of org.hisp.dhis.category.CategoryOption in project dhis2-core by dhis2.
the class PreCheckDataRelationsValidationHookTest method eventValidationFailsWhenOnlyCOsAreSetToCONotInProgramCC.
@Test
void eventValidationFailsWhenOnlyCOsAreSetToCONotInProgramCC() {
OrganisationUnit orgUnit = setupOrgUnit();
Program program = setupProgram(orgUnit);
CategoryCombo cc = categoryCombo();
program.setCategoryCombo(cc);
CategoryOption co = createCategoryOption('B');
when(preheat.getCategoryOption(co.getUid())).thenReturn(co);
when(categoryService.getCategoryOptionCombo(cc, Sets.newHashSet(co))).thenReturn(null);
Event event = eventBuilder().attributeCategoryOptions(co.getUid()).build();
hook.validateEvent(reporter, event);
assertEquals(1, reporter.getReportList().size());
assertTrue(reporter.hasErrorReport(r -> r.getErrorCode() == TrackerErrorCode.E1117 && r.getErrorMessage().contains(program.getCategoryCombo().getUid()) && r.getErrorMessage().contains(co.getUid())));
assertNull(reporter.getValidationContext().getCachedEventCategoryOptionCombo(event.getEvent()), "AOC id should not be cached");
verify(preheat, times(0)).put(any(), (IdentifiableObject) any());
}
use of org.hisp.dhis.category.CategoryOption in project dhis2-core by dhis2.
the class PreCheckDataRelationsValidationHookTest method defaultCategoryCombo.
private CategoryCombo defaultCategoryCombo() {
CategoryOption co = new CategoryOption(CategoryOption.DEFAULT_NAME);
co.setAutoFields();
assertTrue(co.isDefault(), "tests rely on this CO being the default one");
Category ca = createCategory('A', co);
CategoryCombo cc = createCategoryCombo('A', ca);
cc.setName(CategoryCombo.DEFAULT_CATEGORY_COMBO_NAME);
assertTrue(cc.isDefault(), "tests rely on this CC being the default one");
cc.setDataDimensionType(DataDimensionType.ATTRIBUTE);
CategoryOptionCombo aoc = createCategoryOptionCombo(cc, co);
assertTrue(aoc.isDefault(), "tests rely on this AOC being the default one");
cc.setOptionCombos(Sets.newHashSet(aoc));
return cc;
}
use of org.hisp.dhis.category.CategoryOption in project dhis2-core by dhis2.
the class PreCheckDataRelationsValidationHookTest method eventValidationFailsAccumulatingAOCAndCOsNotFoundErrors.
@Test
void eventValidationFailsAccumulatingAOCAndCOsNotFoundErrors() {
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);
String UNKNOWN_CO_ID1 = CodeGenerator.generateUid();
when(preheat.getCategoryOption(UNKNOWN_CO_ID1)).thenReturn(null);
String UNKNOWN_CO_ID2 = CodeGenerator.generateUid();
when(preheat.getCategoryOption(UNKNOWN_CO_ID2)).thenReturn(null);
String UNKNOWN_AOC_ID = CodeGenerator.generateUid();
when(preheat.getCategoryOptionCombo(UNKNOWN_AOC_ID)).thenReturn(null);
Event event = eventBuilder().attributeOptionCombo(UNKNOWN_AOC_ID).attributeCategoryOptions(UNKNOWN_CO_ID1 + ";" + co.getUid() + ";" + UNKNOWN_CO_ID2).build();
hook.validateEvent(reporter, event);
assertEquals(3, reporter.getReportList().size());
assertTrue(reporter.hasErrorReport(r -> r.getErrorCode() == TrackerErrorCode.E1115));
assertTrue(reporter.hasErrorReport(r -> r.getErrorCode() == TrackerErrorCode.E1116 && r.getErrorMessage().contains(UNKNOWN_CO_ID1)));
assertTrue(reporter.hasErrorReport(r -> r.getErrorCode() == TrackerErrorCode.E1116 && r.getErrorMessage().contains(UNKNOWN_CO_ID2)));
assertNull(reporter.getValidationContext().getCachedEventCategoryOptionCombo(event.getEvent()), "AOC id should not be cached");
}
Aggregations