Search in sources :

Example 16 with TrackerPreheat

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;
}
Also used : TrackerIdentifier(org.hisp.dhis.tracker.TrackerIdentifier) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 17 with TrackerPreheat

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;
}
Also used : CategoryCombo(org.hisp.dhis.category.CategoryCombo) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 18 with TrackerPreheat

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;
}
Also used : TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 19 with TrackerPreheat

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;
}
Also used : TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat)

Example 20 with TrackerPreheat

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);
    });
}
Also used : ValueType(org.hisp.dhis.common.ValueType) Attribute(org.hisp.dhis.tracker.domain.Attribute) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ReservedValueService(org.hisp.dhis.reservedvalue.ReservedValueService) Session(org.hibernate.Session) AuditType(org.hisp.dhis.common.AuditType) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackerObjectReport(org.hisp.dhis.tracker.report.TrackerObjectReport) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) TrackedEntityAttributeValueAuditService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAuditService) AtomicMode(org.hisp.dhis.tracker.AtomicMode) AccessLevel(lombok.AccessLevel) Map(java.util.Map) FlushMode(org.hisp.dhis.tracker.FlushMode) TrackerSideEffectDataBundle(org.hisp.dhis.tracker.job.TrackerSideEffectDataBundle) TrackedEntityAttributeValueAudit(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAudit) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) FileResource(org.hisp.dhis.fileresource.FileResource) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Set(java.util.Set) TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Optional(java.util.Optional) TrackerDto(org.hisp.dhis.tracker.domain.TrackerDto) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)

Aggregations

TrackerPreheat (org.hisp.dhis.tracker.preheat.TrackerPreheat)33 Test (org.junit.jupiter.api.Test)17 List (java.util.List)11 TrackerImportParams (org.hisp.dhis.tracker.TrackerImportParams)10 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)10 Event (org.hisp.dhis.tracker.domain.Event)9 Collectors (java.util.stream.Collectors)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 Optional (java.util.Optional)6 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)6 ProgramStage (org.hisp.dhis.program.ProgramStage)5 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)5 Component (org.springframework.stereotype.Component)5 Lists (com.google.common.collect.Lists)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 Set (java.util.Set)4 RequiredArgsConstructor (lombok.RequiredArgsConstructor)4 Program (org.hisp.dhis.program.Program)4 ProgramInstance (org.hisp.dhis.program.ProgramInstance)4