Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,39 @@ public static boolean sendSyncRequest(
return false;
}

public static Optional<ImportSummary> runSyncRequest(
RestTemplate restTemplate,
RequestCallback requestCallback,
ResponseExtractor<ImportSummary> responseExtractor,
String syncUrl,
int maxSyncAttempts) {
boolean networkErrorOccurred = true;
int syncAttemptsDone = 0;
ImportSummary responseSummary = null;

while (networkErrorOccurred) {
networkErrorOccurred = false;
syncAttemptsDone++;
try {
responseSummary =
restTemplate.execute(syncUrl, HttpMethod.POST, requestCallback, responseExtractor);
} catch (HttpServerErrorException ex) {
log.error(
"Internal error happened during sync request: {}", ex.getResponseBodyAsString(), ex);
if (syncAttemptsDone <= maxSyncAttempts) {
networkErrorOccurred = true;
} else {
throw ex;
}
} catch (ResourceAccessException ex) {
log.error("Exception during sync request: {}", ex.getMessage(), ex);
throw ex;
}
}

return Optional.ofNullable(responseSummary);
}

public static Optional<WebMessageResponse> runSyncRequest(
RestTemplate restTemplate,
RequestCallback requestCallback,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,27 @@ private void filter() {

private <T extends TrackerDto> void collectDeletables(Class<T> type, List<T> entities) {
for (T entity : entities) {
if (isValid(entity)) {
if (isValid(entity) && !isAlreadyDeleted(entity)) {
collectPersistable(type, entity);
}
}
}

private boolean isAlreadyDeleted(TrackerDto entity) {
if (entity instanceof Event ev) {
org.hisp.dhis.program.Event existing = preheat.getEvent(ev.getEvent());
return existing != null && existing.isDeleted();
} else if (entity instanceof Enrollment en) {
org.hisp.dhis.program.Enrollment existing = preheat.getEnrollment(en.getEnrollment());
return existing != null && existing.isDeleted();
} else if (entity instanceof TrackedEntity te) {
org.hisp.dhis.trackedentity.TrackedEntity existing =
preheat.getTrackedEntity(te.getTrackedEntity());
return existing != null && existing.isDeleted();
}
return false;
}

private <T extends TrackerDto> void collectPersistables(
Class<T> type, List<Function<T, TrackerDto>> parents, List<T> entities) {
for (T entity : entities) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public void validate(

Event existingEvent = bundle.getPreheat().getEvent(event.getEvent());

// If the event is soft-deleted no operation is allowed
if (existingEvent != null && existingEvent.isDeleted()) {
reporter.addError(event, E1082, event.getEvent());
return;
if (importStrategy.isDelete()) {
reporter.addWarning(event, E1082, event.getEvent());
} else {
reporter.addError(event, E1082, event.getEvent());
}
}

if (existingEvent != null && importStrategy.isCreate()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@
import static org.hisp.dhis.dxf2.sync.SyncUtils.runSyncRequest;
import static org.hisp.dhis.scheduling.JobProgress.FailurePolicy.SKIP_ITEM;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.commons.jackson.config.JacksonObjectMapperConfig;
import org.hisp.dhis.dxf2.importsummary.ImportCount;
import org.hisp.dhis.dxf2.importsummary.ImportStatus;
import org.hisp.dhis.dxf2.importsummary.ImportSummary;
import org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException;
Expand All @@ -65,8 +67,9 @@
import org.hisp.dhis.tracker.export.event.EventOperationParams;
import org.hisp.dhis.tracker.export.event.EventService;
import org.hisp.dhis.tracker.imports.TrackerImportStrategy;
import org.hisp.dhis.tracker.imports.report.ImportReport;
import org.hisp.dhis.tracker.imports.report.Stats;
import org.hisp.dhis.webapi.controller.tracker.export.event.EventMapper;
import org.hisp.dhis.webmessage.WebMessageResponse;
import org.mapstruct.factory.Mappers;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
Expand All @@ -82,6 +85,7 @@
public class SingleEventDataSynchronizationService extends TrackerDataSynchronizationWithPaging {
private static final String PROCESS_NAME = "Single event programs data synchronization";
private static final EventMapper EVENT_MAPPER = Mappers.getMapper(EventMapper.class);
private static final ObjectMapper JSON_MAPPER = JacksonObjectMapperConfig.staticJsonMapper();

private final EventService eventService;
private final SystemSettingsService systemSettingsService;
Expand Down Expand Up @@ -292,16 +296,32 @@ private ImportSummary sendTrackerRequest(
SystemSettings settings,
String url) {
RequestCallback requestCallback = createRequestCallback(events, instance);

Optional<WebMessageResponse> response =
runSyncRequest(
String syncUrl = url + "&async=false";
return runSyncRequest(
restTemplate,
requestCallback,
SyncEndpoint.TRACKER_IMPORT.getKlass(),
url,
settings.getSyncMaxAttempts());
response ->
toImportSummary(JSON_MAPPER.readValue(response.getBody(), ImportReport.class)),
syncUrl,
settings.getSyncMaxAttempts())
.orElse(null);
}

return response.map(ImportSummary.class::cast).orElse(null);
static ImportSummary toImportSummary(ImportReport report) {
ImportSummary summary = new ImportSummary();
summary.setStatus(
switch (report.getStatus()) {
case OK -> ImportStatus.SUCCESS;
case WARNING -> ImportStatus.WARNING;
case ERROR -> ImportStatus.ERROR;
});
Stats stats = report.getStats();
if (stats != null) {
summary.setImportCount(
new ImportCount(
stats.getCreated(), stats.getUpdated(), stats.getIgnored(), stats.getDeleted()));
}
return summary;
}

private RequestCallback createRequestCallback(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2004-2025, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.webapi.controller.tracker.sync;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.hisp.dhis.dxf2.importsummary.ImportStatus;
import org.hisp.dhis.dxf2.importsummary.ImportSummary;
import org.hisp.dhis.tracker.imports.report.ImportReport;
import org.hisp.dhis.tracker.imports.report.Stats;
import org.hisp.dhis.tracker.imports.report.Status;
import org.junit.jupiter.api.Test;

class SingleEventDataSynchronizationServiceTest {

@Test
void shouldMapOkStatusToSuccess() {
ImportReport report = ImportReport.builder().status(Status.OK).build();

ImportSummary summary = SingleEventDataSynchronizationService.toImportSummary(report);

assertEquals(ImportStatus.SUCCESS, summary.getStatus());
}

@Test
void shouldMapWarningStatusToWarning() {
ImportReport report = ImportReport.builder().status(Status.WARNING).build();

ImportSummary summary = SingleEventDataSynchronizationService.toImportSummary(report);

assertEquals(ImportStatus.WARNING, summary.getStatus());
}

@Test
void shouldMapErrorStatusToError() {
ImportReport report = ImportReport.builder().status(Status.ERROR).build();

ImportSummary summary = SingleEventDataSynchronizationService.toImportSummary(report);

assertEquals(ImportStatus.ERROR, summary.getStatus());
}

@Test
void shouldMapStats() {
Stats stats = Stats.builder().created(1).updated(2).deleted(3).ignored(4).build();
ImportReport report = ImportReport.builder().status(Status.OK).stats(stats).build();

ImportSummary summary = SingleEventDataSynchronizationService.toImportSummary(report);

assertEquals(1, summary.getImportCount().getImported());
assertEquals(2, summary.getImportCount().getUpdated());
assertEquals(3, summary.getImportCount().getDeleted());
assertEquals(4, summary.getImportCount().getIgnored());
}

@Test
void shouldLeaveImportCountZeroedWhenStatsAbsent() {
ImportReport report = ImportReport.builder().status(Status.OK).build();

ImportSummary summary = SingleEventDataSynchronizationService.toImportSummary(report);

assertEquals(0, summary.getImportCount().getImported());
assertEquals(0, summary.getImportCount().getUpdated());
assertEquals(0, summary.getImportCount().getDeleted());
assertEquals(0, summary.getImportCount().getIgnored());
}
}
Loading