Search in sources :

Example 1 with TrackedEntityInstances

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstances in project dhis2-core by dhis2.

the class JacksonTrackedEntityInstanceService method addTrackedEntityInstanceList.

private ImportSummaries addTrackedEntityInstanceList(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
    ImportSummaries importSummaries = new ImportSummaries();
    List<TrackedEntityInstance> create = new ArrayList<>();
    List<TrackedEntityInstance> update = new ArrayList<>();
    List<String> delete = new ArrayList<>();
    if (importOptions.getImportStrategy().isCreate()) {
        create.addAll(trackedEntityInstances);
    } else if (importOptions.getImportStrategy().isCreateAndUpdate()) {
        for (TrackedEntityInstance trackedEntityInstance : trackedEntityInstances) {
            if (StringUtils.isEmpty(trackedEntityInstance.getTrackedEntityInstance())) {
                create.add(trackedEntityInstance);
            } else {
                if (!teiService.trackedEntityInstanceExists(trackedEntityInstance.getTrackedEntityInstance())) {
                    create.add(trackedEntityInstance);
                } else {
                    update.add(trackedEntityInstance);
                }
            }
        }
    } else if (importOptions.getImportStrategy().isUpdate()) {
        update.addAll(trackedEntityInstances);
    } else if (importOptions.getImportStrategy().isDelete()) {
        delete.addAll(trackedEntityInstances.stream().map(TrackedEntityInstance::getTrackedEntityInstance).collect(Collectors.toList()));
    }
    importSummaries.addImportSummaries(addTrackedEntityInstances(create, importOptions));
    importSummaries.addImportSummaries(updateTrackedEntityInstances(update, importOptions));
    importSummaries.addImportSummaries(deleteTrackedEntityInstances(delete));
    return importSummaries;
}
Also used : ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 2 with TrackedEntityInstances

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstances in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method addTrackedEntityInstances.

@Override
@Transactional
public ImportSummaries addTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
    importOptions = updateImportOptions(importOptions);
    ImportSummaries importSummaries = new ImportSummaries();
    List<Enrollment> enrollments = new ArrayList<>();
    List<TrackedEntityInstance> validTeis = resolveImportableTeis(trackedEntityInstances, importSummaries);
    List<List<TrackedEntityInstance>> partitions = Lists.partition(validTeis, FLUSH_FREQUENCY);
    for (List<TrackedEntityInstance> _trackedEntityInstances : partitions) {
        reloadUser(importOptions);
        prepareCaches(_trackedEntityInstances, importOptions.getUser());
        for (TrackedEntityInstance trackedEntityInstance : _trackedEntityInstances) {
            ImportSummary importSummary = addTrackedEntityInstance(trackedEntityInstance, importOptions, false, true);
            importSummaries.addImportSummary(importSummary);
            if (importSummary.isStatus(ImportStatus.SUCCESS)) {
                enrollments.addAll(trackedEntityInstance.getEnrollments());
            }
        }
        clearSession();
    }
    ImportSummaries enrollmentImportSummaries = enrollmentService.addEnrollmentList(enrollments, importOptions);
    linkEnrollmentSummaries(importSummaries, enrollmentImportSummaries, enrollments);
    return importSummaries;
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) List(java.util.List) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with TrackedEntityInstances

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstances in project dhis2-core by dhis2.

the class AbstractEnrollmentService method prepareCaches.

