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;
}
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;
}
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));
}
}
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));
}
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));
}
Aggregations