use of org.hisp.dhis.tracker.preheat.TrackerPreheat in project dhis2-core by dhis2.
the class PreCheckDataRelationsValidationHook method resolveAttributeOptionCombo.
private CategoryOptionCombo resolveAttributeOptionCombo(ValidationErrorReporter reporter, Event event, Program program) {
TrackerPreheat preheat = reporter.getValidationContext().getBundle().getPreheat();
CategoryOptionCombo aoc;
if (hasNoAttributeOptionComboSet(event) && program.getCategoryCombo().isDefault()) {
aoc = preheat.getDefault(CategoryOptionCombo.class);
} else if (hasNoAttributeOptionComboSet(event) && hasAttributeCategoryOptionsSet(event)) {
aoc = fetchAttributeOptionCombo(reporter, event, program);
if (aoc != null) {
// TODO validation hooks should not need to mutate the payload.
// This
// should be moved to a pre-processor.
event.setAttributeOptionCombo(reporter.getValidationContext().getIdentifiers().getCategoryOptionComboIdScheme().getIdentifier(aoc));
// TODO validation hooks should not need to populate the
// preheat. Move this to the preheat.
// We need the AOC in the preheat so we can allow users not to
// send it. We need to set it on the
// ProgramStageInstance before persisting.
TrackerIdentifier identifier = preheat.getIdentifiers().getCategoryOptionComboIdScheme();
preheat.put(identifier, aoc);
}
} else {
// Note: there is a potential case when there are multiple AOCs in
// the default CC
// this should not happen, but it's technically possible. In this
// case with event.AOC provided,
// stick to the given AOC in the payload instead of
// preheat.getDefault( CategoryOptionCombo.class )
aoc = preheat.getCategoryOptionCombo(event.getAttributeOptionCombo());
}
return aoc;
}
use of org.hisp.dhis.tracker.preheat.TrackerPreheat in project dhis2-core by dhis2.
the class PreCheckDataRelationsValidationHook method fetchAttributeOptionCombo.
private CategoryOptionCombo fetchAttributeOptionCombo(ValidationErrorReporter reporter, Event event, Program program) {
CategoryCombo categoryCombo = program.getCategoryCombo();
String cacheKey = event.getAttributeCategoryOptions() + categoryCombo.getUid();
Optional<String> cachedAOCId = reporter.getValidationContext().getCachedEventAOCProgramCC(cacheKey);
TrackerPreheat preheat = reporter.getValidationContext().getBundle().getPreheat();
if (cachedAOCId.isPresent()) {
return preheat.getCategoryOptionCombo(cachedAOCId.get());
}
CategoryOptionCombo aoc = categoryService.getCategoryOptionCombo(categoryCombo, getCategoryOptions(preheat, event));
reporter.getValidationContext().putCachedEventAOCProgramCC(cacheKey, aoc != null ? aoc.getUid() : null);
return aoc;
}
use of org.hisp.dhis.tracker.preheat.TrackerPreheat in project dhis2-core by dhis2.
the class PreCheckDataRelationsValidationHook method validateAttributeCategoryOptionsAreInProgramCategoryCombo.
/**
* Validates that given AOC and COs match. This ensures that a payload
* contains all COs of an AOC and that every CO is in the AOC.
*
* When called after
* {@link #validateAttributeOptionComboIsInProgramCategoryCombo} we also
* know that the COs are in the event programs category combo.
*
* @param reporter validation error reporter
* @param event event to validate
* @return return true if cos are in event program cc, false otherwise
*/
private boolean validateAttributeCategoryOptionsAreInProgramCategoryCombo(ValidationErrorReporter reporter, Event event) {
if (hasNoAttributeOptionComboSet(event) || hasNoAttributeCategoryOptionsSet(event)) {
return true;
}
TrackerPreheat preheat = reporter.getValidationContext().getBundle().getPreheat();
CategoryOptionCombo aoc = preheat.getCategoryOptionCombo(event.getAttributeOptionCombo());
if (isNotAOCForCOs(preheat, event, aoc)) {
reporter.addError(event, TrackerErrorCode.E1053, event.getAttributeCategoryOptions(), event.getAttributeOptionCombo());
return false;
}
return true;
}
use of org.hisp.dhis.tracker.preheat.TrackerPreheat in project dhis2-core by dhis2.
the class DefaultTrackerBundleService method create.
@Override
public TrackerBundle create(TrackerImportParams params) {
TrackerBundle trackerBundle = ParamsConverter.convert(params);
TrackerPreheat preheat = trackerPreheatService.preheat(params);
trackerBundle.setPreheat(preheat);
return trackerBundle;
}
use of org.hisp.dhis.tracker.preheat.TrackerPreheat in project dhis2-core by dhis2.
the class AbstractTrackerPersister method handleTrackedEntityAttributeValues.
protected void handleTrackedEntityAttributeValues(Session session, TrackerPreheat preheat, List<Attribute> payloadAttributes, TrackedEntityInstance trackedEntityInstance) {
if (payloadAttributes.isEmpty()) {
return;
}
Map<String, TrackedEntityAttributeValue> attributeValueByUid = trackedEntityInstance.getTrackedEntityAttributeValues().stream().collect(Collectors.toMap(teav -> teav.getAttribute().getUid(), Function.identity()));
payloadAttributes.forEach(attribute -> {
// We cannot get the value from attributeToStore because it uses
// encryption logic, so we need to use the one from payload
boolean isDelete = StringUtils.isEmpty(attribute.getValue());
TrackedEntityAttributeValue trackedEntityAttributeValue = attributeValueByUid.get(attribute.getAttribute());
boolean isUpdated = false;
boolean isNew = Objects.isNull(trackedEntityAttributeValue);
if (isDelete && isNew) {
return;
}
if (isDelete) {
delete(session, preheat, trackedEntityAttributeValue, trackedEntityInstance);
} else {
if (!isNew) {
isUpdated = !trackedEntityAttributeValue.getPlainValue().equals(attribute.getValue());
}
trackedEntityAttributeValue = Optional.ofNullable(trackedEntityAttributeValue).orElseGet(() -> new TrackedEntityAttributeValue().setAttribute(getTrackedEntityAttributeFromPreheat(preheat, attribute.getAttribute())).setEntityInstance(trackedEntityInstance)).setStoredBy(attribute.getStoredBy()).setValue(attribute.getValue());
saveOrUpdate(session, preheat, isNew, trackedEntityInstance, trackedEntityAttributeValue, isUpdated);
}
handleReservedValue(trackedEntityAttributeValue);
});
}
Aggregations