Search in sources :

Example 41 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class DefaultTrackerImportService method preHeat.

private TrackerBundle preHeat(TrackerImportParams params, TrackerTimingsStats opsTimer) {
    TrackerBundle trackerBundle = opsTimer.exec(PREHEAT_OPS, () -> preheatBundle(params));
    notifyOps(params, PREHEAT_OPS, opsTimer);
    return trackerBundle;
}
Also used : TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle)

Example 42 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class AbstractTrackerDtoValidationHook method validate.

/**
 * Delegating validate method, this delegates validation to the different
 * implementing hooks.
 *
 * @param context validation context
 */
@Override
public void validate(ValidationErrorReporter reporter, TrackerImportValidationContext context) {
    TrackerBundle bundle = context.getBundle();
    /*
         * Validate the bundle, by passing each Tracker entities collection to
         * the validation hooks. If a validation hook reports errors and has
         * 'removeOnError=true' the Tracker entity under validation will be
         * removed from the bundle.
         */
    validateTrackerDtos(reporter, context, bundle.getTrackedEntities());
    validateTrackerDtos(reporter, context, bundle.getEnrollments());
    validateTrackerDtos(reporter, context, bundle.getEvents());
    validateTrackerDtos(reporter, context, bundle.getRelationships());
}
Also used : TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle)

Example 43 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class AssignValueImplementer method addOrOverwriteAttribute.

private void addOrOverwriteAttribute(EnrollmentActionRule actionRule, TrackerBundle bundle) {
    Enrollment enrollment = bundle.getEnrollment(actionRule.getEnrollment()).get();
    Optional<TrackedEntity> trackedEntity = bundle.getTrackedEntity(enrollment.getTrackedEntity());
    List<Attribute> attributes;
    if (trackedEntity.isPresent()) {
        attributes = trackedEntity.get().getAttributes();
        Optional<Attribute> optionalAttribute = attributes.stream().filter(at -> at.getAttribute().equals(actionRule.getField())).findAny();
        if (optionalAttribute.isPresent()) {
            optionalAttribute.get().setValue(actionRule.getData());
            return;
        }
    }
    attributes = enrollment.getAttributes();
    Optional<Attribute> optionalAttribute = attributes.stream().filter(at -> at.getAttribute().equals(actionRule.getField())).findAny();
    if (optionalAttribute.isPresent()) {
        optionalAttribute.get().setValue(actionRule.getData());
    } else {
        attributes.add(createAttribute(actionRule.getField(), actionRule.getData()));
    }
}
Also used : DataValue(org.hisp.dhis.tracker.domain.DataValue) RuleActionAssign(org.hisp.dhis.rules.models.RuleActionAssign) ValueType(org.hisp.dhis.common.ValueType) Attribute(org.hisp.dhis.tracker.domain.Attribute) RequiredArgsConstructor(lombok.RequiredArgsConstructor) DataElement(org.hisp.dhis.dataelement.DataElement) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Lists(com.google.common.collect.Lists) Map(java.util.Map) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) ProgramRuleIssue(org.hisp.dhis.tracker.programrule.ProgramRuleIssue) Event(org.hisp.dhis.tracker.domain.Event) EventActionRule(org.hisp.dhis.tracker.programrule.EventActionRule) EnrollmentActionRule(org.hisp.dhis.tracker.programrule.EnrollmentActionRule) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Set(java.util.Set) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Sets(com.google.common.collect.Sets) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) List(java.util.List) Component(org.springframework.stereotype.Component) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Optional(java.util.Optional) IssueType(org.hisp.dhis.tracker.programrule.IssueType) SettingKey(org.hisp.dhis.setting.SettingKey) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) RuleActionImplementer(org.hisp.dhis.tracker.programrule.RuleActionImplementer) Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Enrollment(org.hisp.dhis.tracker.domain.Enrollment)

Example 44 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class EventProgramPreProcessor method process.