private void prepareCaches(List<Enrollment> enrollments, User user) {
    Collection<String> orgUnits = enrollments.stream().map(Enrollment::getOrgUnit).collect(Collectors.toSet());
    if (!orgUnits.isEmpty()) {
        Query query = Query.from(schemaService.getDynamicSchema(OrganisationUnit.class));
        query.setUser(user);
        query.add(Restrictions.in("id", orgUnits));
        queryService.query(query).forEach(ou -> organisationUnitCache.put(ou.getUid(), (OrganisationUnit) ou));
    }
    Collection<String> programs = enrollments.stream().map(Enrollment::getProgram).collect(Collectors.toSet());
    if (!programs.isEmpty()) {
        Query query = Query.from(schemaService.getDynamicSchema(Program.class));
        query.setUser(user);
        query.add(Restrictions.in("id", programs));
        queryService.query(query).forEach(pr -> programCache.put(pr.getUid(), (Program) pr));
    }
    Collection<String> trackedEntityAttributes = new HashSet<>();
    enrollments.forEach(e -> e.getAttributes().forEach(at -> trackedEntityAttributes.add(at.getAttribute())));
    if (!trackedEntityAttributes.isEmpty()) {
        Query query = Query.from(schemaService.getDynamicSchema(TrackedEntityAttribute.class));
        query.setUser(user);
        query.add(Restrictions.in("id", trackedEntityAttributes));
        queryService.query(query).forEach(tea -> trackedEntityAttributeCache.put(tea.getUid(), (TrackedEntityAttribute) tea));
    }
    Collection<String> trackedEntityInstances = enrollments.stream().map(Enrollment::getTrackedEntityInstance).collect(toList());
    if (!trackedEntityInstances.isEmpty()) {
        Query query = Query.from(schemaService.getDynamicSchema(org.hisp.dhis.trackedentity.TrackedEntityInstance.class));
        query.setUser(user);
        query.add(Restrictions.in("id", trackedEntityInstances));
        queryService.query(query).forEach(te -> trackedEntityInstanceCache.put(te.getUid(), (org.hisp.dhis.trackedentity.TrackedEntityInstance) te));
    }
}
Also used : Authorities(org.hisp.dhis.security.Authorities) FIRST_PAGE(org.hisp.dhis.common.SlimPager.FIRST_PAGE) EventService(org.hisp.dhis.dxf2.events.event.EventService) Date(java.util.Date) Restrictions(org.hisp.dhis.query.Restrictions) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) EnrollmentEvaluationEvent(org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) NoteHelper(org.hisp.dhis.dxf2.events.NoteHelper) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) SetValuedMap(org.apache.commons.collections4.SetValuedMap) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) Transactional(javax.transaction.Transactional) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) QueryService(org.hisp.dhis.query.QueryService) Event(org.hisp.dhis.dxf2.events.event.Event) Objects(java.util.Objects) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) TEA_VALUE_MAX_LENGTH(org.hisp.dhis.trackedentity.TrackedEntityAttributeService.TEA_VALUE_MAX_LENGTH) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CollectionUtils.isNotEmpty(org.apache.commons.collections4.CollectionUtils.isNotEmpty) TrackerEnrollmentWebHookEvent(org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent) TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) TrackedEntityCommentService(org.hisp.dhis.trackedentitycomment.TrackedEntityCommentService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramEnrollmentNotificationEvent(org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent) Constants(org.hisp.dhis.dxf2.Constants) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) GeoUtils(org.hisp.dhis.system.util.GeoUtils) ObjectUtils.defaultIfNull(org.apache.commons.lang3.ObjectUtils.defaultIfNull) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Notifier(org.hisp.dhis.system.notification.Notifier) RelationshipParams(org.hisp.dhis.dxf2.events.RelationshipParams) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) SlimPager(org.hisp.dhis.common.SlimPager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) User(org.hisp.dhis.user.User) IdentifiableObjectCallable(org.hisp.dhis.system.callable.IdentifiableObjectCallable) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) I18nManager(org.hisp.dhis.i18n.I18nManager) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) IdSchemes(org.hisp.dhis.common.IdSchemes) Pager(org.hisp.dhis.common.Pager) RelationshipService(org.hisp.dhis.dxf2.events.relationship.RelationshipService) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DEFAULT_PAGE_SIZE(org.hisp.dhis.common.Pager.DEFAULT_PAGE_SIZE) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Maps(com.google.common.collect.Maps) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) Note(org.hisp.dhis.dxf2.events.event.Note) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramStatus(org.hisp.dhis.program.ProgramStatus) Collectors.toList(java.util.stream.Collectors.toList) CollectionUtils(org.hisp.dhis.commons.collection.CollectionUtils) FeatureType(org.hisp.dhis.organisationunit.FeatureType) NotificationLevel(org.hisp.dhis.system.notification.NotificationLevel) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ERROR(org.hisp.dhis.system.notification.NotificationLevel.ERROR) CodeGenerator(org.hisp.dhis.common.CodeGenerator) ProgramService(org.hisp.dhis.program.ProgramService) Collections(java.util.Collections) DateUtils(org.hisp.dhis.util.DateUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) Query(org.hisp.dhis.query.Query) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) HashSet(java.util.HashSet)