@Override
public void process(TrackerBundle bundle) {
    List<Event> eventsToPreprocess = bundle.getEvents().stream().filter(e -> Strings.isEmpty(e.getProgram()) || Strings.isEmpty(e.getProgramStage())).collect(Collectors.toList());
    for (Event event : eventsToPreprocess) {
        // Extract program from program stage
        if (Strings.isNotEmpty(event.getProgramStage())) {
            ProgramStage programStage = bundle.getPreheat().get(ProgramStage.class, event.getProgramStage());
            if (Objects.nonNull(programStage)) {
                // TODO remove if once metadata import is fixed
                if (programStage.getProgram() == null) {
                    // a validation error for this edge case
                    return;
                }
                event.setProgram(programStage.getProgram().getUid());
                bundle.getPreheat().put(TrackerIdentifier.UID, programStage.getProgram());
            }
        } else // If it is a program event, extract program stage from program
        if (Strings.isNotEmpty(event.getProgram())) {
            Program program = bundle.getPreheat().get(Program.class, event.getProgram());
            if (Objects.nonNull(program) && program.isWithoutRegistration()) {
                Optional<ProgramStage> programStage = program.getProgramStages().stream().findFirst();
                if (programStage.isPresent()) {
                    event.setProgramStage(programStage.get().getUid());
                    bundle.getPreheat().put(TrackerIdentifier.UID, programStage.get());
                }
            }
        }
    }
}
Also used : Objects(java.util.Objects) Event(org.hisp.dhis.tracker.domain.Event) List(java.util.List) Component(org.springframework.stereotype.Component) Optional(java.util.Optional) Strings(org.apache.logging.log4j.util.Strings) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Collectors(java.util.stream.Collectors) TrackerIdentifier(org.hisp.dhis.tracker.TrackerIdentifier) Program(org.hisp.dhis.program.Program) ProgramStage(org.hisp.dhis.program.ProgramStage) Program(org.hisp.dhis.program.Program) Optional(java.util.Optional) Event(org.hisp.dhis.tracker.domain.Event) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 45 with TrackerBundle

use of org.hisp.dhis.tracker.bundle.TrackerBundle in project dhis2-core by dhis2.

the class DefaultTrackerProgramRuleService method getAttributes.

// Get all the attributes linked to enrollment from the payload and the DB,
// using the one from payload
// if they are present in both places
private List<TrackedEntityAttributeValue> getAttributes(Enrollment enrollment, TrackerBundle bundle) {
    List<TrackedEntityAttributeValue> attributeValues = attributeValueTrackerConverterService.from(bundle.getPreheat(), enrollment.getAttributes());
    TrackedEntityInstance trackedEntity = bundle.getPreheat().getTrackedEntity(bundle.getIdentifier(), enrollment.getTrackedEntity());
    List<TrackedEntityAttributeValue> payloadAttributeValues = bundle.getTrackedEntity(enrollment.getTrackedEntity()).map(tei -> attributeValueTrackerConverterService.from(bundle.getPreheat(), tei.getAttributes())).orElse(Lists.newArrayList());
    attributeValues.addAll(payloadAttributeValues);
    if (trackedEntity != null) {
        List<String> payloadAttributeValuesIds = payloadAttributeValues.stream().map(av -> av.getAttribute().getUid()).collect(Collectors.toList());
        attributeValues.addAll(trackedEntity.getTrackedEntityAttributeValues().stream().filter(av -> !payloadAttributeValuesIds.contains(av.getAttribute().getUid())).collect(Collectors.toList()));
    }
    return attributeValues;
}
Also used : RuleEffects(org.hisp.dhis.rules.models.RuleEffects) Attribute(org.hisp.dhis.tracker.domain.Attribute) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) Program(org.hisp.dhis.program.Program) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) RuleEngineConverterService(org.hisp.dhis.tracker.converter.RuleEngineConverterService) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Qualifier(org.springframework.beans.factory.annotation.Qualifier) TrackerIdScheme(org.hisp.dhis.tracker.TrackerIdScheme) TrackerProgramRuleService(org.hisp.dhis.tracker.TrackerProgramRuleService) Event(org.hisp.dhis.tracker.domain.Event) NonNull(lombok.NonNull) TrackerConverterService(org.hisp.dhis.tracker.converter.TrackerConverterService) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Set(java.util.Set) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) Stream(java.util.stream.Stream) ProgramRuleEngine(org.hisp.dhis.programrule.engine.ProgramRuleEngine) Collections(java.util.Collections) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) Transactional(org.springframework.transaction.annotation.Transactional) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Aggregations

TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)74 Test (org.junit.jupiter.api.Test)43 Event (org.hisp.dhis.tracker.domain.Event)29 User (org.hisp.dhis.user.User)21 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)19 List (java.util.List)17 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)15 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)14 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)14 Mockito.mock (org.mockito.Mockito.mock)14 TrackerErrorCode (org.hisp.dhis.tracker.report.TrackerErrorCode)13 TrackerImportStrategy (org.hisp.dhis.tracker.TrackerImportStrategy)12 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)12 TrackerImportValidationContext (org.hisp.dhis.tracker.validation.TrackerImportValidationContext)12 Collections (java.util.Collections)11 ProgramStage (org.hisp.dhis.program.ProgramStage)11 Objects (java.util.Objects)10 Program (org.hisp.dhis.program.Program)10 ValidationMode (org.hisp.dhis.tracker.ValidationMode)10 Relationship (org.hisp.dhis.tracker.domain.Relationship)10