Example 4 with TrackedEntityInstances

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstances in project dhis2-core by dhis2.

the class TrackedEntityInstanceAggregateTest method testFetchTrackedEntityInstancesWithEvents.

@Test
void testFetchTrackedEntityInstancesWithEvents() {
    doInTransaction(() -> {
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
    });
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setIncludeAllAttributes(true);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    params.setIncludeEnrollments(true);
    params.setIncludeEvents(true);
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(trackedEntityInstances, hasSize(4));
    assertThat(trackedEntityInstances.get(0).getEnrollments(), hasSize(1));
    assertThat(trackedEntityInstances.get(1).getEnrollments(), hasSize(1));
    assertThat(trackedEntityInstances.get(2).getEnrollments(), hasSize(1));
    assertThat(trackedEntityInstances.get(3).getEnrollments(), hasSize(1));
    assertThat(trackedEntityInstances.get(0).getEnrollments().get(0).getEvents(), hasSize(5));
    assertThat(trackedEntityInstances.get(1).getEnrollments().get(0).getEvents(), hasSize(5));
    assertThat(trackedEntityInstances.get(2).getEnrollments().get(0).getEvents(), hasSize(5));
    assertThat(trackedEntityInstances.get(3).getEnrollments().get(0).getEvents(), hasSize(5));
}
Also used : TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Example 5 with TrackedEntityInstances

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstances in project dhis2-core by dhis2.

the class TrackedEntityInstanceAggregateTest method testFetchTrackedEntityInstancesWithoutEvents.

@Test
void testFetchTrackedEntityInstancesWithoutEvents() {
    doInTransaction(() -> {
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
        this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
    });
    TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
    queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    queryParams.setTrackedEntityType(trackedEntityTypeA);
    queryParams.setIncludeAllAttributes(true);
    TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
    params.setIncludeEnrollments(true);
    params.setIncludeEvents(false);
    final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
    assertThat(trackedEntityInstances, hasSize(4));
    assertThat(trackedEntityInstances.get(0).getEnrollments(), hasSize(1));
    assertThat(trackedEntityInstances.get(1).getEnrollments(), hasSize(1));
    assertThat(trackedEntityInstances.get(2).getEnrollments(), hasSize(1));
    assertThat(trackedEntityInstances.get(3).getEnrollments(), hasSize(1));
    assertThat(trackedEntityInstances.get(0).getEnrollments().get(0).getEvents(), hasSize(0));
    assertThat(trackedEntityInstances.get(1).getEnrollments().get(0).getEvents(), hasSize(0));
    assertThat(trackedEntityInstances.get(2).getEnrollments().get(0).getEvents(), hasSize(0));
    assertThat(trackedEntityInstances.get(3).getEnrollments().get(0).getEvents(), hasSize(0));
}
Also used : TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.dxf2.TrackerTest)

Aggregations

TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)30 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)29 TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)29 TrackerTest (org.hisp.dhis.dxf2.TrackerTest)25 Test (org.junit.jupiter.api.Test)25 Date (java.util.Date)11 List (java.util.List)10 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)10 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)9 Collectors (java.util.stream.Collectors)8 ArrayList (java.util.ArrayList)7 Set (java.util.Set)7 Map (java.util.Map)6 Event (org.hisp.dhis.dxf2.events.event.Event)6 TrackedEntityInstanceService (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService)6 User (org.hisp.dhis.user.User)6 Collections (java.util.Collections)5 ProgramOwner (org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner)5 Relationship (org.hisp.dhis.dxf2.events.trackedentity.Relationship)5 HashMap (java.util.HashMap)4